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 void GuiImage::load(FileName const & filename)
69 if (!original_.isNull()) {
70 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
71 finishedLoading(false);
75 if (!original_.load(toqstr(filename.absFilename()))) {
76 LYXERR(Debug::GRAPHICS, "Unable to open image");
77 finishedLoading(false);
80 transformed_ = original_;
81 finishedLoading(true);
85 // This code is taken from KImageEffect::toGray
86 static QImage & toGray(QImage & img)
88 if (img.width() == 0 || img.height() == 0)
91 int const pixels = img.depth() > 8 ?
92 img.width() * img.height() : img.numColors();
94 unsigned int *data = img.depth() > 8 ?
95 reinterpret_cast<unsigned int *>(img.bits()) :
96 reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
98 for(int i = 0; i < pixels; ++i){
99 int const val = qGray(data[i]);
100 data[i] = qRgba(val, val, val, qAlpha(data[i]));
106 bool GuiImage::setPixmap(Params const & params)
108 if (original_.isNull() || params.display == NoDisplay)
111 switch (params.display) {
112 case GrayscaleDisplay: {
113 toGray(transformed_);
117 case MonochromeDisplay: {
118 transformed_.convertToFormat(transformed_.format(), Qt::MonoOnly);
126 transformed_pixmap_ = QPixmap::fromImage(transformed_);
131 void GuiImage::clip(Params const & params)
133 if (transformed_.isNull())
136 if (params.bb.empty())
137 // No clipping is necessary.
140 int const new_width = params.bb.xr - params.bb.xl;
141 int const new_height = params.bb.yt - params.bb.yb;
143 // No need to check if the width, height are > 0 because the
144 // Bounding Box would be empty() in this case.
145 if (new_width > original_.width() || new_height > original_.height()) {
146 // Bounds are invalid.
150 if (new_width == original_.width() && new_height == original_.height())
153 int const xoffset_l = params.bb.xl;
154 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
155 original_.height() - params.bb.yt : 0);
157 transformed_ = original_.copy(xoffset_l, yoffset_t,
158 new_width, new_height);
162 void GuiImage::rotate(Params const & params)
164 if (transformed_.isNull())
171 m.rotate(-params.angle);
173 transformed_ = transformed_.transformed(m);
177 void GuiImage::scale(Params const & params)
179 if (transformed_.isNull())
182 Dimension dim = scaledDimension(params);
184 if (dim.width() == width() && dim.height() == height())
188 m.scale(double(dim.width()) / width(), double(dim.height()) / height());
189 transformed_ = transformed_.transformed(m);
192 } // namespace graphics