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/filename.h"
23 #include "support/lstrings.h" // ascii_lowercase
29 #include <QImageReader>
31 #include <boost/bind.hpp>
32 #include <boost/tuple/tuple.hpp>
34 using lyx::support::ascii_lowercase;
43 QPictureIO StaticPicture;
48 /// Access to this class is through this static method.
49 Image::ImagePtr QLImage::newImage()
52 ptr.reset(new QLImage);
57 /// Return the list of loadable formats.
58 Image::FormatList QLImage::loadableFormats()
60 static FormatList fmts;
65 // The formats recognised by LyX
66 Formats::const_iterator begin = formats.begin();
67 Formats::const_iterator end = formats.end();
70 // lyxerr[Debug::GRAPHICS]
71 // << "D:/msys/home/yns/src/lyx-devel/lib/images/banner.ppm is of format: "
72 // << fromqstr(Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.ppm"))
74 // if (Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.ppm"))
75 // lyxerr[Debug::GRAPHICS]
76 // << "pictureFormat not returned NULL\n" << endl;
77 // << "Supported formats are: " << Pic.inputFormats() << endl;
79 QList<QByteArray> qt_formats = QImageReader::supportedImageFormats ();
81 lyxerr[Debug::GRAPHICS]
82 << "\nThe image loader can load the following directly:\n";
84 if (qt_formats.empty())
85 lyxerr[Debug::GRAPHICS]
86 << "\nQt4 Problem: No Format available!" << endl;
88 for (QList<QByteArray>::const_iterator it =qt_formats.begin(); it != qt_formats.end(); ++it) {
90 lyxerr[Debug::GRAPHICS] << (const char *) *it << ", ";
92 string ext = ascii_lowercase((const char *) *it);
98 Formats::const_iterator fit =
100 bind(equal_to<string>(),
101 bind(&Format::extension, _1),
104 fmts.push_back(fit->name());
107 if (lyxerr.debugging()) {
108 lyxerr[Debug::GRAPHICS]
109 << "\nOf these, LyX recognises the following formats:\n";
111 FormatList::const_iterator fbegin = fmts.begin();
112 FormatList::const_iterator fend = fmts.end();
113 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
115 lyxerr[Debug::GRAPHICS] << ", ";
116 lyxerr[Debug::GRAPHICS] << *fit;
118 lyxerr[Debug::GRAPHICS] << '\n' << endl;
131 QLImage::QLImage(QLImage const & other)
132 : Image(other), original_(other.original_),
133 transformed_(other.transformed_),
134 transformed_pixmap_(other.transformed_pixmap_)
138 Image * QLImage::clone_impl() const
140 return new QLImage(*this);
144 unsigned int QLImage::getWidth_impl() const
146 return transformed_.width();
150 unsigned int QLImage::getHeight_impl() const
152 return transformed_.height();
156 void QLImage::load_impl(support::FileName const & filename)
158 if (!original_.isNull()) {
159 lyxerr[Debug::GRAPHICS]
160 << "Image is loaded already!" << endl;
161 finishedLoading(false);
165 if (!original_.load(toqstr(filename.absFilename()))) {
166 lyxerr[Debug::GRAPHICS]
167 << "Unable to open image" << endl;
168 finishedLoading(false);
171 transformed_ = original_;
172 finishedLoading(true);
178 // This code is taken from KImageEffect::toGray
179 QImage & toGray(QImage & img)
181 if (img.width() == 0 || img.height() == 0)
184 int const pixels = img.depth() > 8 ?
185 img.width() * img.height() : img.numColors();
187 unsigned int *data = img.depth() > 8 ?
188 reinterpret_cast<unsigned int *>(img.bits()) :
189 reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
191 for(int i = 0; i < pixels; ++i){
192 int const val = qGray(data[i]);
193 data[i] = qRgba(val, val, val, qAlpha(data[i]));
201 bool QLImage::setPixmap_impl(Params const & params)
203 if (original_.isNull() || params.display == NoDisplay)
206 switch (params.display) {
207 case GrayscaleDisplay: {
208 toGray(transformed_);
212 case MonochromeDisplay: {
213 transformed_.convertToFormat(transformed_.format(), Qt::MonoOnly);
221 transformed_pixmap_ = QPixmap::fromImage(transformed_);
226 void QLImage::clip_impl(Params const & params)
228 if (transformed_.isNull())
231 if (params.bb.empty())
232 // No clipping is necessary.
235 int const new_width = params.bb.xr - params.bb.xl;
236 int const new_height = params.bb.yt - params.bb.yb;
238 // No need to check if the width, height are > 0 because the
239 // Bounding Box would be empty() in this case.
240 if (new_width > original_.width() || new_height > original_.height()) {
241 // Bounds are invalid.
245 if (new_width == original_.width() && new_height == original_.height())
248 int const xoffset_l = params.bb.xl;
249 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
250 original_.height() - params.bb.yt : 0);
252 transformed_ = original_.copy(xoffset_l, yoffset_t,
253 new_width, new_height);
257 void QLImage::rotate_impl(Params const & params)
259 if (transformed_.isNull())
266 m.rotate(-params.angle);
268 transformed_ = transformed_.transformed(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_.transformed(m);
289 } // namespace graphics