summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/image/qimage/tst_qimage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/gui/image/qimage/tst_qimage.cpp')
-rw-r--r--tests/auto/gui/image/qimage/tst_qimage.cpp226
1 files changed, 217 insertions, 9 deletions
diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp
index 415f016519..3e3d0a49bc 100644
--- a/tests/auto/gui/image/qimage/tst_qimage.cpp
+++ b/tests/auto/gui/image/qimage/tst_qimage.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -108,6 +108,8 @@ private slots:
void smoothScaleAlpha();
void smoothScaleFormats_data();
void smoothScaleFormats();
+ void smoothScaleNoConversion_data();
+ void smoothScaleNoConversion();
void transformed_data();
void transformed();
@@ -167,6 +169,11 @@ private slots:
void largeInplaceRgbConversion_data();
void largeInplaceRgbConversion();
+ void colorSpaceRgbConversion_data();
+ void colorSpaceRgbConversion();
+ void colorSpaceCmykConversion_data();
+ void colorSpaceCmykConversion();
+
void deepCopyWhenPaintingActive();
void scaled_QTBUG19157();
@@ -229,6 +236,7 @@ private slots:
void largeRasterScale();
void metadataChangeWithReadOnlyPixels();
+ void scaleIndexed();
#if defined(Q_OS_WIN)
void toWinHBITMAP_data();
@@ -236,6 +244,9 @@ private slots:
void fromMonoHBITMAP();
#endif // Q_OS_WIN
+ void tofromPremultipliedFormat_data();
+ void tofromPremultipliedFormat();
+
private:
const QString m_prefix;
};
@@ -315,7 +326,9 @@ static QLatin1String formatToString(QImage::Format format)
return QLatin1String("RGBA32FPx4");
case QImage::Format_RGBA32FPx4_Premultiplied:
return QLatin1String("RGBA32FPx4pm");
- default:
+ case QImage::Format_CMYK8888:
+ return QLatin1String("CMYK8888");
+ case QImage::NImageFormats:
break;
};
Q_UNREACHABLE();
@@ -1133,10 +1146,9 @@ void tst_QImage::rotate_data()
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<int>("degrees");
- QList<int> degrees;
- degrees << 0 << 90 << 180 << 270;
+ constexpr int degrees[] = {0, 90, 180, 270};
- foreach (int d, degrees) {
+ for (int d : degrees) {
const QString dB = QString::number(d);
for (int i = QImage::Format_Indexed8; i < QImage::NImageFormats; i++) {
QImage::Format format = static_cast<QImage::Format>(i);
@@ -1502,6 +1514,8 @@ void tst_QImage::setPixelWithAlpha_data()
continue;
if (c == QImage::Format_Alpha8)
continue;
+ if (c == QImage::Format_CMYK8888)
+ continue;
QTest::newRow(qPrintable(formatToString(QImage::Format(c)))) << QImage::Format(c);
}
}
@@ -2059,6 +2073,24 @@ void tst_QImage::smoothScaleFormats()
QVERIFY(rotated.hasAlphaChannel());
}
+void tst_QImage::smoothScaleNoConversion_data()
+{
+ QTest::addColumn<QImage::Format>("format");
+ QTest::addRow("Mono") << QImage::Format_Mono;
+ QTest::addRow("MonoLSB") << QImage::Format_MonoLSB;
+ QTest::addRow("Indexed8") << QImage::Format_Indexed8;
+}
+
+void tst_QImage::smoothScaleNoConversion()
+{
+ QFETCH(QImage::Format, format);
+ QImage img(128, 128, format);
+ img.fill(1);
+ img.setColorTable(QList<QRgb>() << qRgba(255,0,0,255) << qRgba(0,0,0,0));
+ img = img.scaled(QSize(48, 48), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+ QVERIFY(img.hasAlphaChannel());
+}
+
static int count(const QImage &img, int x, int y, int dx, int dy, QRgb pixel)
{
int i = 0;
@@ -2545,7 +2577,8 @@ void tst_QImage::rgbSwapped_data()
for (int i = QImage::Format_Indexed8; i < QImage::NImageFormats; ++i) {
if (i == QImage::Format_Alpha8
|| i == QImage::Format_Grayscale8
- || i == QImage::Format_Grayscale16) {
+ || i == QImage::Format_Grayscale16
+ || i == QImage::Format_CMYK8888) {
continue;
}
QTest::addRow("%s", formatToString(QImage::Format(i)).data()) << QImage::Format(i);
@@ -3027,13 +3060,15 @@ void tst_QImage::inplaceRgbConversion_data()
for (int i = QImage::Format_RGB32; i < QImage::NImageFormats; ++i) {
if (i == QImage::Format_Alpha8
|| i == QImage::Format_Grayscale8
- || i == QImage::Format_Grayscale16) {
+ || i == QImage::Format_Grayscale16
+ || i == QImage::Format_CMYK8888) {
continue;
}
for (int j = QImage::Format_RGB32; j < QImage::NImageFormats; ++j) {
if (j == QImage::Format_Alpha8
|| j == QImage::Format_Grayscale8
- || j == QImage::Format_Grayscale16) {
+ || j == QImage::Format_Grayscale16
+ || j == QImage::Format_CMYK8888) {
continue;
}
if (i == j)
@@ -3214,6 +3249,144 @@ void tst_QImage::largeInplaceRgbConversion()
}
}
+void tst_QImage::colorSpaceRgbConversion_data()
+{
+ QTest::addColumn<QImage::Format>("fromFormat");
+ QTest::addColumn<QImage::Format>("toFormat");
+
+ // The various possible code paths for color space conversions compatible with RGB color spaces:
+ QImage::Format formats[] = {
+ QImage::Format_RGB32,
+ QImage::Format_ARGB32,
+ QImage::Format_ARGB32_Premultiplied,
+ QImage::Format_RGBX64,
+ QImage::Format_RGBA64,
+ QImage::Format_RGBA64_Premultiplied,
+ QImage::Format_RGBX32FPx4,
+ QImage::Format_RGBA32FPx4,
+ QImage::Format_RGBA32FPx4_Premultiplied,
+ QImage::Format_Grayscale8,
+ QImage::Format_Grayscale16,
+ };
+
+ for (auto fromFormat : formats) {
+ const QLatin1String formatI = formatToString(fromFormat);
+ for (auto toFormat : formats) {
+ QTest::addRow("%s -> %s", formatI.data(), formatToString(toFormat).data())
+ << fromFormat << toFormat;
+ }
+ }
+}
+
+void tst_QImage::colorSpaceRgbConversion()
+{
+ // Test that all color space conversions work
+ QFETCH(QImage::Format, fromFormat);
+ QFETCH(QImage::Format, toFormat);
+
+ bool srcGrayscale = fromFormat == QImage::Format_Grayscale8 || fromFormat == QImage::Format_Grayscale16;
+ bool dstGrayscale = toFormat == QImage::Format_Grayscale8 || toFormat == QImage::Format_Grayscale16;
+
+ QImage image(16, 16, fromFormat);
+ image.setColorSpace(QColorSpace::SRgb);
+
+ for (int i = 0; i < image.height(); ++i) {
+ for (int j = 0; j < image.width(); ++j) {
+ if (srcGrayscale || dstGrayscale)
+ image.setPixel(j, i, qRgb((i + j) * 8, (i + j) * 8, (i + j) * 8));
+ else
+ image.setPixel(j, i, qRgb(j * 16, i * 16, (i + j) * 8));
+ }
+ }
+
+ QImage imageConverted = image.convertedToColorSpace(QColorSpace::DisplayP3, toFormat);
+ QCOMPARE(imageConverted.format(), toFormat);
+ QCOMPARE(imageConverted.size(), image.size());
+ if (dstGrayscale) {
+ int gray = 0;
+ for (int x = 0; x < image.width(); ++x) {
+ int newGray = qGray(imageConverted.pixel(x, 6));
+ QCOMPARE_GE(newGray, gray);
+ gray = newGray;
+ }
+ } else {
+ int red = 0;
+ int blue = 0;
+ for (int x = 0; x < image.width(); ++x) {
+ int newRed = qRed(imageConverted.pixel(x, 5));
+ int newBlue = qBlue(imageConverted.pixel(x, 7));
+ QCOMPARE_GE(newBlue, blue);
+ QCOMPARE_GE(newRed, red);
+ blue = newBlue;
+ red = newRed;
+ }
+ }
+}
+
+
+void tst_QImage::colorSpaceCmykConversion_data()
+{
+ QTest::addColumn<QImage::Format>("toFormat");
+
+ QImage::Format formats[] = {
+ QImage::Format_RGB32,
+ QImage::Format_ARGB32,
+ QImage::Format_ARGB32_Premultiplied,
+ QImage::Format_RGBX64,
+ QImage::Format_RGBA64,
+ QImage::Format_RGBA64_Premultiplied,
+ QImage::Format_RGBX32FPx4,
+ QImage::Format_RGBA32FPx4,
+ QImage::Format_RGBA32FPx4_Premultiplied,
+ QImage::Format_Grayscale8,
+ QImage::Format_Grayscale16,
+ };
+
+ for (auto toFormat : formats)
+ QTest::addRow("CMYK8888 -> %s", formatToString(toFormat).data()) << toFormat;
+}
+
+void tst_QImage::colorSpaceCmykConversion()
+{
+ QFETCH(QImage::Format, toFormat);
+
+ bool dstGrayscale = toFormat == QImage::Format_Grayscale8 || toFormat == QImage::Format_Grayscale16;
+
+ QImage image(16, 16, QImage::Format_CMYK8888);
+ QFile iccProfile(m_prefix +"CGATS001Compat-v2-micro.icc");
+ iccProfile.open(QIODevice::ReadOnly);
+ image.setColorSpace(QColorSpace::fromIccProfile(iccProfile.readAll()));
+ QVERIFY(image.colorSpace().isValid());
+
+ for (int i = 0; i < image.height(); ++i) {
+ for (int j = 0; j < image.width(); ++j) {
+ if (dstGrayscale)
+ image.setPixel(j, i, qRgb((i + j) * 8, (i + j) * 8, (i + j) * 8));
+ else
+ image.setPixel(j, i, qRgb(j * 16, i * 16, (i + j) * 8));
+ }
+ }
+
+ QImage imageConverted = image.convertedToColorSpace(QColorSpace::SRgb, toFormat);
+ QCOMPARE(imageConverted.format(), toFormat);
+ QCOMPARE(imageConverted.size(), image.size());
+ if (dstGrayscale) {
+ int gray = 0;
+ for (int x = 0; x < image.width(); ++x) {
+ int newGray = qGray(imageConverted.pixel(x, 6));
+ QCOMPARE_GE(newGray, gray);
+ gray = newGray;
+ }
+ } else {
+ int red = 0;
+ for (int x = 0; x < image.width(); ++x) {
+ int newRed = qRed(imageConverted.pixel(x, 5));
+ QCOMPARE_GE(newRed, red);
+ red = newRed;
+ }
+ }
+}
+
void tst_QImage::deepCopyWhenPaintingActive()
{
QImage image(64, 64, QImage::Format_ARGB32_Premultiplied);
@@ -3322,7 +3495,8 @@ void tst_QImage::invertPixelsRGB_data()
for (int i = QImage::Format_RGB32; i < QImage::NImageFormats; ++i) {
if (i == QImage::Format_Alpha8
|| i == QImage::Format_Grayscale8
- || i == QImage::Format_Grayscale16) {
+ || i == QImage::Format_Grayscale16
+ || i == QImage::Format_CMYK8888) {
continue;
}
QTest::addRow("%s", formatToString(QImage::Format(i)).data()) << QImage::Format(i);
@@ -3689,10 +3863,12 @@ void tst_QImage::metadataPassthrough()
QCOMPARE(alphaMask.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(alphaMask.devicePixelRatio(), a.devicePixelRatio());
+#ifndef QT_NO_IMAGE_HEURISTIC_MASK
QImage heuristicMask = a.createHeuristicMask();
QCOMPARE(heuristicMask.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(heuristicMask.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(heuristicMask.devicePixelRatio(), a.devicePixelRatio());
+#endif
QImage maskFromColor = a.createMaskFromColor(qRgb(0, 0, 0));
QCOMPARE(maskFromColor.dotsPerMeterX(), a.dotsPerMeterX());
@@ -4080,6 +4256,16 @@ void tst_QImage::metadataChangeWithReadOnlyPixels()
QCOMPARE(image.constBits(), (const uchar *)data);
}
+void tst_QImage::scaleIndexed()
+{
+ QImage image(10, 10, QImage::Format_Indexed8);
+ image.setColor(0, qRgb(0,0,0));
+ image.setColor(1, qRgb(1,1,1));
+ image.fill(1);
+ image.setDevicePixelRatio(2);
+ QImage image2 = image.scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation); // do not crash
+}
+
#if defined(Q_OS_WIN)
static inline QColor COLORREFToQColor(COLORREF cr)
@@ -4192,5 +4378,27 @@ void tst_QImage::fromMonoHBITMAP() // QTBUG-72343, corruption for mono bitmaps
#endif // Q_OS_WIN
+void tst_QImage::tofromPremultipliedFormat_data()
+{
+ QTest::addColumn<QImage::Format>("unpremul");
+ QTest::addColumn<QImage::Format>("premul");
+
+ // Test all available formats with both premultiplied and unpremultiplied versions
+ QTest::newRow("argb32") << QImage::Format_ARGB32 << QImage::Format_ARGB32_Premultiplied;
+ QTest::newRow("rgba8888") << QImage::Format_RGBA8888 << QImage::Format_RGBA8888_Premultiplied;
+ QTest::newRow("rgba64") << QImage::Format_RGBA64 << QImage::Format_RGBA64_Premultiplied;
+ QTest::newRow("rgba16fpx4") << QImage::Format_RGBA16FPx4 << QImage::Format_RGBA16FPx4_Premultiplied;
+ QTest::newRow("rgba32fpx4") << QImage::Format_RGBA32FPx4 << QImage::Format_RGBA32FPx4_Premultiplied;
+}
+
+void tst_QImage::tofromPremultipliedFormat()
+{
+ QFETCH(QImage::Format, unpremul);
+ QFETCH(QImage::Format, premul);
+
+ QCOMPARE(qt_toPremultipliedFormat(unpremul), premul);
+ QCOMPARE(qt_toUnpremultipliedFormat(premul), unpremul);
+}
+
QTEST_GUILESS_MAIN(tst_QImage)
#include "tst_qimage.moc"