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>
39 using lyx::support::float_equal;
40 using lyx::support::prefixIs;
41 using lyx::support::rtrim;
51 unsigned int packedcolor(LColor::color c);
59 /// Access to this class is through this static method.
60 Image::ImagePtr xformsImage::newImage()
65 ptr.reset(new xformsImage);
70 /// Return the list of loadable formats.
71 Image::FormatList xformsImage::loadableFormats()
73 static FormatList fmts;
79 // The formats recognised by LyX
80 Formats::const_iterator begin = formats.begin();
81 Formats::const_iterator end = formats.end();
83 lyxerr[Debug::GRAPHICS]
84 << "\nThe image loader can load the following directly:\n";
86 // Don't forget the Fortran numbering used by xforms!
87 int const nformats = flimage_get_number_of_formats();
88 for (int i = 1; i <= nformats; ++i) {
90 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
91 string const formal_name =
92 info->formal_name ? info->formal_name : string();
94 info->extension ? info->extension : string();
96 if (ext.empty() || ext == "gz")
99 if (ext == "rgb") ext = "sgi";
101 lyxerr[Debug::GRAPHICS]
102 << formal_name << ", extension \"" << ext << "\"\n";
104 Formats::const_iterator it =
106 lyx::compare_memfun(&Format::extension, ext));
108 fmts.push_back(it->name());
111 lyxerr[Debug::GRAPHICS]
112 << "\nOf these, LyX recognises the following formats:\n";
114 FormatList::const_iterator fbegin = fmts.begin();
115 FormatList::const_iterator fend = fmts.end();
116 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
118 lyxerr[Debug::GRAPHICS] << ", ";
119 lyxerr[Debug::GRAPHICS] << *fit;
121 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
127 xformsImage::xformsImage()
130 pixmap_status_(PIXMAP_UNINITIALISED)
134 xformsImage::xformsImage(xformsImage const & other)
138 pixmap_status_(PIXMAP_UNINITIALISED)
141 image_ = flimage_dup(other.image_);
142 image_->u_vdata = this;
147 xformsImage::~xformsImage()
150 flimage_free(image_);
152 XFreePixmap(fl_get_display(), pixmap_);
156 Pixmap xformsImage::getPixmap() const
158 if (!pixmap_status_ == PIXMAP_SUCCESS)
164 Image * xformsImage::clone_impl() const
166 return new xformsImage(*this);
170 unsigned int xformsImage::getWidth_impl() const
179 unsigned int xformsImage::getHeight_impl() const
187 bool xformsImage::isDrawable_impl() const
193 void xformsImage::load_impl(string const & filename)
196 lyxerr[Debug::GRAPHICS]
197 << "Image is loaded already!" << std::endl;
198 finishedLoading(false);
202 image_ = flimage_open(filename.c_str());
204 lyxerr[Debug::GRAPHICS]
205 << "Unable to open image" << std::endl;
206 finishedLoading(false);
210 // Set this now and we won't need to bother again.
211 image_->fill_color = packedcolor(LColor::graphicsbg);
213 // Used by the callback routines to return to this
214 image_->u_vdata = this;
216 // Begin the reading process.
217 flimage_read(image_);
221 bool xformsImage::setPixmap_impl(Params const & params)
223 if (!image_ || params.display == NoDisplay)
226 Display * display = fl_get_display();
228 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
229 XFreePixmap(display, pixmap_);
232 switch (params.display) {
233 case MonochromeDisplay:
234 color_key = FL_IMAGE_MONO;
236 case GrayscaleDisplay:
237 color_key = FL_IMAGE_GRAY;
240 default: // NoDisplay cannot happen!
241 color_key = FL_IMAGE_RGB;
245 if (color_key != FL_IMAGE_RGB) {
246 flimage_convert(image_, color_key, 0);
249 unsigned int fill = packedcolor(LColor::graphicsbg);
250 if (fill != image_->fill_color) {
251 // the background color has changed.
252 // Note that in grayscale/monochrome images the background is
253 // grayed also, so this call will have no visible effect. Sorry!
254 flimage_replace_pixel(image_, image_->fill_color, fill);
255 image_->fill_color = fill;
258 image_->xdisplay = display;
259 Screen * screen = ScreenOfDisplay(display, fl_screen);
261 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
262 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
264 return pixmap_status_ == PIXMAP_SUCCESS;
268 void xformsImage::clip_impl(Params const & params)
273 if (params.bb.empty())
274 // No clipping is necessary.
277 int const new_width = params.bb.xr - params.bb.xl;
278 int const new_height = params.bb.yt - params.bb.yb;
280 // No need to check if the width, height are > 0 because the
281 // Bounding Box would be empty() in this case.
282 if (new_width > image_->w || new_height > image_->h)
283 // Bounds are invalid.
286 if (new_width == image_->w && new_height == image_->h)
287 // Bounds are unchanged.
290 // flimage.h: image_ members w and h are of type int
291 // (though always >= 0)
292 // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
293 // type unsigned int.
294 // We must, therefore, be careful...
295 int const xoffset_l = params.bb.xl;
296 int const yoffset_b = params.bb.yb;
297 int const xoffset_r = image_->w > int(params.bb.xr) ?
298 image_->w - params.bb.xr : 0;
299 int const yoffset_t = image_->h > int(params.bb.yt) ?
300 image_->h - params.bb.yt : 0;
302 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
306 void xformsImage::rotate_impl(Params const & params)
311 if (float_equal(params.angle, 0.0, 0.1))
312 // No rotation is necessary.
315 // The angle passed to flimage_rotate is the angle in one-tenth of a
318 // Work around xforms bug when params.angle == 270
319 // the 'InternalError: bad special angle' error.
320 // This bug fix is not needed in xforms 1.0 and greater.
321 if (float_equal(params.angle, 270.0, 0.1)) {
322 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
323 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
325 flimage_rotate(image_,
326 int(params.angle * 10),
332 void xformsImage::scale_impl(Params const & params)
339 boost::tie(width, height) = getScaledDimensions(params);
341 if (width == getWidth() && height == getHeight())
345 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
349 void xformsImage::statusCB(string const & status_message)
351 if (status_message.empty())
354 if (prefixIs(status_message, "Done Reading")) {
356 flimage_close(image_);
359 finishedLoading(true);
364 void xformsImage::errorCB(string const & error_message)
366 if (error_message.empty())
370 flimage_close(image_);
373 finishedLoading(false);
376 } // namespace graphics
384 int status_report(FL_IMAGE * ob, const char *s)
386 BOOST_ASSERT(ob && ob->u_vdata);
388 string const str = s ? rtrim(s) : string();
392 lyxerr[Debug::GRAPHICS]
393 << "xforms image loader. Status: " << str << std::endl;
395 lyx::graphics::xformsImage * ptr =
396 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
403 static void error_report(FL_IMAGE * ob, const char *s)
405 BOOST_ASSERT(ob && ob->u_vdata);
407 string const str = s ? rtrim(s) : string();
411 lyxerr[Debug::GRAPHICS]
412 << "xforms image loader. Error: " << str << std::endl;
414 lyx::graphics::xformsImage * ptr =
415 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
425 static bool initialised = false;
430 flimage_enable_bmp();
431 flimage_enable_fits();
432 flimage_enable_gif();
433 #ifdef USE_JPEG_IMAGE_LOADER
434 flimage_enable_jpeg();
437 // xforms itself uses pngtopnm to convert to a loadable format.
438 // We prefer to use our own conversion mechanism, therefore.
439 // flimage_enable_png();
441 flimage_enable_pnm();
443 // xforms recognises PS but not EPS
444 // It dies horribly with lots of older PostScript files.
445 // Easiest, therefore, to disable PS support and insist that a PS-type
446 // file is converted to a bitmap format.
447 // flimage_enable_ps();
449 flimage_enable_sgi();
450 flimage_enable_tiff();
451 flimage_enable_xbm();
452 flimage_enable_xwd();
453 // xforms can load most XPM files, but will occasionally crash
454 // with some files created by ImakeMagick's convert program.
455 // Turn off xpm support for the time being.
456 // flimage_enable_xpm();
458 // xforms stores this permanently (does not make a copy) so
459 // this should never be destroyed.
460 static FLIMAGE_SETUP setup;
461 setup.visual_cue = status_report;
462 setup.error_message = error_report;
463 flimage_setup(&setup);
467 unsigned int packedcolor(LColor::color col)
469 unsigned int r, g, b;
470 bool const success = getRGBColor(col, r, g, b);
472 // Set to black on failure
473 return FL_PACK(255, 255, 255);
475 return FL_PACK(r, g, b);