1 /* This file is part of
2 * =================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich.
6 * Copyright 1995-2001 The LyX Team.
8 * This file Copyright 2000 Baruch Even
9 * ================================================= */
14 #pragma implementation
17 #include "graphics/GraphicsCache.h"
18 #include "graphics/GraphicsCacheItem.h"
19 #include "frontends/support/LyXImage.h"
20 #include "graphics/ImageLoaderXPM.h"
21 #include "converter.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "lyx_gui_misc.h"
26 #include "support/LAssert.h"
32 * The order of conversion:
34 * The c-tor calls convertImage()
36 * convertImage() verifies that we need to do conversion, if not it will just
37 * call the loadImage()
38 * if conversion is needed, it will initiate the conversion.
40 * When the conversion is completed imageConverted() is called, which in turn
43 * Since we currently do everything synchronously, convertImage() calls
44 * imageConverted() right after it does the call to the conversion process.
47 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
48 : imageStatus_(GraphicsCacheItem::Loading)
52 bool success = convertImage(filename);
53 if (! success) // Conversion failed miserably (couldn't even start).
54 setStatus(ErrorConverting);
58 GraphicsCacheItem::~GraphicsCacheItem()
62 GraphicsCacheItem::ImageStatus
63 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
66 void GraphicsCacheItem::setStatus(ImageStatus new_status)
68 imageStatus_ = new_status;
73 GraphicsCacheItem::getImage() const { return image_.get(); }
77 GraphicsCacheItem::imageConverted(bool success)
79 lyxerr << "imageConverted, status=" << success << endl;
82 lyxerr << "(GraphicsCacheItem::imageConverter) "
83 "Error converting image." << endl;
84 setStatus(GraphicsCacheItem::ErrorConverting);
88 // Do the actual image loading from file to memory.
95 string const findTargetFormat(string const & from)
97 typedef ImageLoader::FormatList FormatList;
98 FormatList formats = ImageLoaderXPM().loadableFormats();
99 lyx::Assert(formats.size() > 0); // There must be a format to load from.
101 FormatList::const_iterator iter = formats.begin();
102 FormatList::const_iterator end = formats.end();
104 for (; iter != end; ++iter) {
105 if (converters.IsReachable(from, *iter))
110 // We do not know how to convert the image to something loadable.
111 lyxerr << "ERROR: Do not know how to convert image." << std::endl;
113 string const first(_("Cannot convert image to display format"));
114 string const second1(_("Need converter from "));
115 string const second2(_(" to "));
116 string const second(second1 + from + second2 + formats[0]);
118 WriteAlert(first, second);
130 GraphicsCacheItem::convertImage(string const & filename)
132 string const from = GetExtension(filename);
133 string const to = findTargetFormat(from);
137 // Take only the filename part of the file, without path or extension.
138 string temp = OnlyFilename(filename);
139 temp = ChangeExtension(filename, string());
141 // Add some stuff to have it a unique temp file.
142 // This tempfile is deleted in loadImage after it is loaded to memory.
143 tempfile = lyx::tempName(string(), temp);
144 // Remove the temp file, we only want the name...
145 lyx::unlink(tempfile);
147 bool result = converters.Convert(0, filename, tempfile, from, to);
148 tempfile.append(".xpm");
150 // For now we are synchronous
151 imageConverted(result);
153 // Cleanup after the conversion.
154 lyx::unlink(tempfile);
161 // This function gets called from the callback after the image has been
162 // converted successfully.
164 GraphicsCacheItem::loadImage()
166 lyxerr << "Loading XPM Image... ";
168 ImageLoaderXPM imageLoader;
169 if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
170 lyxerr << "Success." << endl;
171 image_.reset(imageLoader.getImage());
172 setStatus(GraphicsCacheItem::Loaded);
174 lyxerr << "Loading " << tempfile << "Failed" << endl;
175 setStatus(GraphicsCacheItem::ErrorReading);