3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
7 * \author John Levon <moz@compsoc.man.ac.uk>
13 #pragma implementation
17 #include "graphics/GraphicsParams.h"
18 #include "converter.h"
20 #include "support/LAssert.h"
21 #include "support/lyxfunctional.h" // compare_memfun
27 #include <boost/tuple/tuple.hpp>
34 /// Access to this class is through this static method.
35 Image::ImagePtr QLImage::newImage()
38 ptr.reset(new QLImage);
43 /// Return the list of loadable formats.
44 Image::FormatList QLImage::loadableFormats()
46 static FormatList fmts;
51 // The formats recognised by LyX
52 Formats::const_iterator begin = formats.begin();
53 Formats::const_iterator end = formats.end();
55 lyxerr[Debug::GRAPHICS]
56 << "\nThe image loader can load the following directly:\n";
58 QStrList qt_formats = QImageIO::inputFormats();
60 QStrListIterator it(qt_formats);
62 for (; it.current(); ++it) {
63 lyxerr[Debug::GRAPHICS] << it.current() << endl;
65 string ext = lowercase(it.current());
71 Formats::const_iterator fit =
72 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
74 fmts.push_back(fit->name());
77 lyxerr[Debug::GRAPHICS]
78 << "\nOf these, LyX recognises the following formats:\n";
80 FormatList::const_iterator fbegin = fmts.begin();
81 FormatList::const_iterator fend = fmts.end();
82 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
84 lyxerr[Debug::GRAPHICS] << ", ";
85 lyxerr[Debug::GRAPHICS] << *fit;
87 lyxerr[Debug::GRAPHICS] << '\n' << endl;
99 QLImage::QLImage(QLImage const & other)
100 : Image(other), pixmap_(other.pixmap_), xformed_pixmap_(other.xformed_pixmap_)
110 Image * QLImage::clone() const
112 return new QLImage(*this);
116 unsigned int QLImage::getWidth() const
118 return xformed_pixmap_.width();
122 unsigned int QLImage::getHeight() const
124 return xformed_pixmap_.height();
128 void QLImage::load(string const & filename)
130 if (!pixmap_.isNull()) {
131 lyxerr[Debug::GRAPHICS]
132 << "Image is loaded already!" << endl;
133 finishedLoading(false);
137 if (!pixmap_.load(filename.c_str())) {
138 lyxerr[Debug::GRAPHICS]
139 << "Unable to open image" << endl;
140 finishedLoading(false);
143 lyxerr[Debug::GRAPHICS] << "just Loaded." << endl;
144 xformed_pixmap_ = pixmap_;
145 lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
146 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
147 finishedLoading(true);
151 bool QLImage::setPixmap(Params const & params)
153 lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
154 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
155 if (pixmap_.isNull() || params.display == NoDisplay)
158 lyxerr[Debug::GRAPHICS] << "setPixmap()" << endl;
163 switch (params.display) {
164 case MonochromeDisplay:
165 color_key = FL_IMAGE_MONO;
167 case GrayscaleDisplay:
168 color_key = FL_IMAGE_GRAY;
171 default: // NoDisplay cannot happen!
172 color_key = FL_IMAGE_RGB;
176 if (color_key != FL_IMAGE_RGB) {
177 flimage_convert(image_, color_key, 0);
180 unsigned int fill = packedcolor(LColor::graphicsbg);
181 if (fill != image_->fill_color) {
182 // the background color has changed.
183 // Note that in grayscale/monochrome images the background is
184 // grayed also, so this call will have no visible effect. Sorry!
185 flimage_replace_pixel(image_, image_->fill_color, fill);
186 image_->fill_color = fill;
194 void QLImage::clip(Params const & params)
196 lyxerr << "clip isNull " << pixmap_.isNull() << ", " <<xformed_pixmap_.isNull() << endl;
197 if (xformed_pixmap_.isNull())
200 if (params.bb.empty())
201 // No clipping is necessary.
204 int const new_width = params.bb.xr - params.bb.xl;
205 int const new_height = params.bb.yt - params.bb.yb;
207 // No need to check if the width, height are > 0 because the
208 // Bounding Box would be empty() in this case.
209 if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
210 // Bounds are invalid.
214 if (new_width == pixmap_.width() && new_height == pixmap_.height())
217 int const xoffset_l = std::max(0, int(params.bb.xl));
218 int const yoffset_t = std::max(0, pixmap_.height() - int(params.bb.yt));
220 xformed_pixmap_.resize(new_width, new_height);
222 p.begin(&xformed_pixmap_);
223 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
228 void QLImage::rotate(Params const & params)
230 lyxerr << "rotate isNull " << pixmap_.isNull() << ", " <<xformed_pixmap_.isNull() << endl;
231 if (xformed_pixmap_.isNull())
237 // The angle passed to flimage_rotate is the angle in one-tenth of a
240 lyxerr[Debug::GRAPHICS] << "rotating image by " << params.angle << " degrees" << endl;
243 m.rotate(-params.angle);
244 xformed_pixmap_ = xformed_pixmap_.xForm(m);
248 void QLImage::scale(Params const & params)
250 lyxerr << "scale isNull " << pixmap_.isNull() << ", " <<xformed_pixmap_.isNull() << endl;
251 if (xformed_pixmap_.isNull())
256 boost::tie(width, height) = getScaledDimensions(params);
258 if (width == getWidth() && height == getHeight())
261 lyxerr[Debug::GRAPHICS] << "resizing image to " << width << "(" <<
262 (double(width)/getWidth()) << ")," << height << "(" <<
263 (double(height)/getHeight()) << ")" << endl;
265 m.scale(double(width) / getWidth(), double(height) / getHeight());
266 xformed_pixmap_ = xformed_pixmap_.xForm(m);