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 "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.
76 string const findTargetFormat(string const & from)
78 typedef ImageLoader::FormatList FormatList;
79 FormatList formats = ImageLoaderXPM().loadableFormats();
80 Assert(formats.size() > 0); // There must be a format to load from.
82 FormatList::const_iterator iter = formats.begin();
83 FormatList::const_iterator end = formats.end();
85 for (; iter != end; ++iter) {
86 if (converters.IsReachable(from, *iter))
90 // We do not know how to convert the image to something loadable.
91 lyxerr << "ERROR: Do not know how to convert image." << std::endl;
93 string const first(_("Cannot convert image to display format"));
94 string const second1(_("Need converter from "));
95 string const second2(_(" to "));
96 string const second(second1 + from + second2 + formats[0]);
98 WriteAlert(first, second);
110 GraphicsCacheItem::convertImage(string const & filename)
112 string const from = GetExtension(filename);
113 string const to = findTargetFormat(from);
117 // Take only the filename part of the file, without path or extension.
118 string temp = OnlyFilename(filename);
119 temp = ChangeExtension(filename, string());
121 // Add some stuff to have it a unique temp file.
122 // This tempfile is deleted in loadImage after it is loaded to memory.
123 tempfile = lyx::tempName(string(), temp);
124 // Remove the temp file, we only want the name...
125 lyx::unlink(tempfile);
127 converters.Convert(0, filename, tempfile, from, to);
133 // This function gets called from the callback after the image has been
134 // converted successfully.
136 GraphicsCacheItem::loadImage()
138 lyxerr << "Loading XPM Image... ";
140 ImageLoaderXPM imageLoader;
141 if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
142 lyxerr << "Success." << endl;
143 image_.reset(imageLoader.getImage());
144 imageStatus_ = GraphicsCacheItem::Loaded;
146 lyxerr << "Loading " << tempfile << "Failed" << endl;
147 imageStatus_ = GraphicsCacheItem::ErrorReading;
150 // remove the xpm file now.
151 lyx::unlink(tempfile);
152 // and remove the reference to the filename.