]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QLImage.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / QLImage.C
1 /**
2  * \file QLImage.C
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 "QLImage.h"
15 #include "qt_helpers.h"
16
17 #include "debug.h"
18 #include "format.h"
19
20 #include "graphics/GraphicsParams.h"
21
22 #include "support/lstrings.h"       // lowercase
23
24 #include <qimage.h>
25 #include <qpainter.h>
26
27 #include <boost/bind.hpp>
28 #include <boost/tuple/tuple.hpp>
29
30 using lyx::support::lowercase;
31
32 using boost::bind;
33
34 using std::endl;
35 using std::equal_to;
36 using std::find_if;
37 using std::string;
38
39
40 namespace lyx {
41 namespace graphics {
42
43 /// Access to this class is through this static method.
44 Image::ImagePtr QLImage::newImage()
45 {
46         ImagePtr ptr;
47         ptr.reset(new QLImage);
48         return ptr;
49 }
50
51
52 /// Return the list of loadable formats.
53 Image::FormatList QLImage::loadableFormats()
54 {
55         static FormatList fmts;
56
57         if (!fmts.empty())
58                 return fmts;
59
60         // The formats recognised by LyX
61         Formats::const_iterator begin = formats.begin();
62         Formats::const_iterator end   = formats.end();
63
64         lyxerr[Debug::GRAPHICS]
65                 << "\nThe image loader can load the following directly:\n";
66
67         QStrList qt_formats = QImageIO::inputFormats();
68
69         QStrListIterator it(qt_formats);
70
71         for (; it.current(); ++it) {
72                 lyxerr[Debug::GRAPHICS] << it.current() << endl;
73
74                 string ext = lowercase(it.current());
75
76                 // special case
77                 if (ext == "jpeg")
78                         ext = "jpg";
79
80                 Formats::const_iterator fit =
81                         find_if(begin, end,
82                                 bind(equal_to<string>(),
83                                      bind(&Format::extension, _1),
84                                      ext));
85                 if (fit != end)
86                         fmts.push_back(fit->name());
87         }
88
89         if (lyxerr.debugging()) {
90                 lyxerr[Debug::GRAPHICS]
91                         << "\nOf these, LyX recognises the following formats:\n";
92
93                 FormatList::const_iterator fbegin = fmts.begin();
94                 FormatList::const_iterator fend   = fmts.end();
95                 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
96                         if (fit != fbegin)
97                                 lyxerr[Debug::GRAPHICS] << ", ";
98                         lyxerr[Debug::GRAPHICS] << *fit;
99                 }
100                 lyxerr[Debug::GRAPHICS] << '\n' << endl;
101         }
102
103         return fmts;
104 }
105
106
107 QLImage::QLImage()
108         : Image()
109 {
110 }
111
112
113 QLImage::QLImage(QLImage const & other)
114         : Image(other), original_(other.original_),
115           transformed_(other.transformed_),
116           transformed_pixmap_(other.transformed_pixmap_)
117 {}
118
119
120 Image * QLImage::clone_impl() const
121 {
122         return new QLImage(*this);
123 }
124
125
126 unsigned int QLImage::getWidth_impl() const
127 {
128         return transformed_.width();
129 }
130
131
132 unsigned int QLImage::getHeight_impl() const
133 {
134         return transformed_.height();
135 }
136
137
138 void QLImage::load_impl(string const & filename)
139 {
140         if (!original_.isNull()) {
141                 lyxerr[Debug::GRAPHICS]
142                         << "Image is loaded already!" << endl;
143                 finishedLoading(false);
144                 return;
145         }
146
147         if (!original_.load(toqstr(filename))) {
148                 lyxerr[Debug::GRAPHICS]
149                         << "Unable to open image" << endl;
150                 finishedLoading(false);
151                 return;
152         }
153         transformed_ = original_;
154         finishedLoading(true);
155 }
156
157
158 namespace {
159
160 // This code is taken from KImageEffect::toGray
161 QImage & toGray(QImage & img)
162 {
163         if (img.width() == 0 || img.height() == 0)
164                 return img;
165
166         int const pixels = img.depth() > 8 ?
167                 img.width() * img.height() : img.numColors();
168
169         unsigned int * const data = img.depth() > 8 ?
170                 (unsigned int *)img.bits() :
171                 (unsigned int *)img.colorTable();
172
173         for(int i = 0; i < pixels; ++i){
174                 int const val = qGray(data[i]);
175                 data[i] = qRgba(val, val, val, qAlpha(data[i]));
176         }
177         return img;
178 }
179
180 } // namespace anon
181
182
183 bool QLImage::setPixmap_impl(Params const & params)
184 {
185         if (original_.isNull() || params.display == NoDisplay)
186                 return false;
187
188         switch (params.display) {
189         case GrayscaleDisplay: {
190                 toGray(transformed_);
191                 break;
192         }
193
194         case MonochromeDisplay: {
195                 transformed_.convertDepth(transformed_.depth(), Qt::MonoOnly);
196                 break;
197         }
198
199         default:
200                 break;
201         }
202
203         transformed_pixmap_ = transformed_;
204         return true;
205 }
206
207
208 void QLImage::clip_impl(Params const & params)
209 {
210         if (transformed_.isNull())
211                 return;
212
213         if (params.bb.empty())
214                 // No clipping is necessary.
215                 return;
216
217         int const new_width  = params.bb.xr - params.bb.xl;
218         int const new_height = params.bb.yt - params.bb.yb;
219
220         // No need to check if the width, height are > 0 because the
221         // Bounding Box would be empty() in this case.
222         if (new_width > original_.width() || new_height > original_.height()) {
223                 // Bounds are invalid.
224                 return;
225         }
226
227         if (new_width == original_.width() && new_height == original_.height())
228                 return;
229
230         int const xoffset_l = params.bb.xl;
231         int const yoffset_t = (original_.height() > int(params.bb.yt) ?
232                                original_.height() - params.bb.yt : 0);
233
234         transformed_ = original_.copy(xoffset_l, yoffset_t,
235                                       new_width, new_height);
236 }
237
238
239 void QLImage::rotate_impl(Params const & params)
240 {
241         if (transformed_.isNull())
242                 return;
243
244         if (!params.angle)
245                 return;
246
247         QWMatrix m;
248         m.rotate(-params.angle);
249
250         transformed_.setAlphaBuffer(true);
251         transformed_ = transformed_.xForm(m);
252 }
253
254
255 void QLImage::scale_impl(Params const & params)
256 {
257         if (transformed_.isNull())
258                 return;
259
260         unsigned int width;
261         unsigned int height;
262         boost::tie(width, height) = getScaledDimensions(params);
263
264         if (width == getWidth() && height == getHeight())
265                 return;
266
267         QWMatrix m;
268         m.scale(double(width) / getWidth(), double(height) / getHeight());
269         transformed_ = transformed_.xForm(m);
270 }
271
272 } // namespace graphics
273 } // lyx