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/LAssert.h"
19 #include "support/lstrings.h"
20 #include "support/lyxfunctional.h" // compare_memfun
21 #include "support/lyxlib.h"
23 #include "lyx_forms.h"
28 # ifdef HAVE_X11_FLIMAGE_H
29 # include <X11/flimage.h>
33 #include <boost/tuple/tuple.hpp>
35 using namespace lyx::support;
43 unsigned int packedcolor(LColor::color c);
51 /// Access to this class is through this static method.
52 Image::ImagePtr xformsImage::newImage()
57 ptr.reset(new xformsImage);
62 /// Return the list of loadable formats.
63 Image::FormatList xformsImage::loadableFormats()
65 static FormatList fmts;
71 // The formats recognised by LyX
72 Formats::const_iterator begin = formats.begin();
73 Formats::const_iterator end = formats.end();
75 lyxerr[Debug::GRAPHICS]
76 << "\nThe image loader can load the following directly:\n";
78 // Don't forget the Fortran numbering used by xforms!
79 int const nformats = flimage_get_number_of_formats();
80 for (int i = 1; i <= nformats; ++i) {
82 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
83 string const formal_name =
84 info->formal_name ? info->formal_name : string();
86 info->extension ? info->extension : string();
88 if (ext.empty() || ext == "gz")
91 if (ext == "rgb") ext = "sgi";
93 lyxerr[Debug::GRAPHICS]
94 << formal_name << ", extension \"" << ext << "\"\n";
96 Formats::const_iterator it =
98 lyx::compare_memfun(&Format::extension, ext));
100 fmts.push_back(it->name());
103 lyxerr[Debug::GRAPHICS]
104 << "\nOf these, LyX recognises the following formats:\n";
106 FormatList::const_iterator fbegin = fmts.begin();
107 FormatList::const_iterator fend = fmts.end();
108 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
110 lyxerr[Debug::GRAPHICS] << ", ";
111 lyxerr[Debug::GRAPHICS] << *fit;
113 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
119 xformsImage::xformsImage()
122 pixmap_status_(PIXMAP_UNINITIALISED)
126 xformsImage::xformsImage(xformsImage const & other)
130 pixmap_status_(PIXMAP_UNINITIALISED)
133 image_ = flimage_dup(other.image_);
134 image_->u_vdata = this;
139 xformsImage::~xformsImage()
142 flimage_free(image_);
144 XFreePixmap(fl_get_display(), pixmap_);
148 Image * xformsImage::clone() const
150 return new xformsImage(*this);
154 unsigned int xformsImage::getWidth() const
163 unsigned int xformsImage::getHeight() const
171 bool xformsImage::isDrawable() const
177 Pixmap xformsImage::getPixmap() const
179 if (!pixmap_status_ == PIXMAP_SUCCESS)
185 void xformsImage::load(string const & filename)
188 lyxerr[Debug::GRAPHICS]
189 << "Image is loaded already!" << std::endl;
190 finishedLoading(false);
194 image_ = flimage_open(filename.c_str());
196 lyxerr[Debug::GRAPHICS]
197 << "Unable to open image" << std::endl;
198 finishedLoading(false);
202 // Set this now and we won't need to bother again.
203 image_->fill_color = packedcolor(LColor::graphicsbg);
205 // Used by the callback routines to return to this
206 image_->u_vdata = this;
208 // Begin the reading process.
209 flimage_read(image_);
213 bool xformsImage::setPixmap(Params const & params)
215 if (!image_ || params.display == NoDisplay)
218 Display * display = fl_get_display();
220 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
221 XFreePixmap(display, pixmap_);
224 switch (params.display) {
225 case MonochromeDisplay:
226 color_key = FL_IMAGE_MONO;
228 case GrayscaleDisplay:
229 color_key = FL_IMAGE_GRAY;
232 default: // NoDisplay cannot happen!
233 color_key = FL_IMAGE_RGB;
237 if (color_key != FL_IMAGE_RGB) {
238 flimage_convert(image_, color_key, 0);
241 unsigned int fill = packedcolor(LColor::graphicsbg);
242 if (fill != image_->fill_color) {
243 // the background color has changed.
244 // Note that in grayscale/monochrome images the background is
245 // grayed also, so this call will have no visible effect. Sorry!
246 flimage_replace_pixel(image_, image_->fill_color, fill);
247 image_->fill_color = fill;
250 image_->xdisplay = display;
251 Screen * screen = ScreenOfDisplay(display, fl_screen);
253 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
254 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
256 return pixmap_status_ == PIXMAP_SUCCESS;
260 void xformsImage::clip(Params const & params)
265 if (params.bb.empty())
266 // No clipping is necessary.
269 int const new_width = params.bb.xr - params.bb.xl;
270 int const new_height = params.bb.yt - params.bb.yb;
272 // No need to check if the width, height are > 0 because the
273 // Bounding Box would be empty() in this case.
274 if (new_width > image_->w || new_height > image_->h)
275 // Bounds are invalid.
278 if (new_width == image_->w && new_height == image_->h)
279 // Bounds are unchanged.
282 // flimage.h: image_ members w and h are of type int
283 // (though always >= 0)
284 // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
285 // type unsigned int.
286 // We must, therefore, be careful...
287 int const xoffset_l = params.bb.xl;
288 int const yoffset_b = params.bb.yb;
289 int const xoffset_r = image_->w > int(params.bb.xr) ?
290 image_->w - params.bb.xr : 0;
291 int const yoffset_t = image_->h > int(params.bb.yt) ?
292 image_->h - params.bb.yt : 0;
294 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
298 void xformsImage::rotate(Params const & params)
303 if (float_equal(params.angle, 0.0, 0.1))
304 // No rotation is necessary.
307 // The angle passed to flimage_rotate is the angle in one-tenth of a
310 // Work around xforms bug when params.angle == 270
311 // the 'InternalError: bad special angle' error.
312 // This bug fix is not needed in xforms 1.0 and greater.
313 if (float_equal(params.angle, 270.0, 0.1)) {
314 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
315 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
317 flimage_rotate(image_,
318 int(params.angle * 10),
324 void xformsImage::scale(Params const & params)
331 boost::tie(width, height) = getScaledDimensions(params);
333 if (width == getWidth() && height == getHeight())
337 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
341 void xformsImage::statusCB(string const & status_message)
343 if (status_message.empty())
346 if (prefixIs(status_message, "Done Reading")) {
348 flimage_close(image_);
351 finishedLoading(true);
356 void xformsImage::errorCB(string const & error_message)
358 if (error_message.empty())
362 flimage_close(image_);
365 finishedLoading(false);
368 } // namespace graphics
374 namespace grfx = lyx::graphics;
378 int status_report(FL_IMAGE * ob, const char *s)
380 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 grfx::xformsImage * ptr =
390 static_cast<grfx::xformsImage *>(ob->u_vdata);
397 static void error_report(FL_IMAGE * ob, const char *s)
399 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 grfx::xformsImage * ptr =
409 static_cast<grfx::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);