3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "xformsImage.h"
20 #include "graphics/GraphicsParams.h"
22 #include "support/lstrings.h"
23 #include "support/lyxfunctional.h" // compare_memfun
24 #include "support/lyxlib.h"
26 #include "lyx_forms.h"
31 # ifdef HAVE_X11_FLIMAGE_H
32 # include <X11/flimage.h>
36 #include <boost/tuple/tuple.hpp>
38 using lyx::support::float_equal;
39 using lyx::support::prefixIs;
40 using lyx::support::rtrim;
49 unsigned int packedcolor(LColor::color c);
57 /// Access to this class is through this static method.
58 Image::ImagePtr xformsImage::newImage()
63 ptr.reset(new xformsImage);
68 /// Return the list of loadable formats.
69 Image::FormatList xformsImage::loadableFormats()
71 static FormatList fmts;
77 // The formats recognised by LyX
78 Formats::const_iterator begin = formats.begin();
79 Formats::const_iterator end = formats.end();
81 lyxerr[Debug::GRAPHICS]
82 << "\nThe image loader can load the following directly:\n";
84 // Don't forget the Fortran numbering used by xforms!
85 int const nformats = flimage_get_number_of_formats();
86 for (int i = 1; i <= nformats; ++i) {
88 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
89 string const formal_name =
90 info->formal_name ? info->formal_name : string();
92 info->extension ? info->extension : string();
94 if (ext.empty() || ext == "gz")
97 if (ext == "rgb") ext = "sgi";
99 lyxerr[Debug::GRAPHICS]
100 << formal_name << ", extension \"" << ext << "\"\n";
102 Formats::const_iterator it =
104 lyx::compare_memfun(&Format::extension, ext));
106 fmts.push_back(it->name());
109 lyxerr[Debug::GRAPHICS]
110 << "\nOf these, LyX recognises the following formats:\n";
112 FormatList::const_iterator fbegin = fmts.begin();
113 FormatList::const_iterator fend = fmts.end();
114 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
116 lyxerr[Debug::GRAPHICS] << ", ";
117 lyxerr[Debug::GRAPHICS] << *fit;
119 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
125 xformsImage::xformsImage()
128 pixmap_status_(PIXMAP_UNINITIALISED)
132 xformsImage::xformsImage(xformsImage const & other)
136 pixmap_status_(PIXMAP_UNINITIALISED)
139 image_ = flimage_dup(other.image_);
140 image_->u_vdata = this;
145 xformsImage::~xformsImage()
148 flimage_free(image_);
150 XFreePixmap(fl_get_display(), pixmap_);
154 Image * xformsImage::clone() const
156 return new xformsImage(*this);
160 unsigned int xformsImage::getWidth() const
169 unsigned int xformsImage::getHeight() const
177 bool xformsImage::isDrawable() const
183 Pixmap xformsImage::getPixmap() const
185 if (!pixmap_status_ == PIXMAP_SUCCESS)
191 void xformsImage::load(string const & filename)
194 lyxerr[Debug::GRAPHICS]
195 << "Image is loaded already!" << std::endl;
196 finishedLoading(false);
200 image_ = flimage_open(filename.c_str());
202 lyxerr[Debug::GRAPHICS]
203 << "Unable to open image" << std::endl;
204 finishedLoading(false);
208 // Set this now and we won't need to bother again.
209 image_->fill_color = packedcolor(LColor::graphicsbg);
211 // Used by the callback routines to return to this
212 image_->u_vdata = this;
214 // Begin the reading process.
215 flimage_read(image_);
219 bool xformsImage::setPixmap(Params const & params)
221 if (!image_ || params.display == NoDisplay)
224 Display * display = fl_get_display();
226 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
227 XFreePixmap(display, pixmap_);
230 switch (params.display) {
231 case MonochromeDisplay:
232 color_key = FL_IMAGE_MONO;
234 case GrayscaleDisplay:
235 color_key = FL_IMAGE_GRAY;
238 default: // NoDisplay cannot happen!
239 color_key = FL_IMAGE_RGB;
243 if (color_key != FL_IMAGE_RGB) {
244 flimage_convert(image_, color_key, 0);
247 unsigned int fill = packedcolor(LColor::graphicsbg);
248 if (fill != image_->fill_color) {
249 // the background color has changed.
250 // Note that in grayscale/monochrome images the background is
251 // grayed also, so this call will have no visible effect. Sorry!
252 flimage_replace_pixel(image_, image_->fill_color, fill);
253 image_->fill_color = fill;
256 image_->xdisplay = display;
257 Screen * screen = ScreenOfDisplay(display, fl_screen);
259 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
260 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
262 return pixmap_status_ == PIXMAP_SUCCESS;
266 void xformsImage::clip(Params const & params)
271 if (params.bb.empty())
272 // No clipping is necessary.
275 int const new_width = params.bb.xr - params.bb.xl;
276 int const new_height = params.bb.yt - params.bb.yb;
278 // No need to check if the width, height are > 0 because the
279 // Bounding Box would be empty() in this case.
280 if (new_width > image_->w || new_height > image_->h)
281 // Bounds are invalid.
284 if (new_width == image_->w && new_height == image_->h)
285 // Bounds are unchanged.
288 // flimage.h: image_ members w and h are of type int
289 // (though always >= 0)
290 // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
291 // type unsigned int.
292 // We must, therefore, be careful...
293 int const xoffset_l = params.bb.xl;
294 int const yoffset_b = params.bb.yb;
295 int const xoffset_r = image_->w > int(params.bb.xr) ?
296 image_->w - params.bb.xr : 0;
297 int const yoffset_t = image_->h > int(params.bb.yt) ?
298 image_->h - params.bb.yt : 0;
300 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
304 void xformsImage::rotate(Params const & params)
309 if (float_equal(params.angle, 0.0, 0.1))
310 // No rotation is necessary.
313 // The angle passed to flimage_rotate is the angle in one-tenth of a
316 // Work around xforms bug when params.angle == 270
317 // the 'InternalError: bad special angle' error.
318 // This bug fix is not needed in xforms 1.0 and greater.
319 if (float_equal(params.angle, 270.0, 0.1)) {
320 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
321 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
323 flimage_rotate(image_,
324 int(params.angle * 10),
330 void xformsImage::scale(Params const & params)
337 boost::tie(width, height) = getScaledDimensions(params);
339 if (width == getWidth() && height == getHeight())
343 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
347 void xformsImage::statusCB(string const & status_message)
349 if (status_message.empty())
352 if (prefixIs(status_message, "Done Reading")) {
354 flimage_close(image_);
357 finishedLoading(true);
362 void xformsImage::errorCB(string const & error_message)
364 if (error_message.empty())
368 flimage_close(image_);
371 finishedLoading(false);
374 } // namespace graphics
382 int status_report(FL_IMAGE * ob, const char *s)
384 BOOST_ASSERT(ob && ob->u_vdata);
386 string const str = s ? rtrim(s) : string();
390 lyxerr[Debug::GRAPHICS]
391 << "xforms image loader. Status: " << str << std::endl;
393 lyx::graphics::xformsImage * ptr =
394 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
401 static void error_report(FL_IMAGE * ob, const char *s)
403 BOOST_ASSERT(ob && ob->u_vdata);
405 string const str = s ? rtrim(s) : string();
409 lyxerr[Debug::GRAPHICS]
410 << "xforms image loader. Error: " << str << std::endl;
412 lyx::graphics::xformsImage * ptr =
413 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
423 static bool initialised = false;
428 flimage_enable_bmp();
429 flimage_enable_fits();
430 flimage_enable_gif();
431 #ifdef USE_JPEG_IMAGE_LOADER
432 flimage_enable_jpeg();
435 // xforms itself uses pngtopnm to convert to a loadable format.
436 // We prefer to use our own conversion mechanism, therefore.
437 // flimage_enable_png();
439 flimage_enable_pnm();
441 // xforms recognises PS but not EPS
442 // It dies horribly with lots of older PostScript files.
443 // Easiest, therefore, to disable PS support and insist that a PS-type
444 // file is converted to a bitmap format.
445 // flimage_enable_ps();
447 flimage_enable_sgi();
448 flimage_enable_tiff();
449 flimage_enable_xbm();
450 flimage_enable_xwd();
451 // xforms can load most XPM files, but will occasionally crash
452 // with some files created by ImakeMagick's convert program.
453 // Turn off xpm support for the time being.
454 // flimage_enable_xpm();
456 // xforms stores this permanently (does not make a copy) so
457 // this should never be destroyed.
458 static FLIMAGE_SETUP setup;
459 setup.visual_cue = status_report;
460 setup.error_message = error_report;
461 flimage_setup(&setup);
465 unsigned int packedcolor(LColor::color col)
467 unsigned int r, g, b;
468 bool const success = getRGBColor(col, r, g, b);
470 // Set to black on failure
471 return FL_PACK(255,255,255);
473 return FL_PACK(r, g, b);