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"
19 #include "graphics/GraphicsParams.h"
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/lstrings.h" // ascii_lowercase
27 #include <QImageReader>
30 using namespace lyx::support;
35 /// Access to this class is through this static method.
36 Image * GuiImage::newImage()
42 GuiImage::GuiImage(GuiImage const & other)
43 : Image(other), original_(other.original_),
44 transformed_(other.transformed_), is_transformed_(other.is_transformed_)
48 Image * GuiImage::clone() const
50 return new GuiImage(*this);
54 unsigned int GuiImage::width() const
56 return is_transformed_ ? transformed_.width() : original_.width();
60 unsigned int GuiImage::height() const
62 return is_transformed_ ? transformed_.height() : original_.height();
66 bool GuiImage::load(FileName const & filename)
68 if (!original_.isNull()) {
69 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
73 if (!original_.load(toqstr(filename.absFilename()))) {
74 LYXERR(Debug::GRAPHICS, "Unable to open image");
81 // This code is taken from KImageEffect::toGray
82 static QPixmap toGray(QPixmap const & pix)
84 if (pix.width() == 0 || pix.height() == 0)
87 QImage img = pix.toImage();
88 int const pixels = img.depth() > 8 ?
89 img.width() * img.height() : img.numColors();
91 unsigned int *data = img.depth() > 8 ?
92 reinterpret_cast<unsigned int *>(img.bits()) :
93 reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
95 for(int i = 0; i < pixels; ++i){
96 int const val = qGray(data[i]);
97 data[i] = qRgba(val, val, val, qAlpha(data[i]));
99 return QPixmap::fromImage(img);
103 bool GuiImage::setPixmap(Params const & params)
105 if (original_.isNull() || params.display == NoDisplay)
108 is_transformed_ = clip(params);
109 is_transformed_ |= rotate(params);
110 is_transformed_ |= scale(params);
112 switch (params.display) {
113 case GrayscaleDisplay: {
114 transformed_ = is_transformed_
115 ? toGray(transformed_) : toGray(original_);
116 is_transformed_ = true;
120 case MonochromeDisplay: {
121 QImage img = is_transformed_
122 ? transformed_.toImage() : original_.toImage();
123 img.convertToFormat(img.format(), Qt::MonoOnly);
124 transformed_ = QPixmap::fromImage(img);
125 is_transformed_ = true;
133 if (!is_transformed_)
134 // Clear it out to save some memory.
135 transformed_ = QPixmap();
141 bool GuiImage::clip(Params const & params)
143 if (params.bb.empty())
144 // No clipping is necessary.
147 int const new_width = params.bb.xr - params.bb.xl;
148 int const new_height = params.bb.yt - params.bb.yb;
150 // No need to check if the width, height are > 0 because the
151 // Bounding Box would be empty() in this case.
152 if (new_width > original_.width() || new_height > original_.height()) {
153 // Bounds are invalid.
157 if (new_width == original_.width() && new_height == original_.height())
160 int const xoffset_l = params.bb.xl;
161 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
162 original_.height() - params.bb.yt : 0);
164 transformed_ = original_.copy(xoffset_l, yoffset_t,
165 new_width, new_height);
170 bool GuiImage::rotate(Params const & params)
175 if (!is_transformed_)
176 transformed_ = original_;
179 m.rotate(-params.angle);
180 transformed_ = transformed_.transformed(m);
185 bool GuiImage::scale(Params const & params)
187 Dimension dim = scaledDimension(params);
189 if (dim.width() == width() && dim.height() == height())
192 if (!is_transformed_)
193 transformed_ = original_;
196 m.scale(double(dim.width()) / width(), double(dim.height()) / height());
197 transformed_ = transformed_.transformed(m);
202 } // namespace graphics