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
14 #pragma implementation
17 #include "xformsImage.h"
18 #include "graphics/GraphicsParams.h"
20 #include "converter.h" // formats
22 #include "support/LAssert.h"
23 #include "support/lyxfunctional.h" // compare_memfun
25 #include FORMS_H_LOCATION
30 # ifdef HAVE_X11_FLIMAGE_H
31 # include <X11/flimage.h>
35 #include <boost/tuple/tuple.hpp>
43 unsigned int packedcolor(LColor::color c);
50 /// Access to this class is through this static method.
51 Image::ImagePtr xformsImage::newImage()
56 ptr.reset(new xformsImage);
61 /// Return the list of loadable formats.
62 Image::FormatList xformsImage::loadableFormats()
64 static FormatList fmts;
70 // The formats recognised by LyX
71 Formats::const_iterator begin = formats.begin();
72 Formats::const_iterator end = formats.end();
74 lyxerr[Debug::GRAPHICS]
75 << "\nThe image loader can load the following directly:\n";
77 // Don't forget the Fortran numbering used by xforms!
78 int const nformats = flimage_get_number_of_formats();
79 for (int i = 1; i <= nformats; ++i) {
81 FLIMAGE_FORMAT_INFO const * info = flimage_get_format_info(i);
82 string const formal_name =
83 info->formal_name ? info->formal_name : string();
85 info->extension ? info->extension : string();
87 if (ext.empty() || ext == "gz")
90 if (ext == "rgb") ext = "sgi";
92 lyxerr[Debug::GRAPHICS]
93 << formal_name << ", extension \"" << ext << "\"\n";
95 Formats::const_iterator it =
97 lyx::compare_memfun(&Format::extension, ext));
99 fmts.push_back(it->name());
102 lyxerr[Debug::GRAPHICS]
103 << "\nOf these, LyX recognises the following formats:\n";
105 FormatList::const_iterator fbegin = fmts.begin();
106 FormatList::const_iterator fend = fmts.end();
107 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
109 lyxerr[Debug::GRAPHICS] << ", ";
110 lyxerr[Debug::GRAPHICS] << *fit;
112 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
118 xformsImage::xformsImage()
121 pixmap_status_(PIXMAP_UNINITIALISED)
125 xformsImage::xformsImage(xformsImage const & other)
129 pixmap_status_(PIXMAP_UNINITIALISED)
132 image_ = flimage_dup(other.image_);
133 image_->u_vdata = this;
138 xformsImage::~xformsImage()
141 flimage_free(image_);
143 XFreePixmap(fl_get_display(), pixmap_);
147 Image * xformsImage::clone() const
149 return new xformsImage(*this);
153 unsigned int xformsImage::getWidth() const
158 // Why, oh why, do we need such hacks?
159 // Angus 12 July 2002
160 return image_->w + 4;
164 unsigned int xformsImage::getHeight() const
172 bool xformsImage::isDrawable() const
178 Pixmap xformsImage::getPixmap() const
180 if (!pixmap_status_ == PIXMAP_SUCCESS)
186 void xformsImage::load(string const & filename)
189 lyxerr[Debug::GRAPHICS]
190 << "Image is loaded already!" << std::endl;
191 finishedLoading(false);
195 image_ = flimage_open(filename.c_str());
197 lyxerr[Debug::GRAPHICS]
198 << "Unable to open image" << std::endl;
199 finishedLoading(false);
203 // Set this now and we won't need to bother again.
204 image_->fill_color = packedcolor(LColor::graphicsbg);
206 // Used by the callback routines to return to this
207 image_->u_vdata = this;
209 // Begin the reading process.
210 flimage_read(image_);
214 bool xformsImage::setPixmap(Params const & params)
216 if (!image_ || params.display == NoDisplay)
219 Display * display = fl_get_display();
221 if (pixmap_ && pixmap_status_ == PIXMAP_SUCCESS)
222 XFreePixmap(display, pixmap_);
225 switch (params.display) {
226 case MonochromeDisplay:
227 color_key = FL_IMAGE_MONO;
229 case GrayscaleDisplay:
230 color_key = FL_IMAGE_GRAY;
233 default: // NoDisplay cannot happen!
234 color_key = FL_IMAGE_RGB;
238 if (color_key != FL_IMAGE_RGB) {
239 flimage_convert(image_, color_key, 0);
242 unsigned int fill = packedcolor(LColor::graphicsbg);
243 if (fill != image_->fill_color) {
244 // the background color has changed.
245 // Note that in grayscale/monochrome images the background is
246 // grayed also, so this call will have no visible effect. Sorry!
247 flimage_replace_pixel(image_, image_->fill_color, fill);
248 image_->fill_color = fill;
251 image_->xdisplay = display;
252 Screen * screen = ScreenOfDisplay(display, fl_screen);
254 pixmap_ = flimage_to_pixmap(image_, XRootWindowOfScreen(screen));
255 pixmap_status_ = pixmap_ ? PIXMAP_SUCCESS : PIXMAP_FAILED;
257 return pixmap_status_ == PIXMAP_SUCCESS;
261 void xformsImage::clip(Params const & params)
266 if (params.bb.empty())
267 // No clipping is necessary.
270 int const new_width = params.bb.xr - params.bb.xl;
271 int const new_height = params.bb.yt - params.bb.yb;
273 // No need to check if the width, height are > 0 because the
274 // Bounding Box would be empty() in this case.
275 if (new_width > image_->w || new_height > image_->h)
276 // Bounds are invalid.
279 if (new_width == image_->w && new_height == image_->h)
280 // Bounds are unchanged.
283 // flimage.h: image_ members w and h are of type int
284 // (though always >= 0)
285 // GraphicsParams.h: params.bb members xl, xr, yt and yb are of
286 // type unsigned int.
287 // We must, therefore, be careful...
288 int const xoffset_l = params.bb.xl;
289 int const yoffset_b = params.bb.yb;
290 int const xoffset_r = image_->w > params.bb.xr ?
291 image_->w - params.bb.xr : 0;
292 int const yoffset_t = image_->h > params.bb.yt ?
293 image_->h - params.bb.yt : 0;
295 flimage_crop(image_, xoffset_l, yoffset_t, xoffset_r, yoffset_b);
299 void xformsImage::rotate(Params const & params)
305 // No rotation is necessary.
308 // The angle passed to flimage_rotate is the angle in one-tenth of a
311 // Work around xforms bug when params.angle == 270
312 // the 'InternalError: bad special angle' error.
313 // This bug fix is not needed in xforms 1.0 and greater.
314 if (params.angle == 270) {
315 flimage_rotate(image_, 900, FLIMAGE_SUBPIXEL);
316 flimage_rotate(image_, 1800, FLIMAGE_SUBPIXEL);
318 flimage_rotate(image_,
319 int(params.angle * 10),
325 void xformsImage::scale(Params const & params)
332 boost::tie(width, height) = getScaledDimensions(params);
334 if (width == getWidth() && height == getHeight())
338 flimage_scale(image_, width, height, FLIMAGE_SUBPIXEL);
342 void xformsImage::statusCB(string const & status_message)
344 if (status_message.empty())
347 if (prefixIs(status_message, "Done Reading")) {
349 flimage_close(image_);
352 finishedLoading(true);
357 void xformsImage::errorCB(string const & error_message)
359 if (error_message.empty())
363 flimage_close(image_);
366 finishedLoading(false);
376 int status_report(FL_IMAGE * ob, const char *s)
378 lyx::Assert(ob && ob->u_vdata);
380 string const str = s ? rtrim(s) : string();
384 lyxerr[Debug::GRAPHICS]
385 << "xforms image loader. Status : " << str << std::endl;
387 grfx::xformsImage * ptr =
388 static_cast<grfx::xformsImage *>(ob->u_vdata);
395 static void error_report(FL_IMAGE * ob, const char *s)
397 lyx::Assert(ob && ob->u_vdata);
399 string const str = s ? rtrim(s) : string();
403 lyxerr[Debug::GRAPHICS]
404 << "xforms image loader. Error : " << str << std::endl;
406 grfx::xformsImage * ptr =
407 static_cast<grfx::xformsImage *>(ob->u_vdata);
417 static bool initialised = false;
422 flimage_enable_bmp();
423 flimage_enable_fits();
424 flimage_enable_gif();
425 #ifdef USE_JPEG_IMAGE_LOADER
426 flimage_enable_jpeg();
429 // xforms itself uses pngtopnm to convert to a loadable format.
430 // We prefer to use our own conversion mechanism, therefore.
431 // flimage_enable_png();
433 flimage_enable_pnm();
435 // xforms recognises PS but not EPS
436 // It dies horribly with lots of older PostScript files.
437 // Easiest, therefore, to disable PS support and insist that a PS-type
438 // file is converted to a bitmap format.
439 // flimage_enable_ps();
441 flimage_enable_sgi();
442 flimage_enable_tiff();
443 flimage_enable_xbm();
444 flimage_enable_xwd();
445 flimage_enable_xpm();
447 // xforms stores this permanently (does not make a copy) so
448 // this should never be destroyed.
449 static FLIMAGE_SETUP setup;
450 setup.visual_cue = status_report;
451 setup.error_message = error_report;
452 flimage_setup(&setup);
456 unsigned int packedcolor(LColor::color c)
458 string const x11color = lcolor.getX11Name(c);
460 Display * display = fl_get_display();
461 Colormap cmap = fl_state[fl_get_vclass()].colormap;
464 if (XLookupColor(display, cmap, x11color.c_str(), &xcol, &ccol) == 0)
465 // Unable to parse x11color.
466 return FL_PACK(255,255,255);
468 // Note that X stores the RGB values in the range 0 - 65535
469 // whilst we require them in the range 0 - 255.
470 unsigned int const r = xcol.red / 256;
471 unsigned int const g = xcol.green / 256;
472 unsigned int const b = xcol.blue / 256;
474 return FL_PACK(r, g, b);