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.cpp157
1 files changed, 121 insertions, 36 deletions
diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp
index 85d258de5b..18812fd090 100644
--- a/tests/auto/gui/image/qimage/tst_qimage.cpp
+++ b/tests/auto/gui/image/qimage/tst_qimage.cpp
@@ -108,6 +108,7 @@ private slots:
void smoothScale();
void smoothScale2_data();
void smoothScale2();
+ void smoothScale3_data();
void smoothScale3();
void smoothScale4();
@@ -197,6 +198,9 @@ private slots:
void pixelColor();
void pixel();
+ void ditherGradient_data();
+ void ditherGradient();
+
private:
const QString m_prefix;
};
@@ -292,17 +296,13 @@ void tst_QImage::swap()
void tst_QImage::create()
{
bool cr = true;
-#if !defined(Q_OS_WINCE)
QT_TRY {
-#endif
//QImage image(7000000, 7000000, 8, 256, QImage::IgnoreEndian);
QImage image(7000000, 7000000, QImage::Format_Indexed8);
image.setColorCount(256);
cr = !image.isNull();
-#if !defined(Q_OS_WINCE)
} QT_CATCH (...) {
}
-#endif
QVERIFY( !cr );
}
@@ -1750,9 +1750,12 @@ static inline int rand8()
return int(256. * (qrand() / (RAND_MAX + 1.0)));
}
-// compares img.scale against the bilinear filtering used by QPainter
-void tst_QImage::smoothScale3()
+void tst_QImage::smoothScale3_data()
{
+ QTest::addColumn<QImage>("img");
+ QTest::addColumn<qreal>("scale_x");
+ QTest::addColumn<qreal>("scale_y");
+
QImage img(128, 128, QImage::Format_RGB32);
for (int y = 0; y < img.height(); ++y) {
for (int x = 0; x < img.width(); ++x) {
@@ -1765,36 +1768,49 @@ void tst_QImage::smoothScale3()
}
}
- qreal scales[2] = { .5, 2 };
+ QTest::newRow("(0.5, 0.5)") << img << qreal(0.5) << qreal(0.5);
+ QTest::newRow("(0.5, 1.0)") << img << qreal(0.5) << qreal(1.0);
+ QTest::newRow("(1.0, 0.5)") << img << qreal(1.0) << qreal(0.5);
+ QTest::newRow("(0.5, 2.0)") << img << qreal(0.5) << qreal(2.0);
+ QTest::newRow("(1.0, 2.0)") << img << qreal(1.0) << qreal(2.0);
+ QTest::newRow("(2.0, 0.5)") << img << qreal(2.0) << qreal(0.5);
+ QTest::newRow("(2.0, 1.0)") << img << qreal(2.0) << qreal(1.0);
+ QTest::newRow("(2.0, 2.0)") << img << qreal(2) << qreal(2);
+}
+// compares img.scale against the bilinear filtering used by QPainter
+void tst_QImage::smoothScale3()
+{
+ QFETCH(QImage, img);
+ QFETCH(qreal, scale_x);
+ QFETCH(qreal, scale_y);
- for (int i = 0; i < 2; ++i) {
- QImage a = img.scaled(img.size() * scales[i], Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
- QImage b(a.size(), a.format());
- b.fill(0x0);
+ QImage a = img.scaled(img.width() * scale_x, img.height() * scale_y, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+ QImage b(a.size(), a.format());
+ b.fill(0x0);
- QPainter p(&b);
- p.setRenderHint(QPainter::SmoothPixmapTransform);
- p.scale(scales[i], scales[i]);
- p.drawImage(0, 0, img);
- p.end();
- int err = 0;
-
- for (int y = 0; y < a.height(); ++y) {
- for (int x = 0; x < a.width(); ++x) {
- QRgb ca = a.pixel(x, y);
- QRgb cb = b.pixel(x, y);
-
- // tolerate a little bit of rounding errors
- bool r = true;
- r &= qAbs(qRed(ca) - qRed(cb)) <= 18;
- r &= qAbs(qGreen(ca) - qGreen(cb)) <= 18;
- r &= qAbs(qBlue(ca) - qBlue(cb)) <= 18;
- if (!r)
- err++;
- }
+ QPainter p(&b);
+ p.setRenderHint(QPainter::SmoothPixmapTransform);
+ p.scale(scale_x, scale_y);
+ p.drawImage(0, 0, img);
+ p.end();
+ int err = 0;
+
+ for (int y = 0; y < a.height(); ++y) {
+ for (int x = 0; x < a.width(); ++x) {
+ QRgb ca = a.pixel(x, y);
+ QRgb cb = b.pixel(x, y);
+
+ // tolerate a little bit of rounding errors
+ int tolerance = 3;
+ bool r = true;
+ r &= qAbs(qRed(ca) - qRed(cb)) <= tolerance;
+ r &= qAbs(qGreen(ca) - qGreen(cb)) <= tolerance;
+ r &= qAbs(qBlue(ca) - qBlue(cb)) <= tolerance;
+ if (!r)
+ err++;
}
- QCOMPARE(err, 0);
}
+ QCOMPARE(err, 0);
}
// Tests smooth upscale is smooth
@@ -1819,11 +1835,7 @@ void tst_QImage::smoothScale4()
void tst_QImage::smoothScaleBig()
{
-#if defined(Q_OS_WINCE)
- int bigValue = 2000;
-#else
int bigValue = 200000;
-#endif
QImage tall(4, bigValue, QImage::Format_ARGB32);
tall.fill(0x0);
@@ -3222,5 +3234,78 @@ void tst_QImage::pixel()
}
}
+void tst_QImage::ditherGradient_data()
+{
+ QTest::addColumn<QImage>("image");
+ QTest::addColumn<QImage::Format>("format");
+ QTest::addColumn<int>("flags");
+ QTest::addColumn<int>("minimumExpectedGradient");
+
+ QImage rgb32(256, 16, QImage::Format_RGB32);
+ QLinearGradient gradient(QRectF(rgb32.rect()).topLeft(), QRectF(rgb32.rect()).topRight());
+ gradient.setColorAt(0.0, QColor(0, 0, 0));
+ gradient.setColorAt(1.0, QColor(255, 255, 255));
+ QPainter p;
+ p.begin(&rgb32);
+ p.fillRect(rgb32.rect(), gradient);
+ p.end();
+
+ QTest::newRow("rgb32 -> rgb444 (no dither)") << rgb32 << QImage::Format_RGB444 << 0 << 16;
+ QTest::newRow("rgb32 -> rgb444 (dithering)") << rgb32 << QImage::Format_RGB444 << int(Qt::PreferDither | Qt::OrderedDither) << 33;
+ QTest::newRow("rgb32 -> argb4444pm (dithering)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 33;
+ QTest::newRow("rgb32 -> rgb16 (no dither)") << rgb32 << QImage::Format_RGB16 << 0 << 32;
+ QTest::newRow("rgb32 -> rgb16 (dithering)") << rgb32 << QImage::Format_RGB16 << int(Qt::PreferDither | Qt::OrderedDither) << 65;
+ QTest::newRow("rgb32 -> rgb666 (no dither)") << rgb32 << QImage::Format_RGB666 << 0 << 64;
+ QTest::newRow("rgb32 -> rgb666 (dithering)") << rgb32 << QImage::Format_RGB666 << int(Qt::PreferDither | Qt::OrderedDither) << 129;
+
+ // Test we get the same results for opaque input in the ARGBPM implementation.
+ rgb32 = qMove(rgb32).convertToFormat(QImage::Format_ARGB32_Premultiplied);
+ QTest::newRow("argb32pm -> argb4444pm (no dither)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << 0 << 16;
+ QTest::newRow("argb32pm -> rgb444 (dithering)") << rgb32 << QImage::Format_RGB444 << int(Qt::PreferDither | Qt::OrderedDither) << 33;
+ QTest::newRow("argb32pm -> argb4444pm (dithering)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 33;
+ QTest::newRow("argb32pm -> argb8565pm (no dither)") << rgb32 << QImage::Format_ARGB8565_Premultiplied << 0 << 32;
+ QTest::newRow("argb32pm -> argb8565pm (dithering)") << rgb32 << QImage::Format_ARGB8565_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 65;
+ QTest::newRow("argb32pm -> argb6666pm (no dither)") << rgb32 << QImage::Format_ARGB6666_Premultiplied << 0 << 64;
+ QTest::newRow("argb32pm -> argb6666pm (dithering)") << rgb32 << QImage::Format_ARGB6666_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 129;
+
+ QImage rgb30(1024, 16, QImage::Format_RGB30);
+ QLinearGradient gradient30(QRectF(rgb30.rect()).topLeft(), QRectF(rgb30.rect()).topRight());
+ gradient30.setColorAt(0.0, QColor(0, 0, 0));
+ gradient30.setColorAt(1.0, QColor(255, 255, 255));
+ p.begin(&rgb30);
+ p.fillRect(rgb30.rect(), gradient30);
+ p.end();
+
+ QTest::newRow("rgb30 -> rgb32 (no dither)") << rgb30 << QImage::Format_RGB32 << 0 << 256;
+ QTest::newRow("rgb30 -> rgb32 (dithering)") << rgb30 << QImage::Format_RGB32 << int(Qt::PreferDither | Qt::OrderedDither) << 513;
+ QTest::newRow("rgb30 -> rgb888 (no dither)") << rgb30 << QImage::Format_RGB888 << 0 << 256;
+ QTest::newRow("rgb30 -> rgb888 (dithering)") << rgb30 << QImage::Format_RGB888 << int(Qt::PreferDither | Qt::OrderedDither) << 513;
+}
+
+void tst_QImage::ditherGradient()
+{
+ QFETCH(QImage, image);
+ QFETCH(QImage::Format, format);
+ QFETCH(int, flags);
+ QFETCH(int, minimumExpectedGradient);
+
+ QImage converted = image.convertToFormat(format, (Qt::ImageConversionFlags)flags);
+ int observedGradientSteps = 0;
+ int lastTotal = -1;
+ for (int i = 0; i < converted.width(); ++i) {
+ int total = 0;
+ for (int j = 0; j < converted.height(); ++j) {
+ uint c = converted.pixel(i, j);
+ QCOMPARE(qAlpha(c), 255);
+ total += qRed(c);
+ }
+ if (total > lastTotal) {
+ observedGradientSteps++;
+ lastTotal = total;
+ }
+ }
+ QVERIFY(observedGradientSteps >= minimumExpectedGradient);
+}
+
QTEST_GUILESS_MAIN(tst_QImage)
#include "tst_qimage.moc"