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
29 #include <boost/tuple/tuple.hpp>
36 /// Access to this class is through this static method.
37 Image::ImagePtr QLImage::newImage()
40 ptr.reset(new QLImage);
45 /// Return the list of loadable formats.
46 Image::FormatList QLImage::loadableFormats()
48 static FormatList fmts;
53 // The formats recognised by LyX
54 Formats::const_iterator begin = formats.begin();
55 Formats::const_iterator end = formats.end();
57 lyxerr[Debug::GRAPHICS]
58 << "\nThe image loader can load the following directly:\n";
60 QStrList qt_formats = QImageIO::inputFormats();
62 QStrListIterator it(qt_formats);
64 for (; it.current(); ++it) {
65 lyxerr[Debug::GRAPHICS] << it.current() << endl;
67 string ext = lowercase(it.current());
73 Formats::const_iterator fit =
74 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
76 fmts.push_back(fit->name());
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;
101 QLImage::QLImage(QLImage const & other)
102 : Image(other), pixmap_(other.pixmap_),
103 xformed_pixmap_(other.xformed_pixmap_)
113 Image * QLImage::clone() const
115 return new QLImage(*this);
119 unsigned int QLImage::getWidth() const
121 return xformed_pixmap_.width();
125 unsigned int QLImage::getHeight() const
127 return xformed_pixmap_.height();
131 void QLImage::load(string const & filename)
133 if (!pixmap_.isNull()) {
134 lyxerr[Debug::GRAPHICS]
135 << "Image is loaded already!" << endl;
136 finishedLoading(false);
140 if (!pixmap_.load(filename.c_str())) {
141 lyxerr[Debug::GRAPHICS]
142 << "Unable to open image" << endl;
143 finishedLoading(false);
146 lyxerr[Debug::GRAPHICS] << "just Loaded." << endl;
147 xformed_pixmap_ = pixmap_;
148 lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
149 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
150 finishedLoading(true);
154 bool QLImage::setPixmap(Params const & params)
156 lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
157 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
158 if (pixmap_.isNull() || params.display == NoDisplay)
161 lyxerr[Debug::GRAPHICS] << "setPixmap()" << endl;
163 // FIXME: it's a fake kind of grayscale !
165 switch (params.display) {
166 case GrayscaleDisplay:
167 case MonochromeDisplay: {
168 QImage i(xformed_pixmap_.convertToImage());
169 xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
178 unsigned int fill = packedcolor(LColor::graphicsbg);
179 if (fill != image_->fill_color) {
180 // the background color has changed.
181 // Note that in grayscale/monochrome images the background is
182 // grayed also, so this call will have no visible effect. Sorry!
183 flimage_replace_pixel(image_, image_->fill_color, fill);
184 image_->fill_color = fill;
192 void QLImage::clip(Params const & params)
194 if (xformed_pixmap_.isNull())
197 if (params.bb.empty())
198 // No clipping is necessary.
201 int const new_width = params.bb.xr - params.bb.xl;
202 int const new_height = params.bb.yt - params.bb.yb;
204 // No need to check if the width, height are > 0 because the
205 // Bounding Box would be empty() in this case.
206 if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
207 // Bounds are invalid.
211 if (new_width == pixmap_.width() && new_height == pixmap_.height())
214 int const xoffset_l = params.bb.xl;
215 int const yoffset_t = (pixmap_.height() > params.bb.yt ?
216 pixmap_.height() - params.bb.yt : 0);
218 xformed_pixmap_.resize(new_width, new_height);
220 p.begin(&xformed_pixmap_);
221 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
226 void QLImage::rotate(Params const & params)
228 if (xformed_pixmap_.isNull())
234 // The angle passed to flimage_rotate is the angle in one-tenth of a
237 lyxerr[Debug::GRAPHICS] << "rotating image by " << params.angle << " degrees" << endl;
240 m.rotate(-params.angle);
241 xformed_pixmap_ = xformed_pixmap_.xForm(m);
245 void QLImage::scale(Params const & params)
247 if (xformed_pixmap_.isNull())
252 boost::tie(width, height) = getScaledDimensions(params);
254 if (width == getWidth() && height == getHeight())
257 lyxerr[Debug::GRAPHICS] << "resizing image to " << width << '('
258 << (double(width)/getWidth()) << "),"
260 << (double(height)/getHeight()) << ')' << endl;
262 m.scale(double(width) / getWidth(), double(height) / getHeight());
263 xformed_pixmap_ = xformed_pixmap_.xForm(m);