3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
7 * \author John Levon <moz@compsoc.man.ac.uk>
13 #pragma implementation
17 #include "graphics/GraphicsParams.h"
18 #include "converter.h"
20 #include "support/LAssert.h"
21 #include "support/lyxfunctional.h" // compare_memfun
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() << std::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' << std::endl;
99 QLImage::QLImage(QLImage const & other)
100 : Image(other), pixmap_(other.pixmap_)
110 Image * QLImage::clone() const
112 return new QLImage(*this);
116 unsigned int QLImage::getWidth() const
118 return pixmap_.width();
122 unsigned int QLImage::getHeight() const
124 return pixmap_.height();
128 void QLImage::load(string const & filename)
130 if (!pixmap_.isNull()) {
131 lyxerr[Debug::GRAPHICS]
132 << "Image is loaded already!" << std::endl;
133 finishedLoading(false);
137 if (!pixmap_.load(filename.c_str())) {
138 lyxerr[Debug::GRAPHICS]
139 << "Unable to open image" << std::endl;
140 finishedLoading(false);
143 finishedLoading(true);
147 bool QLImage::setPixmap(Params const & params)
149 if (pixmap_.isNull() || params.display == NoDisplay)
155 switch (params.display) {
156 case MonochromeDisplay:
157 color_key = FL_IMAGE_MONO;
159 case GrayscaleDisplay:
160 color_key = FL_IMAGE_GRAY;
163 default: // NoDisplay cannot happen!
164 color_key = FL_IMAGE_RGB;
168 if (color_key != FL_IMAGE_RGB) {
169 flimage_convert(image_, color_key, 0);
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;
182 xformed_pixmap_ = pixmap_;
187 void QLImage::clip(Params const & params)
189 if (pixmap_.isNull())
192 if (params.bb.empty())
193 // No clipping is necessary.
196 int const new_width = params.bb.xr - params.bb.xl;
197 int const new_height = params.bb.yt - params.bb.yb;
199 // No need to check if the width, height are > 0 because the
200 // Bounding Box would be empty() in this case.
201 if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
202 // Bounds are invalid.
206 if (new_width == pixmap_.width() && new_height == pixmap_.height())
209 int const xoffset_l = std::max(0, params.bb.xl);
210 int const yoffset_t = std::max(0, pixmap_.height() - params.bb.yt);
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 / 10.0);
233 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())
249 xformed_pixmap_.resize(width, height);