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 Pixmap xformsImage::getPixmap() const
172 if (!pixmap_status_ == PIXMAP_SUCCESS)
178 void xformsImage::load(string const & filename)
181 lyxerr[Debug::GRAPHICS]
182 << "Image is loaded already!" << std::endl;
183 finishedLoading(false);
187 image_ = flimage_open(filename.c_str());
189 lyxerr[Debug::GRAPHICS]
190 << "Unable to open image" << std::endl;
191 finishedLoading(false);
195 // Set this now and we won't need to bother again.
196 image_->fill_color = packedcolor(LColor::graphicsbg);
198 // Used by the callback routines to return to this
199 image_->u_vdata = this;
201 // Begin the reading process.
202 flimage_read(image_);
206 bool xformsImage::setPixmap(Params const & params)
208 if (!image_ || params.display == NoDisplay)
211 Display * display = fl_get_display();
213 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
214 XFreePixmap(display, pixmap_);
217 switch (params.display) {
218 case MonochromeDisplay:
219 color_key = FL_IMAGE_MONO;
221 case GrayscaleDisplay:
222 color_key = FL_IMAGE_GRAY;
225 default: // NoDisplay cannot happen!
226 color_key = FL_IMAGE_RGB;
230 if (color_key != FL_IMAGE_RGB) {
231 flimage_convert(image_, color_key, 0);
234 unsigned int fill = packedcolor(LColor::graphicsbg);
235 if (fill != image_->fill_color) {
236 // the background color has changed.
237 // Note that in grayscale/monochrome images the background is
238 // grayed also, so this call will have no visible effect. Sorry!
239 flimage_replace_pixel(image_, image_->fill_color, fill);
240 image_->fill_color = fill;
243 image_->xdisplay = display;
244 Screen * screen = ScreenOfDisplay(display, fl_screen);
246 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
247 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
249 return pixmap_status_ == PIXMAP_SUCCESS;
253 void xformsImage::clip(Params const & params)
258 if (params.bb.empty())
259 // No clipping is necessary.
262 int const new_width = params.bb.xr - params.bb.xl;
263 int const new_height = params.bb.yt - params.bb.yb;
265 // No need to check if the width, height are > 0 because the
266 // Bounding Box would be empty() in this case.
267 if (new_width > image_->w || new_height > image_->h)
268 // Bounds are invalid.
271 if (new_width == image_->w && new_height == image_->h)
272 // Bounds are unchanged.
275 int const xoffset_l = std::max(0, params.bb.xl);
276 int const xoffset_r = std::max(0, image_->w - params.bb.xr);
277 int const yoffset_t = std::max(0, image_->h - params.bb.yt);
278 int const yoffset_b = std::max(0, params.bb.yb);
280 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
284 void xformsImage::rotate(Params const & params)
290 // No rotation is necessary.
293 // The angle passed to flimage_rotate is the angle in one-tenth of a
296 // Work around xforms bug when params.angle == 270
297 // the 'InternalError: bad special angle' error.
298 // This bug fix is not needed in xforms 1.0 and greater.
299 if (params.angle == 270) {
300 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
301 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
303 flimage_rotate(image_, params.angle * 10, FLIMAGE_SUBPIXEL);
308 void xformsImage::scale(Params const & params)
315 boost::tie(width, height) = getScaledDimensions(params);
317 if (width == getWidth() && height == getHeight())
321 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
325 void xformsImage::statusCB(string const & status_message)
327 if (status_message.empty())
330 if (prefixIs(status_message, "Done Reading")) {
332 flimage_close(image_);
335 finishedLoading(true);
340 void xformsImage::errorCB(string const & error_message)
342 if (error_message.empty())
346 flimage_close(image_);
349 finishedLoading(false);
359 int status_report(FL_IMAGE * ob, const char *s)
361 lyx::Assert(ob && ob->u_vdata);
363 string const str = s ? strip(s) : string();
367 lyxerr[Debug::GRAPHICS]
368 << "xforms image loader. Status : " << str << std::endl;
370 grfx::xformsImage * ptr =
371 static_cast<grfx::xformsImage *>(ob->u_vdata);
378 static void error_report(FL_IMAGE * ob, const char *s)
380 lyx::Assert(ob && ob->u_vdata);
382 string const str = s ? strip(s) : string();
386 lyxerr[Debug::GRAPHICS]
387 << "xforms image loader. Error : " << str << std::endl;
389 grfx::xformsImage * ptr =
390 static_cast<grfx::xformsImage *>(ob->u_vdata);
400 static bool initialised = false;
405 flimage_enable_bmp();
406 flimage_enable_fits();
407 flimage_enable_gif();
408 #ifdef HAVE_FLIMAGE_ENABLE_JPEG
409 flimage_enable_jpeg();
412 // xforms itself uses pngtopnm to convert to a loadable format.
413 // We prefer to use our own conversion mechanism, therefore.
414 // flimage_enable_png();
416 flimage_enable_pnm();
418 #ifdef HAVE_FLIMAGE_ENABLE_PS
419 // xforms recognises PS but not EPS
423 flimage_enable_sgi();
424 flimage_enable_tiff();
425 flimage_enable_xbm();
426 flimage_enable_xwd();
427 flimage_enable_xpm();
429 // xforms stores this permanently (does not make a copy) so
430 // this should never be destroyed.
431 static FLIMAGE_SETUP setup;
432 setup.visual_cue = status_report;
433 setup.error_message = error_report;
434 flimage_setup(&setup);
438 unsigned int packedcolor(LColor::color c)
440 string const x11color = lcolor.getX11Name(c);
442 Display * display = fl_get_display();
443 Colormap cmap = fl_state[fl_get_vclass()].colormap;
446 if (XLookupColor(display, cmap, x11color.c_str(), &xcol, &ccol) == 0)
447 // Unable to parse x11color.
448 return FL_PACK(255,255,255);
450 // Note that X stores the RGB values in the range 0 - 65535
451 // whilst we require them in the range 0 - 255.
452 unsigned int const r = xcol.red / 256;
453 unsigned int const g = xcol.green / 256;
454 unsigned int const b = xcol.blue / 256;
456 return FL_PACK(r, g, b);