diff options
Diffstat (limited to 'src/gui/image')
-rw-r--r-- | src/gui/image/image.pri | 18 | ||||
-rw-r--r-- | src/gui/image/qbmphandler.cpp | 10 | ||||
-rw-r--r-- | src/gui/image/qimage.cpp | 287 | ||||
-rw-r--r-- | src/gui/image/qimage_p.h | 3 | ||||
-rw-r--r-- | src/gui/image/qimagereader.cpp | 2 | ||||
-rw-r--r-- | src/gui/image/qpixmap.cpp | 3 | ||||
-rw-r--r-- | src/gui/image/qpixmap_raster.cpp | 201 | ||||
-rw-r--r-- | src/gui/image/qpixmap_raster_p.h | 3 | ||||
-rw-r--r-- | src/gui/image/qpixmap_x11.cpp | 6 | ||||
-rw-r--r-- | src/gui/image/qpixmapcache.cpp | 29 | ||||
-rw-r--r-- | src/gui/image/qpixmapdata.cpp | 2 | ||||
-rw-r--r-- | src/gui/image/qpnghandler.cpp | 10 | ||||
-rw-r--r-- | src/gui/image/qppmhandler.cpp | 10 | ||||
-rw-r--r-- | src/gui/image/qxbmhandler.cpp | 10 | ||||
-rw-r--r-- | src/gui/image/qxpmhandler.cpp | 8 |
15 files changed, 480 insertions, 122 deletions
diff --git a/src/gui/image/image.pri b/src/gui/image/image.pri index 8d75fdd..f5f1bc0 100644 --- a/src/gui/image/image.pri +++ b/src/gui/image/image.pri @@ -63,7 +63,7 @@ embedded { } x11 { HEADERS += image/qpixmap_x11_p.h - SOURCES += image/qpixmap_x11.cpp + SOURCES += image/qpixmap_x11.cpp } mac { HEADERS += image/qpixmap_mac_p.h @@ -93,12 +93,12 @@ SOURCES += \ SOURCES += image/qpnghandler.cpp contains(QT_CONFIG, system-png) { - unix:LIBS_PRIVATE += -lpng - win32:LIBS += libpng.lib + unix|win32-g++*:LIBS_PRIVATE += -lpng + win32:!win32-g++*:LIBS += libpng.lib } else { - DEFINES *= QT_USE_BUNDLED_LIBPNG + DEFINES *= QT_USE_BUNDLED_LIBPNG !isEqual(QT_ARCH, i386):!isEqual(QT_ARCH, x86_64):DEFINES += PNG_NO_ASSEMBLER_CODE - INCLUDEPATH += ../3rdparty/libpng ../3rdparty/zlib + INCLUDEPATH += ../3rdparty/libpng SOURCES += ../3rdparty/libpng/png.c \ ../3rdparty/libpng/pngerror.c \ ../3rdparty/libpng/pngget.c \ @@ -114,6 +114,14 @@ SOURCES += \ ../3rdparty/libpng/pngwrite.c \ ../3rdparty/libpng/pngwtran.c \ ../3rdparty/libpng/pngwutil.c + + contains(QT_CONFIG, system-zlib) { + symbian:LIBS_PRIVATE += -llibz + else:if(unix|win32-g++*):LIBS_PRIVATE += -lz + else:LIBS += zdll.lib + } else { + INCLUDEPATH += ../3rdparty/zlib + } } } else { DEFINES *= QT_NO_IMAGEFORMAT_PNG diff --git a/src/gui/image/qbmphandler.cpp b/src/gui/image/qbmphandler.cpp index 42e19b8..074b8f0 100644 --- a/src/gui/image/qbmphandler.cpp +++ b/src/gui/image/qbmphandler.cpp @@ -674,13 +674,15 @@ bool QBmpHandler::readHeader() bool QBmpHandler::canRead() const { - if (state == Ready) { - if (!canRead(device())) - return false; + if (state == Ready && !canRead(device())) + return false; + + if (state != Error) { setFormat("bmp"); return true; } - return state != Error; + + return false; } bool QBmpHandler::canRead(QIODevice *device) diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index d89ffe6..bb8a994 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -2274,6 +2274,8 @@ bool QImage::create(const QSize& size, int depth, int numColors, QImage::Endian typedef void (*Image_Converter)(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags); +typedef bool (*InPlace_Image_Converter)(QImageData *data, Qt::ImageConversionFlags); + static void convert_ARGB_to_ARGB_PM(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags) { Q_ASSERT(src->format == QImage::Format_ARGB32); @@ -2298,6 +2300,169 @@ static void convert_ARGB_to_ARGB_PM(QImageData *dest, const QImageData *src, Qt: } } +static bool convert_ARGB_to_ARGB_PM_inplace(QImageData *data, Qt::ImageConversionFlags) +{ + Q_ASSERT(data->format == QImage::Format_ARGB32); + + const int pad = (data->bytes_per_line >> 2) - data->width; + QRgb *rgb_data = (QRgb *) data->data; + + for (int i = 0; i < data->height; ++i) { + const QRgb *end = rgb_data + data->width; + while (rgb_data < end) { + *rgb_data = PREMUL(*rgb_data); + ++rgb_data; + } + rgb_data += pad; + } + data->format = QImage::Format_ARGB32_Premultiplied; + return true; +} + +static bool convert_indexed8_to_ARGB_PM_inplace(QImageData *data, Qt::ImageConversionFlags) +{ + Q_ASSERT(data->format == QImage::Format_Indexed8); + const int depth = 32; + + const int dst_bytes_per_line = ((data->width * depth + 31) >> 5) << 2; + const int nbytes = dst_bytes_per_line * data->height; + uchar *const newData = (uchar *)realloc(data->data, nbytes); + if (!newData) + return false; + + data->data = newData; + + // start converting from the end because the end image is bigger than the source + uchar *src_data = newData + data->nbytes; // end of src + quint32 *dest_data = (quint32 *) (newData + nbytes); // end of dest > end of src + const int width = data->width; + const int src_pad = data->bytes_per_line - width; + const int dest_pad = (dst_bytes_per_line >> 2) - width; + + for (int i = 0; i < data->height; ++i) { + src_data -= src_pad; + dest_data -= dest_pad; + for (int pixI = 0; pixI < width; ++pixI) { + --src_data; + --dest_data; + const uint pixel = data->colortable[*src_data]; + *dest_data = (quint32) PREMUL(pixel); + } + } + + data->format = QImage::Format_ARGB32_Premultiplied; + data->bytes_per_line = dst_bytes_per_line; + data->depth = depth; + data->nbytes = nbytes; + + return true; +} + +static bool convert_indexed8_to_RGB_inplace(QImageData *data, Qt::ImageConversionFlags) +{ + Q_ASSERT(data->format == QImage::Format_Indexed8); + const int depth = 32; + + const int dst_bytes_per_line = ((data->width * depth + 31) >> 5) << 2; + const int nbytes = dst_bytes_per_line * data->height; + uchar *const newData = (uchar *)realloc(data->data, nbytes); + if (!newData) + return false; + + data->data = newData; + + // start converting from the end because the end image is bigger than the source + uchar *src_data = newData + data->nbytes; + quint32 *dest_data = (quint32 *) (newData + nbytes); + const int width = data->width; + const int src_pad = data->bytes_per_line - width; + const int dest_pad = (dst_bytes_per_line >> 2) - width; + + for (int i = 0; i < data->height; ++i) { + src_data -= src_pad; + dest_data -= dest_pad; + for (int pixI = 0; pixI < width; ++pixI) { + --src_data; + --dest_data; + *dest_data = (quint32) data->colortable[*src_data]; + } + } + + data->format = QImage::Format_RGB32; + data->bytes_per_line = dst_bytes_per_line; + data->depth = depth; + data->nbytes = nbytes; + + return true; +} + +static bool convert_indexed8_to_RGB16_inplace(QImageData *data, Qt::ImageConversionFlags) +{ + Q_ASSERT(data->format == QImage::Format_Indexed8); + const int depth = 16; + + const int dst_bytes_per_line = ((data->width * depth + 31) >> 5) << 2; + const int nbytes = dst_bytes_per_line * data->height; + uchar *const newData = (uchar *)realloc(data->data, nbytes); + if (!newData) + return false; + + data->data = newData; + + // start converting from the end because the end image is bigger than the source + uchar *src_data = newData + data->nbytes; + quint16 *dest_data = (quint16 *) (newData + nbytes); + const int width = data->width; + const int src_pad = data->bytes_per_line - width; + const int dest_pad = (dst_bytes_per_line >> 1) - width; + + for (int i = 0; i < data->height; ++i) { + src_data -= src_pad; + dest_data -= dest_pad; + for (int pixI = 0; pixI < width; ++pixI) { + --src_data; + --dest_data; + const uint pixel = data->colortable[*src_data]; + *dest_data = qt_colorConvert<quint16, quint32>(pixel, 0); + } + } + + data->format = QImage::Format_RGB16; + data->bytes_per_line = dst_bytes_per_line; + data->depth = depth; + data->nbytes = nbytes; + + return true; +} + +static bool convert_RGB_to_RGB16_inplace(QImageData *data, Qt::ImageConversionFlags) +{ + Q_ASSERT(data->format == QImage::Format_RGB32); + const int depth = 16; + + const int dst_bytes_per_line = ((data->width * depth + 31) >> 5) << 2; + const int src_bytes_per_line = data->bytes_per_line; + quint32 *src_data = (quint32 *) data->data; + quint16 *dst_data = (quint16 *) data->data; + + for (int i = 0; i < data->height; ++i) { + qt_memconvert(dst_data, src_data, data->width); + src_data = (quint32 *) (((char*)src_data) + src_bytes_per_line); + dst_data = (quint16 *) (((char*)dst_data) + dst_bytes_per_line); + } + data->format = QImage::Format_RGB16; + data->bytes_per_line = dst_bytes_per_line; + data->depth = depth; + data->nbytes = dst_bytes_per_line * data->height; + uchar *const newData = (uchar *)realloc(data->data, data->nbytes); + if (newData) { + data->data = newData; + return true; + } else { + return false; + } +} + static void convert_ARGB_PM_to_ARGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags) { Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied); @@ -3447,6 +3612,103 @@ static const Image_Converter converter_map[QImage::NImageFormats][QImage::NImage } // Format_ARGB4444_Premultiplied }; +static const InPlace_Image_Converter inplace_converter_map[QImage::NImageFormats][QImage::NImageFormats] = +{ + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_Mono + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_MonoLSB + { + 0, + 0, + 0, + 0, + 0, + convert_indexed8_to_RGB_inplace, + convert_indexed8_to_ARGB_PM_inplace, + convert_indexed8_to_RGB16_inplace, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }, // Format_Indexed8 + { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + convert_RGB_to_RGB16_inplace, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }, // Format_ARGB32 + { + 0, + 0, + 0, + 0, + 0, + 0, + convert_ARGB_to_ARGB_PM_inplace, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }, // Format_ARGB32 + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_ARGB32_Premultiplied + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_RGB16 + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_ARGB8565_Premultiplied + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_RGB666 + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_ARGB6666_Premultiplied + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_RGB555 + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_ARGB8555_Premultiplied + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_RGB888 + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, // Format_RGB444 + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + } // Format_ARGB4444_Premultiplied +}; + /*! Returns a copy of the image in the given \a format. @@ -4210,6 +4472,7 @@ QImage QImage::createHeuristicMask(bool clipTight) const int w = width(); int h = height(); QImage m(w, h, Format_MonoLSB); + QIMAGE_SANITYCHECK_MEMORY(m); m.setColorCount(2); m.setColor(0, QColor(Qt::color0).rgba()); m.setColor(1, QColor(Qt::color1).rgba()); @@ -4302,6 +4565,7 @@ QImage QImage::createMaskFromColor(QRgb color, Qt::MaskMode mode) const if (!d) return QImage(); QImage maskImage(size(), QImage::Format_MonoLSB); + QIMAGE_SANITYCHECK_MEMORY(maskImage); maskImage.fill(0); uchar *s = maskImage.bits(); @@ -4362,6 +4626,7 @@ QImage QImage::mirrored(bool horizontal, bool vertical) const int h = d->height; // Create result image, copy colormap QImage result(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(result); // check if we ran out of of memory.. if (!result.d) @@ -4499,6 +4764,7 @@ QImage QImage::rgbSwapped() const case Format_ARGB32: case Format_ARGB32_Premultiplied: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { uint *q = (uint*)res.scanLine(i); uint *p = (uint*)scanLine(i); @@ -4512,6 +4778,7 @@ QImage QImage::rgbSwapped() const break; case Format_RGB16: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { ushort *q = (ushort*)res.scanLine(i); const ushort *p = (const ushort*)scanLine(i); @@ -4525,6 +4792,7 @@ QImage QImage::rgbSwapped() const break; case Format_ARGB8565_Premultiplied: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { quint8 *p = (quint8*)scanLine(i); const quint8 *end = p + d->width * sizeof(qargb8565); @@ -4537,6 +4805,7 @@ QImage QImage::rgbSwapped() const break; case Format_RGB666: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { qrgb666 *q = reinterpret_cast<qrgb666*>(res.scanLine(i)); const qrgb666 *p = reinterpret_cast<const qrgb666*>(scanLine(i)); @@ -4549,6 +4818,7 @@ QImage QImage::rgbSwapped() const break; case Format_ARGB6666_Premultiplied: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { qargb6666 *q = reinterpret_cast<qargb6666*>(res.scanLine(i)); const qargb6666 *p = reinterpret_cast<const qargb6666*>(scanLine(i)); @@ -4561,6 +4831,7 @@ QImage QImage::rgbSwapped() const break; case Format_RGB555: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { ushort *q = (ushort*)res.scanLine(i); const ushort *p = (const ushort*)scanLine(i); @@ -4574,6 +4845,7 @@ QImage QImage::rgbSwapped() const break; case Format_ARGB8555_Premultiplied: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { quint8 *p = (quint8*)scanLine(i); const quint8 *end = p + d->width * sizeof(qargb8555); @@ -4586,6 +4858,7 @@ QImage QImage::rgbSwapped() const break; case Format_RGB888: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { quint8 *q = reinterpret_cast<quint8*>(res.scanLine(i)); const quint8 *p = reinterpret_cast<const quint8*>(scanLine(i)); @@ -4601,6 +4874,7 @@ QImage QImage::rgbSwapped() const break; case Format_RGB444: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { quint8 *q = reinterpret_cast<quint8*>(res.scanLine(i)); const quint8 *p = reinterpret_cast<const quint8*>(scanLine(i)); @@ -4615,6 +4889,7 @@ QImage QImage::rgbSwapped() const break; case Format_ARGB4444_Premultiplied: res = QImage(d->width, d->height, d->format); + QIMAGE_SANITYCHECK_MEMORY(res); for (int i = 0; i < d->height; i++) { quint8 *q = reinterpret_cast<quint8*>(res.scanLine(i)); const quint8 *p = reinterpret_cast<const quint8*>(scanLine(i)); @@ -6263,6 +6538,18 @@ QTransform QImage::trueMatrix(const QTransform &matrix, int w, int h) return matrix * QTransform().translate(-delta.x(), -delta.y()); } +bool QImageData::convertInPlace(QImage::Format newFormat, Qt::ImageConversionFlags flags) +{ + if (format == newFormat) + return true; + + const InPlace_Image_Converter *const converterPtr = &inplace_converter_map[format][newFormat]; + InPlace_Image_Converter converter = *converterPtr; + if (converter) + return converter(this, flags); + else + return false; +} /*! \typedef QImage::DataPtr diff --git a/src/gui/image/qimage_p.h b/src/gui/image/qimage_p.h index 0c19647..f1a0c47 100644 --- a/src/gui/image/qimage_p.h +++ b/src/gui/image/qimage_p.h @@ -96,6 +96,9 @@ struct Q_GUI_EXPORT QImageData { // internal image data bool checkForAlphaPixels() const; + // Convert the image in-place, minimizing memory reallocation + // Return false if the conversion cannot be done in-place. + bool convertInPlace(QImage::Format newFormat, Qt::ImageConversionFlags); #ifndef QT_NO_IMAGE_TEXT QMap<QString, QString> text; diff --git a/src/gui/image/qimagereader.cpp b/src/gui/image/qimagereader.cpp index 93d5cd3..af43e90 100644 --- a/src/gui/image/qimagereader.cpp +++ b/src/gui/image/qimagereader.cpp @@ -301,7 +301,7 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device, if (!handler && !testFormat.isEmpty()) { if (false) { #ifndef QT_NO_IMAGEFORMAT_PNG - } else if (testFormat == "png") { + } else if (testFormat == "png") { handler = new QPngHandler; #endif #ifndef QT_NO_IMAGEFORMAT_BMP diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index 20e4b50..fd2c139 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -1766,6 +1766,9 @@ QPixmap QPixmap::transformed(const QMatrix &matrix, Qt::TransformationMode mode) function returns the actual matrix used for transforming the pixmap. + \note When using the native X11 graphics system, the pixmap + becomes invalid when the QApplication instance is destroyed. + \sa QBitmap, QImage, QImageReader, QImageWriter */ diff --git a/src/gui/image/qpixmap_raster.cpp b/src/gui/image/qpixmap_raster.cpp index 9dc15fc..13c03a1 100644 --- a/src/gui/image/qpixmap_raster.cpp +++ b/src/gui/image/qpixmap_raster.cpp @@ -47,6 +47,9 @@ #include "qbitmap.h" #include "qimage.h" +#include <QBuffer> +#include <QImageReader> +#include <private/qsimd_p.h> #include <private/qwidget_p.h> #include <private/qdrawhelper_p.h> @@ -127,91 +130,26 @@ void QRasterPixmapData::resize(int width, int height) setSerialNumber(image.serialNumber()); } +bool QRasterPixmapData::fromData(const uchar *buffer, uint len, const char *format, + Qt::ImageConversionFlags flags) +{ + QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buffer), len); + QBuffer b(&a); + b.open(QIODevice::ReadOnly); + QImage image = QImageReader(&b, format).read(); + if (image.isNull()) + return false; + + createPixmapForImage(image, flags, /* inplace = */true); + return !isNull(); +} + void QRasterPixmapData::fromImage(const QImage &sourceImage, Qt::ImageConversionFlags flags) { Q_UNUSED(flags); - -#ifdef Q_WS_QWS - QImage::Format format; - if (pixelType() == BitmapType) { - format = QImage::Format_Mono; - } else { - format = QScreen::instance()->pixelFormat(); - if (format == QImage::Format_Invalid) - format = QImage::Format_ARGB32_Premultiplied; - else if (format == QImage::Format_Indexed8) // currently not supported - format = QImage::Format_RGB444; - } - - if (sourceImage.hasAlphaChannel() - && ((flags & Qt::NoOpaqueDetection) - || const_cast<QImage &>(sourceImage).data_ptr()->checkForAlphaPixels())) { - switch (format) { - case QImage::Format_RGB16: - format = QImage::Format_ARGB8565_Premultiplied; - break; - case QImage::Format_RGB666: - format = QImage::Format_ARGB6666_Premultiplied; - break; - case QImage::Format_RGB555: - format = QImage::Format_ARGB8555_Premultiplied; - break; - case QImage::Format_RGB444: - format = QImage::Format_ARGB4444_Premultiplied; - break; - default: - format = QImage::Format_ARGB32_Premultiplied; - break; - } - } else if (format == QImage::Format_Invalid) { - format = QImage::Format_ARGB32_Premultiplied; - } - - image = sourceImage.convertToFormat(format); -#else - if (pixelType() == BitmapType) { - image = sourceImage.convertToFormat(QImage::Format_MonoLSB); - } else { - if (sourceImage.depth() == 1) { - image = sourceImage.hasAlphaChannel() - ? sourceImage.convertToFormat(QImage::Format_ARGB32_Premultiplied) - : sourceImage.convertToFormat(QImage::Format_RGB32); - } else { - - QImage::Format opaqueFormat = QNativeImage::systemFormat(); - QImage::Format alphaFormat = QImage::Format_ARGB32_Premultiplied; - -#ifndef QT_HAVE_NEON - switch (opaqueFormat) { - case QImage::Format_RGB16: - alphaFormat = QImage::Format_ARGB8565_Premultiplied; - break; - default: // We don't care about the others... - break; - } -#endif - - if (!sourceImage.hasAlphaChannel() - || ((flags & Qt::NoOpaqueDetection) == 0 - && !const_cast<QImage &>(sourceImage).data_ptr()->checkForAlphaPixels())) { - image = sourceImage.convertToFormat(opaqueFormat); - } else { - image = sourceImage.convertToFormat(alphaFormat); - } - } - } -#endif - if (image.d) { - w = image.d->width; - h = image.d->height; - d = image.d->depth; - } else { - w = h = d = 0; - } - is_null = (w <= 0 || h <= 0); - - setSerialNumber(image.serialNumber()); + QImage image = sourceImage; + createPixmapForImage(image, flags, /* inplace = */false); } // from qwindowsurface.cpp @@ -240,7 +178,7 @@ void QRasterPixmapData::fill(const QColor &color) if (alpha != 255) { if (!image.hasAlphaChannel()) { QImage::Format toFormat; -#ifndef QT_HAVE_NEON +#if !(defined(QT_HAVE_NEON) || defined(QT_ALWAYS_HAVE_SSE2)) if (image.format() == QImage::Format_RGB16) toFormat = QImage::Format_ARGB8565_Premultiplied; else if (image.format() == QImage::Format_RGB666) @@ -398,6 +336,105 @@ int QRasterPixmapData::metric(QPaintDevice::PaintDeviceMetric metric) const return 0; } +void QRasterPixmapData::createPixmapForImage(QImage &sourceImage, Qt::ImageConversionFlags flags, bool inPlace) +{ + QImage::Format format; +#ifdef Q_WS_QWS + if (pixelType() == BitmapType) { + format = QImage::Format_Mono; + } else { + format = QScreen::instance()->pixelFormat(); + if (format == QImage::Format_Invalid) + format = QImage::Format_ARGB32_Premultiplied; + else if (format == QImage::Format_Indexed8) // currently not supported + format = QImage::Format_RGB444; + } + + if (sourceImage.hasAlphaChannel() + && ((flags & Qt::NoOpaqueDetection) + || const_cast<QImage &>(sourceImage).data_ptr()->checkForAlphaPixels())) { + switch (format) { + case QImage::Format_RGB16: + format = QImage::Format_ARGB8565_Premultiplied; + break; + case QImage::Format_RGB666: + format = QImage::Format_ARGB6666_Premultiplied; + break; + case QImage::Format_RGB555: + format = QImage::Format_ARGB8555_Premultiplied; + break; + case QImage::Format_RGB444: + format = QImage::Format_ARGB4444_Premultiplied; + break; + default: + format = QImage::Format_ARGB32_Premultiplied; + break; + } + } else if (format == QImage::Format_Invalid) { + format = QImage::Format_ARGB32_Premultiplied; + } +#else + if (pixelType() == BitmapType) { + format = QImage::Format_MonoLSB; + } else { + if (sourceImage.depth() == 1) { + format = sourceImage.hasAlphaChannel() + ? QImage::Format_ARGB32_Premultiplied + : QImage::Format_RGB32; + } else { + QImage::Format opaqueFormat = QNativeImage::systemFormat(); + QImage::Format alphaFormat = QImage::Format_ARGB32_Premultiplied; + +#if !defined(QT_HAVE_NEON) && !defined(QT_ALWAYS_HAVE_SSE2) + switch (opaqueFormat) { + case QImage::Format_RGB16: + alphaFormat = QImage::Format_ARGB8565_Premultiplied; + break; + default: // We don't care about the others... + break; + } +#endif + + if (!sourceImage.hasAlphaChannel()) { + format = opaqueFormat; + } else if ((flags & Qt::NoOpaqueDetection) == 0 + && !const_cast<QImage &>(sourceImage).data_ptr()->checkForAlphaPixels()) + { + // image has alpha format but is really opaque, so try to do a + // more efficient conversion + if (sourceImage.format() == QImage::Format_ARGB32 + || sourceImage.format() == QImage::Format_ARGB32_Premultiplied) + { + if (!inPlace) + sourceImage.detach(); + sourceImage.d->format = QImage::Format_RGB32; + } + format = opaqueFormat; + } else { + format = alphaFormat; + } + } + } +#endif + + if (inPlace && sourceImage.d->convertInPlace(format, flags)) { + image = sourceImage; + } else { + image = sourceImage.convertToFormat(format); + } + + if (image.d) { + w = image.d->width; + h = image.d->height; + d = image.d->depth; + } else { + w = h = d = 0; + } + is_null = (w <= 0 || h <= 0); + + setSerialNumber(image.serialNumber()); +} + QImage* QRasterPixmapData::buffer() { return ℑ diff --git a/src/gui/image/qpixmap_raster_p.h b/src/gui/image/qpixmap_raster_p.h index 6cdd3d7..d7e3f85 100644 --- a/src/gui/image/qpixmap_raster_p.h +++ b/src/gui/image/qpixmap_raster_p.h @@ -72,6 +72,7 @@ public: void resize(int width, int height); void fromFile(const QString &filename, Qt::ImageConversionFlags flags); + bool fromData(const uchar *buffer, uint len, const char *format, Qt::ImageConversionFlags flags); void fromImage(const QImage &image, Qt::ImageConversionFlags flags); bool scroll(int dx, int dy, const QRect &rect); @@ -85,6 +86,8 @@ public: protected: int metric(QPaintDevice::PaintDeviceMetric metric) const; + void createPixmapForImage(QImage &sourceImage, Qt::ImageConversionFlags flags, bool inPlace); + void setImage(const QImage &image); QImage image; private: diff --git a/src/gui/image/qpixmap_x11.cpp b/src/gui/image/qpixmap_x11.cpp index 6bebefc..e8dc5ae 100644 --- a/src/gui/image/qpixmap_x11.cpp +++ b/src/gui/image/qpixmap_x11.cpp @@ -1250,10 +1250,8 @@ void QX11PixmapData::release() pengine = 0; if (!X11) { -#ifndef QT_NO_DEBUG - qWarning("~QX11PixmapData(): QPixmap objects must be destroyed before the QApplication" - " object, otherwise the native pixmap object will be leaked."); -#endif + // At this point, the X server will already have freed our resources, + // so there is nothing to do. return; } diff --git a/src/gui/image/qpixmapcache.cpp b/src/gui/image/qpixmapcache.cpp index 7a6a73f..ca21a0c 100644 --- a/src/gui/image/qpixmapcache.cpp +++ b/src/gui/image/qpixmapcache.cpp @@ -196,9 +196,10 @@ public: static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key); QList< QPair<QString,QPixmap> > allPixmaps() const; - void flushDetachedPixmaps(bool nt); + bool flushDetachedPixmaps(bool nt); private: + enum { soon_time = 10000, flush_time = 30000 }; int *keyArray; int theid; int ps; @@ -236,38 +237,44 @@ QPMCache::~QPMCache() cleaning-up, and to not cut down the size of the cache while the cache is in active use. - When the last pixmap has been deleted from the cache, kill the - timer so Qt won't keep the CPU from going into sleep mode. + When the last detached pixmap has been deleted from the cache, kill the + timer so Qt won't keep the CPU from going into sleep mode. Currently + the timer is not restarted when the pixmap becomes unused, but it does + restart once something else is added (i.e. the cache space is actually needed). + + Returns true if any were removed. */ -void QPMCache::flushDetachedPixmaps(bool nt) +bool QPMCache::flushDetachedPixmaps(bool nt) { int mc = maxCost(); setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1); setMaxCost(mc); ps = totalCost(); + bool any = false; QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin(); while (it != cacheKeys.end()) { if (!contains(it.value())) { releaseKey(it.value()); it = cacheKeys.erase(it); + any = true; } else { ++it; } } + + return any; } void QPMCache::timerEvent(QTimerEvent *) { bool nt = totalCost() == ps; - flushDetachedPixmaps(nt); - - if (!size()) { + if (!flushDetachedPixmaps(nt)) { killTimer(theid); theid = 0; } else if (nt != t) { killTimer(theid); - theid = startTimer(nt ? 10000 : 30000); + theid = startTimer(nt ? soon_time : flush_time); t = nt; } } @@ -315,7 +322,7 @@ bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) if (success) { cacheKeys.insert(key, cacheKey); if (!theid) { - theid = startTimer(30000); + theid = startTimer(flush_time); t = false; } } else { @@ -331,7 +338,7 @@ QPixmapCache::Key QPMCache::insert(const QPixmap &pixmap, int cost) bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); if (success) { if (!theid) { - theid = startTimer(30000); + theid = startTimer(flush_time); t = false; } } else { @@ -352,7 +359,7 @@ bool QPMCache::replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); if (success) { if(!theid) { - theid = startTimer(30000); + theid = startTimer(flush_time); t = false; } const_cast<QPixmapCache::Key&>(key) = cacheKey; diff --git a/src/gui/image/qpixmapdata.cpp b/src/gui/image/qpixmapdata.cpp index ea4fe6b..31ca909 100644 --- a/src/gui/image/qpixmapdata.cpp +++ b/src/gui/image/qpixmapdata.cpp @@ -146,7 +146,7 @@ bool QPixmapData::fromData(const uchar *buf, uint len, const char *format, Qt::I void QPixmapData::copy(const QPixmapData *data, const QRect &rect) { - fromImage(data->toImage().copy(rect), Qt::AutoColor); + fromImage(data->toImage().copy(rect), Qt::NoOpaqueDetection); } bool QPixmapData::scroll(int dx, int dy, const QRect &rect) diff --git a/src/gui/image/qpnghandler.cpp b/src/gui/image/qpnghandler.cpp index dd31834..2cf8222 100644 --- a/src/gui/image/qpnghandler.cpp +++ b/src/gui/image/qpnghandler.cpp @@ -892,13 +892,15 @@ QPngHandler::~QPngHandler() bool QPngHandler::canRead() const { - if (d->state == QPngHandlerPrivate::Ready) { - if (!canRead(device())) - return false; + if (d->state == QPngHandlerPrivate::Ready && !canRead(device())) + return false; + + if (d->state != QPngHandlerPrivate::Error) { setFormat("png"); return true; } - return d->state != QPngHandlerPrivate::Error; + + return false; } bool QPngHandler::canRead(QIODevice *device) diff --git a/src/gui/image/qppmhandler.cpp b/src/gui/image/qppmhandler.cpp index cbbbef4..a9e796c 100644 --- a/src/gui/image/qppmhandler.cpp +++ b/src/gui/image/qppmhandler.cpp @@ -409,13 +409,15 @@ bool QPpmHandler::readHeader() bool QPpmHandler::canRead() const { - if (state == Ready) { - if (!canRead(device(), &subType)) - return false; + if (state == Ready && !canRead(device(), &subType)) + return false; + + if (state != Error) { setFormat(subType); return true; } - return state != Error; + + return false; } bool QPpmHandler::canRead(QIODevice *device, QByteArray *subType) diff --git a/src/gui/image/qxbmhandler.cpp b/src/gui/image/qxbmhandler.cpp index 385340a..0dd4e99 100644 --- a/src/gui/image/qxbmhandler.cpp +++ b/src/gui/image/qxbmhandler.cpp @@ -261,13 +261,15 @@ bool QXbmHandler::readHeader() bool QXbmHandler::canRead() const { - if (state == Ready) { - if (!canRead(device())) - return false; + if (state == Ready && !canRead(device())) + return false; + + if (state != Error) { setFormat("xbm"); return true; } - return state != Error; + + return false; } bool QXbmHandler::canRead(QIODevice *device) diff --git a/src/gui/image/qxpmhandler.cpp b/src/gui/image/qxpmhandler.cpp index a475cd0..b97afd3 100644 --- a/src/gui/image/qxpmhandler.cpp +++ b/src/gui/image/qxpmhandler.cpp @@ -1225,11 +1225,15 @@ bool QXpmHandler::readImage(QImage *image) bool QXpmHandler::canRead() const { - if (state == Ready && canRead(device())) { + if (state == Ready && !canRead(device())) + return false; + + if (state != Error) { setFormat("xpm"); return true; } - return state != Error; + + return false; } bool QXpmHandler::canRead(QIODevice *device) |