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