]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImage.cpp
proper fix for bug 4936.
[lyx.git] / src / frontends / qt4 / GuiImage.cpp
1 /**
2  * \file GuiImage.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiImage.h"
15 #include "qt_helpers.h"
16
17 #include "Format.h"
18
19 #include "graphics/GraphicsParams.h"
20
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/lstrings.h"       // ascii_lowercase
24
25 #include <QPainter>
26 #include <QImage>
27 #include <QImageReader>
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33 namespace graphics {
34
35 /// Access to this class is through this static method.
36 Image * GuiImage::newImage()
37 {
38         return new GuiImage;
39 }
40
41
42 GuiImage::GuiImage(GuiImage const & other)
43         : Image(other), original_(other.original_),
44           transformed_(other.transformed_), is_transformed_(other.is_transformed_)
45 {}
46
47
48 Image * GuiImage::clone() const
49 {
50         return new GuiImage(*this);
51 }
52
53
54 unsigned int GuiImage::width() const
55 {
56         return is_transformed_ ? transformed_.width() : original_.width();
57 }
58
59
60 unsigned int GuiImage::height() const
61 {
62         return is_transformed_ ? transformed_.height() : original_.height();
63 }
64
65
66 bool GuiImage::load(FileName const & filename)
67 {
68         if (!original_.isNull()) {
69                 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
70                 return false;
71         }
72
73         if (!original_.load(toqstr(filename.absFilename()))) {
74                 LYXERR(Debug::GRAPHICS, "Unable to open image");
75                 return false;
76         }
77         return true;
78 }
79
80
81 // This code is taken from KImageEffect::toGray
82 static QPixmap toGray(QPixmap const & pix)
83 {
84         if (pix.width() == 0 || pix.height() == 0)
85                 return pix;
86
87         QImage img = pix.toImage();
88         int const pixels = img.depth() > 8 ?
89                 img.width() * img.height() : img.numColors();
90
91         unsigned int *data = img.depth() > 8 ?
92                 reinterpret_cast<unsigned int *>(img.bits()) :
93                 reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
94
95         for(int i = 0; i < pixels; ++i){
96                 int const val = qGray(data[i]);
97                 data[i] = qRgba(val, val, val, qAlpha(data[i]));
98         }
99         return QPixmap::fromImage(img);
100 }
101
102
103 bool GuiImage::setPixmap(Params const & params)
104 {
105         if (original_.isNull() || params.display == NoDisplay)
106                 return false;
107
108         is_transformed_ = clip(params);
109         is_transformed_ |= rotate(params);
110         is_transformed_ |= scale(params);
111
112         switch (params.display) {
113         case GrayscaleDisplay: {
114                 transformed_ = is_transformed_
115                         ? toGray(transformed_) :  toGray(original_);
116                 is_transformed_ = true;
117                 break;
118         }
119
120         case MonochromeDisplay: {
121                 QImage img = is_transformed_
122                         ? transformed_.toImage() : original_.toImage();
123                 img.convertToFormat(img.format(), Qt::MonoOnly);
124                 transformed_ = QPixmap::fromImage(img);
125                 is_transformed_ = true;
126                 break;
127         }
128
129         default:
130                 break;
131         }
132
133         if (!is_transformed_)
134                 // Clear it out to save some memory.
135                 transformed_ = QPixmap();
136
137         return true;
138 }
139
140
141 bool GuiImage::clip(Params const & params)
142 {
143         if (params.bb.empty())
144                 // No clipping is necessary.
145                 return false;
146
147         int const new_width  = params.bb.xr - params.bb.xl;
148         int const new_height = params.bb.yt - params.bb.yb;
149
150         // No need to check if the width, height are > 0 because the
151         // Bounding Box would be empty() in this case.
152         if (new_width > original_.width() || new_height > original_.height()) {
153                 // Bounds are invalid.
154                 return false;
155         }
156
157         if (new_width == original_.width() && new_height == original_.height())
158                 return false;
159
160         int const xoffset_l = params.bb.xl;
161         int const yoffset_t = (original_.height() > int(params.bb.yt) ?
162                                original_.height() - params.bb.yt : 0);
163
164         transformed_ = original_.copy(xoffset_l, yoffset_t,
165                                       new_width, new_height);
166         return true;
167 }
168
169
170 bool GuiImage::rotate(Params const & params)
171 {
172         if (!params.angle)
173                 return false;
174
175         if (!is_transformed_)
176                 transformed_ = original_;
177
178         QMatrix m;
179         m.rotate(-params.angle);
180         transformed_ = transformed_.transformed(m);
181         return true;
182 }
183
184
185 bool GuiImage::scale(Params const & params)
186 {
187         Dimension dim = scaledDimension(params);
188
189         if (dim.width() == width() && dim.height() == height())
190                 return false;
191
192         if (!is_transformed_)
193                 transformed_ = original_;
194
195         QMatrix m;
196         m.scale(double(dim.width()) / width(), double(dim.height()) / height());
197         transformed_ = transformed_.transformed(m);
198
199         return true;
200 }
201
202 } // namespace graphics
203 } // lyx