]> git.lyx.org Git - features.git/commitdiff
* GuiImage:
authorAbdelrazak Younes <younes@lyx.org>
Fri, 19 Sep 2008 11:27:24 +0000 (11:27 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Fri, 19 Sep 2008 11:27:24 +0000 (11:27 +0000)
- switch to QImage backend instead of QPixmap. In any case this was done internally by Qt for any image loading or transformation. This should relieve the X11 server a bit for big images.
- try to clear out the memory after a transformation by calling QImage::detach(). Unfortunately there seems to be a bug somewhere in Qt... see (http://bugzilla.lyx.org/show_bug.cgi?id=5002).

* GraphicsImage: get rid of scaledDimension()

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26455 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/qt4/GuiImage.cpp
src/frontends/qt4/GuiImage.h
src/frontends/qt4/GuiPainter.cpp
src/graphics/GraphicsImage.cpp
src/graphics/GraphicsImage.h

index 80cb94f9198b2482eae368bb76b409d100cf9ef2..cf580b12530150370c2bbdb3b121602c35dec3b4 100644 (file)
@@ -23,7 +23,6 @@
 #include "support/lstrings.h"       // ascii_lowercase
 
 #include <QPainter>
-#include <QImage>
 #include <QImageReader>
 
 using namespace std;
@@ -39,9 +38,14 @@ Image * GuiImage::newImage()
 }
 
 
+GuiImage::GuiImage() : is_transformed_(false)
+{}
+
+
 GuiImage::GuiImage(GuiImage const & other)
        : Image(other), original_(other.original_),
-         transformed_(other.transformed_), is_transformed_(other.is_transformed_)
+       transformed_(other.transformed_), is_transformed_(other.is_transformed_),
+       fname_(other.fname_)
 {}
 
 
@@ -51,6 +55,12 @@ Image * GuiImage::clone() const
 }
 
 
+QImage const & GuiImage::image() const
+{
+       return is_transformed_ ? transformed_ : original_;
+}
+
+
 unsigned int GuiImage::width() const
 {
        return is_transformed_ ? transformed_.width() : original_.width();
@@ -69,13 +79,18 @@ bool GuiImage::load(FileName const & filename)
                LYXERR(Debug::GRAPHICS, "Image is loaded already!");
                return false;
        }
-
        fname_ = toqstr(filename.absFilename());
+       return load();
+}
+
 
+bool GuiImage::load()
+{
        if (!original_.load(fname_)) {
                LYXERR(Debug::GRAPHICS, "Unable to open image");
                return false;
        }
+       original_.detach();
        return true;
 }
 
@@ -86,7 +101,7 @@ bool GuiImage::setPixmap(Params const & params)
                return false;
 
        if (original_.isNull()) {
-               if (!original_.load(fname_))
+               if (!load())
                        return false;
        }
                
@@ -95,10 +110,13 @@ bool GuiImage::setPixmap(Params const & params)
        is_transformed_ |= scale(params);
 
        // Clear the pixmap to save some memory.
-       if (is_transformed_)
-               original_ = QPixmap();
-       else
-               transformed_ = QPixmap();
+       if (is_transformed_) {
+               original_.detach();
+               original_ = QImage();
+       } else {
+               transformed_.detach();
+               transformed_ = QImage();
+       }
 
        return true;
 }
@@ -113,22 +131,23 @@ bool GuiImage::clip(Params const & params)
        int const new_width  = params.bb.xr - params.bb.xl;
        int const new_height = params.bb.yt - params.bb.yb;
 
+       QImage const & image = is_transformed_ ? transformed_ : original_;
+
        // No need to check if the width, height are > 0 because the
        // Bounding Box would be empty() in this case.
-       if (new_width > original_.width() || new_height > original_.height()) {
+       if (new_width > image.width() || new_height > image.height()) {
                // Bounds are invalid.
                return false;
        }
 
-       if (new_width == original_.width() && new_height == original_.height())
+       if (new_width == image.width() && new_height == image.height())
                return false;
 
        int const xoffset_l = params.bb.xl;
-       int const yoffset_t = (original_.height() > int(params.bb.yt) ?
-                              original_.height() - params.bb.yt : 0);
+       int const yoffset_t = (image.height() > int(params.bb.yt))
+               ? image.height() - params.bb.yt : 0;
 
-       transformed_ = original_.copy(xoffset_l, yoffset_t,
-                                     new_width, new_height);
+       transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
        return true;
 }
 
@@ -138,30 +157,37 @@ bool GuiImage::rotate(Params const & params)
        if (!params.angle)
                return false;
 
-       if (!is_transformed_)
-               transformed_ = original_;
-
+       QImage const & image = is_transformed_ ? transformed_ : original_;
        QMatrix m;
-       m.rotate(-params.angle);
-       transformed_ = transformed_.transformed(m);
+       m.rotate(- params.angle);
+       transformed_ = image.transformed(m);
        return true;
 }
 
 
 bool GuiImage::scale(Params const & params)
 {
-       Dimension dim = scaledDimension(params);
+       QImage const & image = is_transformed_ ? transformed_ : original_;
 
-       if (dim.width() == width() && dim.height() == height())
-               return false;
+       unsigned int w = image.width();
+       unsigned int h = image.height();
 
-       if (!is_transformed_)
-               transformed_ = original_;
+       // scale only when value > 0
+       if (params.scale > 0) {
+               w = (w * params.scale) / 100;
+               h = (h * params.scale) / 100;
+       }
 
-       QMatrix m;
-       m.scale(double(dim.width()) / width(), double(dim.height()) / height());
-       transformed_ = transformed_.transformed(m);
+       LYXERR(Debug::GRAPHICS, "\n\tparams.scale  : " << params.scale
+                                << "\n\twidth         : " << w
+                                << "\n\theight        : " << h);
 
+       if (w == image.width() && h == image.height())
+               return false;
+       
+       QMatrix m;
+       m.scale(double(w) / image.width(), double(h) / image.height());
+       transformed_ = image.transformed(m);
        return true;
 }
 
index 2bdfb6af73e084cffffe55cff6e0ed642f6cbf13..0da2802cd5e447e172270600d144796de78d996d 100644 (file)
@@ -15,7 +15,7 @@
 
 #include "graphics/GraphicsImage.h"
 
-#include <QPixmap>
+#include <QImage>
 #include <QString>
 
 namespace lyx {
@@ -27,9 +27,8 @@ public:
        /// Access to this class is through this static method.
        static Image * newImage();
 
-       /// Retrieve the buffered pixmap.
-       QPixmap const & pixmap() const
-       { return is_transformed_? transformed_ : original_; }
+       /// Retrieve the rendered image.
+       QImage const & image() const;
 
 private:
        /// Create a copy
@@ -44,6 +43,7 @@ private:
         * Load the image file into memory.
         */
        bool load(support::FileName const & filename);
+       bool load();
        /**
         * Finishes the process of modifying transformed_, using
         * \c params to decide on color, grayscale etc.
@@ -59,16 +59,17 @@ private:
        bool scale(Params const & params);
 
        /// Access to the class is through newImage() and clone.
-       GuiImage() {}
+       GuiImage();
        ///
        GuiImage(GuiImage const &);
 
        /// The original loaded image.
-       QPixmap original_;
+       QImage original_;
 
        /// The transformed image for display.
-       QPixmap transformed_;
-       /// Buffer the pixmap itself
+       QImage transformed_;
+
+       ///
        bool is_transformed_;
        ///
        QString fname_;
index 0cbbb6b86daf2e873c0cc49056b135a178f9ab50..4e6d516386bf50912ce34ac1527679cfd25b137d 100644 (file)
@@ -263,7 +263,7 @@ void GuiPainter::image(int x, int y, int w, int h, graphics::Image const & i)
        if (!isDrawingEnabled())
                return;
 
-       drawPixmap(x, y, qlimage.pixmap(), 0, 0, w, h);
+       drawImage(x, y, qlimage.image(), 0, 0, w, h);
 }
 
 
index 2009032b9257f32973297c2260319e57c27b994a..86e43663ea41d033a4fc19acb5e2822ac6759519 100644 (file)
@@ -25,23 +25,5 @@ namespace graphics {
 // instance of a viable derived class.
 boost::function<Image *()> Image::newImage;
 
-Dimension Image::scaledDimension(Params const & params) const
-{
-       // scale only when value > 0
-       unsigned int w = width();
-       unsigned int h = height();
-       if (params.scale) {
-               w = (w * params.scale) / 100;
-               h = (h * params.scale) / 100;
-       }
-
-       LYXERR(Debug::GRAPHICS, "graphics::Image::getScaledDimensions()"
-               << "\n\tparams.scale       : " << params.scale
-               << "\n\twidth              : " << w
-               << "\n\theight             : " << h);
-
-       return Dimension(w, h, 0);
-}
-
 } // namespace graphics
 } // namespace lyx
index f316572d967b9bb19c6df5aac61e755fab16c6c4..ad9c5c588fd2606cc5ead3be9ff0f404596e519b 100644 (file)
@@ -85,12 +85,6 @@ protected:
        Image() {}
        /// Don't copy the signal finishedLoading
        Image(Image const &) {}
-
-       /** Uses the params to ascertain the dimensions of the scaled image.
-        *  Returned as Dimension(width, height, 0 descend).
-        *  If something goes wrong, returns make_pair(getWidth(), getHeight(), 0)
-        */
-       Dimension scaledDimension(Params const & params) const;
 };