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_),
45 transformed_pixmap_(other.transformed_pixmap_)
49 Image * GuiImage::clone() const
51 return new GuiImage(*this);
55 unsigned int GuiImage::width() const
57 return transformed_.width();
61 unsigned int GuiImage::height() const
63 return transformed_.height();
67 bool GuiImage::load(FileName const & filename)
69 if (!original_.isNull()) {
70 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
74 if (!original_.load(toqstr(filename.absFilename()))) {
75 LYXERR(Debug::GRAPHICS, "Unable to open image");
78 transformed_ = original_;
83 // This code is taken from KImageEffect::toGray
84 static QImage & toGray(QImage & img)
86 if (img.width() == 0 || img.height() == 0)
89 int const pixels = img.depth() > 8 ?
90 img.width() * img.height() : img.numColors();
92 unsigned int *data = img.depth() > 8 ?
93 reinterpret_cast<unsigned int *>(img.bits()) :
94 reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
96 for(int i = 0; i < pixels; ++i){
97 int const val = qGray(data[i]);
98 data[i] = qRgba(val, val, val, qAlpha(data[i]));
104 bool GuiImage::setPixmap(Params const & params)
106 if (original_.isNull() || params.display == NoDisplay)
109 switch (params.display) {
110 case GrayscaleDisplay: {
111 toGray(transformed_);
115 case MonochromeDisplay: {
116 transformed_.convertToFormat(transformed_.format(), Qt::MonoOnly);
124 transformed_pixmap_ = QPixmap::fromImage(transformed_);
129 void GuiImage::clip(Params const & params)
131 if (transformed_.isNull())
134 if (params.bb.empty())
135 // No clipping is necessary.
138 int const new_width = params.bb.xr - params.bb.xl;
139 int const new_height = params.bb.yt - params.bb.yb;
141 // No need to check if the width, height are > 0 because the
142 // Bounding Box would be empty() in this case.
143 if (new_width > original_.width() || new_height > original_.height()) {
144 // Bounds are invalid.
148 if (new_width == original_.width() && new_height == original_.height())
151 int const xoffset_l = params.bb.xl;
152 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
153 original_.height() - params.bb.yt : 0);
155 transformed_ = original_.copy(xoffset_l, yoffset_t,
156 new_width, new_height);
160 void GuiImage::rotate(Params const & params)
162 if (transformed_.isNull())
169 m.rotate(-params.angle);
171 transformed_ = transformed_.transformed(m);
175 void GuiImage::scale(Params const & params)
177 if (transformed_.isNull())
180 Dimension dim = scaledDimension(params);
182 if (dim.width() == width() && dim.height() == height())
186 m.scale(double(dim.width()) / width(), double(dim.height()) / height());
187 transformed_ = transformed_.transformed(m);
190 } // namespace graphics