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::frontend::getRGBColor;
40 using lyx::support::float_equal;
41 using lyx::support::prefixIs;
42 using lyx::support::rtrim;
52 unsigned int packedcolor(LColor::color c);
60 /// Access to this class is through this static method.
61 Image::ImagePtr xformsImage::newImage()
66 ptr.reset(new xformsImage);
71 /// Return the list of loadable formats.
72 Image::FormatList xformsImage::loadableFormats()
74 static FormatList fmts;
80 // The formats recognised by LyX
81 Formats::const_iterator begin = formats.begin();
82 Formats::const_iterator end = formats.end();
84 lyxerr[Debug::GRAPHICS]
85 << "\nThe image loader can load the following directly:\n";
87 // Don't forget the Fortran numbering used by xforms!
88 int const nformats = flimage_get_number_of_formats();
89 for (int i = 1; i <= nformats; ++i) {
91 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
92 string const formal_name =
93 info->formal_name ? info->formal_name : string();
95 info->extension ? info->extension : string();
97 if (ext.empty() || ext == "gz")
100 if (ext == "rgb") ext = "sgi";
102 lyxerr[Debug::GRAPHICS]
103 << formal_name << ", extension \"" << ext << "\"\n";
105 Formats::const_iterator it =
107 lyx::compare_memfun(&Format::extension, ext));
109 fmts.push_back(it->name());
112 lyxerr[Debug::GRAPHICS]
113 << "\nOf these, LyX recognises the following formats:\n";
115 FormatList::const_iterator fbegin = fmts.begin();
116 FormatList::const_iterator fend = fmts.end();
117 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
119 lyxerr[Debug::GRAPHICS] << ", ";
120 lyxerr[Debug::GRAPHICS] << *fit;
122 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
128 xformsImage::xformsImage()
131 pixmap_status_(PIXMAP_UNINITIALISED)
135 xformsImage::xformsImage(xformsImage const & other)
139 pixmap_status_(PIXMAP_UNINITIALISED)
142 image_ = flimage_dup(other.image_);
143 image_->u_vdata = this;
148 xformsImage::~xformsImage()
151 flimage_free(image_);
153 XFreePixmap(fl_get_display(), pixmap_);
157 Pixmap xformsImage::getPixmap() const
159 if (!pixmap_status_ == PIXMAP_SUCCESS)
165 Image * xformsImage::clone_impl() const
167 return new xformsImage(*this);
171 unsigned int xformsImage::getWidth_impl() const
180 unsigned int xformsImage::getHeight_impl() const
188 bool xformsImage::isDrawable_impl() const
194 void xformsImage::load_impl(string const & filename)
197 lyxerr[Debug::GRAPHICS]
198 << "Image is loaded already!" << std::endl;
199 finishedLoading(false);
203 image_ = flimage_open(filename.c_str());
205 lyxerr[Debug::GRAPHICS]
206 << "Unable to open image" << std::endl;
207 finishedLoading(false);
211 // Set this now and we won't need to bother again.
212 image_->fill_color = packedcolor(LColor::graphicsbg);
214 // Used by the callback routines to return to this
215 image_->u_vdata = this;
217 // Begin the reading process.
218 flimage_read(image_);
222 bool xformsImage::setPixmap_impl(Params const & params)
224 if (!image_ || params.display == NoDisplay)
227 Display * display = fl_get_display();
229 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
230 XFreePixmap(display, pixmap_);
233 switch (params.display) {
234 case MonochromeDisplay:
235 color_key = FL_IMAGE_MONO;
237 case GrayscaleDisplay:
238 color_key = FL_IMAGE_GRAY;
241 default: // NoDisplay cannot happen!
242 color_key = FL_IMAGE_RGB;
246 if (color_key != FL_IMAGE_RGB) {
247 flimage_convert(image_, color_key, 0);
250 unsigned int fill = packedcolor(LColor::graphicsbg);
251 if (fill != image_->fill_color) {
252 // the background color has changed.
253 // Note that in grayscale/monochrome images the background is
254 // grayed also, so this call will have no visible effect. Sorry!
255 flimage_replace_pixel(image_, image_->fill_color, fill);
256 image_->fill_color = fill;
259 image_->xdisplay = display;
260 Screen * screen = ScreenOfDisplay(display, fl_screen);
262 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
263 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
265 return pixmap_status_ == PIXMAP_SUCCESS;
269 void xformsImage::clip_impl(Params const & params)
274 if (params.bb.empty())
275 // No clipping is necessary.
278 int const new_width = params.bb.xr - params.bb.xl;
279 int const new_height = params.bb.yt - params.bb.yb;
281 // No need to check if the width, height are > 0 because the
282 // Bounding Box would be empty() in this case.
283 if (new_width > image_->w || new_height > image_->h)
284 // Bounds are invalid.
287 if (new_width == image_->w && new_height == image_->h)
288 // Bounds are unchanged.
291 // flimage.h: image_ members w and h are of type int
292 // (though always >= 0)
293 // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
294 // type unsigned int.
295 // We must, therefore, be careful...
296 int const xoffset_l = params.bb.xl;
297 int const yoffset_b = params.bb.yb;
298 int const xoffset_r = image_->w > int(params.bb.xr) ?
299 image_->w - params.bb.xr : 0;
300 int const yoffset_t = image_->h > int(params.bb.yt) ?
301 image_->h - params.bb.yt : 0;
303 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
307 void xformsImage::rotate_impl(Params const & params)
312 if (float_equal(params.angle, 0.0, 0.1))
313 // No rotation is necessary.
316 // The angle passed to flimage_rotate is the angle in one-tenth of a
319 // Work around xforms bug when params.angle == 270
320 // the 'InternalError: bad special angle' error.
321 // This bug fix is not needed in xforms 1.0 and greater.
322 if (float_equal(params.angle, 270.0, 0.1)) {
323 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
324 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
326 flimage_rotate(image_,
327 int(params.angle * 10),
333 void xformsImage::scale_impl(Params const & params)
340 boost::tie(width, height) = getScaledDimensions(params);
342 if (width == getWidth() && height == getHeight())
346 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
350 void xformsImage::statusCB(string const & status_message)
352 if (status_message.empty())
355 if (prefixIs(status_message, "Done Reading")) {
357 flimage_close(image_);
360 finishedLoading(true);
365 void xformsImage::errorCB(string const & error_message)
367 if (error_message.empty())
371 flimage_close(image_);
374 finishedLoading(false);
377 } // namespace graphics
385 int status_report(FL_IMAGE * ob, const char *s)
387 BOOST_ASSERT(ob && ob->u_vdata);
389 string const str = s ? rtrim(s) : string();
393 lyxerr[Debug::GRAPHICS]
394 << "xforms image loader. Status: " << str << std::endl;
396 lyx::graphics::xformsImage * ptr =
397 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
404 static void error_report(FL_IMAGE * ob, const char *s)
406 BOOST_ASSERT(ob && ob->u_vdata);
408 string const str = s ? rtrim(s) : string();
412 lyxerr[Debug::GRAPHICS]
413 << "xforms image loader. Error: " << str << std::endl;
415 lyx::graphics::xformsImage * ptr =
416 static_cast<lyx::graphics::xformsImage *>(ob->u_vdata);
426 static bool initialised = false;
431 flimage_enable_bmp();
432 flimage_enable_fits();
433 flimage_enable_gif();
434 #ifdef USE_JPEG_IMAGE_LOADER
435 flimage_enable_jpeg();
438 // xforms itself uses pngtopnm to convert to a loadable format.
439 // We prefer to use our own conversion mechanism, therefore.
440 // flimage_enable_png();
442 flimage_enable_pnm();
444 // xforms recognises PS but not EPS
445 // It dies horribly with lots of older PostScript files.
446 // Easiest, therefore, to disable PS support and insist that a PS-type
447 // file is converted to a bitmap format.
448 // flimage_enable_ps();
450 flimage_enable_sgi();
451 flimage_enable_tiff();
452 flimage_enable_xbm();
453 flimage_enable_xwd();
454 // xforms can load most XPM files, but will occasionally crash
455 // with some files created by ImakeMagick's convert program.
456 // Turn off xpm support for the time being.
457 // flimage_enable_xpm();
459 // xforms stores this permanently (does not make a copy) so
460 // this should never be destroyed.
461 static FLIMAGE_SETUP setup;
462 setup.visual_cue = status_report;
463 setup.error_message = error_report;
464 flimage_setup(&setup);
468 unsigned int packedcolor(LColor::color col)
470 unsigned int r, g, b;
471 bool const success = getRGBColor(col, r, g, b);
473 // Set to black on failure
474 return FL_PACK(255, 255, 255);
476 return FL_PACK(r, g, b);