[Qt-interest] Question about QImage smoothScale
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Thu Dec 11 15:12:30 CET 2008
Khizer Kapadia wrote on Thursday, December 11, 2008 1:20 PM:
> I need to resize an image (from large to small) before performing
> some complex operations on it. The need to resize is so that the
> picture size (in bytes) is small, allowing faster operation.
So far so good...
>
> I tried QImage::smoothScale and didn't notice any increase in speed.
What /exactly/ are you comparing? Note that smoothScale() returns a scaled COPY of the image - the original image (on which you call the method smoothScale() is *not* modified!.
Also note that smoothScale() is part of Qt 3.3 and is replaced by the method http://doc.trolltech.com/4.4/qimage.html#scaled in Qt 4. So from now on I am referring to that method instead:
So for example this has *no* effect:
QImage largeImage ("/some/file/path/to/large/image.png");
QSize smallSize (640, 480);
// the image COPY is not assigned to anything, so it is "lost in space"
largeImage.scaled(smallSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
So in case you would expect now that successive operations on 'largeImage' are now faster... you just fell into a deception trap ;)
> Does this kind of resizing decrease the size of image?. And what
> about QPixmap::resize?, does it perform a similar function?.
Again I am referring to the new Qt 4 method http://doc.trolltech.com/4.4/qpixmap.html#scaled (resize() does not exist anymore in Qt 4). Yes, it does a similar thing: it scales (aha!) the QPixmap to the desired size, just as scaled() on a QImage scales the QImage to the desired size - both methods return the scaled *copy*!
If the difference between QPixmap and QImage is not clear to you, read the docs:
http://doc.trolltech.com/4.4/qpixmap.htm
http://doc.trolltech.com/4.4/qimage.html
Basically QImage is for pixel manipulation ("hardware independent") whereas QPixmap is for fast drawing on your display ("hardware dependent", e.g. the colour depth of your display).
Anyway, what you are looking after is something like:
QImage largeImage ("/some/file/path/to/large/image.png");
QImage smallImage;
QSize smallSize (640, 480);
smallImage = largeImage.scaled(smallSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
// all successive operations are now done with 'smallImage' and should be considerably faster than
// when done with 'largeImage'
...
The same example goes in analogy with a QPixmap (which is to be preferred if you merely want to *display* the images on screen).
Note that depending on your needs the following gains you a bit of speed, too:
smallImage = largeImage.scaled(..., ..., Qt::FastTransformation);
No interpolation is done with Qt::FastTransformation ("nearest neighbour" is done), but since you are downscaling the resulting image might be visually acceptable as well (but you might still see "jagged edges" etc.)!
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list