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