diff options
author | gunnar <gunnar@trolltech.com> | 2009-08-24 09:37:21 (GMT) |
---|---|---|
committer | gunnar <gunnar@trolltech.com> | 2009-08-24 10:55:39 (GMT) |
commit | dd21f5600a8bf07d1ac9b5367005790db86892ae (patch) | |
tree | 8b83795c3c20064da0bd3f1196df14ea38d6ef7f /src/gui | |
parent | d1111859c26526227c07793aa6747efa65496055 (diff) | |
download | Qt-dd21f5600a8bf07d1ac9b5367005790db86892ae.zip Qt-dd21f5600a8bf07d1ac9b5367005790db86892ae.tar.gz Qt-dd21f5600a8bf07d1ac9b5367005790db86892ae.tar.bz2 |
make QPixmap rely on QPixmapData for loading from files and from data...
Reviewed-By: Eskil
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/image/qpixmap.cpp | 29 | ||||
-rw-r--r-- | src/gui/image/qpixmapdata.cpp | 39 | ||||
-rw-r--r-- | src/gui/image/qpixmapdata_p.h | 6 |
3 files changed, 43 insertions, 31 deletions
diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index 00e78ad..658b8e0 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -832,20 +832,11 @@ bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConvers if (QPixmapCache::find(key, *this)) return true; - QImage image = QImageReader(fileName, format).read(); - if (image.isNull()) - return false; - - QPixmap pm; - if (data->pixelType() == QPixmapData::BitmapType) - pm = QBitmap::fromImage(image, flags); - else - pm = fromImage(image, flags); - if (!pm.isNull()) { - *this = pm; + if (data->fromFile(fileName, format, flags)) { QPixmapCache::insert(key, *this); return true; } + return false; } @@ -870,21 +861,7 @@ bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConvers bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags) { - QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len); - QBuffer b(&a); - b.open(QIODevice::ReadOnly); - - QImage image = QImageReader(&b, format).read(); - QPixmap pm; - if (data->pixelType() == QPixmapData::BitmapType) - pm = QBitmap::fromImage(image, flags); - else - pm = fromImage(image, flags); - if (!pm.isNull()) { - *this = pm; - return true; - } - return false; + return data->fromData(buf, len, format, flags); } /*! diff --git a/src/gui/image/qpixmapdata.cpp b/src/gui/image/qpixmapdata.cpp index 001a930..c8b91db 100644 --- a/src/gui/image/qpixmapdata.cpp +++ b/src/gui/image/qpixmapdata.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "qpixmapdata_p.h" +#include <QtCore/qbuffer.h> #include <QtGui/qbitmap.h> #include <QtGui/qimagereader.h> @@ -66,14 +67,44 @@ QPixmapData::~QPixmapData() { } -void QPixmapData::fromFile(const QString &fileName, const char *format, +static QImage makeBitmapCompliantIfNeeded(QPixmapData *d, const QImage &image, Qt::ImageConversionFlags flags) +{ + if (d->pixelType() == QPixmapData::BitmapType) { + QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags); + + // make sure image.color(0) == Qt::color0 (white) + // and image.color(1) == Qt::color1 (black) + const QRgb c0 = QColor(Qt::black).rgb(); + const QRgb c1 = QColor(Qt::white).rgb(); + if (img.color(0) == c0 && img.color(1) == c1) { + img.invertPixels(); + img.setColor(0, c1); + img.setColor(1, c0); + } + return img; + } + + return image; +} + +bool QPixmapData::fromFile(const QString &fileName, const char *format, Qt::ImageConversionFlags flags) { - const QImage image = QImageReader(fileName, format).read(); + QImage image = QImageReader(fileName, format).read(); if (image.isNull()) - return; + return false; + fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags); + return !isNull(); +} - fromImage(image, flags); +bool QPixmapData::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags) +{ + QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len); + QBuffer b(&a); + b.open(QIODevice::ReadOnly); + QImage image = QImageReader(&b, format).read(); + fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags); + return !isNull(); } void QPixmapData::copy(const QPixmapData *data, const QRect &rect) diff --git a/src/gui/image/qpixmapdata_p.h b/src/gui/image/qpixmapdata_p.h index 70890c0..9040641 100644 --- a/src/gui/image/qpixmapdata_p.h +++ b/src/gui/image/qpixmapdata_p.h @@ -75,8 +75,12 @@ public: virtual void resize(int width, int height) = 0; virtual void fromImage(const QImage &image, Qt::ImageConversionFlags flags) = 0; - virtual void fromFile(const QString &filename, const char *format, + + virtual bool fromFile(const QString &filename, const char *format, + Qt::ImageConversionFlags flags); + virtual bool fromData(const uchar *buffer, uint len, const char *format, Qt::ImageConversionFlags flags); + virtual void copy(const QPixmapData *data, const QRect &rect); virtual bool scroll(int dx, int dy, const QRect &rect); |