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
14 #include "xformsImage.h"
15 #include "graphics/GraphicsParams.h"
19 #include "support/LAssert.h"
20 #include "support/lyxfunctional.h" // compare_memfun
22 #include "lyx_forms.h"
27 # ifdef HAVE_X11_FLIMAGE_H
28 # include <X11/flimage.h>
32 #include <boost/tuple/tuple.hpp>
40 unsigned int packedcolor(LColor::color c);
47 /// Access to this class is through this static method.
48 Image::ImagePtr xformsImage::newImage()
53 ptr.reset(new xformsImage);
58 /// Return the list of loadable formats.
59 Image::FormatList xformsImage::loadableFormats()
61 static FormatList fmts;
67 // The formats recognised by LyX
68 Formats::const_iterator begin = formats.begin();
69 Formats::const_iterator end = formats.end();
71 lyxerr[Debug::GRAPHICS]
72 << "\nThe image loader can load the following directly:\n";
74 // Don't forget the Fortran numbering used by xforms!
75 int const nformats = flimage_get_number_of_formats();
76 for (int i = 1; i <= nformats; ++i) {
78 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
79 string const formal_name =
80 info->formal_name ? info->formal_name : string();
82 info->extension ? info->extension : string();
84 if (ext.empty() || ext == "gz")
87 if (ext == "rgb") ext = "sgi";
89 lyxerr[Debug::GRAPHICS]
90 << formal_name << ", extension \"" << ext << "\"\n";
92 Formats::const_iterator it =
94 lyx::compare_memfun(&Format::extension, ext));
96 fmts.push_back(it->name());
99 lyxerr[Debug::GRAPHICS]
100 << "\nOf these, LyX recognises the following formats:\n";
102 FormatList::const_iterator fbegin = fmts.begin();
103 FormatList::const_iterator fend = fmts.end();
104 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
106 lyxerr[Debug::GRAPHICS] << ", ";
107 lyxerr[Debug::GRAPHICS] << *fit;
109 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
115 xformsImage::xformsImage()
118 pixmap_status_(PIXMAP_UNINITIALISED)
122 xformsImage::xformsImage(xformsImage const & other)
126 pixmap_status_(PIXMAP_UNINITIALISED)
129 image_ = flimage_dup(other.image_);
130 image_->u_vdata = this;
135 xformsImage::~xformsImage()
138 flimage_free(image_);
140 XFreePixmap(fl_get_display(), pixmap_);
144 Image * xformsImage::clone() const
146 return new xformsImage(*this);
150 unsigned int xformsImage::getWidth() const
159 unsigned int xformsImage::getHeight() const
167 bool xformsImage::isDrawable() const
173 Pixmap xformsImage::getPixmap() const
175 if (!pixmap_status_ == PIXMAP_SUCCESS)
181 void xformsImage::load(string const & filename)
184 lyxerr[Debug::GRAPHICS]
185 << "Image is loaded already!" << std::endl;
186 finishedLoading(false);
190 image_ = flimage_open(filename.c_str());
192 lyxerr[Debug::GRAPHICS]
193 << "Unable to open image" << std::endl;
194 finishedLoading(false);
198 // Set this now and we won't need to bother again.
199 image_->fill_color = packedcolor(LColor::graphicsbg);
201 // Used by the callback routines to return to this
202 image_->u_vdata = this;
204 // Begin the reading process.
205 flimage_read(image_);
209 bool xformsImage::setPixmap(Params const & params)
211 if (!image_ || params.display == NoDisplay)
214 Display * display = fl_get_display();
216 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
217 XFreePixmap(display, pixmap_);
220 switch (params.display) {
221 case MonochromeDisplay:
222 color_key = FL_IMAGE_MONO;
224 case GrayscaleDisplay:
225 color_key = FL_IMAGE_GRAY;
228 default: // NoDisplay cannot happen!
229 color_key = FL_IMAGE_RGB;
233 if (color_key != FL_IMAGE_RGB) {
234 flimage_convert(image_, color_key, 0);
237 unsigned int fill = packedcolor(LColor::graphicsbg);
238 if (fill != image_->fill_color) {
239 // the background color has changed.
240 // Note that in grayscale/monochrome images the background is
241 // grayed also, so this call will have no visible effect. Sorry!
242 flimage_replace_pixel(image_, image_->fill_color, fill);
243 image_->fill_color = fill;
246 image_->xdisplay = display;
247 Screen * screen = ScreenOfDisplay(display, fl_screen);
249 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
250 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
252 return pixmap_status_ == PIXMAP_SUCCESS;
256 void xformsImage::clip(Params const & params)
261 if (params.bb.empty())
262 // No clipping is necessary.
265 int const new_width = params.bb.xr - params.bb.xl;
266 int const new_height = params.bb.yt - params.bb.yb;
268 // No need to check if the width, height are > 0 because the
269 // Bounding Box would be empty() in this case.
270 if (new_width > image_->w || new_height > image_->h)
271 // Bounds are invalid.
274 if (new_width == image_->w && new_height == image_->h)
275 // Bounds are unchanged.
278 // flimage.h: image_ members w and h are of type int
279 // (though always >= 0)
280 // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
281 // type unsigned int.
282 // We must, therefore, be careful...
283 int const xoffset_l = params.bb.xl;
284 int const yoffset_b = params.bb.yb;
285 int const xoffset_r = image_->w > int(params.bb.xr) ?
286 image_->w - params.bb.xr : 0;
287 int const yoffset_t = image_->h > int(params.bb.yt) ?
288 image_->h - params.bb.yt : 0;
290 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
294 void xformsImage::rotate(Params const & params)
299 if (lyx::float_equal(params.angle, 0.0, 0.1))
300 // No rotation is necessary.
303 // The angle passed to flimage_rotate is the angle in one-tenth of a
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 (lyx::float_equal(params.angle, 270.0, 0.1)) {
310 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
311 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
313 flimage_rotate(image_,
314 int(params.angle * 10),
320 void xformsImage::scale(Params const & params)
327 boost::tie(width, height) = getScaledDimensions(params);
329 if (width == getWidth() && height == getHeight())
333 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
337 void xformsImage::statusCB(string const & status_message)
339 if (status_message.empty())
342 if (prefixIs(status_message, "Done Reading")) {
344 flimage_close(image_);
347 finishedLoading(true);
352 void xformsImage::errorCB(string const & error_message)
354 if (error_message.empty())
358 flimage_close(image_);
361 finishedLoading(false);
371 int status_report(FL_IMAGE * ob, const char *s)
373 lyx::Assert(ob && ob->u_vdata);
375 string const str = s ? rtrim(s) : string();
379 lyxerr[Debug::GRAPHICS]
380 << "xforms image loader. Status: " << str << std::endl;
382 grfx::xformsImage * ptr =
383 static_cast<grfx::xformsImage *>(ob->u_vdata);
390 static void error_report(FL_IMAGE * ob, const char *s)
392 lyx::Assert(ob && ob->u_vdata);
394 string const str = s ? rtrim(s) : string();
398 lyxerr[Debug::GRAPHICS]
399 << "xforms image loader. Error: " << str << std::endl;
401 grfx::xformsImage * ptr =
402 static_cast<grfx::xformsImage *>(ob->u_vdata);
412 static bool initialised = false;
417 flimage_enable_bmp();
418 flimage_enable_fits();
419 flimage_enable_gif();
420 #ifdef USE_JPEG_IMAGE_LOADER
421 flimage_enable_jpeg();
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();
428 flimage_enable_pnm();
430 // xforms recognises PS but not EPS
431 // It dies horribly with lots of older PostScript files.
432 // Easiest, therefore, to disable PS support and insist that a PS-type
433 // file is converted to a bitmap format.
434 // flimage_enable_ps();
436 flimage_enable_sgi();
437 flimage_enable_tiff();
438 flimage_enable_xbm();
439 flimage_enable_xwd();
440 // xforms can load most XPM files, but will occasionally crash
441 // with some files created by ImakeMagick's convert program.
442 // Turn off xpm support for the time being.
443 // flimage_enable_xpm();
445 // xforms stores this permanently (does not make a copy) so
446 // this should never be destroyed.
447 static FLIMAGE_SETUP setup;
448 setup.visual_cue = status_report;
449 setup.error_message = error_report;
450 flimage_setup(&setup);
454 unsigned int packedcolor(LColor::color col)
456 unsigned int r, g, b;
457 bool const success = getRGBColor(col, r, g, b);
459 // Set to black on failure
460 return FL_PACK(255,255,255);
462 return FL_PACK(r, g, b);