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
27 #include <QImageReader>
29 #include <boost/bind.hpp>
30 #include <boost/tuple/tuple.hpp>
32 using lyx::support::ascii_lowercase;
44 /// Access to this class is through this static method.
45 Image::ImagePtr GuiImage::newImage()
48 ptr.reset(new GuiImage);
53 /// Return the list of loadable formats.
54 Image::FormatList GuiImage::loadableFormats()
56 static FormatList fmts;
61 // The formats recognised by LyX
62 Formats::const_iterator begin = formats.begin();
63 Formats::const_iterator end = formats.end();
66 // LYXERR(Debug::GRAPHICS,
67 // "D:/msys/home/yns/src/lyx-devel/lib/images/banner.png mis of format: "
68 // << fromqstr(Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.png")))
69 // if (Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.png"))
70 // LYXERR(Debug::GRAPHICS, "pictureFormat not returned NULL\n"
71 // << "Supported formats are: " << Pic.inputFormats());
73 QList<QByteArray> qt_formats = QImageReader::supportedImageFormats();
75 LYXERR(Debug::GRAPHICS,
76 "\nThe image loader can load the following directly:\n");
78 if (qt_formats.empty())
79 LYXERR(Debug::GRAPHICS, "\nQt4 Problem: No Format available!");
81 for (QList<QByteArray>::const_iterator it = qt_formats.begin(); it != qt_formats.end(); ++it) {
83 LYXERR(Debug::GRAPHICS, (const char *) *it << ", ");
85 string ext = ascii_lowercase((const char *) *it);
91 Formats::const_iterator fit =
93 bind(equal_to<string>(),
94 bind(&Format::extension, _1),
97 fmts.push_back(fit->name());
100 if (lyxerr.debugging()) {
101 LYXERR(Debug::GRAPHICS, "\nOf these, LyX recognises the following formats:");
103 FormatList::const_iterator fbegin = fmts.begin();
104 FormatList::const_iterator fend = fmts.end();
105 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
107 LYXERR(Debug::GRAPHICS, ", ");
108 LYXERR(Debug::GRAPHICS, *fit);
110 LYXERR(Debug::GRAPHICS, '\n');
117 GuiImage::GuiImage(GuiImage const & other)
118 : Image(other), original_(other.original_),
119 transformed_(other.transformed_),
120 transformed_pixmap_(other.transformed_pixmap_)
124 Image * GuiImage::clone_impl() const
126 return new GuiImage(*this);
130 unsigned int GuiImage::getWidth_impl() const
132 return transformed_.width();
136 unsigned int GuiImage::getHeight_impl() const
138 return transformed_.height();
142 void GuiImage::load_impl(support::FileName const & filename)
144 if (!original_.isNull()) {
145 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
146 finishedLoading(false);
150 if (!original_.load(toqstr(filename.absFilename()))) {
151 LYXERR(Debug::GRAPHICS, "Unable to open image");
152 finishedLoading(false);
155 transformed_ = original_;
156 finishedLoading(true);
160 // This code is taken from KImageEffect::toGray
161 static QImage & toGray(QImage & img)
163 if (img.width() == 0 || img.height() == 0)
166 int const pixels = img.depth() > 8 ?
167 img.width() * img.height() : img.numColors();
169 unsigned int *data = img.depth() > 8 ?
170 reinterpret_cast<unsigned int *>(img.bits()) :
171 reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
173 for(int i = 0; i < pixels; ++i){
174 int const val = qGray(data[i]);
175 data[i] = qRgba(val, val, val, qAlpha(data[i]));
181 bool GuiImage::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_.convertToFormat(transformed_.format(), Qt::MonoOnly);
201 transformed_pixmap_ = QPixmap::fromImage(transformed_);
206 void GuiImage::clip_impl(Params const & params)
208 if (transformed_.isNull())
211 if (params.bb.empty())
212 // No clipping is necessary.
215 int const new_width = params.bb.xr - params.bb.xl;
216 int const new_height = params.bb.yt - params.bb.yb;
218 // No need to check if the width, height are > 0 because the
219 // Bounding Box would be empty() in this case.
220 if (new_width > original_.width() || new_height > original_.height()) {
221 // Bounds are invalid.
225 if (new_width == original_.width() && new_height == original_.height())
228 int const xoffset_l = params.bb.xl;
229 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
230 original_.height() - params.bb.yt : 0);
232 transformed_ = original_.copy(xoffset_l, yoffset_t,
233 new_width, new_height);
237 void GuiImage::rotate_impl(Params const & params)
239 if (transformed_.isNull())
246 m.rotate(-params.angle);
248 transformed_ = transformed_.transformed(m);
252 void GuiImage::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_.transformed(m);
269 } // namespace graphics