2002-09-24 13:57:09 +00:00
|
|
|
/**
|
2002-07-12 01:48:53 +00:00
|
|
|
* \file QLImage.C
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-07-12 01:48:53 +00:00
|
|
|
*
|
2002-09-24 13:57:09 +00:00
|
|
|
* \author Angus Leeming
|
2002-10-20 01:48:28 +00:00
|
|
|
* \author John Levon
|
2002-09-24 13:57:09 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-07-12 01:48:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2003-09-29 10:50:51 +00:00
|
|
|
#include "debug.h"
|
2002-07-12 01:48:53 +00:00
|
|
|
#include "QLImage.h"
|
|
|
|
#include "graphics/GraphicsParams.h"
|
2003-02-28 09:49:49 +00:00
|
|
|
#include "format.h"
|
2003-04-15 12:13:40 +00:00
|
|
|
#include "support/lstrings.h" // lowercase
|
2002-07-12 01:48:53 +00:00
|
|
|
#include "support/lyxfunctional.h" // compare_memfun
|
2002-12-17 20:37:13 +00:00
|
|
|
#include "qt_helpers.h"
|
2002-07-12 01:48:53 +00:00
|
|
|
|
|
|
|
#include <qimage.h>
|
|
|
|
#include <qpainter.h>
|
|
|
|
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
using lyx::support::lowercase;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2002-07-17 02:12:12 +00:00
|
|
|
using std::endl;
|
2003-09-08 00:33:41 +00:00
|
|
|
using std::find_if;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
2002-07-12 01:48:53 +00:00
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace graphics {
|
2002-07-12 01:48:53 +00:00
|
|
|
|
|
|
|
/// Access to this class is through this static method.
|
|
|
|
Image::ImagePtr QLImage::newImage()
|
|
|
|
{
|
|
|
|
ImagePtr ptr;
|
|
|
|
ptr.reset(new QLImage);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Return the list of loadable formats.
|
|
|
|
Image::FormatList QLImage::loadableFormats()
|
|
|
|
{
|
|
|
|
static FormatList fmts;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-07-12 01:48:53 +00:00
|
|
|
if (!fmts.empty())
|
|
|
|
return fmts;
|
|
|
|
|
|
|
|
// The formats recognised by LyX
|
|
|
|
Formats::const_iterator begin = formats.begin();
|
|
|
|
Formats::const_iterator end = formats.end();
|
|
|
|
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "\nThe image loader can load the following directly:\n";
|
|
|
|
|
|
|
|
QStrList qt_formats = QImageIO::inputFormats();
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-07-12 01:48:53 +00:00
|
|
|
QStrListIterator it(qt_formats);
|
|
|
|
|
|
|
|
for (; it.current(); ++it) {
|
2003-03-27 04:29:14 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << it.current() << endl;
|
2002-07-12 01:48:53 +00:00
|
|
|
|
|
|
|
string ext = lowercase(it.current());
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-07-12 01:48:53 +00:00
|
|
|
// special case
|
|
|
|
if (ext == "jpeg")
|
|
|
|
ext = "jpg";
|
|
|
|
|
|
|
|
Formats::const_iterator fit =
|
|
|
|
find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
|
|
|
|
if (fit != end)
|
|
|
|
fmts.push_back(fit->name());
|
|
|
|
}
|
|
|
|
|
2003-03-27 04:29:14 +00:00
|
|
|
if (lyxerr.debugging()) {
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "\nOf these, LyX recognises the following formats:\n";
|
|
|
|
|
|
|
|
FormatList::const_iterator fbegin = fmts.begin();
|
|
|
|
FormatList::const_iterator fend = fmts.end();
|
|
|
|
for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
|
|
|
|
if (fit != fbegin)
|
|
|
|
lyxerr[Debug::GRAPHICS] << ", ";
|
|
|
|
lyxerr[Debug::GRAPHICS] << *fit;
|
|
|
|
}
|
|
|
|
lyxerr[Debug::GRAPHICS] << '\n' << endl;
|
2002-07-12 01:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QLImage::QLImage()
|
|
|
|
: Image()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QLImage::QLImage(QLImage const & other)
|
2002-10-20 01:48:28 +00:00
|
|
|
: Image(other), pixmap_(other.pixmap_),
|
|
|
|
xformed_pixmap_(other.xformed_pixmap_)
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QLImage::~QLImage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 15:34:39 +00:00
|
|
|
Image * QLImage::clone_impl() const
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
|
|
|
return new QLImage(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 15:34:39 +00:00
|
|
|
unsigned int QLImage::getWidth_impl() const
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
2002-07-19 23:04:55 +00:00
|
|
|
return xformed_pixmap_.width();
|
2002-07-12 01:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 15:34:39 +00:00
|
|
|
unsigned int QLImage::getHeight_impl() const
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
2002-07-19 23:04:55 +00:00
|
|
|
return xformed_pixmap_.height();
|
2002-07-12 01:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 15:34:39 +00:00
|
|
|
void QLImage::load_impl(string const & filename)
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
|
|
|
if (!pixmap_.isNull()) {
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
2002-07-18 01:23:12 +00:00
|
|
|
<< "Image is loaded already!" << endl;
|
2002-07-12 01:48:53 +00:00
|
|
|
finishedLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-12-17 20:37:13 +00:00
|
|
|
if (!pixmap_.load(toqstr(filename))) {
|
2002-07-12 01:48:53 +00:00
|
|
|
lyxerr[Debug::GRAPHICS]
|
2002-07-18 01:23:12 +00:00
|
|
|
<< "Unable to open image" << endl;
|
2002-07-12 01:48:53 +00:00
|
|
|
finishedLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
2002-07-19 23:04:55 +00:00
|
|
|
xformed_pixmap_ = pixmap_;
|
2002-10-20 01:48:28 +00:00
|
|
|
finishedLoading(true);
|
2002-07-12 01:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 15:34:39 +00:00
|
|
|
bool QLImage::setPixmap_impl(Params const & params)
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
|
|
|
if (pixmap_.isNull() || params.display == NoDisplay)
|
|
|
|
return false;
|
|
|
|
|
2002-11-17 10:49:45 +00:00
|
|
|
// FIXME: it's a fake kind of grayscale !
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-07-12 01:48:53 +00:00
|
|
|
switch (params.display) {
|
2002-11-17 10:49:45 +00:00
|
|
|
case GrayscaleDisplay:
|
|
|
|
case MonochromeDisplay: {
|
|
|
|
QImage i(xformed_pixmap_.convertToImage());
|
|
|
|
xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
|
|
|
|
break;
|
|
|
|
}
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-11-17 10:49:45 +00:00
|
|
|
default:
|
|
|
|
break;
|
2002-07-12 01:48:53 +00:00
|
|
|
}
|
2002-11-17 10:49:45 +00:00
|
|
|
// FIXME
|
|
|
|
#if 0
|
2002-07-12 01:48:53 +00:00
|
|
|
unsigned int fill = packedcolor(LColor::graphicsbg);
|
|
|
|
if (fill != image_->fill_color) {
|
|
|
|
// the background color has changed.
|
|
|
|
// Note that in grayscale/monochrome images the background is
|
|
|
|
// grayed also, so this call will have no visible effect. Sorry!
|
|
|
|
flimage_replace_pixel(image_, image_->fill_color, fill);
|
|
|
|
image_->fill_color = fill;
|
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
#endif
|
2002-07-12 01:48:53 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 15:34:39 +00:00
|
|
|
void QLImage::clip_impl(Params const & params)
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
2002-07-19 23:04:55 +00:00
|
|
|
if (xformed_pixmap_.isNull())
|
2002-07-12 01:48:53 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (params.bb.empty())
|
|
|
|
// No clipping is necessary.
|
|
|
|
return;
|
|
|
|
|
|
|
|
int const new_width = params.bb.xr - params.bb.xl;
|
|
|
|
int const new_height = params.bb.yt - params.bb.yb;
|
|
|
|
|
|
|
|
// No need to check if the width, height are > 0 because the
|
|
|
|
// Bounding Box would be empty() in this case.
|
|
|
|
if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
|
|
|
|
// Bounds are invalid.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_width == pixmap_.width() && new_height == pixmap_.height())
|
|
|
|
return;
|
|
|
|
|
2002-08-27 20:30:20 +00:00
|
|
|
int const xoffset_l = params.bb.xl;
|
2003-11-20 01:22:51 +00:00
|
|
|
int const yoffset_t = (pixmap_.height() > int(params.bb.yt) ?
|
2002-11-17 10:49:45 +00:00
|
|
|
pixmap_.height() - params.bb.yt : 0);
|
2002-07-12 01:48:53 +00:00
|
|
|
|
|
|
|
xformed_pixmap_.resize(new_width, new_height);
|
|
|
|
QPainter p;
|
|
|
|
p.begin(&xformed_pixmap_);
|
|
|
|
p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
|
|
|
|
p.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 15:34:39 +00:00
|
|
|
void QLImage::rotate_impl(Params const & params)
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
|
|
|
if (xformed_pixmap_.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!params.angle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The angle passed to flimage_rotate is the angle in one-tenth of a
|
|
|
|
// degree units.
|
|
|
|
|
|
|
|
QWMatrix m;
|
2002-08-08 04:24:36 +00:00
|
|
|
m.rotate(-params.angle);
|
2002-07-19 23:04:55 +00:00
|
|
|
xformed_pixmap_ = xformed_pixmap_.xForm(m);
|
2002-07-12 01:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-16 15:34:39 +00:00
|
|
|
void QLImage::scale_impl(Params const & params)
|
2002-07-12 01:48:53 +00:00
|
|
|
{
|
|
|
|
if (xformed_pixmap_.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int height;
|
|
|
|
boost::tie(width, height) = getScaledDimensions(params);
|
|
|
|
|
|
|
|
if (width == getWidth() && height == getHeight())
|
|
|
|
return;
|
|
|
|
|
2002-07-19 23:04:55 +00:00
|
|
|
QWMatrix m;
|
|
|
|
m.scale(double(width) / getWidth(), double(height) / getHeight());
|
|
|
|
xformed_pixmap_ = xformed_pixmap_.xForm(m);
|
2002-07-12 01:48:53 +00:00
|
|
|
}
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
} // namespace graphics
|
|
|
|
} // lyx
|