3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
12 #pragma implementation
15 #include "xformsImage.h"
16 #include "graphics/GraphicsParams.h"
18 #include "converter.h" // formats
20 #include "support/LAssert.h"
21 #include "support/lyxfunctional.h" // compare_memfun
23 #include FORMS_H_LOCATION
28 # ifdef HAVE_X11_FLIMAGE_H
29 # include <X11/flimage.h>
33 #include <boost/tuple/tuple.hpp>
41 unsigned int packedcolor(LColor::color c);
48 /// Access to this class is through this static method.
49 Image::ImagePtr xformsImage::newImage()
54 ptr.reset(new xformsImage);
59 /// Return the list of loadable formats.
60 Image::FormatList xformsImage::loadableFormats()
62 static FormatList fmts;
68 // The formats recognised by LyX
69 Formats::const_iterator begin = formats.begin();
70 Formats::const_iterator end = formats.end();
72 lyxerr[Debug::GRAPHICS]
73 << "\nThe image loader can load the following directly:\n";
75 // Don't forget the Fortran numbering used by xforms!
76 int const nformats = flimage_get_number_of_formats();
77 for (int i = 1; i <= nformats; ++i) {
79 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
80 string const formal_name =
81 info->formal_name ? info->formal_name : string();
83 info->extension ? info->extension : string();
85 if (ext.empty() || ext == "gz")
88 if (ext == "rgb") ext = "sgi";
90 lyxerr[Debug::GRAPHICS]
91 << formal_name << ", extension \"" << ext << "\"\n";
93 Formats::const_iterator it =
95 lyx::compare_memfun(&Format::extension, ext));
97 fmts.push_back(it->name());
100 lyxerr[Debug::GRAPHICS]
101 << "\nOf these, LyX recognises the following formats:\n";
103 FormatList::const_iterator fbegin = fmts.begin();
104 FormatList::const_iterator fend = fmts.end();
105 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
107 lyxerr[Debug::GRAPHICS] << ", ";
108 lyxerr[Debug::GRAPHICS] << *fit;
110 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
116 xformsImage::xformsImage()
119 pixmap_status_(PIXMAP_UNINITIALISED)
123 xformsImage::xformsImage(xformsImage const & other)
127 pixmap_status_(PIXMAP_UNINITIALISED)
130 image_ = flimage_dup(other.image_);
131 image_->u_vdata = this;
136 xformsImage::~xformsImage()
139 flimage_free(image_);
141 XFreePixmap(fl_get_display(), pixmap_);
145 Image * xformsImage::clone() const
147 return new xformsImage(*this);
151 unsigned int xformsImage::getWidth() const
156 // Why, oh why do we need such hacks?
157 // Angus 12 July 2002
158 return image_->w + 2;
162 unsigned int xformsImage::getHeight() const
170 bool xformsImage::isDrawable() const
176 Pixmap xformsImage::getPixmap() const
178 if (!pixmap_status_ == PIXMAP_SUCCESS)
184 void xformsImage::load(string const & filename)
187 lyxerr[Debug::GRAPHICS]
188 << "Image is loaded already!" << std::endl;
189 finishedLoading(false);
193 image_ = flimage_open(filename.c_str());
195 lyxerr[Debug::GRAPHICS]
196 << "Unable to open image" << std::endl;
197 finishedLoading(false);
201 // Set this now and we won't need to bother again.
202 image_->fill_color = packedcolor(LColor::graphicsbg);
204 // Used by the callback routines to return to this
205 image_->u_vdata = this;
207 // Begin the reading process.
208 flimage_read(image_);
212 bool xformsImage::setPixmap(Params const & params)
214 if (!image_ || params.display == NoDisplay)
217 Display * display = fl_get_display();
219 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
220 XFreePixmap(display, pixmap_);
223 switch (params.display) {
224 case MonochromeDisplay:
225 color_key = FL_IMAGE_MONO;
227 case GrayscaleDisplay:
228 color_key = FL_IMAGE_GRAY;
231 default: // NoDisplay cannot happen!
232 color_key = FL_IMAGE_RGB;
236 if (color_key != FL_IMAGE_RGB) {
237 flimage_convert(image_, color_key, 0);
240 unsigned int fill = packedcolor(LColor::graphicsbg);
241 if (fill != image_->fill_color) {
242 // the background color has changed.
243 // Note that in grayscale/monochrome images the background is
244 // grayed also, so this call will have no visible effect. Sorry!
245 flimage_replace_pixel(image_, image_->fill_color, fill);
246 image_->fill_color = fill;
249 image_->xdisplay = display;
250 Screen * screen = ScreenOfDisplay(display, fl_screen);
252 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
253 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
255 return pixmap_status_ == PIXMAP_SUCCESS;
259 void xformsImage::clip(Params const & params)
264 if (params.bb.empty())
265 // No clipping is necessary.
268 int const new_width = params.bb.xr - params.bb.xl;
269 int const new_height = params.bb.yt - params.bb.yb;
271 // No need to check if the width, height are > 0 because the
272 // Bounding Box would be empty() in this case.
273 if (new_width > image_->w || new_height > image_->h)
274 // Bounds are invalid.
277 if (new_width == image_->w && new_height == image_->h)
278 // Bounds are unchanged.
281 int const xoffset_l = std::max(0, params.bb.xl);
282 int const xoffset_r = std::max(0, image_->w - params.bb.xr);
283 int const yoffset_t = std::max(0, image_->h - params.bb.yt);
284 int const yoffset_b = std::max(0, params.bb.yb);
286 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
290 void xformsImage::rotate(Params const & params)
296 // No rotation is necessary.
299 // The angle passed to flimage_rotate is the angle in one-tenth of a
302 // Work around xforms bug when params.angle == 270
303 // the 'InternalError: bad special angle' error.
304 // This bug fix is not needed in xforms 1.0 and greater.
305 if (params.angle == 270) {
306 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
307 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
309 flimage_rotate(image_, params.angle * 10, FLIMAGE_SUBPIXEL);
314 void xformsImage::scale(Params const & params)
321 boost::tie(width, height) = getScaledDimensions(params);
323 if (width == getWidth() && height == getHeight())
327 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
331 void xformsImage::statusCB(string const & status_message)
333 if (status_message.empty())
336 if (prefixIs(status_message, "Done Reading")) {
338 flimage_close(image_);
341 finishedLoading(true);
346 void xformsImage::errorCB(string const & error_message)
348 if (error_message.empty())
352 flimage_close(image_);
355 finishedLoading(false);
365 int status_report(FL_IMAGE * ob, const char *s)
367 lyx::Assert(ob && ob->u_vdata);
369 string const str = s ? rtrim(s) : string();
373 lyxerr[Debug::GRAPHICS]
374 << "xforms image loader. Status : " << str << std::endl;
376 grfx::xformsImage * ptr =
377 static_cast<grfx::xformsImage *>(ob->u_vdata);
384 static void error_report(FL_IMAGE * ob, const char *s)
386 lyx::Assert(ob && ob->u_vdata);
388 string const str = s ? rtrim(s) : string();
392 lyxerr[Debug::GRAPHICS]
393 << "xforms image loader. Error : " << str << std::endl;
395 grfx::xformsImage * ptr =
396 static_cast<grfx::xformsImage *>(ob->u_vdata);
406 static bool initialised = false;
411 flimage_enable_bmp();
412 flimage_enable_fits();
413 flimage_enable_gif();
414 #ifdef HAVE_FLIMAGE_ENABLE_JPEG
415 flimage_enable_jpeg();
418 // xforms itself uses pngtopnm to convert to a loadable format.
419 // We prefer to use our own conversion mechanism, therefore.
420 // flimage_enable_png();
422 flimage_enable_pnm();
424 #ifdef HAVE_FLIMAGE_ENABLE_PS
425 // xforms recognises PS but not EPS
426 // It dies horribly with lots of older PostScript files.
427 // Easiest, therefore, to disable PS support and insist that a PS-type
428 // file is converted to a bitmap format.
429 // flimage_enable_ps();
432 flimage_enable_sgi();
433 flimage_enable_tiff();
434 flimage_enable_xbm();
435 flimage_enable_xwd();
436 flimage_enable_xpm();
438 // xforms stores this permanently (does not make a copy) so
439 // this should never be destroyed.
440 static FLIMAGE_SETUP setup;
441 setup.visual_cue = status_report;
442 setup.error_message = error_report;
443 flimage_setup(&setup);
447 unsigned int packedcolor(LColor::color c)
449 string const x11color = lcolor.getX11Name(c);
451 Display * display = fl_get_display();
452 Colormap cmap = fl_state[fl_get_vclass()].colormap;
455 if (XLookupColor(display, cmap, x11color.c_str(), &xcol, &ccol) == 0)
456 // Unable to parse x11color.
457 return FL_PACK(255,255,255);
459 // Note that X stores the RGB values in the range 0 - 65535
460 // whilst we require them in the range 0 - 255.
461 unsigned int const r = xcol.red / 256;
462 unsigned int const g = xcol.green / 256;
463 unsigned int const b = xcol.blue / 256;
465 return FL_PACK(r, g, b);