]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImage.cpp
4e747808b2e8fdfc19fdfed0f27239202426f3bd
[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_),
45           transformed_pixmap_(other.transformed_pixmap_)
46 {}
47
48
49 Image * GuiImage::clone() const
50 {
51         return new GuiImage(*this);
52 }
53
54
55 unsigned int GuiImage::width() const
56 {
57         return transformed_.width();
58 }
59
60
61 unsigned int GuiImage::height() const
62 {
63         return transformed_.height();
64 }
65
66
67 void GuiImage::load(FileName const & filename)
68 {
69         if (!original_.isNull()) {
70                 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
71                 finishedLoading(false);
72                 return;
73         }
74
75         if (!original_.load(toqstr(filename.absFilename()))) {
76                 LYXERR(Debug::GRAPHICS, "Unable to open image");
77                 finishedLoading(false);
78                 return;
79         }
80         transformed_ = original_;
81         finishedLoading(true);
82 }
83
84
85 // This code is taken from KImageEffect::toGray
86 static QImage & toGray(QImage & img)
87 {
88         if (img.width() == 0 || img.height() == 0)
89                 return img;
90
91         int const pixels = img.depth() > 8 ?
92                 img.width() * img.height() : img.numColors();
93
94         unsigned int *data = img.depth() > 8 ?
95                 reinterpret_cast<unsigned int *>(img.bits()) :
96                 reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
97
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]));
101         }
102         return img;
103 }
104
105
106 bool GuiImage::setPixmap(Params const & params)
107 {
108         if (original_.isNull() || params.display == NoDisplay)
109                 return false;
110
111         switch (params.display) {
112         case GrayscaleDisplay: {
113                 toGray(transformed_);
114                 break;
115         }
116
117         case MonochromeDisplay: {
118                 transformed_.convertToFormat(transformed_.format(), Qt::MonoOnly);
119                 break;
120         }
121
122         default:
123                 break;
124         }
125
126         transformed_pixmap_ = QPixmap::fromImage(transformed_);
127         return true;
128 }
129
130
131 void GuiImage::clip(Params const & params)
132 {
133         if (transformed_.isNull())
134                 return;
135
136         if (params.bb.empty())
137                 // No clipping is necessary.
138                 return;
139
140         int const new_width  = params.bb.xr - params.bb.xl;
141         int const new_height = params.bb.yt - params.bb.yb;
142
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.
147                 return;
148         }
149
150         if (new_width == original_.width() && new_height == original_.height())
151                 return;
152
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);
156
157         transformed_ = original_.copy(xoffset_l, yoffset_t,
158                                       new_width, new_height);
159 }
160
161
162 void GuiImage::rotate(Params const & params)
163 {
164         if (transformed_.isNull())
165                 return;
166
167         if (!params.angle)
168                 return;
169
170         QMatrix m;
171         m.rotate(-params.angle);
172
173         transformed_ = transformed_.transformed(m);
174 }
175
176
177 void GuiImage::scale(Params const & params)
178 {
179         if (transformed_.isNull())
180                 return;
181
182         Dimension dim = scaledDimension(params);
183
184         if (dim.width() == width() && dim.height() == height())
185                 return;
186
187         QMatrix m;
188         m.scale(double(dim.width()) / width(), double(dim.height()) / height());
189         transformed_ = transformed_.transformed(m);
190 }
191
192 } // namespace graphics
193 } // lyx