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
28 #include <QImageReader>
30 #include <boost/bind.hpp>
31 #include <boost/tuple/tuple.hpp>
33 using lyx::support::lowercase;
42 QPictureIO StaticPicture;
47 /// Access to this class is through this static method.
48 Image::ImagePtr QLImage::newImage()
51 ptr.reset(new QLImage);
56 /// Return the list of loadable formats.
57 Image::FormatList QLImage::loadableFormats()
59 static FormatList fmts;
64 // The formats recognised by LyX
65 Formats::const_iterator begin = formats.begin();
66 Formats::const_iterator end = formats.end();
69 // lyxerr[Debug::GRAPHICS]
70 // << "D:/msys/home/yns/src/lyx-devel/lib/images/banner.ppm is of format: "
71 // << (const char*) Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.ppm")
73 // if (Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.ppm"))
74 // lyxerr[Debug::GRAPHICS]
75 // << "pictureFormat not returned NULL\n" << endl;
76 // << "Supported formats are: " << Pic.inputFormats() << endl;
78 QList<QByteArray> qt_formats = QImageReader::supportedImageFormats ();
80 lyxerr[Debug::GRAPHICS]
81 << "\nThe image loader can load the following directly:\n";
83 if (qt_formats.empty())
84 lyxerr[Debug::GRAPHICS]
85 << "\nQt4 Problem: No Format available!" << endl;
87 for (QList<QByteArray>::const_iterator it =qt_formats.begin(); it != qt_formats.end(); ++it) {
89 lyxerr[Debug::GRAPHICS] << (const char *) *it << ", ";
91 string ext = lowercase((const char *) *it);
97 Formats::const_iterator fit =
99 bind(equal_to<string>(),
100 bind(&Format::extension, _1),
103 fmts.push_back(fit->name());
106 if (lyxerr.debugging()) {
107 lyxerr[Debug::GRAPHICS]
108 << "\nOf these, LyX recognises the following formats:\n";
110 FormatList::const_iterator fbegin = fmts.begin();
111 FormatList::const_iterator fend = fmts.end();
112 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
114 lyxerr[Debug::GRAPHICS] << ", ";
115 lyxerr[Debug::GRAPHICS] << *fit;
117 lyxerr[Debug::GRAPHICS] << '\n' << endl;
130 QLImage::QLImage(QLImage const & other)
131 : Image(other), original_(other.original_),
132 transformed_(other.transformed_),
133 transformed_pixmap_(other.transformed_pixmap_)
137 Image * QLImage::clone_impl() const
139 return new QLImage(*this);
143 unsigned int QLImage::getWidth_impl() const
145 return transformed_.width();
149 unsigned int QLImage::getHeight_impl() const
151 return transformed_.height();
155 void QLImage::load_impl(string const & filename)
157 if (!original_.isNull()) {
158 lyxerr[Debug::GRAPHICS]
159 << "Image is loaded already!" << endl;
160 finishedLoading(false);
164 if (!original_.load(toqstr(filename))) {
165 lyxerr[Debug::GRAPHICS]
166 << "Unable to open image" << endl;
167 finishedLoading(false);
170 transformed_ = original_;
171 finishedLoading(true);
177 // This code is taken from KImageEffect::toGray
178 QImage & toGray(QImage & img)
180 if (img.width() == 0 || img.height() == 0)
183 int const pixels = img.depth() > 8 ?
184 img.width() * img.height() : img.numColors();
186 unsigned int * const data = img.depth() > 8 ?
187 (unsigned int *)img.bits() :
188 (unsigned int *)img.jumpTable();
190 for(int i = 0; i < pixels; ++i){
191 int const val = qGray(data[i]);
192 data[i] = qRgba(val, val, val, qAlpha(data[i]));
200 bool QLImage::setPixmap_impl(Params const & params)
202 if (original_.isNull() || params.display == NoDisplay)
205 switch (params.display) {
206 case GrayscaleDisplay: {
207 toGray(transformed_);
211 case MonochromeDisplay: {
212 transformed_.convertDepth(transformed_.depth(), Qt::MonoOnly);
220 transformed_pixmap_ = transformed_;
225 void QLImage::clip_impl(Params const & params)
227 if (transformed_.isNull())
230 if (params.bb.empty())
231 // No clipping is necessary.
234 int const new_width = params.bb.xr - params.bb.xl;
235 int const new_height = params.bb.yt - params.bb.yb;
237 // No need to check if the width, height are > 0 because the
238 // Bounding Box would be empty() in this case.
239 if (new_width > original_.width() || new_height > original_.height()) {
240 // Bounds are invalid.
244 if (new_width == original_.width() && new_height == original_.height())
247 int const xoffset_l = params.bb.xl;
248 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
249 original_.height() - params.bb.yt : 0);
251 transformed_ = original_.copy(xoffset_l, yoffset_t,
252 new_width, new_height);
256 void QLImage::rotate_impl(Params const & params)
258 if (transformed_.isNull())
265 m.rotate(-params.angle);
267 transformed_.setAlphaBuffer(true);
268 transformed_ = transformed_.xForm(m);
272 void QLImage::scale_impl(Params const & params)
274 if (transformed_.isNull())
279 boost::tie(width, height) = getScaledDimensions(params);
281 if (width == getWidth() && height == getHeight())
285 m.scale(double(width) / getWidth(), double(height) / getHeight());
286 transformed_ = transformed_.xForm(m);
289 } // namespace graphics