diff options
Diffstat (limited to 'src/gui/image/qimage.cpp')
-rw-r--r-- | src/gui/image/qimage.cpp | 819 |
1 files changed, 751 insertions, 68 deletions
diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index 4aff92ac80..175ff8cc49 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> @@ -45,6 +46,7 @@ #include <memory> QT_BEGIN_NAMESPACE +class QCmyk32; using namespace Qt::StringLiterals; @@ -304,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: @@ -360,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 @@ -616,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} */ /*! @@ -710,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 @@ -819,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; } @@ -1068,7 +1093,6 @@ QImage &QImage::operator=(const QImage &image) /*! \fn void QImage::swap(QImage &other) - \since 4.8 Swaps image \a other with this image. This operation is very fast and never fails. @@ -1147,9 +1171,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; } @@ -1214,7 +1239,6 @@ QImage Q_TRACE_INSTRUMENT(qtgui) 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; @@ -1303,7 +1327,6 @@ QImage Q_TRACE_INSTRUMENT(qtgui) 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; } @@ -1388,7 +1411,6 @@ int QImage::depth() const } /*! - \since 4.6 \fn int QImage::colorCount() const Returns the size of the color table for the image. @@ -1652,7 +1674,6 @@ const uchar *QImage::scanLine(int i) const shared pixel data, because the returned data is const. \sa scanLine(), constBits() - \since 4.7 */ const uchar *QImage::constScanLine(int i) const { @@ -1708,7 +1729,6 @@ const uchar *QImage::bits() const shared pixel data, because the returned data is const. \sa bits(), constScanLine() - \since 4.7 */ const uchar *QImage::constBits() const { @@ -1815,7 +1835,6 @@ void QImage::fill(uint pixel) /*! \fn void QImage::fill(Qt::GlobalColor color) \overload - \since 4.8 Fills the image with the given \a color, described as a standard global color. @@ -1841,8 +1860,6 @@ void QImage::fill(Qt::GlobalColor color) If the depth of the image is 8, the image will be filled with the index corresponding the \a color in the color table if present; it will otherwise be filled with 0. - - \since 4.8 */ void QImage::fill(const QColor &color) @@ -2099,7 +2116,6 @@ void QImage::invertPixels(InvertMode mode) #endif /*! - \since 4.6 Resizes the color table to contain \a colorCount entries. If the color table is expanded, all the extra colors will be set to @@ -2203,7 +2219,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); @@ -2633,6 +2648,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); @@ -4305,6 +4323,12 @@ int QImage::metric(PaintDeviceMetric metric) const return d->devicePixelRatio * QPaintDevice::devicePixelRatioFScale(); break; + case PdmDevicePixelRatioF_EncodedA: + Q_FALLTHROUGH(); + case PdmDevicePixelRatioF_EncodedB: + return QPaintDevice::encodeMetricF(metric, d->devicePixelRatio); + break; + default: qWarning("QImage::metric(): Unhandled metric type %d", metric); break; @@ -4581,7 +4605,6 @@ bool QImage::hasAlphaChannel() const } /*! - \since 4.7 Returns the number of bit planes in the image. The number of bit planes is the number of bits of color and @@ -4676,6 +4699,8 @@ QImage QImage::smoothScaled(int w, int h) const src.convertTo(QImage::Format_RGBA32FPx4_Premultiplied); break; #endif + case QImage::Format_CMYK8888: + break; default: if (src.hasAlphaChannel()) src.convertTo(QImage::Format_ARGB32_Premultiplied); @@ -4818,6 +4843,9 @@ QImage Q_TRACE_INSTRUMENT(qtgui) QImage::transformed(const QTransform &matrix, Q // with scaling smoothly more than 2x down. if (hd * 2 < hs || wd * 2 < ws) nonpaintable_scale_xform = true; + // We cannot paint on a CMYK image, so don't try to do so + if (format() == QImage::Format_CMYK8888) + nonpaintable_scale_xform = true; } else { if (mat.type() <= QTransform::TxRotate && mat.m11() == 0 && mat.m22() == 0) { if (mat.m12() == 1. && mat.m21() == -1.) @@ -4849,6 +4877,7 @@ QImage Q_TRACE_INSTRUMENT(qtgui) QImage::transformed(const QTransform &matrix, Q case QImage::Format_RGBX64: case QImage::Format_RGBA64_Premultiplied: #endif + case QImage::Format_CMYK8888: // Use smoothScaled for scaling when we can do so without conversion. if (mat.m11() > 0.0F && mat.m22() > 0.0F) return smoothScaled(wd, hd); @@ -4862,14 +4891,24 @@ QImage Q_TRACE_INSTRUMENT(qtgui) QImage::transformed(const QTransform &matrix, Q || (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()); } } } @@ -4909,9 +4948,16 @@ QImage Q_TRACE_INSTRUMENT(qtgui) QImage::transformed(const QTransform &matrix, Q } else memset(dImage.bits(), 0x00, dImage.d->nbytes); - if (target_format >= QImage::Format_RGB32) { + if (target_format >= QImage::Format_RGB32 && target_format != QImage::Format_CMYK8888) { // 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()); @@ -4981,6 +5027,9 @@ void QImage::setColorSpace(const QColorSpace &colorSpace) return; if (d->colorSpace == colorSpace) return; + if (colorSpace.isValid() && !qt_compatibleColorModelSource(pixelFormat().colorModel(), colorSpace.colorModel())) + return; + detachMetadata(false); if (d) d->colorSpace = colorSpace; @@ -4993,21 +5042,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_compatibleColorModelTarget(pixelFormat().colorModel(), colorSpace.colorModel(), colorSpace.transformModel())) { + *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_compatibleColorModelTarget(toPixelFormat(format).colorModel(), colorSpace.colorModel(), colorSpace.transformModel())) { + 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; } @@ -5018,16 +5106,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_compatibleColorModelTarget(toPixelFormat(format).colorModel(), colorSpace.colorModel(), colorSpace.transformModel())) { + 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; } @@ -5052,6 +5180,14 @@ void QImage::applyColorTransform(const QColorTransform &transform) { if (transform.isIdentity()) return; + + if (!qt_compatibleColorModelSource(pixelFormat().colorModel(), QColorTransformPrivate::get(transform)->colorSpaceIn->colorModel) || + !qt_compatibleColorModelTarget(pixelFormat().colorModel(), QColorTransformPrivate::get(transform)->colorSpaceOut->colorModel, + QColorTransformPrivate::get(transform)->colorSpaceOut->transformModel)) { + qWarning() << "QImage::applyColorTransform can not apply format switching transform without switching format"; + return; + } + detach(); if (!d) return; @@ -5070,7 +5206,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 @@ -5084,7 +5221,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; @@ -5099,7 +5239,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)->apply(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)->apply(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); @@ -5113,6 +5267,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) { @@ -5147,23 +5308,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; + + const QColorSpacePrivate *inColorSpace = QColorTransformPrivate::get(transform)->colorSpaceIn.constData(); + const QColorSpacePrivate *outColorSpace = QColorTransformPrivate::get(transform)->colorSpaceOut.constData(); + if (!qt_compatibleColorModelSource(pixelFormat().colorModel(), inColorSpace->colorModel)) { + qWarning() << "QImage::colorTransformed: Invalid input color space for transform"; + return QImage(); + } + if (!qt_compatibleColorModelTarget(pixelFormat().colorModel(), outColorSpace->colorModel, outColorSpace->transformModel)) { + // All model switching transforms are opaque in at least one end. + switch (outColorSpace->colorModel) { + 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); + + const QColorSpacePrivate *inColorSpace = QColorTransformPrivate::get(transform)->colorSpaceIn.constData(); + const QColorSpacePrivate *outColorSpace = QColorTransformPrivate::get(transform)->colorSpaceOut.constData(); + if (!qt_compatibleColorModelSource(pixelFormat().colorModel(), inColorSpace->colorModel)) { + qWarning() << "QImage::colorTransformed: Invalid input color space for transform"; + return QImage(); + } + if (!qt_compatibleColorModelTarget(toPixelFormat(toFormat).colorModel(), outColorSpace->colorModel, outColorSpace->transformModel)) { + 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)->apply(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)->apply(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)->apply(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)->apply(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)->apply(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)->apply(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)->apply(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)->apply(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)->apply(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)->apply(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)->apply(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 @@ -5174,12 +5809,48 @@ QImage QImage::colorTransformed(const QColorTransform &transform) const & */ QImage QImage::colorTransformed(const QColorTransform &transform) && { - if (!d || !d->colorSpace.isValid()) + if (!d) return QImage(); + + const QColorSpacePrivate *inColorSpace = QColorTransformPrivate::get(transform)->colorSpaceIn.constData(); + const QColorSpacePrivate *outColorSpace = QColorTransformPrivate::get(transform)->colorSpaceOut.constData(); + if (!qt_compatibleColorModelSource(pixelFormat().colorModel(), inColorSpace->colorModel)) { + qWarning() << "QImage::colorTransformed: Invalid input color space for transform"; + return QImage(); + } + if (!qt_compatibleColorModelTarget(pixelFormat().colorModel(), outColorSpace->colorModel, outColorSpace->transformModel)) { + // There is currently no inplace conversion of both colorspace and format, so just use the normal version. + switch (outColorSpace->colorModel) { + 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) @@ -5704,6 +6375,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); @@ -5764,8 +6448,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()) |