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>
30 using namespace lyx::support;
38 /// Access to this class is through this static method.
39 Image::ImagePtr QLImage::newImage()
42 ptr.reset(new QLImage);
47 /// Return the list of loadable formats.
48 Image::FormatList QLImage::loadableFormats()
50 static FormatList fmts;
55 // The formats recognised by LyX
56 Formats::const_iterator begin = formats.begin();
57 Formats::const_iterator end = formats.end();
59 lyxerr[Debug::GRAPHICS]
60 << "\nThe image loader can load the following directly:\n";
62 QStrList qt_formats = QImageIO::inputFormats();
64 QStrListIterator it(qt_formats);
66 for (; it.current(); ++it) {
67 lyxerr[Debug::GRAPHICS] << it.current() << endl;
69 string ext = lowercase(it.current());
75 Formats::const_iterator fit =
76 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
78 fmts.push_back(fit->name());
81 if (lyxerr.debugging()) {
82 lyxerr[Debug::GRAPHICS]
83 << "\nOf these, LyX recognises the following formats:\n";
85 FormatList::const_iterator fbegin = fmts.begin();
86 FormatList::const_iterator fend = fmts.end();
87 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
89 lyxerr[Debug::GRAPHICS] << ", ";
90 lyxerr[Debug::GRAPHICS] << *fit;
92 lyxerr[Debug::GRAPHICS] << '\n' << endl;
105 QLImage::QLImage(QLImage const & other)
106 : Image(other), pixmap_(other.pixmap_),
107 xformed_pixmap_(other.xformed_pixmap_)
117 Image * QLImage::clone() const
119 return new QLImage(*this);
123 unsigned int QLImage::getWidth() const
125 return xformed_pixmap_.width();
129 unsigned int QLImage::getHeight() const
131 return xformed_pixmap_.height();
135 void QLImage::load(string const & filename)
137 if (!pixmap_.isNull()) {
138 lyxerr[Debug::GRAPHICS]
139 << "Image is loaded already!" << endl;
140 finishedLoading(false);
144 if (!pixmap_.load(toqstr(filename))) {
145 lyxerr[Debug::GRAPHICS]
146 << "Unable to open image" << endl;
147 finishedLoading(false);
150 xformed_pixmap_ = pixmap_;
151 finishedLoading(true);
155 bool QLImage::setPixmap(Params const & params)
157 if (pixmap_.isNull() || params.display == NoDisplay)
160 // FIXME: it's a fake kind of grayscale !
162 switch (params.display) {
163 case GrayscaleDisplay:
164 case MonochromeDisplay: {
165 QImage i(xformed_pixmap_.convertToImage());
166 xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
175 unsigned int fill = packedcolor(LColor::graphicsbg);
176 if (fill != image_->fill_color) {
177 // the background color has changed.
178 // Note that in grayscale/monochrome images the background is
179 // grayed also, so this call will have no visible effect. Sorry!
180 flimage_replace_pixel(image_, image_->fill_color, fill);
181 image_->fill_color = fill;
189 void QLImage::clip(Params const & params)
191 if (xformed_pixmap_.isNull())
194 if (params.bb.empty())
195 // No clipping is necessary.
198 int const new_width = params.bb.xr - params.bb.xl;
199 int const new_height = params.bb.yt - params.bb.yb;
201 // No need to check if the width, height are > 0 because the
202 // Bounding Box would be empty() in this case.
203 if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
204 // Bounds are invalid.
208 if (new_width == pixmap_.width() && new_height == pixmap_.height())
211 int const xoffset_l = params.bb.xl;
212 int const yoffset_t = (pixmap_.height() > params.bb.yt ?
213 pixmap_.height() - params.bb.yt : 0);
215 xformed_pixmap_.resize(new_width, new_height);
217 p.begin(&xformed_pixmap_);
218 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
223 void QLImage::rotate(Params const & params)
225 if (xformed_pixmap_.isNull())
231 // The angle passed to flimage_rotate is the angle in one-tenth of a
235 m.rotate(-params.angle);
236 xformed_pixmap_ = xformed_pixmap_.xForm(m);
240 void QLImage::scale(Params const & params)
242 if (xformed_pixmap_.isNull())
247 boost::tie(width, height) = getScaledDimensions(params);
249 if (width == getWidth() && height == getHeight())
253 m.scale(double(width) / getWidth(), double(height) / getHeight());
254 xformed_pixmap_ = xformed_pixmap_.xForm(m);
257 } // namespace graphics