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"
14 #include "graphics/GraphicsParams.h"
18 #include "support/lstrings.h"
19 #include "support/lyxfunctional.h" // compare_memfun
20 #include "support/lyxlib.h"
22 #include "lyx_forms.h"
27 # ifdef HAVE_X11_FLIMAGE_H
28 # include <X11/flimage.h>
32 #include <boost/tuple/tuple.hpp>
34 using lyx::support::float_equal;
35 using lyx::support::prefixIs;
36 using lyx::support::rtrim;
45 unsigned int packedcolor(LColor::color c);
53 /// Access to this class is through this static method.
54 Image::ImagePtr xformsImage::newImage()
59 ptr.reset(new xformsImage);
64 /// Return the list of loadable formats.
65 Image::FormatList xformsImage::loadableFormats()
67 static FormatList fmts;
73 // The formats recognised by LyX
74 Formats::const_iterator begin = formats.begin();
75 Formats::const_iterator end = formats.end();
77 lyxerr[Debug::GRAPHICS]
78 << "\nThe image loader can load the following directly:\n";
80 // Don't forget the Fortran numbering used by xforms!
81 int const nformats = flimage_get_number_of_formats();
82 for (int i = 1; i <= nformats; ++i) {
84 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
85 string const formal_name =
86 info->formal_name ? info->formal_name : string();
88 info->extension ? info->extension : string();
90 if (ext.empty() || ext == "gz")
93 if (ext == "rgb") ext = "sgi";
95 lyxerr[Debug::GRAPHICS]
96 << formal_name << ", extension \"" << ext << "\"\n";
98 Formats::const_iterator it =
100 lyx::compare_memfun(&Format::extension, ext));
102 fmts.push_back(it->name());
105 lyxerr[Debug::GRAPHICS]
106 << "\nOf these, LyX recognises the following formats:\n";
108 FormatList::const_iterator fbegin = fmts.begin();
109 FormatList::const_iterator fend = fmts.end();
110 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
112 lyxerr[Debug::GRAPHICS] << ", ";
113 lyxerr[Debug::GRAPHICS] << *fit;
115 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
121 xformsImage::xformsImage()
124 pixmap_status_(PIXMAP_UNINITIALISED)
128 xformsImage::xformsImage(xformsImage const & other)
132 pixmap_status_(PIXMAP_UNINITIALISED)
135 image_ = flimage_dup(other.image_);
136 image_->u_vdata = this;
141 xformsImage::~xformsImage()
144 flimage_free(image_);
146 XFreePixmap(fl_get_display(), pixmap_);
150 Image * xformsImage::clone() const
152 return new xformsImage(*this);
156 unsigned int xformsImage::getWidth() const
165 unsigned int xformsImage::getHeight() const
173 bool xformsImage::isDrawable() const
179 Pixmap xformsImage::getPixmap() const
181 if (!pixmap_status_ == PIXMAP_SUCCESS)
187 void xformsImage::load(string const & filename)
190 lyxerr[Debug::GRAPHICS]
191 << "Image is loaded already!" << std::endl;
192 finishedLoading(false);
196 image_ = flimage_open(filename.c_str());
198 lyxerr[Debug::GRAPHICS]
199 << "Unable to open image" << std::endl;
200 finishedLoading(false);
204 // Set this now and we won't need to bother again.
205 image_->fill_color = packedcolor(LColor::graphicsbg);
207 // Used by the callback routines to return to this
208 image_->u_vdata = this;
210 // Begin the reading process.
211 flimage_read(image_);
215 bool xformsImage::setPixmap(Params const & params)
217 if (!image_ || params.display == NoDisplay)
220 Display * display = fl_get_display();
222 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
223 XFreePixmap(display, pixmap_);
226 switch (params.display) {
227 case MonochromeDisplay:
228 color_key = FL_IMAGE_MONO;
230 case GrayscaleDisplay:
231 color_key = FL_IMAGE_GRAY;
234 default: // NoDisplay cannot happen!
235 color_key = FL_IMAGE_RGB;
239 if (color_key != FL_IMAGE_RGB) {
240 flimage_convert(image_, color_key, 0);
243 unsigned int fill = packedcolor(LColor::graphicsbg);
244 if (fill != image_->fill_color) {
245 // the background color has changed.
246 // Note that in grayscale/monochrome images the background is
247 // grayed also, so this call will have no visible effect. Sorry!
248 flimage_replace_pixel(image_, image_->fill_color, fill);
249 image_->fill_color = fill;
252 image_->xdisplay = display;
253 Screen * screen = ScreenOfDisplay(display, fl_screen);
255 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
256 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
258 return pixmap_status_ == PIXMAP_SUCCESS;
262 void xformsImage::clip(Params const & params)
267 if (params.bb.empty())
268 // No clipping is necessary.
271 int const new_width = params.bb.xr - params.bb.xl;
272 int const new_height = params.bb.yt - params.bb.yb;
274 // No need to check if the width, height are > 0 because the
275 // Bounding Box would be empty() in this case.
276 if (new_width > image_->w || new_height > image_->h)
277 // Bounds are invalid.
280 if (new_width == image_->w && new_height == image_->h)
281 // Bounds are unchanged.
284 // flimage.h: image_ members w and h are of type int
285 // (though always >= 0)
286 // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
287 // type unsigned int.
288 // We must, therefore, be careful...
289 int const xoffset_l = params.bb.xl;
290 int const yoffset_b = params.bb.yb;
291 int const xoffset_r = image_->w > int(params.bb.xr) ?
292 image_->w - params.bb.xr : 0;
293 int const yoffset_t = image_->h > int(params.bb.yt) ?
294 image_->h - params.bb.yt : 0;
296 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
300 void xformsImage::rotate(Params const & params)
305 if (float_equal(params.angle, 0.0, 0.1))
306 // No rotation is necessary.
309 // The angle passed to flimage_rotate is the angle in one-tenth of a
312 // Work around xforms bug when params.angle == 270
313 // the 'InternalError: bad special angle' error.
314 // This bug fix is not needed in xforms 1.0 and greater.
315 if (float_equal(params.angle, 270.0, 0.1)) {
316 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
317 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
319 flimage_rotate(image_,
320 int(params.angle * 10),
326 void xformsImage::scale(Params const & params)
333 boost::tie(width, height) = getScaledDimensions(params);
335 if (width == getWidth() && height == getHeight())
339 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
343 void xformsImage::statusCB(string const & status_message)
345 if (status_message.empty())
348 if (prefixIs(status_message, "Done Reading")) {
350 flimage_close(image_);
353 finishedLoading(true);
358 void xformsImage::errorCB(string const & error_message)
360 if (error_message.empty())
364 flimage_close(image_);
367 finishedLoading(false);
370 } // namespace graphics
378 int status_report(FL_IMAGE * ob, const char *s)
380 BOOST_ASSERT(ob && ob->u_vdata);
382 string const str = s ? rtrim(s) : string();
386 lyxerr[Debug::GRAPHICS]
387 << "xforms image loader. Status: " << str << std::endl;
389 lyx::graphics::xformsImage * ptr =
390 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
397 static void error_report(FL_IMAGE * ob, const char *s)
399 BOOST_ASSERT(ob && ob->u_vdata);
401 string const str = s ? rtrim(s) : string();
405 lyxerr[Debug::GRAPHICS]
406 << "xforms image loader. Error: " << str << std::endl;
408 lyx::graphics::xformsImage * ptr =
409 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
419 static bool initialised = false;
424 flimage_enable_bmp();
425 flimage_enable_fits();
426 flimage_enable_gif();
427 #ifdef USE_JPEG_IMAGE_LOADER
428 flimage_enable_jpeg();
431 // xforms itself uses pngtopnm to convert to a loadable format.
432 // We prefer to use our own conversion mechanism, therefore.
433 // flimage_enable_png();
435 flimage_enable_pnm();
437 // xforms recognises PS but not EPS
438 // It dies horribly with lots of older PostScript files.
439 // Easiest, therefore, to disable PS support and insist that a PS-type
440 // file is converted to a bitmap format.
441 // flimage_enable_ps();
443 flimage_enable_sgi();
444 flimage_enable_tiff();
445 flimage_enable_xbm();
446 flimage_enable_xwd();
447 // xforms can load most XPM files, but will occasionally crash
448 // with some files created by ImakeMagick's convert program.
449 // Turn off xpm support for the time being.
450 // flimage_enable_xpm();
452 // xforms stores this permanently (does not make a copy) so
453 // this should never be destroyed.
454 static FLIMAGE_SETUP setup;
455 setup.visual_cue = status_report;
456 setup.error_message = error_report;
457 flimage_setup(&setup);
461 unsigned int packedcolor(LColor::color col)
463 unsigned int r, g, b;
464 bool const success = getRGBColor(col, r, g, b);
466 // Set to black on failure
467 return FL_PACK(255,255,255);
469 return FL_PACK(r, g, b);