diff options
Diffstat (limited to 'src/gui/image/qimage.cpp')
-rw-r--r-- | src/gui/image/qimage.cpp | 873 |
1 files changed, 790 insertions, 83 deletions
diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index ebe26b1687..3bbf21320e 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -21,6 +21,7 @@ #include <stdlib.h> #include <limits.h> #include <qpa/qplatformpixmap.h> +#include <private/qcolorspace_p.h> #include <private/qcolortransform_p.h> #include <private/qmemrotate_p.h> #include <private/qimagescale_p.h> @@ -35,8 +36,9 @@ #include <private/qfont_p.h> #if QT_CONFIG(thread) -#include "qsemaphore.h" -#include "qthreadpool.h" +#include <qsemaphore.h> +#include <qthreadpool.h> +#include <private/qthreadpool_p.h> #endif #include <qtgui_tracepoints_p.h> @@ -44,6 +46,7 @@ #include <memory> QT_BEGIN_NAMESPACE +class QCmyk32; using namespace Qt::StringLiterals; @@ -63,6 +66,17 @@ QT_WARNING_DISABLE_MSVC(4723) return QImage(); \ } +Q_TRACE_PREFIX(qtgui, + "#include <qimagereader.h>" +); + +Q_TRACE_METADATA(qtgui, +"ENUM { } QImage::Format;" \ +"FLAGS { } Qt::ImageConversionFlags;" +); + +Q_TRACE_PARAM_REPLACE(Qt::AspectRatioMode, int); +Q_TRACE_PARAM_REPLACE(Qt::TransformationMode, int); static QImage rotated90(const QImage &src); static QImage rotated180(const QImage &src); @@ -70,7 +84,7 @@ static QImage rotated270(const QImage &src); static int next_qimage_serial_number() { - static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(0); + Q_CONSTINIT static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(0); return 1 + serial.fetchAndAddRelaxed(1); } @@ -94,12 +108,12 @@ QImageData::QImageData() Creates a new image data. Returns \nullptr if invalid parameters are give or anything else failed. */ -QImageData * QImageData::create(const QSize &size, QImage::Format format) +QImageData * Q_TRACE_INSTRUMENT(qtgui) QImageData::create(const QSize &size, QImage::Format format) { if (size.isEmpty() || format <= QImage::Format_Invalid || format >= QImage::NImageFormats) return nullptr; // invalid parameter(s) - Q_TRACE_SCOPE(QImageData_create, size, static_cast<int>(format)); + Q_TRACE_SCOPE(QImageData_create, size, format); int width = size.width(); int height = size.height(); @@ -292,6 +306,7 @@ bool QImageData::checkForAlphaPixels() const case QImage::Format_RGBX64: case QImage::Format_RGBX16FPx4: case QImage::Format_RGBX32FPx4: + case QImage::Format_CMYK8888: break; case QImage::Format_Invalid: case QImage::NImageFormats: @@ -348,7 +363,7 @@ bool QImageData::checkForAlphaPixels() const refer to the \l{How to Create Qt Plugins}{Plugin HowTo}. \warning Painting on a QImage with the format - QImage::Format_Indexed8 is not supported. + QImage::Format_Indexed8 or QImage::Format_CMYK8888 is not supported. \tableofcontents @@ -604,8 +619,8 @@ bool QImageData::checkForAlphaPixels() const \endtable - \sa QImageReader, QImageWriter, QPixmap, QSvgRenderer, {Image Composition Example}, - {Image Viewer Example}, {Scribble Example}, {Pixelator Example} + \sa QImageReader, QImageWriter, QPixmap, QSvgRenderer, + {Image Composition Example}, {Scribble Example} */ /*! @@ -698,40 +713,62 @@ bool QImageData::checkForAlphaPixels() const The unused bits are always zero. \value Format_ARGB4444_Premultiplied The image is stored using a premultiplied 16-bit ARGB format (4-4-4-4). - \value Format_RGBX8888 The image is stored using a 32-bit byte-ordered RGB(x) format (8-8-8-8). - This is the same as the Format_RGBA8888 except alpha must always be 255. (added in Qt 5.2) - \value Format_RGBA8888 The image is stored using a 32-bit byte-ordered RGBA format (8-8-8-8). + \value [since 5.2] + Format_RGBX8888 The image is stored using a 32-bit byte-ordered RGB(x) format (8-8-8-8). + This is the same as the Format_RGBA8888 except alpha must always be 255. + \value [since 5.2] + Format_RGBA8888 The image is stored using a 32-bit byte-ordered RGBA format (8-8-8-8). Unlike ARGB32 this is a byte-ordered format, which means the 32bit encoding differs between big endian and little endian architectures, being respectively (0xRRGGBBAA) and (0xAABBGGRR). The order of the colors - is the same on any architecture if read as bytes 0xRR,0xGG,0xBB,0xAA. (added in Qt 5.2) - \value Format_RGBA8888_Premultiplied The image is stored using a - premultiplied 32-bit byte-ordered RGBA format (8-8-8-8). (added in Qt 5.2) - \value Format_BGR30 The image is stored using a 32-bit BGR format (x-10-10-10). (added in Qt 5.4) - \value Format_A2BGR30_Premultiplied The image is stored using a 32-bit premultiplied ABGR format (2-10-10-10). (added in Qt 5.4) - \value Format_RGB30 The image is stored using a 32-bit RGB format (x-10-10-10). (added in Qt 5.4) - \value Format_A2RGB30_Premultiplied The image is stored using a 32-bit premultiplied ARGB format (2-10-10-10). (added in Qt 5.4) - \value Format_Alpha8 The image is stored using an 8-bit alpha only format. (added in Qt 5.5) - \value Format_Grayscale8 The image is stored using an 8-bit grayscale format. (added in Qt 5.5) - \value Format_Grayscale16 The image is stored using an 16-bit grayscale format. (added in Qt 5.13) - \value Format_RGBX64 The image is stored using a 64-bit halfword-ordered RGB(x) format (16-16-16-16). - This is the same as the Format_RGBA64 except alpha must always be 65535. (added in Qt 5.12) - \value Format_RGBA64 The image is stored using a 64-bit halfword-ordered RGBA format (16-16-16-16). (added in Qt 5.12) - \value Format_RGBA64_Premultiplied The image is stored using a premultiplied 64-bit halfword-ordered - RGBA format (16-16-16-16). (added in Qt 5.12) - \value Format_BGR888 The image is stored using a 24-bit BGR format. (added in Qt 5.14) - \value Format_RGBX16FPx4 The image is stored using a 4 16-bit halfword floating point RGBx format (16FP-16FP-16FP-16FP). - This is the same as the Format_RGBA16FPx4 except alpha must always be 1.0. (added in Qt 6.2) - \value Format_RGBA16FPx4 The image is stored using a 4 16-bit halfword floating point RGBA format (16FP-16FP-16FP-16FP). (added in Qt 6.2) - \value Format_RGBA16FPx4_Premultiplied The image is stored using a premultiplied 4 16-bit halfword floating point - RGBA format (16FP-16FP-16FP-16FP). (added in Qt 6.2) - \value Format_RGBX32FPx4 The image is stored using a 4 32-bit floating point RGBx format (32FP-32FP-32FP-32FP). - This is the same as the Format_RGBA32FPx4 except alpha must always be 1.0. (added in Qt 6.2) - \value Format_RGBA32FPx4 The image is stored using a 4 32-bit floating point RGBA format (32FP-32FP-32FP-32FP). (added in Qt 6.2) - \value Format_RGBA32FPx4_Premultiplied The image is stored using a premultiplied 4 32-bit floating point - RGBA format (32FP-32FP-32FP-32FP). (added in Qt 6.2) - - \note Drawing into a QImage with QImage::Format_Indexed8 is not + is the same on any architecture if read as bytes 0xRR,0xGG,0xBB,0xAA. + \value [since 5.2] + Format_RGBA8888_Premultiplied The image is stored using a + premultiplied 32-bit byte-ordered RGBA format (8-8-8-8). + \value [since 5.4] + Format_BGR30 The image is stored using a 32-bit BGR format (x-10-10-10). + \value [since 5.4] + Format_A2BGR30_Premultiplied The image is stored using a 32-bit premultiplied ABGR format (2-10-10-10). + \value [since 5.4] + Format_RGB30 The image is stored using a 32-bit RGB format (x-10-10-10). + \value [since 5.4] + Format_A2RGB30_Premultiplied The image is stored using a 32-bit premultiplied ARGB format (2-10-10-10). + \value [since 5.5] + Format_Alpha8 The image is stored using an 8-bit alpha only format. + \value [since 5.5] + Format_Grayscale8 The image is stored using an 8-bit grayscale format. + \value [since 5.13] + Format_Grayscale16 The image is stored using an 16-bit grayscale format. + \value [since 5.12] + Format_RGBX64 The image is stored using a 64-bit halfword-ordered RGB(x) format (16-16-16-16). + This is the same as the Format_RGBA64 except alpha must always be 65535. + \value [since 5.12] + Format_RGBA64 The image is stored using a 64-bit halfword-ordered RGBA format (16-16-16-16). + \value [since 5.12] + Format_RGBA64_Premultiplied The image is stored using a premultiplied 64-bit halfword-ordered + RGBA format (16-16-16-16). + \value [since 5.14] + Format_BGR888 The image is stored using a 24-bit BGR format. + \value [since 6.2] + Format_RGBX16FPx4 The image is stored using a four 16-bit halfword floating point RGBx format (16FP-16FP-16FP-16FP). + This is the same as the Format_RGBA16FPx4 except alpha must always be 1.0. + \value [since 6.2] + Format_RGBA16FPx4 The image is stored using a four 16-bit halfword floating point RGBA format (16FP-16FP-16FP-16FP). + \value [since 6.2] + Format_RGBA16FPx4_Premultiplied The image is stored using a premultiplied four 16-bit halfword floating point + RGBA format (16FP-16FP-16FP-16FP). + \value [since 6.2] + Format_RGBX32FPx4 The image is stored using a four 32-bit floating point RGBx format (32FP-32FP-32FP-32FP). + This is the same as the Format_RGBA32FPx4 except alpha must always be 1.0. + \value [since 6.2] + Format_RGBA32FPx4 The image is stored using a four 32-bit floating point RGBA format (32FP-32FP-32FP-32FP). + \value [since 6.2] + Format_RGBA32FPx4_Premultiplied The image is stored using a premultiplied four 32-bit floating point + RGBA format (32FP-32FP-32FP-32FP). + \value [since 6.8] + Format_CMYK8888 The image is stored using a 32-bit byte-ordered CMYK format. + + \note Drawing into a QImage with format QImage::Format_Indexed8 or QImage::Format_CMYK8888 is not supported. \note Avoid most rendering directly to most of these formats using QPainter. Rendering @@ -807,7 +844,7 @@ QImageData *QImageData::create(uchar *data, int width, int height, qsizetype bp // recalculate the total with this value params.bytesPerLine = bpl; - if (mul_overflow<qsizetype>(bpl, height, ¶ms.totalSize)) + if (qMulOverflow<qsizetype>(bpl, height, ¶ms.totalSize)) return nullptr; } @@ -1135,9 +1172,10 @@ static void copyPhysicalMetadata(QImageData *dst, const QImageData *src) static void copyMetadata(QImageData *dst, const QImageData *src) { - // Doesn't copy colortable and alpha_clut, or offset. + // Doesn't copy colortable and alpha_clut. copyPhysicalMetadata(dst, src); dst->text = src->text; + dst->offset = src->offset; dst->colorSpace = src->colorSpace; } @@ -1182,7 +1220,7 @@ static void copyMetadata(QImage *dst, const QImage &src) \sa QImage() */ -QImage QImage::copy(const QRect& r) const +QImage Q_TRACE_INSTRUMENT(qtgui) QImage::copy(const QRect& r) const { Q_TRACE_SCOPE(QImage_copy, r); if (!d) @@ -1202,7 +1240,6 @@ QImage QImage::copy(const QRect& r) const } else memcpy(image.bits(), bits(), d->nbytes); image.d->colortable = d->colortable; - image.d->offset = d->offset; image.d->has_alpha_clut = d->has_alpha_clut; copyMetadata(image.d, d); return image; @@ -1291,7 +1328,6 @@ QImage QImage::copy(const QRect& r) const } copyMetadata(image.d, d); - image.d->offset = offset(); image.d->has_alpha_clut = d->has_alpha_clut; return image; } @@ -1487,12 +1523,14 @@ void QImage::setDevicePixelRatio(qreal scaleFactor) } /*! - Returns the size of the pixmap in device independent pixels. + Returns the size of the image in device independent pixels. - This value should be used when using the pixmap size in user interface + This value should be used when using the image size in user interface size calculations. - The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio(), + The return value is equivalent to image.size() / image.devicePixelRatio(). + + \since 6.2 */ QSizeF QImage::deviceIndependentSize() const { @@ -1910,7 +1948,7 @@ void QImage::fill(const QColor &color) if (!hasAlphaChannel()) a = 1.0f; if (depth() == 64) { - QRgbaFloat16 c16{r, g, b, a}; + QRgbaFloat16 c16{qfloat16(r), qfloat16(g), qfloat16(b), qfloat16(a)}; if (d->format == Format_RGBA16FPx4_Premultiplied) c16 = c16.premultiplied(); qt_rectfill<QRgbaFloat16>(reinterpret_cast<QRgbaFloat16 *>(d->data), c16, @@ -1996,11 +2034,11 @@ void QImage::invertPixels(InvertMode mode) qfloat16 *p = reinterpret_cast<qfloat16 *>(d->data); qfloat16 *end = reinterpret_cast<qfloat16 *>(d->data + d->nbytes); while (p < end) { - p[0] = 1.0f - p[0]; - p[1] = 1.0f - p[1]; - p[2] = 1.0f - p[2]; + p[0] = qfloat16(1) - p[0]; + p[1] = qfloat16(1) - p[1]; + p[2] = qfloat16(1) - p[2]; if (mode == InvertRgba) - p[3] = 1.0f - p[3]; + p[3] = qfloat16(1) - p[3]; p += 4; } } else if (format() >= QImage::Format_RGBX32FPx4 && format() <= QImage::Format_RGBA32FPx4_Premultiplied) { @@ -2189,7 +2227,6 @@ QImage QImage::convertToFormat_helper(Format format, Qt::ImageConversionFlags fl QIMAGE_SANITYCHECK_MEMORY(image); - image.d->offset = offset(); copyMetadata(image.d, d); converter(image.d, d, flags); @@ -2619,6 +2656,9 @@ void QImage::setPixel(int x, int y, uint index_or_rgb) case Format_A2RGB30_Premultiplied: ((uint *)s)[x] = qConvertArgb32ToA2rgb30<PixelOrderRGB>(index_or_rgb); return; + case Format_RGBX64: + ((QRgba64 *)s)[x] = QRgba64::fromArgb32(index_or_rgb | 0xff000000); + return; case Format_RGBA64: case Format_RGBA64_Premultiplied: ((QRgba64 *)s)[x] = QRgba64::fromArgb32(index_or_rgb); @@ -2803,7 +2843,7 @@ void QImage::setPixelColor(int x, int y, const QColor &color) color.getRgbF(&r, &g, &b, &a); if (d->format == Format_RGBX16FPx4) a = 1.0f; - QRgbaFloat16 c16f{r, g, b, a}; + QRgbaFloat16 c16f{qfloat16(r), qfloat16(g), qfloat16(b), qfloat16(a)}; if (d->format == Format_RGBA16FPx4_Premultiplied) c16f = c16f.premultiplied(); ((QRgbaFloat16 *)s)[x] = c16f; @@ -2978,7 +3018,7 @@ bool QImage::isGrayscale() const \sa isNull(), {QImage#Image Transformations}{Image Transformations} */ -QImage QImage::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const +QImage Q_TRACE_INSTRUMENT(qtgui) QImage::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const { if (!d) { qWarning("QImage::scaled: Image is a null image"); @@ -3015,7 +3055,7 @@ QImage QImage::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::Transf \sa {QImage#Image Transformations}{Image Transformations} */ -QImage QImage::scaledToWidth(int w, Qt::TransformationMode mode) const +QImage Q_TRACE_INSTRUMENT(qtgui) QImage::scaledToWidth(int w, Qt::TransformationMode mode) const { if (!d) { qWarning("QImage::scaleWidth: Image is a null image"); @@ -3045,7 +3085,7 @@ QImage QImage::scaledToWidth(int w, Qt::TransformationMode mode) const \sa {QImage#Image Transformations}{Image Transformations} */ -QImage QImage::scaledToHeight(int h, Qt::TransformationMode mode) const +QImage Q_TRACE_INSTRUMENT(qtgui) QImage::scaledToHeight(int h, Qt::TransformationMode mode) const { if (!d) { qWarning("QImage::scaleHeight: Image is a null image"); @@ -3078,7 +3118,7 @@ QImage QImage::scaledToHeight(int h, Qt::TransformationMode mode) const \sa createHeuristicMask(), {QImage#Image Transformations}{Image Transformations} */ -QImage QImage::createAlphaMask(Qt::ImageConversionFlags flags) const +QImage Q_TRACE_INSTRUMENT(qtgui) QImage::createAlphaMask(Qt::ImageConversionFlags flags) const { if (!d || d->format == QImage::Format_RGB32) return QImage(); @@ -3530,7 +3570,7 @@ static inline void rgbSwapped_generic(int width, int height, const QImage *src, /*! \internal */ -QImage QImage::rgbSwapped_helper() const +QImage Q_TRACE_INSTRUMENT(qtgui) QImage::rgbSwapped_helper() const { if (isNull()) return *this; @@ -3886,10 +3926,15 @@ bool QImage::save(QIODevice* device, const char* format, int quality) const bool QImageData::doImageIO(const QImage *image, QImageWriter *writer, int quality) const { if (quality > 100 || quality < -1) - qWarning("QPixmap::save: Quality out of range [-1, 100]"); + qWarning("QImage::save: Quality out of range [-1, 100]"); if (quality >= 0) writer->setQuality(qMin(quality,100)); - return writer->write(*image); + const bool result = writer->write(*image); +#ifdef QT_DEBUG + if (!result) + qWarning("QImage::save: failed to write image - %s", qPrintable(writer->errorString())); +#endif + return result; } /***************************************************************************** @@ -4672,6 +4717,8 @@ QImage QImage::smoothScaled(int w, int h) const static QImage rotated90(const QImage &image) { QImage out(image.height(), image.width(), image.format()); + if (out.isNull()) + return out; copyMetadata(&out, image); if (image.colorCount() > 0) out.setColorTable(image.colorTable()); @@ -4700,6 +4747,8 @@ static QImage rotated180(const QImage &image) return image.mirrored(true, true); QImage out(image.width(), image.height(), image.format()); + if (out.isNull()) + return out; copyMetadata(&out, image); if (image.colorCount() > 0) out.setColorTable(image.colorTable()); @@ -4712,6 +4761,8 @@ static QImage rotated180(const QImage &image) static QImage rotated270(const QImage &image) { QImage out(image.height(), image.width(), image.format()); + if (out.isNull()) + return out; copyMetadata(&out, image); if (image.colorCount() > 0) out.setColorTable(image.colorTable()); @@ -4757,12 +4808,15 @@ static QImage rotated270(const QImage &image) Transformations} */ -QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode ) const +QImage Q_TRACE_INSTRUMENT(qtgui) QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode ) const { if (!d) return QImage(); - Q_TRACE_SCOPE(QImage_transformed, matrix, mode); + Q_TRACE_PARAM_REPLACE(const QTransform &, double[9]); + Q_TRACE_SCOPE(QImage_transformed, QList<double>({matrix.m11(), matrix.m12(), matrix.m13(), + matrix.m21(), matrix.m22(), matrix.m23(), + matrix.m31(), matrix.m32(), matrix.m33()}).data(), mode); // source image data const int ws = width(); @@ -4834,14 +4888,24 @@ QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode || (ws * hs) >= (1<<20) #endif ) { + QImage scaledImage; if (mat.m11() < 0.0F && mat.m22() < 0.0F) { // horizontal/vertical flip - return smoothScaled(wd, hd).mirrored(true, true).convertToFormat(format()); + scaledImage = smoothScaled(wd, hd).mirrored(true, true); } else if (mat.m11() < 0.0F) { // horizontal flip - return smoothScaled(wd, hd).mirrored(true, false).convertToFormat(format()); + scaledImage = smoothScaled(wd, hd).mirrored(true, false); } else if (mat.m22() < 0.0F) { // vertical flip - return smoothScaled(wd, hd).mirrored(false, true).convertToFormat(format()); + scaledImage = smoothScaled(wd, hd).mirrored(false, true); } else { // no flipping - return smoothScaled(wd, hd).convertToFormat(format()); + scaledImage = smoothScaled(wd, hd); + } + + switch (format()) { + case QImage::Format_Mono: + case QImage::Format_MonoLSB: + case QImage::Format_Indexed8: + return scaledImage; + default: + return scaledImage.convertToFormat(format()); } } } @@ -4883,7 +4947,14 @@ QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode if (target_format >= QImage::Format_RGB32) { // Prevent QPainter from applying devicePixelRatio corrections - const QImage sImage = (devicePixelRatio() != 1) ? QImage(constBits(), width(), height(), format()) : *this; + QImage sImage = (devicePixelRatio() != 1) ? QImage(constBits(), width(), height(), format()) : *this; + if (sImage.d != d + && (d->format == QImage::Format_MonoLSB + || d->format == QImage::Format_Mono + || d->format == QImage::Format_Indexed8)) { + sImage.d->colortable = d->colortable; + sImage.d->has_alpha_clut = d->has_alpha_clut; + } Q_ASSERT(sImage.devicePixelRatio() == 1); Q_ASSERT(sImage.devicePixelRatio() == dImage.devicePixelRatio()); @@ -4953,6 +5024,9 @@ void QImage::setColorSpace(const QColorSpace &colorSpace) return; if (d->colorSpace == colorSpace) return; + if (colorSpace.isValid() && !qt_compatibleColorModel(pixelFormat().colorModel(), colorSpace.colorModel())) + return; + detachMetadata(false); if (d) d->colorSpace = colorSpace; @@ -4965,21 +5039,60 @@ void QImage::setColorSpace(const QColorSpace &colorSpace) If the image has no valid color space, the method does nothing. + \note If \a colorSpace is not compatible with the current format, the image + will be converted to one that is. + \sa convertedToColorSpace(), setColorSpace() */ void QImage::convertToColorSpace(const QColorSpace &colorSpace) { - if (!d) - return; - if (!d->colorSpace.isValid()) + if (!d || !d->colorSpace.isValid()) return; - if (!colorSpace.isValid()) { + if (!colorSpace.isValidTarget()) { qWarning() << "QImage::convertToColorSpace: Output colorspace is not valid"; return; } if (d->colorSpace == colorSpace) return; + if (!qt_compatibleColorModel(pixelFormat().colorModel(), colorSpace.colorModel())) { + *this = convertedToColorSpace(colorSpace); + return; + } applyColorTransform(d->colorSpace.transformationToColorSpace(colorSpace)); + if (d->ref.loadRelaxed() != 1) + detachMetadata(false); + d->colorSpace = colorSpace; +} + +/*! + \since 6.8 + + Converts the image to \a colorSpace and \a format. + + If the image has no valid color space, the method does nothing, + nor if the color space is not compatible with with the format. + + The specified image conversion \a flags control how the image data + is handled during the format conversion process. + + \sa convertedToColorSpace(), setColorSpace() +*/ +void QImage::convertToColorSpace(const QColorSpace &colorSpace, QImage::Format format, Qt::ImageConversionFlags flags) +{ + if (!d || !d->colorSpace.isValid()) + return; + if (!colorSpace.isValidTarget()) { + qWarning() << "QImage::convertToColorSpace: Output colorspace is not valid"; + return; + } + if (!qt_compatibleColorModel(toPixelFormat(format).colorModel(), colorSpace.colorModel())) { + qWarning() << "QImage::convertToColorSpace: Color space is not compatible with format"; + return; + } + + if (d->colorSpace == colorSpace) + return convertTo(format, flags); + applyColorTransform(d->colorSpace.transformationToColorSpace(colorSpace), format, flags); d->colorSpace = colorSpace; } @@ -4990,16 +5103,56 @@ void QImage::convertToColorSpace(const QColorSpace &colorSpace) If the image has no valid color space, a null QImage is returned. - \sa convertToColorSpace() + \note If \a colorSpace is not compatible with the current format, + the returned image will also be converted to a format this is. + For more control over returned image format, see the three argument + overload of this method. + + \sa convertToColorSpace(), colorTransformed() */ QImage QImage::convertedToColorSpace(const QColorSpace &colorSpace) const { - if (!d || !d->colorSpace.isValid() || !colorSpace.isValid()) + if (!d || !d->colorSpace.isValid()) return QImage(); + if (!colorSpace.isValidTarget()) { + qWarning() << "QImage::convertedToColorSpace: Output colorspace is not valid"; + return QImage(); + } if (d->colorSpace == colorSpace) return *this; - QImage image = copy(); - image.convertToColorSpace(colorSpace); + QImage image = colorTransformed(d->colorSpace.transformationToColorSpace(colorSpace)); + image.setColorSpace(colorSpace); + return image; +} + +/*! + \since 6.8 + + Returns the image converted to \a colorSpace and \a format. + + If the image has no valid color space, a null QImage is returned. + + The specified image conversion \a flags control how the image data + is handled during the format conversion process. + + \sa colorTransformed() +*/ +QImage QImage::convertedToColorSpace(const QColorSpace &colorSpace, QImage::Format format, Qt::ImageConversionFlags flags) const +{ + if (!d || !d->colorSpace.isValid()) + return QImage(); + if (!colorSpace.isValidTarget()) { + qWarning() << "QImage::convertedToColorSpace: Output colorspace is not valid"; + return QImage(); + } + if (!qt_compatibleColorModel(toPixelFormat(format).colorModel(), colorSpace.colorModel())) { + qWarning() << "QImage::convertedToColorSpace: Color space is not compatible with format"; + return QImage(); + } + if (d->colorSpace == colorSpace) + return convertedTo(format, flags); + QImage image = colorTransformed(d->colorSpace.transformationToColorSpace(colorSpace), format, flags); + image.setColorSpace(colorSpace); return image; } @@ -5024,6 +5177,13 @@ void QImage::applyColorTransform(const QColorTransform &transform) { if (transform.isIdentity()) return; + + if (!qt_compatibleColorModel(pixelFormat().colorModel(), QColorTransformPrivate::get(transform)->colorSpaceIn->colorModel) || + !qt_compatibleColorModel(pixelFormat().colorModel(), QColorTransformPrivate::get(transform)->colorSpaceOut->colorModel)) { + qWarning() << "QImage::applyColorTransform can not apply format switching transform without switching format"; + return; + } + detach(); if (!d) return; @@ -5042,7 +5202,8 @@ void QImage::applyColorTransform(const QColorTransform &transform) && oldFormat != QImage::Format_RGBA64_Premultiplied) convertTo(QImage::Format_RGBA64); } else if (oldFormat != QImage::Format_ARGB32 && oldFormat != QImage::Format_RGB32 - && oldFormat != QImage::Format_ARGB32_Premultiplied) { + && oldFormat != QImage::Format_ARGB32_Premultiplied && oldFormat != QImage::Format_CMYK8888 + && oldFormat != QImage::Format_Grayscale8 && oldFormat != QImage::Format_Grayscale16) { if (hasAlphaChannel()) convertTo(QImage::Format_ARGB32); else @@ -5056,7 +5217,10 @@ void QImage::applyColorTransform(const QColorTransform &transform) case Format_RGBA32FPx4_Premultiplied: flags = QColorTransformPrivate::Premultiplied; break; + case Format_Grayscale8: + case Format_Grayscale16: case Format_RGB32: + case Format_CMYK8888: case Format_RGBX64: case Format_RGBX32FPx4: flags = QColorTransformPrivate::InputOpaque; @@ -5071,7 +5235,21 @@ void QImage::applyColorTransform(const QColorTransform &transform) std::function<void(int,int)> transformSegment; - if (qt_fpColorPrecision(format())) { + if (format() == Format_Grayscale8) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + uint8_t *scanline = reinterpret_cast<uint8_t *>(d->data + y * d->bytes_per_line); + QColorTransformPrivate::get(transform)->applyGray(scanline, scanline, width(), flags); + } + }; + } else if (format() == Format_Grayscale16) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + uint16_t *scanline = reinterpret_cast<uint16_t *>(d->data + y * d->bytes_per_line); + QColorTransformPrivate::get(transform)->applyGray(scanline, scanline, width(), flags); + } + }; + } else if (qt_fpColorPrecision(format())) { transformSegment = [&](int yStart, int yEnd) { for (int y = yStart; y < yEnd; ++y) { QRgbaFloat32 *scanline = reinterpret_cast<QRgbaFloat32 *>(d->data + y * d->bytes_per_line); @@ -5085,6 +5263,13 @@ void QImage::applyColorTransform(const QColorTransform &transform) QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags); } }; + } else if (oldFormat == QImage::Format_CMYK8888) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + QCmyk32 *scanline = reinterpret_cast<QCmyk32 *>(d->data + y * d->bytes_per_line); + QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags); + } + }; } else { transformSegment = [&](int yStart, int yEnd) { for (int y = yStart; y < yEnd; ++y) { @@ -5097,7 +5282,7 @@ void QImage::applyColorTransform(const QColorTransform &transform) #if QT_CONFIG(thread) && !defined(Q_OS_WASM) int segments = (qsizetype(width()) * height()) >> 16; segments = std::min(segments, height()); - QThreadPool *threadPool = QThreadPool::globalInstance(); + QThreadPool *threadPool = QThreadPoolPrivate::qtGuiInstance(); if (segments > 1 && threadPool && !threadPool->contains(QThread::currentThread())) { QSemaphore semaphore; int y = 0; @@ -5119,23 +5304,497 @@ void QImage::applyColorTransform(const QColorTransform &transform) } /*! + \since 6.8 + + Applies the color transformation \a transform to all pixels in the image, and converts the format of the image to \a toFormat. + + The specified image conversion \a flags control how the image data + is handled during the format conversion process. +*/ +void QImage::applyColorTransform(const QColorTransform &transform, QImage::Format toFormat, Qt::ImageConversionFlags flags) +{ + if (!d) + return; + if (transform.isIdentity()) + return convertTo(toFormat, flags); + + *this = colorTransformed(transform, toFormat, flags); +} + +/*! \since 6.4 Returns the image color transformed using \a transform on all pixels in the image. + \note If \a transform has a source color space which is incompatible with the format of this image, + returns a null QImage. If \a transform has a target color space which is incompatible with the format + of this image, the image will also be converted to a compatible format. For more control about the + choice of the target pixel format, see the three argument overload of this method. + \sa applyColorTransform() */ QImage QImage::colorTransformed(const QColorTransform &transform) const & { - if (!d || !d->colorSpace.isValid()) + if (!d) return QImage(); if (transform.isIdentity()) return *this; + + QColorSpace::ColorModel inColorModel = QColorTransformPrivate::get(transform)->colorSpaceIn->colorModel; + QColorSpace::ColorModel outColorModel = QColorTransformPrivate::get(transform)->colorSpaceOut->colorModel; + if (!qt_compatibleColorModel(pixelFormat().colorModel(), inColorModel)) { + qWarning() << "QImage::colorTransformed: Invalid input color space for transform"; + return QImage(); + } + if (!qt_compatibleColorModel(pixelFormat().colorModel(), outColorModel)) { + // All model switching transforms are opaque in at least one end. + switch (outColorModel) { + case QColorSpace::ColorModel::Rgb: + return colorTransformed(transform, qt_highColorPrecision(format(), true) ? QImage::Format_RGBX64 : QImage::Format_RGB32); + case QColorSpace::ColorModel::Gray: + return colorTransformed(transform, qt_highColorPrecision(format(), true) ? QImage::Format_Grayscale16 : QImage::Format_Grayscale8); + case QColorSpace::ColorModel::Cmyk: + return colorTransformed(transform, QImage::Format_CMYK8888); + case QColorSpace::ColorModel::Undefined: + break; + } + return QImage(); + } + QImage image = copy(); image.applyColorTransform(transform); return image; } +static bool isRgb32Data(QImage::Format f) +{ + switch (f) { + case QImage::Format_RGB32: + case QImage::Format_ARGB32: + case QImage::Format_ARGB32_Premultiplied: + return true; + default: + break; + } + return false; +} + +static bool isRgb64Data(QImage::Format f) +{ + switch (f) { + case QImage::Format_RGBX64: + case QImage::Format_RGBA64: + case QImage::Format_RGBA64_Premultiplied: + return true; + default: + break; + } + return false; +} + +static bool isRgb32fpx4Data(QImage::Format f) +{ + switch (f) { + case QImage::Format_RGBX32FPx4: + case QImage::Format_RGBA32FPx4: + case QImage::Format_RGBA32FPx4_Premultiplied: + return true; + default: + break; + } + return false; +} + +/*! + \since 6.8 + + Returns the image color transformed using \a transform on all pixels in the image, returning an image of format \a toFormat. + + The specified image conversion \a flags control how the image data + is handled during the format conversion process. + + \note If \a transform has a source color space which is incompatible with the format of this image, + or a target color space that is incompatible with \a toFormat, returns a null QImage. + + \sa applyColorTransform() +*/ +QImage QImage::colorTransformed(const QColorTransform &transform, QImage::Format toFormat, Qt::ImageConversionFlags flags) const & +{ + if (!d) + return QImage(); + if (toFormat == QImage::Format_Invalid) + toFormat = format(); + if (transform.isIdentity()) + return convertedTo(toFormat, flags); + + QColorSpace::ColorModel inColorModel = QColorTransformPrivate::get(transform)->colorSpaceIn->colorModel; + QColorSpace::ColorModel outColorModel = QColorTransformPrivate::get(transform)->colorSpaceOut->colorModel; + if (!qt_compatibleColorModel(pixelFormat().colorModel(), inColorModel)) { + qWarning() << "QImage::colorTransformed: Invalid input color space for transform"; + return QImage(); + } + if (!qt_compatibleColorModel(toPixelFormat(toFormat).colorModel(), outColorModel)) { + qWarning() << "QImage::colorTransformed: Invalid output color space for transform"; + return QImage(); + } + + QImage fromImage = *this; + + QImage::Format tmpFormat = toFormat; + switch (toFormat) { + case QImage::Format_RGB32: + case QImage::Format_ARGB32: + case QImage::Format_ARGB32_Premultiplied: + case QImage::Format_RGBX32FPx4: + case QImage::Format_RGBA32FPx4: + case QImage::Format_RGBA32FPx4_Premultiplied: + case QImage::Format_RGBX64: + case QImage::Format_RGBA64: + case QImage::Format_RGBA64_Premultiplied: + case QImage::Format_Grayscale8: + case QImage::Format_Grayscale16: + case QImage::Format_CMYK8888: + // can be output natively + break; + case QImage::Format_RGB16: + case QImage::Format_RGB444: + case QImage::Format_RGB555: + case QImage::Format_RGB666: + case QImage::Format_RGB888: + case QImage::Format_BGR888: + case QImage::Format_RGBX8888: + tmpFormat = QImage::Format_RGB32; + break; + case QImage::Format_Mono: + case QImage::Format_MonoLSB: + case QImage::Format_Indexed8: + case QImage::Format_ARGB8565_Premultiplied: + case QImage::Format_ARGB6666_Premultiplied: + case QImage::Format_ARGB8555_Premultiplied: + case QImage::Format_ARGB4444_Premultiplied: + case QImage::Format_RGBA8888: + case QImage::Format_RGBA8888_Premultiplied: + tmpFormat = QImage::Format_ARGB32; + break; + case QImage::Format_BGR30: + case QImage::Format_RGB30: + tmpFormat = QImage::Format_RGBX64; + break; + case QImage::Format_A2BGR30_Premultiplied: + case QImage::Format_A2RGB30_Premultiplied: + tmpFormat = QImage::Format_RGBA64; + break; + case QImage::Format_RGBX16FPx4: + case QImage::Format_RGBA16FPx4: + case QImage::Format_RGBA16FPx4_Premultiplied: + tmpFormat = QImage::Format_RGBA32FPx4; + break; + case QImage::Format_Alpha8: + return convertedTo(QImage::Format_Alpha8); + case QImage::Format_Invalid: + case QImage::NImageFormats: + Q_UNREACHABLE(); + break; + } + QColorSpace::ColorModel inColorData = qt_csColorData(pixelFormat().colorModel()); + QColorSpace::ColorModel outColorData = qt_csColorData(toPixelFormat(toFormat).colorModel()); + // Ensure only precision increasing transforms + if (inColorData != outColorData) { + if (fromImage.format() == QImage::Format_Grayscale8 && outColorData == QColorSpace::ColorModel::Rgb) + tmpFormat = QImage::Format_RGB32; + else if (tmpFormat == QImage::Format_Grayscale8 && qt_highColorPrecision(fromImage.format())) + tmpFormat = QImage::Format_Grayscale16; + else if (fromImage.format() == QImage::Format_Grayscale16 && outColorData == QColorSpace::ColorModel::Rgb) + tmpFormat = QImage::Format_RGBX64; + } else { + if (tmpFormat == QImage::Format_Grayscale8 && fromImage.format() == QImage::Format_Grayscale16) + tmpFormat = QImage::Format_Grayscale16; + else if (qt_fpColorPrecision(fromImage.format()) && !qt_fpColorPrecision(tmpFormat)) + tmpFormat = QImage::Format_RGBA32FPx4; + else if (isRgb32Data(tmpFormat) && qt_highColorPrecision(fromImage.format(), true)) + tmpFormat = QImage::Format_RGBA64; + } + + QImage toImage(size(), tmpFormat); + copyMetadata(&toImage, *this); + + std::function<void(int, int)> transformSegment; + QColorTransformPrivate::TransformFlags transFlags = QColorTransformPrivate::Unpremultiplied; + + if (inColorData != outColorData) { + // Needs color model switching transform + if (inColorData == QColorSpace::ColorModel::Gray && outColorData == QColorSpace::ColorModel::Rgb) { + // Gray -> RGB + if (format() == QImage::Format_Grayscale8) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const quint8 *in_scanline = reinterpret_cast<const quint8 *>(d->data + y * d->bytes_per_line); + QRgb *out_scanline = reinterpret_cast<QRgb *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyGray(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } else { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const quint16 *in_scanline = reinterpret_cast<const quint16 *>(d->data + y * d->bytes_per_line); + QRgba64 *out_scanline = reinterpret_cast<QRgba64 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyGray(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } + } else if (inColorData == QColorSpace::ColorModel::Gray && outColorData == QColorSpace::ColorModel::Cmyk) { + // Gray -> CMYK + if (format() == QImage::Format_Grayscale8) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const quint8 *in_scanline = reinterpret_cast<const quint8 *>(d->data + y * d->bytes_per_line); + QCmyk32 *out_scanline = reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyGray(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } else { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const quint16 *in_scanline = reinterpret_cast<const quint16 *>(d->data + y * d->bytes_per_line); + QCmyk32 *out_scanline = reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyGray(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } + } else if (inColorData == QColorSpace::ColorModel::Rgb && outColorData == QColorSpace::ColorModel::Gray) { + // RGB -> Gray + if (tmpFormat == QImage::Format_Grayscale8) { + fromImage.convertTo(QImage::Format_RGB32); + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QRgb *in_scanline = reinterpret_cast<const QRgb *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + quint8 *out_scanline = reinterpret_cast<quint8 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyReturnGray(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } else { + fromImage.convertTo(QImage::Format_RGBX64); + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QRgba64 *in_scanline = reinterpret_cast<const QRgba64 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + quint16 *out_scanline = reinterpret_cast<quint16 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyReturnGray(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } + } else if (inColorData == QColorSpace::ColorModel::Cmyk && outColorData == QColorSpace::ColorModel::Gray) { + // CMYK -> Gray + if (tmpFormat == QImage::Format_Grayscale8) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QCmyk32 *in_scanline = reinterpret_cast<const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + quint8 *out_scanline = reinterpret_cast<quint8 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyReturnGray(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } else { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QCmyk32 *in_scanline = reinterpret_cast<const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + quint16 *out_scanline = reinterpret_cast<quint16 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyReturnGray(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } + } else if (inColorData == QColorSpace::ColorModel::Cmyk && outColorData == QColorSpace::ColorModel::Rgb) { + // CMYK -> RGB + if (isRgb32Data(tmpFormat) ) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QCmyk32 *in_scanline = reinterpret_cast<const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + QRgb *out_scanline = reinterpret_cast<QRgb *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } else if (isRgb64Data(tmpFormat)) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QCmyk32 *in_scanline = reinterpret_cast<const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + QRgba64 *out_scanline = reinterpret_cast<QRgba64 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } else { + Q_ASSERT(isRgb32fpx4Data(tmpFormat)); + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QCmyk32 *in_scanline = reinterpret_cast<const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + QRgbaFloat32 *out_scanline = reinterpret_cast<QRgbaFloat32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque); + } + }; + } + } else if (inColorData == QColorSpace::ColorModel::Rgb && outColorData == QColorSpace::ColorModel::Cmyk) { + // RGB -> CMYK + if (!fromImage.hasAlphaChannel()) + transFlags = QColorTransformPrivate::InputOpaque; + else if (qPixelLayouts[fromImage.format()].premultiplied) + transFlags = QColorTransformPrivate::Premultiplied; + if (isRgb32Data(fromImage.format()) ) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QRgb *in_scanline = reinterpret_cast<const QRgb *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + QCmyk32 *out_scanline = reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } + }; + } else if (isRgb64Data(fromImage.format())) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QRgba64 *in_scanline = reinterpret_cast<const QRgba64 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + QCmyk32 *out_scanline = reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } + }; + } else { + Q_ASSERT(isRgb32fpx4Data(fromImage.format())); + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QRgbaFloat32 *in_scanline = reinterpret_cast<const QRgbaFloat32 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + QCmyk32 *out_scanline = reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } + }; + } + } else { + Q_UNREACHABLE(); + } + } else { + // Conversion on same color model + if (pixelFormat().colorModel() == QPixelFormat::Indexed) { + for (int i = 0; i < d->colortable.size(); ++i) + fromImage.d->colortable[i] = transform.map(d->colortable[i]); + return fromImage.convertedTo(toFormat, flags); + } + + QImage::Format oldFormat = format(); + if (qt_fpColorPrecision(oldFormat)) { + if (oldFormat != QImage::Format_RGBX32FPx4 && oldFormat != QImage::Format_RGBA32FPx4 + && oldFormat != QImage::Format_RGBA32FPx4_Premultiplied) + fromImage.convertTo(QImage::Format_RGBA32FPx4); + } else if (qt_highColorPrecision(oldFormat, true)) { + if (oldFormat != QImage::Format_RGBX64 && oldFormat != QImage::Format_RGBA64 + && oldFormat != QImage::Format_RGBA64_Premultiplied && oldFormat != QImage::Format_Grayscale16) + fromImage.convertTo(QImage::Format_RGBA64); + } else if (oldFormat != QImage::Format_ARGB32 && oldFormat != QImage::Format_RGB32 + && oldFormat != QImage::Format_ARGB32_Premultiplied && oldFormat != QImage::Format_CMYK8888 + && oldFormat != QImage::Format_Grayscale8 && oldFormat != QImage::Format_Grayscale16) { + if (hasAlphaChannel()) + fromImage.convertTo(QImage::Format_ARGB32); + else + fromImage.convertTo(QImage::Format_RGB32); + } + + if (!fromImage.hasAlphaChannel()) + transFlags = QColorTransformPrivate::InputOpaque; + else if (qPixelLayouts[fromImage.format()].premultiplied) + transFlags = QColorTransformPrivate::Premultiplied; + + if (fromImage.format() == Format_Grayscale8) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const quint8 *in_scanline = reinterpret_cast<const quint8 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + if (tmpFormat == Format_Grayscale8) { + quint8 *out_scanline = reinterpret_cast<quint8 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyGray(out_scanline, in_scanline, width(), transFlags); + } else { + Q_ASSERT(tmpFormat == Format_Grayscale16); + quint16 *out_scanline = reinterpret_cast<quint16 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyGray(out_scanline, in_scanline, width(), transFlags); + } + } + }; + } else if (fromImage.format() == Format_Grayscale16) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const quint16 *in_scanline = reinterpret_cast<const quint16 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + quint16 *out_scanline = reinterpret_cast<quint16 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->applyGray(out_scanline, in_scanline, width(), transFlags); + } + }; + } else if (fromImage.format() == Format_CMYK8888) { + Q_ASSERT(tmpFormat == Format_CMYK8888); + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QCmyk32 *in_scanline = reinterpret_cast<const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + QCmyk32 *out_scanline = reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } + }; + } else if (isRgb32fpx4Data(fromImage.format())) { + Q_ASSERT(isRgb32fpx4Data(tmpFormat)); + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QRgbaFloat32 *in_scanline = reinterpret_cast<const QRgbaFloat32 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + QRgbaFloat32 *out_scanline = reinterpret_cast<QRgbaFloat32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } + }; + } else if (isRgb64Data(fromImage.format())) { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QRgba64 *in_scanline = reinterpret_cast<const QRgba64 *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + if (isRgb32fpx4Data(tmpFormat)) { + QRgbaFloat32 *out_scanline = reinterpret_cast<QRgbaFloat32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } else { + Q_ASSERT(isRgb64Data(tmpFormat)); + QRgba64 *out_scanline = reinterpret_cast<QRgba64 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } + } + }; + } else { + transformSegment = [&](int yStart, int yEnd) { + for (int y = yStart; y < yEnd; ++y) { + const QRgb *in_scanline = reinterpret_cast<const QRgb *>(fromImage.constBits() + y * fromImage.bytesPerLine()); + if (isRgb32fpx4Data(tmpFormat)) { + QRgbaFloat32 *out_scanline = reinterpret_cast<QRgbaFloat32 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } else if (isRgb64Data(tmpFormat)) { + QRgba64 *out_scanline = reinterpret_cast<QRgba64 *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } else { + Q_ASSERT(isRgb32Data(tmpFormat)); + QRgb *out_scanline = reinterpret_cast<QRgb *>(toImage.d->data + y * toImage.bytesPerLine()); + QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags); + } + } + }; + } + } + +#if QT_CONFIG(thread) && !defined(Q_OS_WASM) + int segments = (qsizetype(width()) * height()) >> 16; + segments = std::min(segments, height()); + QThreadPool *threadPool = QThreadPoolPrivate::qtGuiInstance(); + if (segments > 1 && threadPool && !threadPool->contains(QThread::currentThread())) { + QSemaphore semaphore; + int y = 0; + for (int i = 0; i < segments; ++i) { + int yn = (height() - y) / (segments - i); + threadPool->start([&, y, yn]() { + transformSegment(y, y + yn); + semaphore.release(1); + }); + y += yn; + } + semaphore.acquire(segments); + } else +#endif + transformSegment(0, height()); + + if (tmpFormat != toFormat) + toImage.convertTo(toFormat); + + return toImage; +} + /*! \since 6.4 \overload @@ -5146,12 +5805,48 @@ QImage QImage::colorTransformed(const QColorTransform &transform) const & */ QImage QImage::colorTransformed(const QColorTransform &transform) && { - if (!d || !d->colorSpace.isValid()) + if (!d) return QImage(); + + QColorSpace::ColorModel inColorModel = QColorTransformPrivate::get(transform)->colorSpaceIn->colorModel; + QColorSpace::ColorModel outColorModel = QColorTransformPrivate::get(transform)->colorSpaceOut->colorModel; + if (!qt_compatibleColorModel(pixelFormat().colorModel(), inColorModel)) { + qWarning() << "QImage::colorTransformed: Invalid input color space for transform"; + return QImage(); + } + if (!qt_compatibleColorModel(pixelFormat().colorModel(), outColorModel)) { + // There is currently no inplace conversion of both colorspace and format, so just use the normal version. + switch (outColorModel) { + case QColorSpace::ColorModel::Rgb: + return colorTransformed(transform, qt_highColorPrecision(format(), true) ? QImage::Format_RGBX64 : QImage::Format_RGB32); + case QColorSpace::ColorModel::Gray: + return colorTransformed(transform, qt_highColorPrecision(format(), true) ? QImage::Format_Grayscale16 : QImage::Format_Grayscale8); + case QColorSpace::ColorModel::Cmyk: + return colorTransformed(transform, QImage::Format_CMYK8888); + case QColorSpace::ColorModel::Undefined: + break; + } + return QImage(); + } + applyColorTransform(transform); return std::move(*this); } +/*! + \since 6.8 + \overload + + Returns the image color transformed using \a transform on all pixels in the image. + + \sa applyColorTransform() +*/ +QImage QImage::colorTransformed(const QColorTransform &transform, QImage::Format format, Qt::ImageConversionFlags flags) && +{ + // There is currently no inplace conversion of both colorspace and format, so just use the normal version. + return colorTransformed(transform, format, flags); +} + bool QImageData::convertInPlace(QImage::Format newFormat, Qt::ImageConversionFlags flags) { if (format == newFormat) @@ -5676,6 +6371,19 @@ static constexpr QPixelFormat pixelformats[] = { /*PREMULTIPLIED*/ QPixelFormat::Premultiplied, /*INTERPRETATION*/ QPixelFormat::FloatingPoint, /*BYTE ORDER*/ QPixelFormat::CurrentSystemEndian), + //QImage::Format_CMYK8888: + QPixelFormat(QPixelFormat::CMYK, + /*RED*/ 8, + /*GREEN*/ 8, + /*BLUE*/ 8, + /*FOURTH*/ 8, + /*FIFTH*/ 0, + /*ALPHA*/ 0, + /*ALPHA USAGE*/ QPixelFormat::IgnoresAlpha, + /*ALPHA POSITION*/ QPixelFormat::AtBeginning, + /*PREMULTIPLIED*/ QPixelFormat::NotPremultiplied, + /*INTERPRETATION*/ QPixelFormat::UnsignedInteger, + /*BYTE ORDER*/ QPixelFormat::CurrentSystemEndian), }; static_assert(sizeof(pixelformats) / sizeof(*pixelformats) == QImage::NImageFormats); @@ -5736,8 +6444,7 @@ QMap<QString, QString> qt_getImageText(const QImage &image, const QString &descr QMap<QString, QString> qt_getImageTextFromDescription(const QString &description) { QMap<QString, QString> text; - const auto pairs = QStringView{description}.split(u"\n\n"); - for (const auto &pair : pairs) { + for (const auto &pair : QStringView{description}.tokenize(u"\n\n")) { int index = pair.indexOf(u':'); if (index >= 0 && pair.indexOf(u' ') < index) { if (!pair.trimmed().isEmpty()) |