3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
15 #include "qt_helpers.h"
20 #include "graphics/GraphicsParams.h"
22 #include "support/FileName.h"
23 #include "support/lstrings.h" // ascii_lowercase
27 #include <QImageReader>
29 #include <boost/bind.hpp>
30 #include <boost/tuple/tuple.hpp>
32 using lyx::support::ascii_lowercase;
44 /// Access to this class is through this static method.
45 Image::ImagePtr GuiImage::newImage()
48 ptr.reset(new GuiImage);
53 /// Return the list of loadable formats.
54 Image::FormatList GuiImage::loadableFormats()
56 static FormatList fmts;
61 // The formats recognised by LyX
62 Formats::const_iterator begin = formats.begin();
63 Formats::const_iterator end = formats.end();
66 // LYXERR(Debug::GRAPHICS)
67 // << "D:/msys/home/yns/src/lyx-devel/lib/images/banner.png mis of format: "
68 // << fromqstr(Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.png"))
70 // if (Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.png"))
71 // LYXERR(Debug::GRAPHICS)
72 // << "pictureFormat not returned NULL\n" << endl;
73 // << "Supported formats are: " << Pic.inputFormats() << endl;
75 QList<QByteArray> qt_formats = QImageReader::supportedImageFormats ();
77 LYXERR(Debug::GRAPHICS)
78 << "\nThe image loader can load the following directly:\n";
80 if (qt_formats.empty()) {
81 LYXERR(Debug::GRAPHICS)
82 << "\nQt4 Problem: No Format available!" << endl;
85 for (QList<QByteArray>::const_iterator it =qt_formats.begin(); it != qt_formats.end(); ++it) {
87 LYXERR(Debug::GRAPHICS) << (const char *) *it << ", ";
89 string ext = ascii_lowercase((const char *) *it);
95 Formats::const_iterator fit =
97 bind(equal_to<string>(),
98 bind(&Format::extension, _1),
101 fmts.push_back(fit->name());
104 if (lyxerr.debugging()) {
105 LYXERR(Debug::GRAPHICS)
106 << "\nOf these, LyX recognises the following formats:\n";
108 FormatList::const_iterator fbegin = fmts.begin();
109 FormatList::const_iterator fend = fmts.end();
110 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
112 LYXERR(Debug::GRAPHICS) << ", ";
114 LYXERR(Debug::GRAPHICS) << *fit;
116 LYXERR(Debug::GRAPHICS) << '\n' << endl;
123 GuiImage::GuiImage(GuiImage const & other)
124 : Image(other), original_(other.original_),
125 transformed_(other.transformed_),
126 transformed_pixmap_(other.transformed_pixmap_)
130 Image * GuiImage::clone_impl() const
132 return new GuiImage(*this);
136 unsigned int GuiImage::getWidth_impl() const
138 return transformed_.width();
142 unsigned int GuiImage::getHeight_impl() const
144 return transformed_.height();
148 void GuiImage::load_impl(support::FileName const & filename)
150 if (!original_.isNull()) {
151 LYXERR(Debug::GRAPHICS)
152 << "Image is loaded already!" << endl;
153 finishedLoading(false);
157 if (!original_.load(toqstr(filename.absFilename()))) {
158 LYXERR(Debug::GRAPHICS)
159 << "Unable to open image" << endl;
160 finishedLoading(false);
163 transformed_ = original_;
164 finishedLoading(true);
168 // This code is taken from KImageEffect::toGray
169 static QImage & toGray(QImage & img)
171 if (img.width() == 0 || img.height() == 0)
174 int const pixels = img.depth() > 8 ?
175 img.width() * img.height() : img.numColors();
177 unsigned int *data = img.depth() > 8 ?
178 reinterpret_cast<unsigned int *>(img.bits()) :
179 reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
181 for(int i = 0; i < pixels; ++i){
182 int const val = qGray(data[i]);
183 data[i] = qRgba(val, val, val, qAlpha(data[i]));
189 bool GuiImage::setPixmap_impl(Params const & params)
191 if (original_.isNull() || params.display == NoDisplay)
194 switch (params.display) {
195 case GrayscaleDisplay: {
196 toGray(transformed_);
200 case MonochromeDisplay: {
201 transformed_.convertToFormat(transformed_.format(), Qt::MonoOnly);
209 transformed_pixmap_ = QPixmap::fromImage(transformed_);
214 void GuiImage::clip_impl(Params const & params)
216 if (transformed_.isNull())
219 if (params.bb.empty())
220 // No clipping is necessary.
223 int const new_width = params.bb.xr - params.bb.xl;
224 int const new_height = params.bb.yt - params.bb.yb;
226 // No need to check if the width, height are > 0 because the
227 // Bounding Box would be empty() in this case.
228 if (new_width > original_.width() || new_height > original_.height()) {
229 // Bounds are invalid.
233 if (new_width == original_.width() && new_height == original_.height())
236 int const xoffset_l = params.bb.xl;
237 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
238 original_.height() - params.bb.yt : 0);
240 transformed_ = original_.copy(xoffset_l, yoffset_t,
241 new_width, new_height);
245 void GuiImage::rotate_impl(Params const & params)
247 if (transformed_.isNull())
254 m.rotate(-params.angle);
256 transformed_ = transformed_.transformed(m);
260 void GuiImage::scale_impl(Params const & params)
262 if (transformed_.isNull())
267 boost::tie(width, height) = getScaledDimensions(params);
269 if (width == getWidth() && height == getHeight())
273 m.scale(double(width) / getWidth(), double(height) / getHeight());
274 transformed_ = transformed_.transformed(m);
277 } // namespace graphics