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>
39 unsigned int packedcolor(LColor::color c);
46 /// Access to this class is through this static method.
47 Image::ImagePtr xformsImage::newImage()
52 ptr.reset(new xformsImage);
57 /// Return the list of loadable formats.
58 Image::FormatList xformsImage::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 xformsImage::xformsImage()
117 pixmap_status_(PIXMAP_UNINITIALISED)
121 xformsImage::xformsImage(xformsImage const & other)
125 pixmap_status_(PIXMAP_UNINITIALISED)
128 image_ = flimage_dup(other.image_);
129 image_->u_vdata = this;
134 xformsImage::~xformsImage()
137 flimage_free(image_);
139 XFreePixmap(fl_get_display(), pixmap_);
143 Image * xformsImage::clone() const
145 return new xformsImage(*this);
149 unsigned int xformsImage::getWidth() const
157 unsigned int xformsImage::getHeight() const
165 Pixmap xformsImage::getPixmap() const
167 if (!pixmap_status_ == PIXMAP_SUCCESS)
173 void xformsImage::load(string const & filename)
176 lyxerr[Debug::GRAPHICS]
177 << "Image is loaded already!" << std::endl;
178 finishedLoading(false);
182 image_ = flimage_open(filename.c_str());
184 lyxerr[Debug::GRAPHICS]
185 << "Unable to open image" << std::endl;
186 finishedLoading(false);
190 // Set this now and we won't need to bother again.
191 image_->fill_color = packedcolor(LColor::graphicsbg);
193 // Used by the callback routines to return to this
194 image_->u_vdata = this;
196 // Begin the reading process.
197 flimage_read(image_);
201 bool xformsImage::setPixmap(Params const & params)
203 if (!image_ || params.display == NoDisplay)
206 Display * display = fl_get_display();
208 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
209 XFreePixmap(display, pixmap_);
212 switch (params.display) {
213 case MonochromeDisplay:
214 color_key = FL_IMAGE_MONO;
216 case GrayscaleDisplay:
217 color_key = FL_IMAGE_GRAY;
220 default: // NoDisplay cannot happen!
221 color_key = FL_IMAGE_RGB;
225 if (color_key != FL_IMAGE_RGB) {
226 flimage_convert(image_, color_key, 0);
229 unsigned int fill = packedcolor(LColor::graphicsbg);
230 if (fill != image_->fill_color) {
231 // the background color has changed.
232 // Note that in grayscale/monochrome images the background is
233 // grayed also, so this call will have no visible effect. Sorry!
234 flimage_replace_pixel(image_, image_->fill_color, fill);
235 image_->fill_color = fill;
238 image_->xdisplay = display;
239 Screen * screen = ScreenOfDisplay(display, fl_screen);
241 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
242 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
244 return pixmap_status_ == PIXMAP_SUCCESS;
248 void xformsImage::clip(Params const & params)
253 if (params.bb.empty())
254 // No clipping is necessary.
257 int const new_width = params.bb.xr - params.bb.xl;
258 int const new_height = params.bb.yt - params.bb.yb;
260 // No need to check if the width, height are > 0 because the
261 // Bounding Box would be empty() in this case.
262 if (new_width > image_->w || new_height > image_->h)
263 // Bounds are invalid.
266 if (new_width == image_->w && new_height == image_->h)
267 // Bounds are unchanged.
270 int const xoffset_l = std::max(0, params.bb.xl);
271 int const xoffset_r = std::max(0, image_->w - params.bb.xr);
272 int const yoffset_t = std::max(0, image_->h - params.bb.yt);
273 int const yoffset_b = std::max(0, params.bb.yb);
275 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
279 void xformsImage::rotate(Params const & params)
285 // No rotation is necessary.
288 // The angle passed to flimage_rotate is the angle in one-tenth of a
291 // Work around xforms bug when params.angle == 270
292 // the 'InternalError: bad special angle' error.
293 // This bug fix is not needed in xforms 1.0 and greater.
294 if (params.angle == 270) {
295 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
296 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
298 flimage_rotate(image_, params.angle * 10, FLIMAGE_SUBPIXEL);
303 void xformsImage::scale(Params const & params)
308 // boost::tie produces horrible compilation errors on my machine
310 std::pair<unsigned int, unsigned int> d = getScaledDimensions(params);
311 unsigned int const width = d.first;
312 unsigned int const height = d.second;
314 if (width == getWidth() && height == getHeight())
318 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
322 void xformsImage::statusCB(string const & status_message)
324 if (status_message.empty())
327 if (prefixIs(status_message, "Done Reading")) {
329 flimage_close(image_);
332 finishedLoading(true);
337 void xformsImage::errorCB(string const & error_message)
339 if (error_message.empty())
343 flimage_close(image_);
346 finishedLoading(false);
356 int status_report(FL_IMAGE * ob, const char *s)
358 lyx::Assert(ob && ob->u_vdata);
360 string const str = s ? strip(s) : string();
364 lyxerr[Debug::GRAPHICS]
365 << "xforms image loader. Status : " << str << std::endl;
367 grfx::xformsImage * ptr =
368 static_cast<grfx::xformsImage *>(ob->u_vdata);
375 static void error_report(FL_IMAGE * ob, const char *s)
377 lyx::Assert(ob && ob->u_vdata);
379 string const str = s ? strip(s) : string();
383 lyxerr[Debug::GRAPHICS]
384 << "xforms image loader. Error : " << str << std::endl;
386 grfx::xformsImage * ptr =
387 static_cast<grfx::xformsImage *>(ob->u_vdata);
397 static bool initialised = false;
402 flimage_enable_bmp();
403 flimage_enable_fits();
404 flimage_enable_gif();
405 #ifdef HAVE_FLIMAGE_ENABLE_JPEG
406 flimage_enable_jpeg();
409 // xforms itself uses pngtopnm to convert to a loadable format.
410 // We prefer to use our own conversion mechanism, therefore.
411 // flimage_enable_png();
413 flimage_enable_pnm();
415 #ifdef HAVE_FLIMAGE_ENABLE_PS
416 // xforms recognises PS but not EPS
420 flimage_enable_sgi();
421 flimage_enable_tiff();
422 flimage_enable_xbm();
423 flimage_enable_xwd();
424 flimage_enable_xpm();
426 // xforms stores this permanently (does not make a copy) so
427 // this should never be destroyed.
428 static FLIMAGE_SETUP setup;
429 setup.visual_cue = status_report;
430 setup.error_message = error_report;
431 flimage_setup(&setup);
435 unsigned int packedcolor(LColor::color c)
437 string const x11color = lcolor.getX11Name(c);
439 Display * display = fl_get_display();
440 Colormap cmap = fl_state[fl_get_vclass()].colormap;
443 if (XLookupColor(display, cmap, x11color.c_str(), &xcol, &ccol) == 0)
444 // Unable to parse x11color.
445 return FL_PACK(255,255,255);
447 // Note that X stores the RGB values in the range 0 - 65535
448 // whilst we require them in the range 0 - 255.
449 unsigned int const r = xcol.red / 256;
450 unsigned int const g = xcol.green / 256;
451 unsigned int const b = xcol.blue / 256;
453 return FL_PACK(r, g, b);