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 #include "qt_helpers.h"
20 #include "graphics/GraphicsParams.h"
22 #include "support/lstrings.h" // lowercase
23 #include "support/lyxfunctional.h" // compare_memfun
28 #include <boost/tuple/tuple.hpp>
30 using lyx::support::lowercase;
40 /// Access to this class is through this static method.
41 Image::ImagePtr QLImage::newImage()
44 ptr.reset(new QLImage);
49 /// Return the list of loadable formats.
50 Image::FormatList QLImage::loadableFormats()
52 static FormatList fmts;
57 // The formats recognised by LyX
58 Formats::const_iterator begin = formats.begin();
59 Formats::const_iterator end = formats.end();
61 lyxerr[Debug::GRAPHICS]
62 << "\nThe image loader can load the following directly:\n";
64 QStrList qt_formats = QImageIO::inputFormats();
66 QStrListIterator it(qt_formats);
68 for (; it.current(); ++it) {
69 lyxerr[Debug::GRAPHICS] << it.current() << endl;
71 string ext = lowercase(it.current());
77 Formats::const_iterator fit =
78 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
80 fmts.push_back(fit->name());
83 if (lyxerr.debugging()) {
84 lyxerr[Debug::GRAPHICS]
85 << "\nOf these, LyX recognises the following formats:\n";
87 FormatList::const_iterator fbegin = fmts.begin();
88 FormatList::const_iterator fend = fmts.end();
89 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
91 lyxerr[Debug::GRAPHICS] << ", ";
92 lyxerr[Debug::GRAPHICS] << *fit;
94 lyxerr[Debug::GRAPHICS] << '\n' << endl;
107 QLImage::QLImage(QLImage const & other)
108 : Image(other), original_(other.original_),
109 transformed_(other.original_)
118 Image * QLImage::clone_impl() const
120 return new QLImage(*this);
124 unsigned int QLImage::getWidth_impl() const
126 return transformed_.width();
130 unsigned int QLImage::getHeight_impl() const
132 return transformed_.height();
136 void QLImage::load_impl(string const & filename)
138 if (!original_.isNull()) {
139 lyxerr[Debug::GRAPHICS]
140 << "Image is loaded already!" << endl;
141 finishedLoading(false);
145 if (!original_.load(toqstr(filename))) {
146 lyxerr[Debug::GRAPHICS]
147 << "Unable to open image" << endl;
148 finishedLoading(false);
151 transformed_ = original_;
152 finishedLoading(true);
158 // This code is taken from KImageEffect::toGray
159 QImage & toGray(QImage & img)
161 if (img.width() == 0 || img.height() == 0)
164 int const pixels = img.depth() > 8 ?
165 img.width() * img.height() : img.numColors();
167 unsigned int * const data = img.depth() > 8 ?
168 (unsigned int *)img.bits() :
169 (unsigned int *)img.colorTable();
171 for(int i = 0; i < pixels; ++i){
172 int const val = qGray(data[i]);
173 data[i] = qRgba(val, val, val, qAlpha(data[i]));
181 bool QLImage::setPixmap_impl(Params const & params)
183 if (original_.isNull() || params.display == NoDisplay)
186 switch (params.display) {
187 case GrayscaleDisplay: {
188 toGray(transformed_);
192 case MonochromeDisplay: {
193 transformed_.convertDepth(transformed_.depth(), Qt::MonoOnly);
205 void QLImage::clip_impl(Params const & params)
207 if (transformed_.isNull())
210 if (params.bb.empty())
211 // No clipping is necessary.
214 int const new_width = params.bb.xr - params.bb.xl;
215 int const new_height = params.bb.yt - params.bb.yb;
217 // No need to check if the width, height are > 0 because the
218 // Bounding Box would be empty() in this case.
219 if (new_width > original_.width() || new_height > original_.height()) {
220 // Bounds are invalid.
224 if (new_width == original_.width() && new_height == original_.height())
227 int const xoffset_l = params.bb.xl;
228 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
229 original_.height() - params.bb.yt : 0);
231 transformed_ = original_.copy(xoffset_l, yoffset_t,
232 new_width, new_height);
236 void QLImage::rotate_impl(Params const & params)
238 if (transformed_.isNull())
245 m.rotate(-params.angle);
247 transformed_.setAlphaBuffer(true);
248 transformed_ = transformed_.xForm(m);
252 void QLImage::scale_impl(Params const & params)
254 if (transformed_.isNull())
259 boost::tie(width, height) = getScaledDimensions(params);
261 if (width == getWidth() && height == getHeight())
265 m.scale(double(width) / getWidth(), double(height) / getHeight());
266 transformed_ = transformed_.xForm(m);
269 } // namespace graphics