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
16 #include "graphics/GraphicsParams.h"
19 #include "support/LAssert.h"
20 #include "support/lstrings.h" // lowercase
21 #include "support/lyxfunctional.h" // compare_memfun
22 #include "qt_helpers.h"
28 #include <boost/tuple/tuple.hpp>
35 /// Access to this class is through this static method.
36 Image::ImagePtr QLImage::newImage()
39 ptr.reset(new QLImage);
44 /// Return the list of loadable formats.
45 Image::FormatList QLImage::loadableFormats()
47 static FormatList fmts;
52 // The formats recognised by LyX
53 Formats::const_iterator begin = formats.begin();
54 Formats::const_iterator end = formats.end();
56 lyxerr[Debug::GRAPHICS]
57 << "\nThe image loader can load the following directly:\n";
59 QStrList qt_formats = QImageIO::inputFormats();
61 QStrListIterator it(qt_formats);
63 for (; it.current(); ++it) {
64 lyxerr[Debug::GRAPHICS] << it.current() << endl;
66 string ext = lowercase(it.current());
72 Formats::const_iterator fit =
73 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
75 fmts.push_back(fit->name());
78 if (lyxerr.debugging()) {
79 lyxerr[Debug::GRAPHICS]
80 << "\nOf these, LyX recognises the following formats:\n";
82 FormatList::const_iterator fbegin = fmts.begin();
83 FormatList::const_iterator fend = fmts.end();
84 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
86 lyxerr[Debug::GRAPHICS] << ", ";
87 lyxerr[Debug::GRAPHICS] << *fit;
89 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 xformed_pixmap_ = pixmap_;
148 finishedLoading(true);
152 bool QLImage::setPixmap(Params const & params)
154 if (pixmap_.isNull() || params.display == NoDisplay)
157 // FIXME: it's a fake kind of grayscale !
159 switch (params.display) {
160 case GrayscaleDisplay:
161 case MonochromeDisplay: {
162 QImage i(xformed_pixmap_.convertToImage());
163 xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
172 unsigned int fill = packedcolor(LColor::graphicsbg);
173 if (fill != image_->fill_color) {
174 // the background color has changed.
175 // Note that in grayscale/monochrome images the background is
176 // grayed also, so this call will have no visible effect. Sorry!
177 flimage_replace_pixel(image_, image_->fill_color, fill);
178 image_->fill_color = fill;
186 void QLImage::clip(Params const & params)
188 if (xformed_pixmap_.isNull())
191 if (params.bb.empty())
192 // No clipping is necessary.
195 int const new_width = params.bb.xr - params.bb.xl;
196 int const new_height = params.bb.yt - params.bb.yb;
198 // No need to check if the width, height are > 0 because the
199 // Bounding Box would be empty() in this case.
200 if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
201 // Bounds are invalid.
205 if (new_width == pixmap_.width() && new_height == pixmap_.height())
208 int const xoffset_l = params.bb.xl;
209 int const yoffset_t = (pixmap_.height() > params.bb.yt ?
210 pixmap_.height() - params.bb.yt : 0);
212 xformed_pixmap_.resize(new_width, new_height);
214 p.begin(&xformed_pixmap_);
215 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
220 void QLImage::rotate(Params const & params)
222 if (xformed_pixmap_.isNull())
228 // The angle passed to flimage_rotate is the angle in one-tenth of a
232 m.rotate(-params.angle);
233 xformed_pixmap_ = xformed_pixmap_.xForm(m);
237 void QLImage::scale(Params const & params)
239 if (xformed_pixmap_.isNull())
244 boost::tie(width, height) = getScaledDimensions(params);
246 if (width == getWidth() && height == getHeight())
250 m.scale(double(width) / getWidth(), double(height) / getHeight());
251 xformed_pixmap_ = xformed_pixmap_.xForm(m);