From c16162214eb8757a62e221c34d38cefc402e5b05 Mon Sep 17 00:00:00 2001 From: Benjamin Poulain Date: Tue, 29 Jun 2010 17:28:19 +0200 Subject: Add QPixmap::fromImageReader() to decode arbitrary images in place. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Decoding in-place with QPixmap::fromData() has limitations, it cannot be used with animated gif images, and the user has no control over the parameters of the image reader. Reader the image directly from the image reader allow us to do a better job when loading images. Reviewed-by: Simon Hausmann Reviewed-by: Samuel Rødal --- src/gui/image/qpixmap.cpp | 23 ++++++++++++++++++++++- src/gui/image/qpixmap.h | 2 ++ src/gui/image/qpixmapdata.cpp | 7 +++++++ src/gui/image/qpixmapdata_p.h | 4 ++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index fd2c139..ef9be8f 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -2018,7 +2018,7 @@ void QPixmap::detach() the color table. If this is too expensive an operation, you can use QBitmap::fromImage() instead. - \sa toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion} + \sa fromImageReader(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion} */ QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags) { @@ -2033,6 +2033,27 @@ QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags) } /*! + \fn QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags) + + Create a QPixmap from an image read directly from an \a imageReader. + The \a flags argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}. + Passing 0 for \a flags sets all the default options. + + On some systems, reading an image directly to QPixmap can use less memory than + reading a QImage to convert it to QPixmap. + + \sa fromImage(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion} +*/ +QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags) +{ + QGraphicsSystem *gs = QApplicationPrivate::graphicsSystem(); + QScopedPointer data(gs ? gs->createPixmapData(QPixmapData::PixmapType) + : QGraphicsSystem::createDefaultPixmapData(QPixmapData::PixmapType)); + data->fromImageReader(imageReader, flags); + return QPixmap(data.take()); +} + +/*! \fn QPixmap QPixmap::grabWindow(WId window, int x, int y, int width, int height) diff --git a/src/gui/image/qpixmap.h b/src/gui/image/qpixmap.h index 82546da..64ca8a3 100644 --- a/src/gui/image/qpixmap.h +++ b/src/gui/image/qpixmap.h @@ -62,6 +62,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Gui) class QImageWriter; +class QImageReader; class QColor; class QVariant; class QX11Info; @@ -134,6 +135,7 @@ public: QImage toImage() const; static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor); + static QPixmap fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags = Qt::AutoColor); bool load(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor); bool loadFromData(const uchar *buf, uint len, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor); diff --git a/src/gui/image/qpixmapdata.cpp b/src/gui/image/qpixmapdata.cpp index 345e3cf..ef1f6c4 100644 --- a/src/gui/image/qpixmapdata.cpp +++ b/src/gui/image/qpixmapdata.cpp @@ -124,6 +124,13 @@ static QImage makeBitmapCompliantIfNeeded(QPixmapData *d, const QImage &image, Q return image; } +void QPixmapData::fromImageReader(QImageReader *imageReader, + Qt::ImageConversionFlags flags) +{ + const QImage image = imageReader->read(); + fromImage(image, flags); +} + bool QPixmapData::fromFile(const QString &fileName, const char *format, Qt::ImageConversionFlags flags) { diff --git a/src/gui/image/qpixmapdata_p.h b/src/gui/image/qpixmapdata_p.h index 9a1505a..ec62b0b 100644 --- a/src/gui/image/qpixmapdata_p.h +++ b/src/gui/image/qpixmapdata_p.h @@ -58,6 +58,8 @@ QT_BEGIN_NAMESPACE +class QImageReader; + class Q_GUI_EXPORT QPixmapData { public: @@ -83,6 +85,8 @@ public: virtual void resize(int width, int height) = 0; virtual void fromImage(const QImage &image, Qt::ImageConversionFlags flags) = 0; + virtual void fromImageReader(QImageReader *imageReader, + Qt::ImageConversionFlags flags); virtual bool fromFile(const QString &filename, const char *format, Qt::ImageConversionFlags flags); -- cgit v0.12