]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem.C
Changed to use the Converter class instead of hardcoding ImageMagick.
[lyx.git] / src / graphics / GraphicsCacheItem.C
1 // -*- C++ -*-
2 /* This file is part of
3  * =================================================
4  * 
5  *          LyX, The Document Processor
6  *          Copyright 1995 Matthias Ettrich.
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  *          This file Copyright 2000 Baruch Even
10  * ================================================= */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
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"
26 #include "debug.h"
27 #include "support/LAssert.h"
28 #include "gettext.h"
29
30 using std::endl;
31
32 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
33         : imageStatus_(GraphicsCacheItem::Loading)
34 {
35         filename_ = filename;
36         
37         bool success = convertImage(filename);
38         // For now we do it synchronously
39         if (success) 
40                 imageConverted(0);
41         else
42                 imageStatus_ = ErrorConverting;
43 }
44
45
46 GraphicsCacheItem::~GraphicsCacheItem()
47 {}
48
49
50 GraphicsCacheItem::ImageStatus 
51 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
52
53
54 LyXImage * 
55 GraphicsCacheItem::getImage() const { return image_.get(); }
56
57
58 void
59 GraphicsCacheItem::imageConverted(int retval)
60 {
61         lyxerr << "imageConverted, retval=" << retval << endl;
62
63         if (retval) {
64                 lyxerr << "(GraphicsCacheItem::imageConverter) "
65                         "Error converting image." << endl;
66                 imageStatus_ = GraphicsCacheItem::ErrorConverting;
67                 return;
68         }
69
70         // Do the actual image loading from file to memory.
71         loadImage();    
72 }
73
74
75 namespace {
76 string const findTargetFormat(string const & from)
77 {
78         typedef ImageLoader::FormatList FormatList;
79         FormatList formats = ImageLoaderXPM().loadableFormats();
80         Assert(formats.size() > 0); // There must be a format to load from.
81         
82         FormatList::const_iterator iter = formats.begin();
83         FormatList::const_iterator end  = formats.end();
84
85         for (; iter != end; ++iter) {
86                 if (converters.IsReachable(from, *iter))
87                         break;
88         }
89         if (iter == end) {
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;
92
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]);
97
98                 WriteAlert(first, second);
99                 
100                 return string();
101         }
102
103         return (*iter);
104 }
105
106 } // anon namespace
107
108         
109 bool
110 GraphicsCacheItem::convertImage(string const & filename)
111 {
112         string const from = GetExtension(filename);
113         string const to = findTargetFormat(from);
114         if (to.empty()) 
115                 return false;
116         
117         // Take only the filename part of the file, without path or extension.
118         string temp = OnlyFilename(filename);
119         temp = ChangeExtension(filename, string());
120         
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);
126
127         converters.Convert(0, filename, tempfile, from, to);
128
129         return true;
130 }
131
132
133 // This function gets called from the callback after the image has been
134 // converted successfully.
135 void
136 GraphicsCacheItem::loadImage()
137 {
138         lyxerr << "Loading XPM Image... ";
139
140         ImageLoaderXPM imageLoader;
141         if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
142                 lyxerr << "Success." << endl;
143                 image_.reset(imageLoader.getImage());
144                 imageStatus_ = GraphicsCacheItem::Loaded;
145         } else {
146                 lyxerr << "Loading " << tempfile << "Failed" << endl;
147                 imageStatus_ = GraphicsCacheItem::ErrorReading;
148         }
149
150         // remove the xpm file now.
151         lyx::unlink(tempfile);
152         // and remove the reference to the filename.
153         tempfile = string();
154 }