2000-07-31 12:30:10 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/* This file is part of
|
|
|
|
* =================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
* Copyright 1995 Matthias Ettrich.
|
|
|
|
* Copyright 1995-2000 The LyX Team.
|
|
|
|
*
|
|
|
|
* This file Copyright 2000 Baruch Even
|
|
|
|
* ================================================= */
|
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2000-08-10 13:15:05 +00:00
|
|
|
#include "graphics/GraphicsCache.h"
|
|
|
|
#include "graphics/GraphicsCacheItem.h"
|
2000-10-12 10:46:06 +00:00
|
|
|
#include "frontends/support/LyXImage.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "graphics/ImageLoaderXPM.h"
|
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
#include "support/syscall.h"
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "debug.h"
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
GraphicsCacheItem::GraphicsCacheItem(string const & filename)
|
|
|
|
: imageStatus_(GraphicsCacheItem::Loading)
|
2000-08-10 13:15:05 +00:00
|
|
|
{
|
2001-02-22 16:53:59 +00:00
|
|
|
filename_ = filename;
|
|
|
|
|
|
|
|
renderXPM(filename);
|
|
|
|
// For now we do it synchronously
|
|
|
|
imageConverted(0);
|
2000-08-10 13:15:05 +00:00
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2000-08-08 09:18:39 +00:00
|
|
|
GraphicsCacheItem::~GraphicsCacheItem()
|
2001-02-22 16:53:59 +00:00
|
|
|
{}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
GraphicsCacheItem::ImageStatus
|
|
|
|
GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
LyXImage *
|
|
|
|
GraphicsCacheItem::getImage() const { return image_.get(); }
|
2000-08-08 09:18:39 +00:00
|
|
|
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
void
|
|
|
|
GraphicsCacheItem::imageConverted(int retval)
|
|
|
|
{
|
|
|
|
lyxerr << "imageConverted, retval=" << retval << endl;
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
if (retval) {
|
|
|
|
lyxerr << "(GraphicsCacheItem::imageConverter) "
|
|
|
|
"Error converting image." << endl;
|
|
|
|
imageStatus_ = GraphicsCacheItem::ErrorConverting;
|
|
|
|
return;
|
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// Do the actual image loading from XPM to memory.
|
|
|
|
loadXPMImage();
|
2000-10-12 10:46:06 +00:00
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
GraphicsCacheItem::renderXPM(string const & filename)
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2001-02-22 16:53:59 +00:00
|
|
|
// Create the command to do the conversion, this depends on ImageMagicks
|
|
|
|
// convert program.
|
|
|
|
string command = "convert ";
|
|
|
|
command += filename;
|
|
|
|
command += " XPM:";
|
|
|
|
|
|
|
|
// Take only the filename part of the file, without path or extension.
|
|
|
|
string temp = OnlyFilename(filename);
|
|
|
|
temp = ChangeExtension(filename, string());
|
|
|
|
|
|
|
|
// Add some stuff to have it a unique temp file.
|
|
|
|
// This tempfile is deleted in loadXPMImage after it is loaded to memory.
|
|
|
|
tempfile = lyx::tempName(string(), temp);
|
|
|
|
// Remove the temp file, we only want the name...
|
|
|
|
lyx::unlink(tempfile);
|
|
|
|
tempfile = ChangeExtension(tempfile, ".xpm");
|
|
|
|
|
|
|
|
command += tempfile;
|
|
|
|
|
|
|
|
// Run the convertor.
|
|
|
|
lyxerr << "Launching convert to xpm, command=" << command << endl;
|
|
|
|
Systemcalls syscall;
|
|
|
|
syscall.startscript(Systemcalls::Wait, command);
|
|
|
|
|
|
|
|
return true;
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// This function gets called from the callback after the image has been
|
|
|
|
// converted successfully.
|
2000-08-08 09:18:39 +00:00
|
|
|
void
|
2001-02-22 16:53:59 +00:00
|
|
|
GraphicsCacheItem::loadXPMImage()
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2001-02-22 16:53:59 +00:00
|
|
|
lyxerr << "Loading XPM Image... ";
|
|
|
|
|
|
|
|
ImageLoaderXPM imageLoader;
|
|
|
|
if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
|
|
|
|
lyxerr << "Success." << endl;
|
|
|
|
image_.reset(imageLoader.getImage());
|
|
|
|
imageStatus_ = GraphicsCacheItem::Loaded;
|
|
|
|
} else {
|
|
|
|
lyxerr << "Fail." << endl;
|
|
|
|
imageStatus_ = GraphicsCacheItem::ErrorReading;
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// remove the xpm file now.
|
|
|
|
lyx::unlink(tempfile);
|
|
|
|
// and remove the reference to the filename.
|
|
|
|
tempfile = string();
|
|
|
|
}
|