]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xformsImage.C
Asymptotic approach to a well-designed graphics loader.
[lyx.git] / src / frontends / xforms / xformsImage.C
1 /*
2  * \file xformsImage.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "xformsImage.h"
16 #include "graphics/GraphicsParams.h"
17 #include "LColor.h"
18 #include "converter.h"              // formats
19 #include "debug.h"
20 #include "support/LAssert.h"
21 #include "support/lyxfunctional.h"  // compare_memfun
22
23 #include FORMS_H_LOCATION
24
25 #ifdef HAVE_FLIMAGE_H
26 # include <flimage.h>
27 #else
28 # ifdef HAVE_X11_FLIMAGE_H
29 # include <X11/flimage.h>
30 # endif
31 #endif
32
33 using std::find_if;
34
35 namespace {
36
37 void init_graphics();
38
39 unsigned int packedcolor(LColor::color c);
40
41 } // namespace anon
42
43
44 namespace grfx {
45
46 /// Access to this class is through this static method.
47 Image::ImagePtr xformsImage::newImage()
48 {
49         init_graphics();
50
51         ImagePtr ptr;
52         ptr.reset(new xformsImage);
53         return ptr;
54 }
55
56
57 /// Return the list of loadable formats.
58 Image::FormatList xformsImage::loadableFormats()
59 {
60         static FormatList fmts;
61         if (!fmts.empty())
62                 return fmts;
63
64         init_graphics();
65
66         // The formats recognised by LyX
67         Formats::const_iterator begin = formats.begin();
68         Formats::const_iterator end   = formats.end();
69
70         lyxerr[Debug::GRAPHICS]
71                 << "\nThe image loader can load the following directly:\n";
72
73         // Don't forget the Fortran numbering used by xforms!
74         int const nformats = flimage_get_number_of_formats();
75         for (int i = 1; i <= nformats; ++i) {
76
77                 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
78                 string const formal_name =
79                         info->formal_name ? info->formal_name : string();
80                 string ext =
81                         info->extension   ? info->extension   : string();
82
83                 if (ext.empty() || ext == "gz")
84                         continue;
85
86                 if (ext == "rgb") ext = "sgi";
87
88                 lyxerr[Debug::GRAPHICS]
89                         << formal_name << ", extension \"" << ext << "\"\n";
90
91                 Formats::const_iterator it =
92                         find_if(begin, end,
93                                 lyx::compare_memfun(&Format::extension, ext));
94                 if (it != end)
95                         fmts.push_back(it->name());
96         }
97
98         lyxerr[Debug::GRAPHICS]
99                 << "\nOf these, LyX recognises the following formats:\n";
100
101         FormatList::const_iterator fbegin = fmts.begin();
102         FormatList::const_iterator fend   = fmts.end();
103         for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
104                 if (fit != fbegin)
105                         lyxerr[Debug::GRAPHICS] << ", ";
106                 lyxerr[Debug::GRAPHICS] << *fit;
107         }
108         lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
109
110         return fmts;
111 }
112
113
114 xformsImage::xformsImage()
115         : image_(0),
116           pixmap_(0),
117           pixmap_status_(PIXMAP_UNINITIALISED)
118 {}
119
120
121 xformsImage::xformsImage(xformsImage const & other)
122         : Image(other),
123           image_(0),
124           pixmap_(0),
125           pixmap_status_(PIXMAP_UNINITIALISED)
126 {
127         if (other.image_) {
128                 image_ = flimage_dup(other.image_);
129                 image_->u_vdata = this;
130         }
131 }
132
133
134 xformsImage::~xformsImage()
135 {
136         if (image_)
137                 flimage_free(image_);
138         if (pixmap_)
139                 XFreePixmap(fl_get_display(), pixmap_);
140 }
141
142
143 Image * xformsImage::clone() const
144 {
145         return new xformsImage(*this);
146 }
147
148
149 unsigned int xformsImage::getWidth() const
150 {
151         if (!image_)
152                 return 0;
153         return image_->w;
154 }
155
156
157 unsigned int xformsImage::getHeight() const
158 {
159         if (!image_)
160                 return 0;
161         return image_->h;
162 }
163
164
165 Pixmap xformsImage::getPixmap() const
166 {
167         if (!pixmap_status_ == PIXMAP_SUCCESS)
168                 return 0;
169         return pixmap_;
170 }
171
172
173 void xformsImage::load(string const & filename)
174 {
175         if (image_) {
176                 lyxerr[Debug::GRAPHICS]
177                         << "Image is loaded already!" << std::endl;
178                 finishedLoading(false);
179                 return;
180         }
181
182         image_ = flimage_open(filename.c_str());
183         if (!image_) {
184                 lyxerr[Debug::GRAPHICS]
185                         << "Unable to open image" << std::endl;
186                 finishedLoading(false);
187                 return;
188         }
189
190         // Set this now and we won't need to bother again.
191         image_->fill_color = packedcolor(LColor::graphicsbg);
192
193         // Used by the callback routines to return to this
194         image_->u_vdata = this;
195
196         // Begin the reading process.
197         flimage_read(image_);
198 }
199
200
201 bool xformsImage::setPixmap(Params const & params)
202 {
203         if (!image_ || params.display == NoDisplay)
204                 return false;
205
206         Display * display = fl_get_display();
207
208         if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
209                 XFreePixmap(display, pixmap_);
210
211         int color_key;
212         switch (params.display) {
213         case MonochromeDisplay:
214                 color_key = FL_IMAGE_MONO;
215                 break;
216         case GrayscaleDisplay:
217                 color_key = FL_IMAGE_GRAY;
218                 break;
219         case ColorDisplay:
220         default: // NoDisplay cannot happen!
221                 color_key = FL_IMAGE_RGB;
222                 break;
223         }
224
225         if (color_key != FL_IMAGE_RGB) {
226                 flimage_convert(image_, color_key, 0);
227         }
228
229         unsigned int fill = packedcolor(LColor::graphicsbg);
230         if (fill != image_->fill_color) {
231                 // the background color has changed.
232                 // Note that in grayscale/monochrome images the background is
233                 // grayed also, so this call will have no visible effect. Sorry!
234                 flimage_replace_pixel(image_, image_->fill_color, fill);
235                 image_->fill_color = fill;
236         }
237
238         image_->xdisplay = display;
239         Screen * screen  = ScreenOfDisplay(display, fl_screen);
240
241         pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
242         pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
243
244         return pixmap_status_ == PIXMAP_SUCCESS;
245 }
246
247
248 void xformsImage::clip(Params const & params)
249 {
250         if (!image_)
251                 return;
252
253         if (params.bb.empty())
254                 // No clipping is necessary.
255                 return;
256
257         int const new_width  = params.bb.xr - params.bb.xl;
258         int const new_height = params.bb.yt - params.bb.yb;
259
260         // No need to check if the width, height are > 0 because the
261         // Bounding Box would be empty() in this case.
262         if (new_width > image_->w || new_height > image_->h)
263                 // Bounds are invalid.
264                 return;
265
266         if (new_width == image_->w && new_height == image_->h)
267                 // Bounds are unchanged.
268                 return;
269
270         int const xoffset_l = std::max(0, params.bb.xl);
271         int const xoffset_r = std::max(0, image_->w - params.bb.xr);
272         int const yoffset_t = std::max(0, image_->h - params.bb.yt);
273         int const yoffset_b = std::max(0, params.bb.yb);
274
275         flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
276 }
277
278
279 void xformsImage::rotate(Params const & params)
280 {
281         if (!image_)
282                 return ;
283
284         if (!params.angle)
285                 // No rotation is necessary.
286                 return;
287
288         // The angle passed to flimage_rotate is the angle in one-tenth of a
289         // degree units.
290
291         // Work around xforms bug when params.angle == 270
292         // the 'InternalError: bad special angle' error.
293         // This bug fix is not needed in xforms 1.0 and greater.
294         if (params.angle == 270) {
295                 flimage_rotate(image_,  900, FLIMAGE_SUBPIXEL);
296                 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
297         } else {
298                 flimage_rotate(image_, params.angle * 10, FLIMAGE_SUBPIXEL);
299         }
300 }
301
302
303 void xformsImage::scale(Params const & params)
304 {
305         if (!image_)
306                 return;
307
308         // boost::tie produces horrible compilation errors on my machine
309         // Angus 25 Feb 2002
310         std::pair<unsigned int, unsigned int> d = getScaledDimensions(params);
311         unsigned int const width  = d.first;
312         unsigned int const height = d.second;
313
314         if (width == getWidth() && height == getHeight())
315                 // No scaling needed
316                 return;
317
318         flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
319 }
320
321
322 void xformsImage::statusCB(string const & status_message)
323 {
324         if (status_message.empty())
325                 return;
326
327         if (prefixIs(status_message, "Done Reading")) {
328                 if (image_) {
329                         flimage_close(image_);
330                 }
331
332                 finishedLoading(true);
333         }
334 }
335
336
337 void xformsImage::errorCB(string const & error_message)
338 {
339         if (error_message.empty())
340                 return;
341
342         if (image_) {
343                 flimage_close(image_);
344         }
345
346         finishedLoading(false);
347 }
348
349 } // namespace grfx
350
351
352 namespace {
353
354 extern "C" {
355
356 int status_report(FL_IMAGE * ob, const char *s)
357 {
358         lyx::Assert(ob && ob->u_vdata);
359
360         string const str = s ? strip(s) : string();
361         if (str.empty())
362                 return 0;
363
364         lyxerr[Debug::GRAPHICS]
365                 << "xforms image loader. Status : " << str << std::endl;
366
367         grfx::xformsImage * ptr =
368                 static_cast<grfx::xformsImage *>(ob->u_vdata);
369         ptr->statusCB(str);
370
371         return 0;
372 }
373
374
375 static void error_report(FL_IMAGE * ob, const char *s)
376 {
377         lyx::Assert(ob && ob->u_vdata);
378
379         string const str = s ? strip(s) : string();
380         if (str.empty())
381                 return;
382
383         lyxerr[Debug::GRAPHICS]
384                 << "xforms image loader. Error : " << str << std::endl;
385
386         grfx::xformsImage * ptr =
387                 static_cast<grfx::xformsImage *>(ob->u_vdata);
388         ptr->errorCB(str);
389 }
390
391 } // extern "C"
392
393
394 void init_graphics()
395 {
396         // Paranoia check
397         static bool initialised = false;
398         if (initialised)
399                 return;
400         initialised = true;
401
402         flimage_enable_bmp();
403         flimage_enable_fits();
404         flimage_enable_gif();
405 #ifdef HAVE_FLIMAGE_ENABLE_JPEG
406         flimage_enable_jpeg();
407 #endif
408
409         // xforms itself uses pngtopnm to convert to a loadable format.
410         // We prefer to use our own conversion mechanism, therefore.
411         // flimage_enable_png();
412
413         flimage_enable_pnm();
414
415 #ifdef HAVE_FLIMAGE_ENABLE_PS
416         // xforms recognises PS but not EPS
417         flimage_enable_ps();
418 #endif
419
420         flimage_enable_sgi();
421         flimage_enable_tiff();
422         flimage_enable_xbm();
423         flimage_enable_xwd();
424         flimage_enable_xpm();
425
426         // xforms stores this permanently (does not make a copy) so
427         // this should never be destroyed.
428         static FLIMAGE_SETUP setup;
429         setup.visual_cue    = status_report;
430         setup.error_message = error_report;
431         flimage_setup(&setup);
432 }
433
434
435 unsigned int packedcolor(LColor::color c)
436 {
437         string const x11color = lcolor.getX11Name(c);
438
439         Display * display = fl_get_display();
440         Colormap cmap     = fl_state[fl_get_vclass()].colormap;
441         XColor xcol;
442         XColor ccol;
443         if (XLookupColor(display, cmap, x11color.c_str(), &xcol, &ccol) == 0)
444                 // Unable to parse x11color.
445                 return FL_PACK(255,255,255);
446
447         // Note that X stores the RGB values in the range 0 - 65535
448         // whilst we require them in the range 0 - 255.
449         unsigned int const r = xcol.red   / 256;
450         unsigned int const g = xcol.green / 256;
451         unsigned int const b = xcol.blue  / 256;
452
453         return FL_PACK(r, g, b);
454 }
455
456 } // namespace anon