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.
13 #include <math.h> /* ceil */
16 #include "qt_helpers.h"
20 #include "graphics/GraphicsParams.h"
22 #include "support/debug.h"
23 #include "support/FileName.h"
24 #include "support/lstrings.h" // ascii_lowercase
27 #include <QImageReader>
30 using namespace lyx::support;
35 /// Implement factory method defined in GraphicsImage.h
42 GuiImage::GuiImage() : is_transformed_(false)
46 GuiImage::GuiImage(GuiImage const & other)
47 : Image(other), original_(other.original_),
48 transformed_(other.transformed_), is_transformed_(other.is_transformed_),
53 Image * GuiImage::clone() const
55 return new GuiImage(*this);
59 QImage const & GuiImage::image() const
61 return is_transformed_ ? transformed_ : original_;
65 unsigned int GuiImage::width() const
67 return static_cast<unsigned int>(ceil(is_transformed_ ?
68 (transformed_.width() / transformed_.devicePixelRatio()) :
69 (original_.width() / original_.devicePixelRatio())));
73 unsigned int GuiImage::height() const
75 return static_cast<unsigned int>(ceil(is_transformed_ ?
76 (transformed_.height() / transformed_.devicePixelRatio()) :
77 (original_.height() / original_.devicePixelRatio())));
81 bool GuiImage::load(FileName const & filename)
83 if (!original_.isNull()) {
84 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
87 fname_ = toqstr(filename.absFileName());
94 if (!original_.load(fname_)) {
95 LYXERR(Debug::GRAPHICS, "Unable to open image");
102 bool GuiImage::setPixmap(Params const & params)
107 if (original_.isNull()) {
112 original_.setDevicePixelRatio(params.pixel_ratio);
114 is_transformed_ = clip(params);
115 is_transformed_ |= rotate(params);
116 is_transformed_ |= scale(params);
118 // Clear the pixmap to save some memory.
120 original_ = QImage();
122 transformed_ = QImage();
128 bool GuiImage::clip(Params const & params)
130 if (params.bb.empty())
131 // No clipping is necessary.
134 double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
135 int const new_width = static_cast<int>((params.bb.xr.inBP() - params.bb.xl.inBP()) * pixelRatio);
136 int const new_height = static_cast<int>((params.bb.yt.inBP() - params.bb.yb.inBP()) * pixelRatio);
138 QImage const & image = is_transformed_ ? transformed_ : original_;
140 // No need to check if the width, height are > 0 because the
141 // Bounding Box would be empty() in this case.
142 if (new_width > image.width() || new_height > image.height()) {
143 // Bounds are invalid.
147 if (new_width == image.width() && new_height == image.height())
150 int const xoffset_l = params.bb.xl.inBP();
151 int const yoffset_t = (image.height() > params.bb.yt.inBP())
152 ? image.height() - params.bb.yt.inBP() : 0;
154 transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
159 bool GuiImage::rotate(Params const & params)
161 if (params.angle == 0)
164 QImage const & image = is_transformed_ ? transformed_ : original_;
166 m.rotate(- params.angle);
167 transformed_ = image.transformed(m);
172 bool GuiImage::scale(Params const & params)
174 QImage const & image = is_transformed_ ? transformed_ : original_;
176 if (params.scale == 100)
179 double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
180 qreal const scale = qreal(params.scale) / 100.0 * pixelRatio;
183 m.scale(scale, scale);
184 // Bilinear filtering is used to scale graphics preview
185 transformed_ = image.transformed(m, Qt::SmoothTransformation);
189 } // namespace graphics