3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
19 #include "graphics/GraphicsParams.h"
20 #include "converter.h"
22 #include "support/LAssert.h"
23 #include "support/lyxfunctional.h" // compare_memfun
24 #include "qt_helpers.h"
30 #include <boost/tuple/tuple.hpp>
37 /// Access to this class is through this static method.
38 Image::ImagePtr QLImage::newImage()
41 ptr.reset(new QLImage);
46 /// Return the list of loadable formats.
47 Image::FormatList QLImage::loadableFormats()
49 static FormatList fmts;
54 // The formats recognised by LyX
55 Formats::const_iterator begin = formats.begin();
56 Formats::const_iterator end = formats.end();
58 lyxerr[Debug::GRAPHICS]
59 << "\nThe image loader can load the following directly:\n";
61 QStrList qt_formats = QImageIO::inputFormats();
63 QStrListIterator it(qt_formats);
65 for (; it.current(); ++it) {
66 lyxerr[Debug::GRAPHICS] << it.current() << endl;
68 string ext = lowercase(it.current());
74 Formats::const_iterator fit =
75 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
77 fmts.push_back(fit->name());
80 lyxerr[Debug::GRAPHICS]
81 << "\nOf these, LyX recognises the following formats:\n";
83 FormatList::const_iterator fbegin = fmts.begin();
84 FormatList::const_iterator fend = fmts.end();
85 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
87 lyxerr[Debug::GRAPHICS] << ", ";
88 lyxerr[Debug::GRAPHICS] << *fit;
90 lyxerr[Debug::GRAPHICS] << '\n' << endl;
102 QLImage::QLImage(QLImage const & other)
103 : Image(other), pixmap_(other.pixmap_),
104 xformed_pixmap_(other.xformed_pixmap_)
114 Image * QLImage::clone() const
116 return new QLImage(*this);
120 unsigned int QLImage::getWidth() const
122 return xformed_pixmap_.width();
126 unsigned int QLImage::getHeight() const
128 return xformed_pixmap_.height();
132 void QLImage::load(string const & filename)
134 if (!pixmap_.isNull()) {
135 lyxerr[Debug::GRAPHICS]
136 << "Image is loaded already!" << endl;
137 finishedLoading(false);
141 if (!pixmap_.load(toqstr(filename))) {
142 lyxerr[Debug::GRAPHICS]
143 << "Unable to open image" << endl;
144 finishedLoading(false);
147 lyxerr[Debug::GRAPHICS] << "just Loaded." << endl;
148 xformed_pixmap_ = pixmap_;
149 lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
150 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
151 finishedLoading(true);
155 bool QLImage::setPixmap(Params const & params)
157 lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
158 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
159 if (pixmap_.isNull() || params.display == NoDisplay)
162 lyxerr[Debug::GRAPHICS] << "setPixmap()" << endl;
164 // FIXME: it's a fake kind of grayscale !
166 switch (params.display) {
167 case GrayscaleDisplay:
168 case MonochromeDisplay: {
169 QImage i(xformed_pixmap_.convertToImage());
170 xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
179 unsigned int fill = packedcolor(LColor::graphicsbg);
180 if (fill != image_->fill_color) {
181 // the background color has changed.
182 // Note that in grayscale/monochrome images the background is
183 // grayed also, so this call will have no visible effect. Sorry!
184 flimage_replace_pixel(image_, image_->fill_color, fill);
185 image_->fill_color = fill;
193 void QLImage::clip(Params const & params)
195 if (xformed_pixmap_.isNull())
198 if (params.bb.empty())
199 // No clipping is necessary.
202 int const new_width = params.bb.xr - params.bb.xl;
203 int const new_height = params.bb.yt - params.bb.yb;
205 // No need to check if the width, height are > 0 because the
206 // Bounding Box would be empty() in this case.
207 if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
208 // Bounds are invalid.
212 if (new_width == pixmap_.width() && new_height == pixmap_.height())
215 int const xoffset_l = params.bb.xl;
216 int const yoffset_t = (pixmap_.height() > params.bb.yt ?
217 pixmap_.height() - params.bb.yt : 0);
219 xformed_pixmap_.resize(new_width, new_height);
221 p.begin(&xformed_pixmap_);
222 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
227 void QLImage::rotate(Params const & params)
229 if (xformed_pixmap_.isNull())
235 // The angle passed to flimage_rotate is the angle in one-tenth of a
238 lyxerr[Debug::GRAPHICS] << "rotating image by " << params.angle << " degrees" << endl;
241 m.rotate(-params.angle);
242 xformed_pixmap_ = xformed_pixmap_.xForm(m);
246 void QLImage::scale(Params const & params)
248 if (xformed_pixmap_.isNull())
253 boost::tie(width, height) = getScaledDimensions(params);
255 if (width == getWidth() && height == getHeight())
258 lyxerr[Debug::GRAPHICS] << "resizing image to " << width << '('
259 << (double(width)/getWidth()) << "),"
261 << (double(height)/getHeight()) << ')' << endl;
263 m.scale(double(width) / getWidth(), double(height) / getHeight());
264 xformed_pixmap_ = xformed_pixmap_.xForm(m);