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 #if QT_VERSION >= 0x050000
68 return static_cast<unsigned int>(ceil(is_transformed_ ?
69 (transformed_.width() / transformed_.devicePixelRatio()) :
70 (original_.width() / original_.devicePixelRatio())));
72 return is_transformed_ ? transformed_.width() : original_.width();
77 unsigned int GuiImage::height() const
79 #if QT_VERSION >= 0x050000
80 return static_cast<unsigned int>(ceil(is_transformed_ ?
81 (transformed_.height() / transformed_.devicePixelRatio()) :
82 (original_.height() / original_.devicePixelRatio())));
84 return is_transformed_ ? transformed_.height() : original_.height();
89 bool GuiImage::load(FileName const & filename)
91 if (!original_.isNull()) {
92 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
95 fname_ = toqstr(filename.absFileName());
100 bool GuiImage::load()
102 if (!original_.load(fname_)) {
103 LYXERR(Debug::GRAPHICS, "Unable to open image");
110 bool GuiImage::setPixmap(Params const & params)
115 if (original_.isNull()) {
120 #if QT_VERSION >= 0x050000
121 original_.setDevicePixelRatio(params.pixel_ratio);
124 is_transformed_ = clip(params);
125 is_transformed_ |= rotate(params);
126 is_transformed_ |= scale(params);
128 // Clear the pixmap to save some memory.
130 original_ = QImage();
132 transformed_ = QImage();
138 bool GuiImage::clip(Params const & params)
140 if (params.bb.empty())
141 // No clipping is necessary.
144 #if QT_VERSION >= 0x050000
145 double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
146 int const new_width = static_cast<int>((params.bb.xr.inBP() - params.bb.xl.inBP()) * pixelRatio);
147 int const new_height = static_cast<int>((params.bb.yt.inBP() - params.bb.yb.inBP()) * pixelRatio);
149 int const new_width = static_cast<int>((params.bb.xr.inBP() - params.bb.xl.inBP()));
150 int const new_height = static_cast<int>((params.bb.yt.inBP() - params.bb.yb.inBP()));
153 QImage const & image = is_transformed_ ? transformed_ : original_;
155 // No need to check if the width, height are > 0 because the
156 // Bounding Box would be empty() in this case.
157 if (new_width > image.width() || new_height > image.height()) {
158 // Bounds are invalid.
162 if (new_width == image.width() && new_height == image.height())
165 int const xoffset_l = params.bb.xl.inBP();
166 int const yoffset_t = (image.height() > params.bb.yt.inBP())
167 ? image.height() - params.bb.yt.inBP() : 0;
169 transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
174 bool GuiImage::rotate(Params const & params)
179 QImage const & image = is_transformed_ ? transformed_ : original_;
181 m.rotate(- params.angle);
182 transformed_ = image.transformed(m);
187 bool GuiImage::scale(Params const & params)
189 QImage const & image = is_transformed_ ? transformed_ : original_;
191 if (params.scale == 100)
194 #if QT_VERSION >= 0x050000
195 double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
196 qreal scale = qreal(params.scale) / 100.0 * pixelRatio;
198 qreal scale = qreal(params.scale) / 100.0;
201 #if (QT_VERSION >= 0x040500) && (QT_VERSION <= 0x040502)
202 // Due to a bug in Qt, LyX will crash for certain
203 // scaling factors and sizes of the image.
204 // see bug #5957: http://www.lyx.org/trac/ticket/5957
209 m.scale(scale, scale);
210 // Bilinear filtering is used to scale graphics preview
211 transformed_ = image.transformed(m, Qt::SmoothTransformation);
215 } // namespace graphics