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
26 #include <QImageReader>
29 using namespace lyx::support;
34 /// Access to this class is through this static method.
35 Image * GuiImage::newImage()
41 GuiImage::GuiImage() : is_transformed_(false)
45 GuiImage::GuiImage(GuiImage const & other)
46 : Image(other), original_(other.original_),
47 transformed_(other.transformed_), is_transformed_(other.is_transformed_),
52 Image * GuiImage::clone() const
54 return new GuiImage(*this);
58 QImage const & GuiImage::image() const
60 return is_transformed_ ? transformed_ : original_;
64 unsigned int GuiImage::width() const
66 return is_transformed_ ? transformed_.width() : original_.width();
70 unsigned int GuiImage::height() const
72 return is_transformed_ ? transformed_.height() : original_.height();
76 bool GuiImage::load(FileName const & filename)
78 if (!original_.isNull()) {
79 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
82 fname_ = toqstr(filename.absFilename());
89 if (!original_.load(fname_)) {
90 LYXERR(Debug::GRAPHICS, "Unable to open image");
98 bool GuiImage::setPixmap(Params const & params)
103 if (original_.isNull()) {
108 is_transformed_ = clip(params);
109 is_transformed_ |= rotate(params);
110 is_transformed_ |= scale(params);
112 // Clear the pixmap to save some memory.
113 if (is_transformed_) {
115 original_ = QImage();
117 transformed_.detach();
118 transformed_ = QImage();
125 bool GuiImage::clip(Params const & params)
127 if (params.bb.empty())
128 // No clipping is necessary.
131 int const new_width = params.bb.xr - params.bb.xl;
132 int const new_height = params.bb.yt - params.bb.yb;
134 QImage const & image = is_transformed_ ? transformed_ : original_;
136 // No need to check if the width, height are > 0 because the
137 // Bounding Box would be empty() in this case.
138 if (new_width > image.width() || new_height > image.height()) {
139 // Bounds are invalid.
143 if (new_width == image.width() && new_height == image.height())
146 int const xoffset_l = params.bb.xl;
147 int const yoffset_t = (image.height() > int(params.bb.yt))
148 ? image.height() - params.bb.yt : 0;
150 transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
155 bool GuiImage::rotate(Params const & params)
160 QImage const & image = is_transformed_ ? transformed_ : original_;
162 m.rotate(- params.angle);
163 transformed_ = image.transformed(m);
168 bool GuiImage::scale(Params const & params)
170 QImage const & image = is_transformed_ ? transformed_ : original_;
172 unsigned int w = image.width();
173 unsigned int h = image.height();
175 // scale only when value > 0
176 if (params.scale > 0) {
177 w = (w * params.scale) / 100;
178 h = (h * params.scale) / 100;
181 LYXERR(Debug::GRAPHICS, "\n\tparams.scale : " << params.scale
182 << "\n\twidth : " << w
183 << "\n\theight : " << h);
185 if (w == image.width() && h == image.height())
189 m.scale(double(w) / image.width(), double(h) / image.height());
190 transformed_ = image.transformed(m);
194 } // namespace graphics