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 // << fromqstr(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 // FIXME this code used to be like this:
188 //unsigned int * const data = img.depth() > 8 ?
189 // (unsigned int *)img.bits() :
190 // (unsigned int *)img.jumpTable();
192 // But Qt doc just say use bits...
193 unsigned int * const data = (unsigned int *)img.bits();
195 for(int i = 0; i < pixels; ++i){
196 int const val = qGray(data[i]);
197 data[i] = qRgba(val, val, val, qAlpha(data[i]));
205 bool QLImage::setPixmap_impl(Params const & params)
207 if (original_.isNull() || params.display == NoDisplay)
210 switch (params.display) {
211 case GrayscaleDisplay: {
212 toGray(transformed_);
216 case MonochromeDisplay: {
217 transformed_.convertToFormat(transformed_.format(), Qt::MonoOnly);
225 transformed_pixmap_ = QPixmap::fromImage(transformed_);
230 void QLImage::clip_impl(Params const & params)
232 if (transformed_.isNull())
235 if (params.bb.empty())
236 // No clipping is necessary.
239 int const new_width = params.bb.xr - params.bb.xl;
240 int const new_height = params.bb.yt - params.bb.yb;
242 // No need to check if the width, height are > 0 because the
243 // Bounding Box would be empty() in this case.
244 if (new_width > original_.width() || new_height > original_.height()) {
245 // Bounds are invalid.
249 if (new_width == original_.width() && new_height == original_.height())
252 int const xoffset_l = params.bb.xl;
253 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
254 original_.height() - params.bb.yt : 0);
256 transformed_ = original_.copy(xoffset_l, yoffset_t,
257 new_width, new_height);
261 void QLImage::rotate_impl(Params const & params)
263 if (transformed_.isNull())
270 m.rotate(-params.angle);
272 // FIXME: alpha chanel detection is automautic for monochrome
273 // and 8-bit images. For 32 bit, is something like still necessary?
274 //transformed_.setAlphaBuffer(true);
275 transformed_ = transformed_.transformed(m);
279 void QLImage::scale_impl(Params const & params)
281 if (transformed_.isNull())
286 boost::tie(width, height) = getScaledDimensions(params);
288 if (width == getWidth() && height == getHeight())
292 m.scale(double(width) / getWidth(), double(height) / getHeight());
293 transformed_ = transformed_.transformed(m);
296 } // namespace graphics