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/lyxlib.h"
25 #include "lyx_forms.h"
30 # ifdef HAVE_X11_FLIMAGE_H
31 # include <X11/flimage.h>
35 #include <boost/bind.hpp>
36 #include <boost/tuple/tuple.hpp>
38 using lyx::frontend::getRGBColor;
40 using lyx::support::float_equal;
41 using lyx::support::prefixIs;
42 using lyx::support::rtrim;
55 unsigned int packedcolor(LColor::color c);
63 /// Access to this class is through this static method.
64 Image::ImagePtr xformsImage::newImage()
69 ptr.reset(new xformsImage);
74 /// Return the list of loadable formats.
75 Image::FormatList xformsImage::loadableFormats()
77 static FormatList fmts;
83 // The formats recognised by LyX
84 Formats::const_iterator begin = formats.begin();
85 Formats::const_iterator end = formats.end();
87 lyxerr[Debug::GRAPHICS]
88 << "\nThe image loader can load the following directly:\n";
90 // Don't forget the Fortran numbering used by xforms!
91 int const nformats = flimage_get_number_of_formats();
92 for (int i = 1; i <= nformats; ++i) {
94 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
95 string const formal_name =
96 info->formal_name ? info->formal_name : string();
98 info->extension ? info->extension : string();
100 if (ext.empty() || ext == "gz")
103 if (ext == "rgb") ext = "sgi";
105 lyxerr[Debug::GRAPHICS]
106 << formal_name << ", extension \"" << ext << "\"\n";
108 Formats::const_iterator it =
110 bind(equal_to<string>(),
111 bind(&Format::extension, _1),
114 fmts.push_back(it->name());
117 lyxerr[Debug::GRAPHICS]
118 << "\nOf these, LyX recognises the following formats:\n";
120 FormatList::const_iterator fbegin = fmts.begin();
121 FormatList::const_iterator fend = fmts.end();
122 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
124 lyxerr[Debug::GRAPHICS] << ", ";
125 lyxerr[Debug::GRAPHICS] << *fit;
127 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
133 xformsImage::xformsImage()
136 pixmap_status_(PIXMAP_UNINITIALISED)
140 xformsImage::xformsImage(xformsImage const & other)
144 pixmap_status_(PIXMAP_UNINITIALISED)
147 image_ = flimage_dup(other.image_);
148 image_->u_vdata = this;
153 xformsImage::~xformsImage()
156 flimage_free(image_);
158 XFreePixmap(fl_get_display(), pixmap_);
162 Pixmap xformsImage::getPixmap() const
164 if (!pixmap_status_ == PIXMAP_SUCCESS)
170 Image * xformsImage::clone_impl() const
172 return new xformsImage(*this);
176 unsigned int xformsImage::getWidth_impl() const
185 unsigned int xformsImage::getHeight_impl() const
193 bool xformsImage::isDrawable_impl() const
199 void xformsImage::load_impl(string const & filename)
202 lyxerr[Debug::GRAPHICS]
203 << "Image is loaded already!" << std::endl;
204 finishedLoading(false);
208 image_ = flimage_open(filename.c_str());
210 lyxerr[Debug::GRAPHICS]
211 << "Unable to open image" << std::endl;
212 finishedLoading(false);
216 // Set this now and we won't need to bother again.
217 image_->fill_color = packedcolor(LColor::graphicsbg);
219 // Used by the callback routines to return to this
220 image_->u_vdata = this;
222 // Begin the reading process.
223 flimage_read(image_);
227 bool xformsImage::setPixmap_impl(Params const & params)
229 if (!image_ || params.display == NoDisplay)
232 Display * display = fl_get_display();
234 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
235 XFreePixmap(display, pixmap_);
238 switch (params.display) {
239 case MonochromeDisplay:
240 color_key = FL_IMAGE_MONO;
242 case GrayscaleDisplay:
243 color_key = FL_IMAGE_GRAY;
246 default: // NoDisplay cannot happen!
247 color_key = FL_IMAGE_RGB;
251 if (color_key != FL_IMAGE_RGB) {
252 flimage_convert(image_, color_key, 0);
255 unsigned int fill = packedcolor(LColor::graphicsbg);
256 if (fill != image_->fill_color) {
257 // the background color has changed.
258 // Note that in grayscale/monochrome images the background is
259 // grayed also, so this call will have no visible effect. Sorry!
260 flimage_replace_pixel(image_, image_->fill_color, fill);
261 image_->fill_color = fill;
264 image_->xdisplay = display;
265 Screen * screen = ScreenOfDisplay(display, fl_screen);
267 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
268 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
270 return pixmap_status_ == PIXMAP_SUCCESS;
274 void xformsImage::clip_impl(Params const & params)
279 if (params.bb.empty())
280 // No clipping is necessary.
283 int const new_width = params.bb.xr - params.bb.xl;
284 int const new_height = params.bb.yt - params.bb.yb;
286 // No need to check if the width, height are > 0 because the
287 // Bounding Box would be empty() in this case.
288 if (new_width > image_->w || new_height > image_->h)
289 // Bounds are invalid.
292 if (new_width == image_->w && new_height == image_->h)
293 // Bounds are unchanged.
296 // flimage.h: image_ members w and h are of type int
297 // (though always >= 0)
298 // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
299 // type unsigned int.
300 // We must, therefore, be careful...
301 int const xoffset_l = params.bb.xl;
302 int const yoffset_b = params.bb.yb;
303 int const xoffset_r = image_->w > int(params.bb.xr) ?
304 image_->w - params.bb.xr : 0;
305 int const yoffset_t = image_->h > int(params.bb.yt) ?
306 image_->h - params.bb.yt : 0;
308 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
312 void xformsImage::rotate_impl(Params const & params)
317 if (float_equal(params.angle, 0.0, 0.1))
318 // No rotation is necessary.
321 // The angle passed to flimage_rotate is the angle in one-tenth of a
324 // Work around xforms bug when params.angle == 270
325 // the 'InternalError: bad special angle' error.
326 // This bug fix is not needed in xforms 1.0 and greater.
327 if (float_equal(params.angle, 270.0, 0.1)) {
328 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
329 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
331 flimage_rotate(image_,
332 int(params.angle * 10),
338 void xformsImage::scale_impl(Params const & params)
345 boost::tie(width, height) = getScaledDimensions(params);
347 if (width == getWidth() && height == getHeight())
351 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
355 void xformsImage::statusCB(string const & status_message)
357 if (status_message.empty())
360 if (prefixIs(status_message, "Done Reading")) {
362 flimage_close(image_);
365 finishedLoading(true);
370 void xformsImage::errorCB(string const & error_message)
372 if (error_message.empty())
376 flimage_close(image_);
379 finishedLoading(false);
382 } // namespace graphics
390 int status_report(FL_IMAGE * ob, const char *s)
392 BOOST_ASSERT(ob && ob->u_vdata);
394 string const str = s ? rtrim(s) : string();
398 lyxerr[Debug::GRAPHICS]
399 << "xforms image loader. Status: " << str << std::endl;
401 lyx::graphics::xformsImage * ptr =
402 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
409 static void error_report(FL_IMAGE * ob, const char *s)
411 BOOST_ASSERT(ob && ob->u_vdata);
413 string const str = s ? rtrim(s) : string();
417 lyxerr[Debug::GRAPHICS]
418 << "xforms image loader. Error: " << str << std::endl;
420 lyx::graphics::xformsImage * ptr =
421 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
431 static bool initialised = false;
436 flimage_enable_bmp();
437 flimage_enable_fits();
438 flimage_enable_gif();
439 #ifdef USE_JPEG_IMAGE_LOADER
440 flimage_enable_jpeg();
443 // xforms itself uses pngtopnm to convert to a loadable format.
444 // We prefer to use our own conversion mechanism, therefore.
445 // flimage_enable_png();
447 flimage_enable_pnm();
449 // xforms recognises PS but not EPS
450 // It dies horribly with lots of older PostScript files.
451 // Easiest, therefore, to disable PS support and insist that a PS-type
452 // file is converted to a bitmap format.
453 // flimage_enable_ps();
455 flimage_enable_sgi();
456 flimage_enable_tiff();
457 flimage_enable_xbm();
458 flimage_enable_xwd();
459 // xforms can load most XPM files, but will occasionally crash
460 // with some files created by ImakeMagick's convert program.
461 // Turn off xpm support for the time being.
462 // flimage_enable_xpm();
464 // xforms stores this permanently (does not make a copy) so
465 // this should never be destroyed.
466 static FLIMAGE_SETUP setup;
467 setup.visual_cue = status_report;
468 setup.error_message = error_report;
469 flimage_setup(&setup);
473 unsigned int packedcolor(LColor::color col)
475 unsigned int r, g, b;
476 bool const success = getRGBColor(col, r, g, b);
478 // Set to black on failure
479 return FL_PACK(255, 255, 255);
481 return FL_PACK(r, g, b);