Simplify and optimize image on screen visualization. Fix one bug or two along the way WRT combined image effects (egg: rotation and grayscale).

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25175 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-06-06 22:51:11 +00:00
parent 87cbf97c91
commit 374ec4be57
4 changed files with 56 additions and 60 deletions

View File

@ -41,8 +41,7 @@ Image * GuiImage::newImage()
GuiImage::GuiImage(GuiImage const & other) GuiImage::GuiImage(GuiImage const & other)
: Image(other), original_(other.original_), : Image(other), original_(other.original_),
transformed_(other.transformed_), transformed_(other.transformed_), is_transformed_(other.is_transformed_)
transformed_pixmap_(other.transformed_pixmap_)
{} {}
@ -54,13 +53,13 @@ Image * GuiImage::clone() const
unsigned int GuiImage::width() const unsigned int GuiImage::width() const
{ {
return transformed_.width(); return is_transformed_ ? transformed_.width() : original_.width();
} }
unsigned int GuiImage::height() const unsigned int GuiImage::height() const
{ {
return transformed_.height(); return is_transformed_ ? transformed_.height() : original_.height();
} }
@ -75,17 +74,17 @@ bool GuiImage::load(FileName const & filename)
LYXERR(Debug::GRAPHICS, "Unable to open image"); LYXERR(Debug::GRAPHICS, "Unable to open image");
return false; return false;
} }
transformed_ = original_;
return true; return true;
} }
// This code is taken from KImageEffect::toGray // This code is taken from KImageEffect::toGray
static QImage & toGray(QImage & img) static QPixmap toGray(QPixmap const & pix)
{ {
if (img.width() == 0 || img.height() == 0) if (pix.width() == 0 || pix.height() == 0)
return img; return pix;
QImage img = pix.toImage();
int const pixels = img.depth() > 8 ? int const pixels = img.depth() > 8 ?
img.width() * img.height() : img.numColors(); img.width() * img.height() : img.numColors();
@ -97,7 +96,7 @@ static QImage & toGray(QImage & img)
int const val = qGray(data[i]); int const val = qGray(data[i]);
data[i] = qRgba(val, val, val, qAlpha(data[i])); data[i] = qRgba(val, val, val, qAlpha(data[i]));
} }
return img; return QPixmap::fromImage(img);
} }
@ -106,14 +105,24 @@ bool GuiImage::setPixmap(Params const & params)
if (original_.isNull() || params.display == NoDisplay) if (original_.isNull() || params.display == NoDisplay)
return false; return false;
is_transformed_ = clip(params);
is_transformed_ |= rotate(params);
is_transformed_ |= scale(params);
switch (params.display) { switch (params.display) {
case GrayscaleDisplay: { case GrayscaleDisplay: {
toGray(transformed_); transformed_ = is_transformed_
? toGray(transformed_) : toGray(original_);
is_transformed_ = true;
break; break;
} }
case MonochromeDisplay: { case MonochromeDisplay: {
transformed_.convertToFormat(transformed_.format(), Qt::MonoOnly); QImage img = is_transformed_
? transformed_.toImage() : original_.toImage();
img.convertToFormat(img.format(), Qt::MonoOnly);
transformed_ = QPixmap::fromImage(img);
is_transformed_ = true;
break; break;
} }
@ -121,19 +130,19 @@ bool GuiImage::setPixmap(Params const & params)
break; break;
} }
transformed_pixmap_ = QPixmap::fromImage(transformed_); if (!is_transformed_)
// Clear it out to save some memory.
transformed_ = QPixmap();
return true; return true;
} }
void GuiImage::clip(Params const & params) bool GuiImage::clip(Params const & params)
{ {
if (transformed_.isNull())
return;
if (params.bb.empty()) if (params.bb.empty())
// No clipping is necessary. // No clipping is necessary.
return; return false;
int const new_width = params.bb.xr - params.bb.xl; int const new_width = params.bb.xr - params.bb.xl;
int const new_height = params.bb.yt - params.bb.yb; int const new_height = params.bb.yt - params.bb.yb;
@ -142,11 +151,11 @@ void GuiImage::clip(Params const & params)
// Bounding Box would be empty() in this case. // Bounding Box would be empty() in this case.
if (new_width > original_.width() || new_height > original_.height()) { if (new_width > original_.width() || new_height > original_.height()) {
// Bounds are invalid. // Bounds are invalid.
return; return false;
} }
if (new_width == original_.width() && new_height == original_.height()) if (new_width == original_.width() && new_height == original_.height())
return; return false;
int const xoffset_l = params.bb.xl; int const xoffset_l = params.bb.xl;
int const yoffset_t = (original_.height() > int(params.bb.yt) ? int const yoffset_t = (original_.height() > int(params.bb.yt) ?
@ -154,37 +163,40 @@ void GuiImage::clip(Params const & params)
transformed_ = original_.copy(xoffset_l, yoffset_t, transformed_ = original_.copy(xoffset_l, yoffset_t,
new_width, new_height); new_width, new_height);
return true;
} }
void GuiImage::rotate(Params const & params) bool GuiImage::rotate(Params const & params)
{ {
if (transformed_.isNull())
return;
if (!params.angle) if (!params.angle)
return; return false;
if (!is_transformed_)
transformed_ = original_;
QMatrix m; QMatrix m;
m.rotate(-params.angle); m.rotate(-params.angle);
transformed_ = transformed_.transformed(m); transformed_ = transformed_.transformed(m);
return true;
} }
void GuiImage::scale(Params const & params) bool GuiImage::scale(Params const & params)
{ {
if (transformed_.isNull())
return;
Dimension dim = scaledDimension(params); Dimension dim = scaledDimension(params);
if (dim.width() == width() && dim.height() == height()) if (dim.width() == width() && dim.height() == height())
return; return false;
if (!is_transformed_)
transformed_ = original_;
QMatrix m; QMatrix m;
m.scale(double(dim.width()) / width(), double(dim.height()) / height()); m.scale(double(dim.width()) / width(), double(dim.height()) / height());
transformed_ = transformed_.transformed(m); transformed_ = transformed_.transformed(m);
return true;
} }
} // namespace graphics } // namespace graphics

View File

@ -15,7 +15,6 @@
#include "graphics/GraphicsImage.h" #include "graphics/GraphicsImage.h"
#include <QImage>
#include <QPixmap> #include <QPixmap>
namespace lyx { namespace lyx {
@ -28,20 +27,18 @@ public:
static Image * newImage(); static Image * newImage();
/// Retrieve the buffered pixmap. /// Retrieve the buffered pixmap.
QPixmap const & qpixmap() const { return transformed_pixmap_; } QPixmap const & pixmap() const
{ return is_transformed_? transformed_ : original_; }
/// Retrieve the buffered pixmap.
QImage const & qimage() const { return transformed_; }
private: private:
/// Create a copy /// Create a copy
virtual Image * clone() const; Image * clone() const;
/// Get the image width /// Get the image width
virtual unsigned int width() const; unsigned int width() const;
/// Get the image height /// Get the image height
virtual unsigned int height() const; unsigned int height() const;
// FIXME Is the image drawable ? // FIXME Is the image drawable ?
virtual bool isDrawable() const { return true; } bool isDrawable() const { return true; }
/** /**
* Load the image file into memory. * Load the image file into memory.
*/ */
@ -51,13 +48,14 @@ private:
* \c params to decide on color, grayscale etc. * \c params to decide on color, grayscale etc.
* \returns true if successful. * \returns true if successful.
*/ */
virtual bool setPixmap(Params const & params); bool setPixmap(Params const & params);
/// Clip the image using params. /// Clip the image using params.
virtual void clip(Params const & params); bool clip(Params const & params);
/// Rotate the image using params. /// Rotate the image using params.
virtual void rotate(Params const & params); bool rotate(Params const & params);
/// Scale the image using params. /// Scale the image using params.
virtual void scale(Params const & params); bool scale(Params const & params);
/// Access to the class is through newImage() and clone. /// Access to the class is through newImage() and clone.
GuiImage() {} GuiImage() {}
@ -65,12 +63,12 @@ private:
GuiImage(GuiImage const &); GuiImage(GuiImage const &);
/// The original loaded image. /// The original loaded image.
QImage original_; QPixmap original_;
/// The transformed image for display. /// The transformed image for display.
QImage transformed_; QPixmap transformed_;
/// Buffer the pixmap itself /// Buffer the pixmap itself
QPixmap transformed_pixmap_; bool is_transformed_;
}; };
} // namespace graphics } // namespace graphics

View File

@ -80,15 +80,6 @@ public:
*/ */
virtual bool setPixmap(Params const & params) = 0; virtual bool setPixmap(Params const & params) = 0;
/// Clip the image using params.
virtual void clip(Params const & params) = 0;
/// Rotate the image using params.
virtual void rotate(Params const & params) = 0;
/// Scale the image using params.
virtual void scale(Params const & params) = 0;
protected: protected:
/// Must define default c-tor explicitly as we define a copy c-tor. /// Must define default c-tor explicitly as we define a copy c-tor.
Image() {} Image() {}

View File

@ -413,11 +413,6 @@ void Loader::Impl::createPixmap()
image_.reset(cached_item_->image()->clone()); image_.reset(cached_item_->image()->clone());
// These do nothing if there's nothing to do
image_->clip(params_);
image_->rotate(params_);
image_->scale(params_);
bool const success = image_->setPixmap(params_); bool const success = image_->setPixmap(params_);
if (success) { if (success) {