]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLImage.C
some tabular fixes for the problems reported by Helge
[lyx.git] / src / frontends / qt2 / 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.original_)
116 {}
117
118
119 QLImage::~QLImage()
120 {
121 }
122
123
124 Image * QLImage::clone_impl() const
125 {
126         return new QLImage(*this);
127 }
128
129
130 unsigned int QLImage::getWidth_impl() const
131 {
132         return transformed_.width();
133 }
134
135
136 unsigned int QLImage::getHeight_impl() const
137 {
138         return transformed_.height();
139 }
140
141
142 void QLImage::load_impl(string const & filename)
143 {
144         if (!original_.isNull()) {
145                 lyxerr[Debug::GRAPHICS]
146                         << "Image is loaded already!" << endl;
147                 finishedLoading(false);
148                 return;
149         }
150
151         if (!original_.load(toqstr(filename))) {
152                 lyxerr[Debug::GRAPHICS]
153                         << "Unable to open image" << endl;
154                 finishedLoading(false);
155                 return;
156         }
157         transformed_ = original_;
158         finishedLoading(true);
159 }
160
161
162 namespace {
163
164 // This code is taken from KImageEffect::toGray
165 QImage & toGray(QImage & img)
166 {
167         if (img.width() == 0 || img.height() == 0)
168                 return img;
169
170         int const pixels = img.depth() > 8 ?
171                 img.width() * img.height() : img.numColors();
172
173         unsigned int * const data = img.depth() > 8 ?
174                 (unsigned int *)img.bits() :
175                 (unsigned int *)img.colorTable();
176
177         for(int i = 0; i < pixels; ++i){
178                 int const val = qGray(data[i]);
179                 data[i] = qRgba(val, val, val, qAlpha(data[i]));
180         }
181         return img;
182 }
183
184 } // namespace anon
185
186
187 bool QLImage::setPixmap_impl(Params const & params)
188 {
189         if (original_.isNull() || params.display == NoDisplay)
190                 return false;
191
192         switch (params.display) {
193         case GrayscaleDisplay: {
194                 toGray(transformed_);
195                 break;
196         }
197
198         case MonochromeDisplay: {
199                 transformed_.convertDepth(transformed_.depth(), Qt::MonoOnly);
200                 break;
201         }
202
203         default:
204                 break;
205         }
206
207         return true;
208 }
209
210
211 void QLImage::clip_impl(Params const & params)
212 {
213         if (transformed_.isNull())
214                 return;
215
216         if (params.bb.empty())
217                 // No clipping is necessary.
218                 return;
219
220         int const new_width  = params.bb.xr - params.bb.xl;
221         int const new_height = params.bb.yt - params.bb.yb;
222
223         // No need to check if the width, height are > 0 because the
224         // Bounding Box would be empty() in this case.
225         if (new_width > original_.width() || new_height > original_.height()) {
226                 // Bounds are invalid.
227                 return;
228         }
229
230         if (new_width == original_.width() && new_height == original_.height())
231                 return;
232
233         int const xoffset_l = params.bb.xl;
234         int const yoffset_t = (original_.height() > int(params.bb.yt) ?
235                                original_.height() - params.bb.yt : 0);
236
237         transformed_ = original_.copy(xoffset_l, yoffset_t,
238                                       new_width, new_height);
239 }
240
241
242 void QLImage::rotate_impl(Params const & params)
243 {
244         if (transformed_.isNull())
245                 return;
246
247         if (!params.angle)
248                 return;
249
250         QWMatrix m;
251         m.rotate(-params.angle);
252
253         transformed_.setAlphaBuffer(true);
254         transformed_ = transformed_.xForm(m);
255 }
256
257
258 void QLImage::scale_impl(Params const & params)
259 {
260         if (transformed_.isNull())
261                 return;
262
263         unsigned int width;
264         unsigned int height;
265         boost::tie(width, height) = getScaledDimensions(params);
266
267         if (width == getWidth() && height == getHeight())
268                 return;
269
270         QWMatrix m;
271         m.scale(double(width) / getWidth(), double(height) / getHeight());
272         transformed_ = transformed_.xForm(m);
273 }
274
275 } // namespace graphics
276 } // lyx