2 /* This file is part of
3 * =================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich.
7 * Copyright 1995-2001 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 "converter.h"
23 #include "support/filetools.h"
24 #include "support/lyxlib.h"
25 #include "lyx_gui_misc.h"
27 #include "support/LAssert.h"
32 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
33 : imageStatus_(GraphicsCacheItem::Loading)
37 bool success = convertImage(filename);
38 // For now we do it synchronously
42 imageStatus_ = ErrorConverting;
46 GraphicsCacheItem::~GraphicsCacheItem()
50 GraphicsCacheItem::ImageStatus
51 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
55 GraphicsCacheItem::getImage() const { return image_.get(); }
59 GraphicsCacheItem::imageConverted(int retval)
61 lyxerr << "imageConverted, retval=" << retval << endl;
64 lyxerr << "(GraphicsCacheItem::imageConverter) "
65 "Error converting image." << endl;
66 imageStatus_ = GraphicsCacheItem::ErrorConverting;
70 // Do the actual image loading from file to memory.
77 string const findTargetFormat(string const & from)
79 typedef ImageLoader::FormatList FormatList;
80 FormatList formats = ImageLoaderXPM().loadableFormats();
81 lyx::Assert(formats.size() > 0); // There must be a format to load from.
83 FormatList::const_iterator iter = formats.begin();
84 FormatList::const_iterator end = formats.end();
86 for (; iter != end; ++iter) {
87 if (converters.IsReachable(from, *iter))
91 // We do not know how to convert the image to something loadable.
92 lyxerr << "ERROR: Do not know how to convert image." << std::endl;
94 string const first(_("Cannot convert image to display format"));
95 string const second1(_("Need converter from "));
96 string const second2(_(" to "));
97 string const second(second1 + from + second2 + formats[0]);
99 WriteAlert(first, second);
111 GraphicsCacheItem::convertImage(string const & filename)
113 string const from = GetExtension(filename);
114 string const to = findTargetFormat(from);
118 // Take only the filename part of the file, without path or extension.
119 string temp = OnlyFilename(filename);
120 temp = ChangeExtension(filename, string());
122 // Add some stuff to have it a unique temp file.
123 // This tempfile is deleted in loadImage after it is loaded to memory.
124 tempfile = lyx::tempName(string(), temp);
125 // Remove the temp file, we only want the name...
126 lyx::unlink(tempfile);
128 converters.Convert(0, filename, tempfile, from, to);
134 // This function gets called from the callback after the image has been
135 // converted successfully.
137 GraphicsCacheItem::loadImage()
139 lyxerr << "Loading XPM Image... ";
141 ImageLoaderXPM imageLoader;
142 if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
143 lyxerr << "Success." << endl;
144 image_.reset(imageLoader.getImage());
145 imageStatus_ = GraphicsCacheItem::Loaded;
147 lyxerr << "Loading " << tempfile << "Failed" << endl;
148 imageStatus_ = GraphicsCacheItem::ErrorReading;
151 // remove the xpm file now.
152 lyx::unlink(tempfile);
153 // and remove the reference to the filename.