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 "xformsGImage.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>
39 unsigned int packedcolor(LColor::color c);
46 /// Access to this class is through this static method.
47 ImagePtr xformsGImage::newImage()
52 ptr.reset(new xformsGImage);
57 /// Return the list of loadable formats.
58 GImage::FormatList xformsGImage::loadableFormats()
60 static FormatList fmts;
66 // The formats recognised by LyX
67 Formats::const_iterator begin = formats.begin();
68 Formats::const_iterator end = formats.end();
70 lyxerr[Debug::GRAPHICS]
71 << "\nThe image loader can load the following directly:\n";
73 // Don't forget the Fortran numbering used by xforms!
74 int const nformats = flimage_get_number_of_formats();
75 for (int i = 1; i <= nformats; ++i) {
77 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
78 string const formal_name =
79 info->formal_name ? info->formal_name : string();
81 info->extension ? info->extension : string();
83 if (ext.empty() || ext == "gz")
86 if (ext == "rgb") ext = "sgi";
88 lyxerr[Debug::GRAPHICS]
89 << formal_name << ", extension \"" << ext << "\"\n";
91 Formats::const_iterator it =
93 lyx::compare_memfun(&Format::extension, ext));
95 fmts.push_back(it->name());
98 lyxerr[Debug::GRAPHICS]
99 << "\nOf these, LyX recognises the following formats:\n";
101 FormatList::const_iterator fbegin = fmts.begin();
102 FormatList::const_iterator fend = fmts.end();
103 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
105 lyxerr[Debug::GRAPHICS] << ", ";
106 lyxerr[Debug::GRAPHICS] << *fit;
108 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
114 xformsGImage::xformsGImage()
117 pixmap_status_(PIXMAP_UNINITIALISED)
121 xformsGImage::xformsGImage(xformsGImage const & other)
125 pixmap_status_(PIXMAP_UNINITIALISED)
128 image_ = flimage_dup(other.image_);
129 image_->u_vdata = this;
134 xformsGImage::~xformsGImage()
137 flimage_free(image_);
139 XFreePixmap(fl_get_display(), pixmap_);
143 GImage * xformsGImage::clone() const
145 return new xformsGImage(*this);
149 unsigned int xformsGImage::getWidth() const
157 unsigned int xformsGImage::getHeight() const
165 Pixmap xformsGImage::getPixmap() const
167 if (!pixmap_status_ == PIXMAP_SUCCESS)
173 void xformsGImage::load(string const & filename, SignalTypePtr on_finish)
176 lyxerr[Debug::GRAPHICS]
177 << "Image is loaded already!" << std::endl;
178 on_finish->operator()(false);
182 image_ = flimage_open(filename.c_str());
184 lyxerr[Debug::GRAPHICS]
185 << "Unable to open image" << std::endl;
186 on_finish->operator()(false);
190 // Store the Signal that will be emitted once the image is loaded.
191 on_finish_ = on_finish;
193 // Set this now and we won't need to bother again.
194 image_->fill_color = packedcolor(LColor::graphicsbg);
196 // Used by the callback routines to return to this
197 image_->u_vdata = this;
199 // Begin the reading process.
200 flimage_read(image_);
204 bool xformsGImage::setPixmap(GParams const & params)
206 if (!image_ || params.display == GParams::NONE)
209 Display * display = fl_get_display();
211 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
212 XFreePixmap(display, pixmap_);
215 switch (params.display) {
216 case GParams::MONOCHROME:
217 color_key = FL_IMAGE_MONO;
219 case GParams::GRAYSCALE:
220 color_key = FL_IMAGE_GRAY;
223 default: // NONE cannot happen!
224 color_key = FL_IMAGE_RGB;
228 if (color_key != FL_IMAGE_RGB) {
229 flimage_convert(image_, color_key, 0);
232 unsigned int fill = packedcolor(LColor::graphicsbg);
233 if (fill != image_->fill_color) {
234 // the background color has changed.
235 // Note that in grayscale/monochrome images the background is
236 // grayed also, so this call will have no visible effect. Sorry!
237 flimage_replace_pixel(image_, image_->fill_color, fill);
238 image_->fill_color = fill;
241 image_->xdisplay = display;
242 Screen * screen = ScreenOfDisplay(display, fl_screen);
244 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
245 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
247 return pixmap_status_ == PIXMAP_SUCCESS;
251 void xformsGImage::clip(GParams const & params)
256 if (params.bb.empty())
257 // No clipping is necessary.
260 int const new_width = params.bb.xr - params.bb.xl;
261 int const new_height = params.bb.yt - params.bb.yb;
263 // No need to check if the width, height are > 0 because the
264 // Bounding Box would be empty() in this case.
265 if (new_width > image_->w || new_height > image_->h)
266 // Bounds are invalid.
269 if (new_width == image_->w && new_height == image_->h)
270 // Bounds are unchanged.
273 int const xoffset_l = std::max(0, params.bb.xl);
274 int const xoffset_r = std::max(0, image_->w - params.bb.xr);
275 int const yoffset_t = std::max(0, image_->h - params.bb.yt);
276 int const yoffset_b = std::max(0, params.bb.yb);
278 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
282 void xformsGImage::rotate(GParams const & params)
288 // No rotation is necessary.
291 // The angle passed to flimage_rotate is the angle in one-tenth of a
294 // Work around xforms bug when params.angle == 270
295 // the 'InternalError: bad special angle' error.
296 // This bug fix is not needed in xforms 1.0 and greater.
297 if (params.angle == 270) {
298 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
299 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
301 flimage_rotate(image_, params.angle * 10, FLIMAGE_SUBPIXEL);
306 void xformsGImage::scale(GParams const & params)
311 // boost::tie produces horrible compilation errors on my machine
313 std::pair<unsigned int, unsigned int> d = getScaledDimensions(params);
314 unsigned int const width = d.first;
315 unsigned int const height = d.second;
317 if (width == getWidth() && height == getHeight())
321 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
325 void xformsGImage::statusCB(string const & status_message)
327 if (status_message.empty() || !on_finish_.get())
330 if (prefixIs(status_message, "Done Reading")) {
332 flimage_close(image_);
335 if (on_finish_.get()) {
336 on_finish_->operator()(true);
343 void xformsGImage::errorCB(string const & error_message)
345 if (error_message.empty() || !on_finish_.get())
349 flimage_close(image_);
352 if (on_finish_.get()) {
353 on_finish_->operator()(false);
365 int status_report(FL_IMAGE * ob, const char *s)
367 lyx::Assert(ob && ob->u_vdata);
369 string const str = s ? strip(s) : string();
373 lyxerr[Debug::GRAPHICS]
374 << "xforms image loader. Status : " << str << std::endl;
376 grfx::xformsGImage * ptr =
377 static_cast<grfx::xformsGImage *>(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 ? strip(s) : string();
392 lyxerr[Debug::GRAPHICS]
393 << "xforms image loader. Error : " << str << std::endl;
395 grfx::xformsGImage * ptr =
396 static_cast<grfx::xformsGImage *>(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
429 flimage_enable_sgi();
430 flimage_enable_tiff();
431 flimage_enable_xbm();
432 flimage_enable_xwd();
433 flimage_enable_xpm();
435 // xforms stores this permanently (does not make a copy) so
436 // this should never be destroyed.
437 static FLIMAGE_SETUP setup;
438 setup.visual_cue = status_report;
439 setup.error_message = error_report;
440 flimage_setup(&setup);
444 unsigned int packedcolor(LColor::color c)
446 string const x11color = lcolor.getX11Name(c);
448 Display * display = fl_get_display();
449 Colormap cmap = fl_state[fl_get_vclass()].colormap;
452 if (XLookupColor(display, cmap, x11color.c_str(), &xcol, &ccol) == 0)
453 // Unable to parse x11color.
454 return FL_PACK(255,255,255);
456 // Note that X stores the RGB values in the range 0 - 65535
457 // whilst we require them in the range 0 - 255.
458 unsigned int const r = xcol.red / 256;
459 unsigned int const g = xcol.green / 256;
460 unsigned int const b = xcol.blue / 256;
462 return FL_PACK(r, g, b);