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"
17 #include "converter.h"
19 #include "support/LAssert.h"
20 #include "support/lyxfunctional.h" // compare_memfun
21 #include "qt_helpers.h"
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_),
101 xformed_pixmap_(other.xformed_pixmap_)
111 Image * QLImage::clone() const
113 return new QLImage(*this);
117 unsigned int QLImage::getWidth() const
119 return xformed_pixmap_.width();
123 unsigned int QLImage::getHeight() const
125 return xformed_pixmap_.height();
129 void QLImage::load(string const & filename)
131 if (!pixmap_.isNull()) {
132 lyxerr[Debug::GRAPHICS]
133 << "Image is loaded already!" << endl;
134 finishedLoading(false);
138 if (!pixmap_.load(toqstr(filename))) {
139 lyxerr[Debug::GRAPHICS]
140 << "Unable to open image" << endl;
141 finishedLoading(false);
144 lyxerr[Debug::GRAPHICS] << "just Loaded." << endl;
145 xformed_pixmap_ = pixmap_;
146 lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
147 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
148 finishedLoading(true);
152 bool QLImage::setPixmap(Params const & params)
154 lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
155 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
156 if (pixmap_.isNull() || params.display == NoDisplay)
159 lyxerr[Debug::GRAPHICS] << "setPixmap()" << endl;
161 // FIXME: it's a fake kind of grayscale !
163 switch (params.display) {
164 case GrayscaleDisplay:
165 case MonochromeDisplay: {
166 QImage i(xformed_pixmap_.convertToImage());
167 xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
176 unsigned int fill = packedcolor(LColor::graphicsbg);
177 if (fill != image_->fill_color) {
178 // the background color has changed.
179 // Note that in grayscale/monochrome images the background is
180 // grayed also, so this call will have no visible effect. Sorry!
181 flimage_replace_pixel(image_, image_->fill_color, fill);
182 image_->fill_color = fill;
190 void QLImage::clip(Params const & params)
192 if (xformed_pixmap_.isNull())
195 if (params.bb.empty())
196 // No clipping is necessary.
199 int const new_width = params.bb.xr - params.bb.xl;
200 int const new_height = params.bb.yt - params.bb.yb;
202 // No need to check if the width, height are > 0 because the
203 // Bounding Box would be empty() in this case.
204 if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
205 // Bounds are invalid.
209 if (new_width == pixmap_.width() && new_height == pixmap_.height())
212 int const xoffset_l = params.bb.xl;
213 int const yoffset_t = (pixmap_.height() > params.bb.yt ?
214 pixmap_.height() - params.bb.yt : 0);
216 xformed_pixmap_.resize(new_width, new_height);
218 p.begin(&xformed_pixmap_);
219 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
224 void QLImage::rotate(Params const & params)
226 if (xformed_pixmap_.isNull())
232 // The angle passed to flimage_rotate is the angle in one-tenth of a
235 lyxerr[Debug::GRAPHICS] << "rotating image by " << params.angle << " degrees" << endl;
238 m.rotate(-params.angle);
239 xformed_pixmap_ = xformed_pixmap_.xForm(m);
243 void QLImage::scale(Params const & params)
245 if (xformed_pixmap_.isNull())
250 boost::tie(width, height) = getScaledDimensions(params);
252 if (width == getWidth() && height == getHeight())
255 lyxerr[Debug::GRAPHICS] << "resizing image to " << width << '('
256 << (double(width)/getWidth()) << "),"
258 << (double(height)/getHeight()) << ')' << endl;
260 m.scale(double(width) / getWidth(), double(height) / getHeight());
261 xformed_pixmap_ = xformed_pixmap_.xForm(m);