2 /* This file is part of
3 * =================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich.
7 * Copyright 1995-2000 The LyX Team.
9 * This file Copyright 2000 Baruch Even
10 * ================================================= */
15 #pragma implementation
18 #include "graphics/GraphicsCache.h"
19 #include "graphics/GraphicsCacheItem.h"
20 #include "frontends/support/LyXImage.h"
21 #include "graphics/ImageLoaderXPM.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/syscall.h"
30 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
31 : imageStatus_(GraphicsCacheItem::Loading)
36 // For now we do it synchronously
41 GraphicsCacheItem::~GraphicsCacheItem()
45 GraphicsCacheItem::ImageStatus
46 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
50 GraphicsCacheItem::getImage() const { return image_.get(); }
54 GraphicsCacheItem::imageConverted(int retval)
56 lyxerr << "imageConverted, retval=" << retval << endl;
59 lyxerr << "(GraphicsCacheItem::imageConverter) "
60 "Error converting image." << endl;
61 imageStatus_ = GraphicsCacheItem::ErrorConverting;
65 // Do the actual image loading from XPM to memory.
71 GraphicsCacheItem::renderXPM(string const & filename)
73 // Create the command to do the conversion, this depends on ImageMagicks
75 string command = "convert ";
79 // Take only the filename part of the file, without path or extension.
80 string temp = OnlyFilename(filename);
81 temp = ChangeExtension(filename, string());
83 // Add some stuff to have it a unique temp file.
84 // This tempfile is deleted in loadXPMImage after it is loaded to memory.
85 tempfile = lyx::tempName(string(), temp);
86 // Remove the temp file, we only want the name...
87 lyx::unlink(tempfile);
88 tempfile = ChangeExtension(tempfile, ".xpm");
93 lyxerr << "Launching convert to xpm, command=" << command << endl;
95 syscall.startscript(Systemcalls::Wait, command);
101 // This function gets called from the callback after the image has been
102 // converted successfully.
104 GraphicsCacheItem::loadXPMImage()
106 lyxerr << "Loading XPM Image... ";
108 ImageLoaderXPM imageLoader;
109 if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
110 lyxerr << "Success." << endl;
111 image_.reset(imageLoader.getImage());
112 imageStatus_ = GraphicsCacheItem::Loaded;
114 lyxerr << "Fail." << endl;
115 imageStatus_ = GraphicsCacheItem::ErrorReading;
118 // remove the xpm file now.
119 lyx::unlink(tempfile);
120 // and remove the reference to the filename.