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>
33 /// Access to this class is through this static method.
34 Image::ImagePtr QLImage::newImage()
37 ptr.reset(new QLImage);
42 /// Return the list of loadable formats.
43 Image::FormatList QLImage::loadableFormats()
45 static FormatList fmts;
50 // The formats recognised by LyX
51 Formats::const_iterator begin = formats.begin();
52 Formats::const_iterator end = formats.end();
54 lyxerr[Debug::GRAPHICS]
55 << "\nThe image loader can load the following directly:\n";
57 QStrList qt_formats = QImageIO::inputFormats();
59 QStrListIterator it(qt_formats);
61 for (; it.current(); ++it) {
62 lyxerr[Debug::GRAPHICS] << it.current() << endl;
64 string ext = lowercase(it.current());
70 Formats::const_iterator fit =
71 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
73 fmts.push_back(fit->name());
76 lyxerr[Debug::GRAPHICS]
77 << "\nOf these, LyX recognises the following formats:\n";
79 FormatList::const_iterator fbegin = fmts.begin();
80 FormatList::const_iterator fend = fmts.end();
81 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
83 lyxerr[Debug::GRAPHICS] << ", ";
84 lyxerr[Debug::GRAPHICS] << *fit;
86 lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
98 QLImage::QLImage(QLImage const & other)
99 : Image(other), pixmap_(other.pixmap_)
109 Image * QLImage::clone() const
111 return new QLImage(*this);
115 unsigned int QLImage::getWidth() const
117 return pixmap_.width();
121 unsigned int QLImage::getHeight() const
123 return pixmap_.height();
127 void QLImage::load(string const & filename)
129 if (!pixmap_.isNull()) {
130 lyxerr[Debug::GRAPHICS]
131 << "Image is loaded already!" << std::endl;
132 finishedLoading(false);
136 if (!pixmap_.load(filename.c_str())) {
137 lyxerr[Debug::GRAPHICS]
138 << "Unable to open image" << std::endl;
139 finishedLoading(false);
142 finishedLoading(true);
146 bool QLImage::setPixmap(Params const & params)
148 if (pixmap_.isNull() || params.display == NoDisplay)
154 switch (params.display) {
155 case MonochromeDisplay:
156 color_key = FL_IMAGE_MONO;
158 case GrayscaleDisplay:
159 color_key = FL_IMAGE_GRAY;
162 default: // NoDisplay cannot happen!
163 color_key = FL_IMAGE_RGB;
167 if (color_key != FL_IMAGE_RGB) {
168 flimage_convert(image_, color_key, 0);
171 unsigned int fill = packedcolor(LColor::graphicsbg);
172 if (fill != image_->fill_color) {
173 // the background color has changed.
174 // Note that in grayscale/monochrome images the background is
175 // grayed also, so this call will have no visible effect. Sorry!
176 flimage_replace_pixel(image_, image_->fill_color, fill);
177 image_->fill_color = fill;
181 xformed_pixmap_ = pixmap_;
186 void QLImage::clip(Params const & params)
188 if (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 = std::max(0, params.bb.xl);
209 int const yoffset_t = std::max(0, pixmap_.height() - params.bb.yt);
211 xformed_pixmap_.resize(new_width, new_height);
213 p.begin(&xformed_pixmap_);
214 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
219 void QLImage::rotate(Params const & params)
221 if (xformed_pixmap_.isNull())
227 // The angle passed to flimage_rotate is the angle in one-tenth of a
231 m.rotate(params.angle / 10.0);
232 xformed_pixmap_.xForm(m);
236 void QLImage::scale(Params const & params)
238 if (xformed_pixmap_.isNull())
243 boost::tie(width, height) = getScaledDimensions(params);
245 if (width == getWidth() && height == getHeight())
248 xformed_pixmap_.resize(width, height);