diff options
Diffstat (limited to 'tests/auto/gui/image')
-rw-r--r-- | tests/auto/gui/image/image.pro | 1 | ||||
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/icons/testtheme/16x16/actions/appointment-new.png | bin | 0 -> 897 bytes | |||
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/icons/testtheme/22x22/actions/appointment-new.png | bin | 0 -> 1411 bytes | |||
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/icons/testtheme/22x22@2/actions/appointment-new.png | bin | 0 -> 1740 bytes | |||
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/icons/testtheme/index.theme | 21 | ||||
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/qiconhighdpi.pro | 8 | ||||
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp | 195 | ||||
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc | 8 | ||||
-rw-r--r-- | tests/auto/gui/image/qimage/tst_qimage.cpp | 97 | ||||
-rw-r--r-- | tests/auto/gui/image/qimagereader/tst_qimagereader.cpp | 76 | ||||
-rw-r--r-- | tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp | 87 | ||||
-rw-r--r-- | tests/auto/gui/image/qpixmap/tst_qpixmap.cpp | 42 |
12 files changed, 463 insertions, 72 deletions
diff --git a/tests/auto/gui/image/image.pro b/tests/auto/gui/image/image.pro index 5a74df4c78..f4e2ab20ba 100644 --- a/tests/auto/gui/image/image.pro +++ b/tests/auto/gui/image/image.pro @@ -10,6 +10,7 @@ SUBDIRS=\ qmovie \ qpicture \ qicon \ + qiconhighdpi !qtHaveModule(network): SUBDIRS -= \ qimagereader diff --git a/tests/auto/gui/image/qiconhighdpi/icons/testtheme/16x16/actions/appointment-new.png b/tests/auto/gui/image/qiconhighdpi/icons/testtheme/16x16/actions/appointment-new.png Binary files differnew file mode 100644 index 0000000000..18b7c6781e --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/icons/testtheme/16x16/actions/appointment-new.png diff --git a/tests/auto/gui/image/qiconhighdpi/icons/testtheme/22x22/actions/appointment-new.png b/tests/auto/gui/image/qiconhighdpi/icons/testtheme/22x22/actions/appointment-new.png Binary files differnew file mode 100644 index 0000000000..d676ffd463 --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/icons/testtheme/22x22/actions/appointment-new.png diff --git a/tests/auto/gui/image/qiconhighdpi/icons/testtheme/22x22@2/actions/appointment-new.png b/tests/auto/gui/image/qiconhighdpi/icons/testtheme/22x22@2/actions/appointment-new.png Binary files differnew file mode 100644 index 0000000000..6d094d7b54 --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/icons/testtheme/22x22@2/actions/appointment-new.png diff --git a/tests/auto/gui/image/qiconhighdpi/icons/testtheme/index.theme b/tests/auto/gui/image/qiconhighdpi/icons/testtheme/index.theme new file mode 100644 index 0000000000..6ab6c15c42 --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/icons/testtheme/index.theme @@ -0,0 +1,21 @@ +[Icon Theme] +Name=Test +Comment=Test Theme + +Directories=16x16/actions,22x22/actions,22x22@2/actions + +[16x16/actions] +Size=16 +Context=Actions +Type=Fixed + +[22x22/actions] +Size=22 +Context=Actions +Type=Fixed + +[22x22@2/actions] +Size=22 +Context=Actions +Scale=2 +Type=Fixed diff --git a/tests/auto/gui/image/qiconhighdpi/qiconhighdpi.pro b/tests/auto/gui/image/qiconhighdpi/qiconhighdpi.pro new file mode 100644 index 0000000000..17553158bc --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/qiconhighdpi.pro @@ -0,0 +1,8 @@ +CONFIG += testcase +TARGET = tst_qicon + +QT += testlib +SOURCES += tst_qiconhighdpi.cpp +RESOURCES = tst_qiconhighdpi.qrc + +TESTDATA += icons/* diff --git a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp new file mode 100644 index 0000000000..ce7f68a0a6 --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp @@ -0,0 +1,195 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <qicon.h> + +class tst_QIconHighDpi : public QObject +{ + Q_OBJECT +public: + tst_QIconHighDpi(); + +private slots: + void initTestCase(); + void fromTheme_data(); + void fromTheme(); +}; + +tst_QIconHighDpi::tst_QIconHighDpi() +{ +} + +void tst_QIconHighDpi::initTestCase() +{ +} + +void tst_QIconHighDpi::fromTheme_data() +{ + QTest::addColumn<int>("requestedSize"); + QTest::addColumn<int>("expectedSize"); + QTest::addColumn<qreal>("expectedDpr"); + + // The pixmaps that we have available can be found in tst_qiconhighdpi.qrc. + // Currently we only have @1 and @2 icons available. + const int dpr = qCeil(qApp->devicePixelRatio()); + + // We have an @2 high DPI version of the 22x22 size of this icon. + switch (dpr) { + case 1: QTest::newRow("22x22,dpr=1") << 22 << 22 << 1.0; break; + case 2: QTest::newRow("22x22,dpr=2") << 22 << 44 << 2.0; break; + case 3: QTest::newRow("22x22,dpr=3") << 22 << 44 << 2.0; break; + } + + // We don't have a high DPI version of the 16x16 size of this icon, + // so directoryMatchesSize() will return false for all directories. + // directorySizeDistance() is then called to find the best match. + // The table below illustrates the results for our available images at various DPRs: + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 16 * 1 = 28 + // 22 * 1 - 16 * 1 = 6 + // 16 * 1 - 16 * 1 = 0 < (16x16) + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 16 * 2 = 12 + // 22 * 1 - 16 * 2 = 10 < (22x22) + // 16 * 1 - 16 * 2 = 16 + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 16 * 3 = 4 < (22x22@2) + // 22 * 1 - 16 * 3 = 26 + // 16 * 1 - 16 * 3 = 32 + // Both of these functions are implementations of the freedesktop icon theme spec, + // which dictates that if there is no matching scale, directorySizeDistance() determines + // the winner, regardless of whether or not the scale is too low for the requested scale. + switch (dpr) { + case 1: QTest::newRow("16x16,dpr=1") << 16 << 16 << 1.0; break; + // PixmapEntry::pixmap() will only downscale the pixmap if actualSize.width() > size.width(). + // In this case, 22 > 32 is false, so a 22x22 pixmap is returned. + case 2: QTest::newRow("16x16,dpr=2") << 16 << 22 << 1.375; break; + case 3: QTest::newRow("16x16,dpr=3") << 16 << 44 << 2.75; break; + } + + // We don't have an 8x8 size of this icon, so: + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 8 * 1 = 36 + // 22 * 1 - 8 * 1 = 14 + // 16 * 1 - 8 * 1 = 8 < (16x16) + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 8 * 2 = 28 + // 22 * 1 - 8 * 2 = 6 + // 16 * 1 - 8 * 2 = 0 < (16x16) + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 8 * 3 = 20 + // 22 * 1 - 8 * 3 = 2 < (22x22) + // 16 * 1 - 8 * 3 = 8 + switch (dpr) { + case 1: QTest::newRow("8x8,dpr=1") << 8 << 8 << 1.0; break; + case 2: QTest::newRow("8x8,dpr=2") << 8 << 16 << 2.0; break; + case 3: QTest::newRow("8x8,dpr=3") << 8 << 22 << 2.75; break; + } + + // We don't have a 44x44 size of this icon, so: + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 44 * 1 = 0 < (22x22@2) + // 22 * 1 - 44 * 1 = 22 + // 16 * 1 - 44 * 1 = 28 + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 44 * 2 = 44 < (22x22@2) + // 22 * 1 - 44 * 2 = 66 + // 16 * 1 - 44 * 2 = 72 + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 44 * 3 = 88 < (22x22@2) + // 22 * 1 - 44 * 3 = 110 + // 16 * 1 - 44 * 3 = 116 + switch (dpr) { + case 1: QTest::newRow("44x44,dpr=1") << 44 << 44 << 1.0; break; + case 2: QTest::newRow("44x44,dpr=2") << 44 << 44 << 1.0; break; + case 3: QTest::newRow("44x44,dpr=3") << 44 << 44 << 1.0; break; + } + + // We don't have a 20x20 size of this icon, so: + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 20 * 1 = 24 + // 22 * 1 - 20 * 1 = 2 < (22x22) + // 16 * 1 - 20 * 1 = 4 + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 20 * 2 = 4 < (22x22@2) + // 22 * 1 - 20 * 2 = 18 + // 16 * 1 - 20 * 2 = 24 + // Available size | Available scale | Requested size | Requested scale | Distance + // 22 * 2 - 20 * 3 = 16 < (22x22@2) + // 22 * 1 - 20 * 3 = 38 + // 16 * 1 - 20 * 3 = 44 + switch (dpr) { + case 1: QTest::newRow("20x20,dpr=1") << 20 << 20 << 1.0; break; + // PixmapEntry::pixmap() will only downscale the pixmap if actualSize.width() > size.width(). + // In this case, 44 > 40 is true, so the 44x44 pixmap is downscaled to 40x40. + case 2: QTest::newRow("20x20,dpr=2") << 20 << 40 << 2.0; break; + case 3: QTest::newRow("20x20,dpr=3") << 20 << 44 << 2.2; break; + } +} + +void tst_QIconHighDpi::fromTheme() +{ + QFETCH(int, requestedSize); + QFETCH(int, expectedSize); + QFETCH(qreal, expectedDpr); + + QString searchPath = QLatin1String(":/icons"); + QIcon::setThemeSearchPaths(QStringList() << searchPath); + QCOMPARE(QIcon::themeSearchPaths().size(), 1); + QCOMPARE(searchPath, QIcon::themeSearchPaths()[0]); + + QString themeName("testtheme"); + QIcon::setThemeName(themeName); + QCOMPARE(QIcon::themeName(), themeName); + + QIcon appointmentIcon = QIcon::fromTheme("appointment-new"); + QVERIFY(!appointmentIcon.isNull()); + QVERIFY(!appointmentIcon.availableSizes(QIcon::Normal, QIcon::Off).isEmpty()); + QVERIFY(appointmentIcon.availableSizes().contains(QSize(16, 16))); + QVERIFY(appointmentIcon.availableSizes().contains(QSize(22, 22))); + + const QPixmap pixmap = appointmentIcon.pixmap(requestedSize); + QCOMPARE(pixmap.size(), QSize(expectedSize, expectedSize)); + // We should get the high DPI version of an image if it exists in the correct directory. + // Note that we didn't pass the QWindow to QIcon::pixmap(), + // because QGuiApplication::devicePixelRatio() will be used if no window was given. + QCOMPARE(pixmap.devicePixelRatio(), expectedDpr); +} + +int main(int argc, char *argv[]) +{ + QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); + QGuiApplication app(argc, argv); + Q_UNUSED(app); + tst_QIconHighDpi test; + QTEST_SET_MAIN_SOURCE_PATH + return QTest::qExec(&test, argc, argv); +} + +#include "tst_qiconhighdpi.moc" diff --git a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc new file mode 100644 index 0000000000..80b5e38ee6 --- /dev/null +++ b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc @@ -0,0 +1,8 @@ +<RCC> + <qresource prefix="/"> + <file>icons/testtheme/16x16/actions/appointment-new.png</file> + <file>icons/testtheme/22x22/actions/appointment-new.png</file> + <file>icons/testtheme/index.theme</file> + <file>icons/testtheme/22x22@2/actions/appointment-new.png</file> + </qresource> +</RCC> diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index 2433fa4115..21481e374d 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -206,6 +206,11 @@ private slots: void ditherGradient_data(); void ditherGradient(); + void reinterpretAsFormat_data(); + void reinterpretAsFormat(); + + void reinterpretAsFormat2(); + #ifdef Q_OS_DARWIN void toCGImage_data(); void toCGImage(); @@ -1088,35 +1093,11 @@ void tst_QImage::rotate_data() degrees << 0 << 90 << 180 << 270; foreach (int d, degrees) { - const QByteArray dB = QByteArray::number(d); - QTest::newRow((dB + " Format_RGB32").constData()) - << QImage::Format_RGB32 << d; - QTest::newRow((dB + " Format_ARGB32").constData()) - << QImage::Format_ARGB32 << d; - QTest::newRow((dB + " Format_ARGB32_Premultiplied").constData()) - << QImage::Format_ARGB32_Premultiplied << d; - QTest::newRow((dB + " Format_RGB16").constData()) - << QImage::Format_RGB16 << d; - QTest::newRow((dB + " Format_ARGB8565_Premultiplied").constData()) - << QImage::Format_ARGB8565_Premultiplied << d; - QTest::newRow((dB + " Format_RGB666").constData()) - << QImage::Format_RGB666 << d; - QTest::newRow((dB + " Format_RGB555").constData()) - << QImage::Format_RGB555 << d; - QTest::newRow((dB + " Format_ARGB8555_Premultiplied").constData()) - << QImage::Format_ARGB8555_Premultiplied << d; - QTest::newRow((dB + " Format_RGB888").constData()) - << QImage::Format_RGB888 << d; - QTest::newRow((dB + " Format_Indexed8").constData()) - << QImage::Format_Indexed8 << d; - QTest::newRow((dB + " Format_RGBX8888").constData()) - << QImage::Format_RGBX8888 << d; - QTest::newRow((dB + " Format_RGBA8888_Premultiplied").constData()) - << QImage::Format_RGBA8888_Premultiplied << d; - QTest::newRow((dB + " Format_Alpha8").constData()) - << QImage::Format_Alpha8 << d; - QTest::newRow((dB + " Format_Grayscale8").constData()) - << QImage::Format_Grayscale8 << d; + const QString dB = QString::number(d); + for (int i = QImage::Format_Indexed8; i < QImage::NImageFormats; i++) { + QImage::Format format = static_cast<QImage::Format>(i); + QTest::newRow(qPrintable(dB + " " + formatToString(format))) << format << d; + } } } @@ -3327,6 +3308,64 @@ void tst_QImage::ditherGradient() QVERIFY(observedGradientSteps >= minimumExpectedGradient); } +void tst_QImage::reinterpretAsFormat_data() +{ + QTest::addColumn<QImage::Format>("in_format"); + QTest::addColumn<QImage::Format>("out_format"); + QTest::addColumn<QColor>("in_color"); + QTest::addColumn<QColor>("out_color"); + +#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN + QTest::newRow("rgb32 -> rgbx8888") << QImage::Format_RGB32 << QImage::Format_RGBX8888 << QColor(Qt::red) << QColor(Qt::blue); + QTest::newRow("rgba8888 -> argb32") << QImage::Format_RGBA8888 << QImage::Format_ARGB32 << QColor(Qt::red) << QColor(Qt::blue); + QTest::newRow("argb32pm -> rgba8888pm") << QImage::Format_RGBA8888_Premultiplied << QImage::Format_ARGB32_Premultiplied << QColor(Qt::green) << QColor(Qt::green); +#endif + QTest::newRow("rgb32 -> argb32") << QImage::Format_RGB32 << QImage::Format_ARGB32 << QColor(Qt::cyan) << QColor(Qt::cyan); + QTest::newRow("argb32pm -> rgb32") << QImage::Format_ARGB32_Premultiplied << QImage::Format_RGB32 << QColor(Qt::transparent) << QColor(Qt::black); + QTest::newRow("argb32 -> rgb32") << QImage::Format_ARGB32 << QImage::Format_RGB32 << QColor(255, 0, 0, 127) << QColor(255, 0, 0); + QTest::newRow("argb32pm -> rgb32") << QImage::Format_ARGB32_Premultiplied << QImage::Format_RGB32 << QColor(255, 0, 0, 127) << QColor(127, 0, 0); +} + +void tst_QImage::reinterpretAsFormat() +{ + QFETCH(QImage::Format, in_format); + QFETCH(QImage::Format, out_format); + QFETCH(QColor, in_color); + QFETCH(QColor, out_color); + + QImage image(1, 1, in_format); + image.setPixelColor(0, 0, in_color); + QVERIFY(image.reinterpretAsFormat(out_format)); + QCOMPARE(image.pixelColor(0, 0), out_color); +} + +void tst_QImage::reinterpretAsFormat2() +{ + const uint imageData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + + { + QImage image(reinterpret_cast<const uchar*>(imageData), 4, 2, QImage::Format_RGB32); + QCOMPARE(image.pixelColor(0, 0), QColor(Qt::black)); + QVERIFY(image.isDetached()); + QVERIFY(image.reinterpretAsFormat(QImage::Format_ARGB32_Premultiplied)); + QCOMPARE(image.constBits(), reinterpret_cast<const uchar*>(imageData)); + QCOMPARE(image.pixelColor(0, 0), QColor(Qt::transparent)); + + QVERIFY(!image.reinterpretAsFormat(QImage::Format_Grayscale8)); + } + { + QImage image(reinterpret_cast<const uchar*>(imageData), 8, 4, QImage::Format_Indexed8); + image.setColor(0, qRgb(255, 255, 255)); + QCOMPARE(image.pixelColor(0, 0), QColor(Qt::white)); + QVERIFY(image.reinterpretAsFormat(QImage::Format_Grayscale8)); + QCOMPARE(image.pixelColor(0, 0), QColor(Qt::black)); + QVERIFY(image.reinterpretAsFormat(QImage::Format_Alpha8)); + QCOMPARE(image.pixelColor(0, 0), QColor(Qt::transparent)); + + QVERIFY(!image.reinterpretAsFormat(QImage::Format_RGB16)); + } +} + #ifdef Q_OS_DARWIN void tst_QImage::toCGImage_data() diff --git a/tests/auto/gui/image/qimagereader/tst_qimagereader.cpp b/tests/auto/gui/image/qimagereader/tst_qimagereader.cpp index bd02dc6255..18649b3de5 100644 --- a/tests/auto/gui/image/qimagereader/tst_qimagereader.cpp +++ b/tests/auto/gui/image/qimagereader/tst_qimagereader.cpp @@ -133,6 +133,9 @@ private slots: void gifImageCount(); void gifLoopCount(); + void ppmMaxval_data(); + void ppmMaxval(); + void readCorruptImage_data(); void readCorruptImage(); void readCorruptBmp(); @@ -956,6 +959,79 @@ void tst_QImageReader::gifLoopCount() } } +void tst_QImageReader::ppmMaxval_data() +{ + QTest::addColumn<bool>("hasColor"); + QTest::addColumn<QByteArray>("bytes"); + + QTest::newRow("PGM plain 8bit full") << false << QByteArray("P2 3 1 255 255 0 127\n"); + QTest::newRow("PGM plain 8bit lim.") << false << QByteArray("P2 3 1 50 50 0 25\n"); + QTest::newRow("PGM plain 16bit full") << false << QByteArray("P2 3 1 65535 65535 0 32767\n"); + QTest::newRow("PGM plain 16bit lim.") << false << QByteArray("P2 3 1 5000 5000 0 2500\n"); + QTest::newRow("PGM raw 8bit full") << false << QByteArray("P5 3 1 255 \xff\x00\x7f", 13 + 3); + QTest::newRow("PGM raw 8bit lim.") << false << QByteArray("P5 3 1 50 \x32\x00\x19", 13 + 3); + QTest::newRow("PGM raw 16bit full") << false << QByteArray("P5 3 1 65535 \xff\xff\x00\x00\x7f\xff", 13 + 3 * 2); + QTest::newRow("PGM raw 16bit lim.") << false << QByteArray("P5 3 1 5000 \x13\x88\x00\x00\x09\xc4", 13 + 3 * 2); + + QTest::newRow("PPM plain 8bit full") << true << QByteArray("P3 3 2 255 " + "255 255 255 0 0 0 127 127 127 " + "255 0 0 0 255 0 0 0 255\n"); + + QTest::newRow("PPM plain 8bit lim.") << true << QByteArray("P3 3 2 50 " + " 50 50 50 0 0 0 25 25 25 " + " 50 0 0 0 50 0 0 0 50\n"); + + QTest::newRow("PPM plain 16bit full") << true << QByteArray("P3 3 2 65535 " + "65535 65535 65535 0 0 0 32767 32767 32767 " + "65535 0 0 0 65535 0 0 0 65535\n"); + + QTest::newRow("PPM plain 16bit lim.") << true << QByteArray("P3 3 2 5000 " + " 5000 5000 5000 0 0 0 2500 2500 2500 " + " 5000 0 0 0 5000 0 0 0 5000\n"); + + QTest::newRow("PPM raw 8bit full") << true << QByteArray("P6 3 2 255 " + "\xff\xff\xff\x00\x00\x00\x7f\x7f\x7f" + "\xff\x00\x00\x00\xff\x00\x00\x00\xff", 13 + 6 * 3); + + QTest::newRow("PPM raw 8bit lim.") << true << QByteArray("P6 3 2 50 " + "\x32\x32\x32\x00\x00\x00\x19\x19\x19" + "\x32\x00\x00\x00\x32\x00\x00\x00\x32", 13 + 6 * 3); + + QTest::newRow("PPM raw 16bit full") << true << QByteArray("P6 3 2 65535 " + "\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x7f\xff\x7f\xff\x7f\xff" + "\xff\xff\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\xff\xff", 13 + 6 * 3 * 2); + + QTest::newRow("PPM raw 16bit lim.") << true << QByteArray("P6 3 2 5000 " + "\x13\x88\x13\x88\x13\x88\x00\x00\x00\x00\x00\x00\x09\xc4\x09\xc4\x09\xc4" + "\x13\x88\x00\x00\x00\x00\x00\x00\x13\x88\x00\x00\x00\x00\x00\x00\x13\x88", 13 + 6 * 3 * 2); +} + +void tst_QImageReader::ppmMaxval() +{ + SKIP_IF_UNSUPPORTED("ppm"); + + QFETCH(bool, hasColor); + QFETCH(QByteArray, bytes); + + QImage img; + img.loadFromData(bytes); + QVERIFY(!img.isNull()); + QCOMPARE(img.width(), 3); + QCOMPARE(img.height(), hasColor ? 2 : 1); + + QCOMPARE(img.pixel(0,0), qRgb(0xff, 0xff, 0xff)); + QCOMPARE(img.pixel(1,0), qRgb(0, 0, 0)); + QRgb gray = img.pixel(2,0); + QVERIFY(qIsGray(gray)); + QVERIFY(qRed(gray) > 0x70 && qRed(gray) < 0x90 ); + + if (hasColor) { + QCOMPARE(img.pixel(0,1), qRgb(0xff, 0, 0)); + QCOMPARE(img.pixel(1,1), qRgb(0, 0xff, 0)); + QCOMPARE(img.pixel(2,1), qRgb(0, 0, 0xff)); + } +} + class Server : public QObject { Q_OBJECT diff --git a/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp b/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp index 191aabdf6a..d5c624833c 100644 --- a/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp +++ b/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp @@ -47,7 +47,7 @@ #include <algorithm> typedef QMap<QString, QString> QStringMap; -typedef QList<int> QIntList; +typedef QVector<int> QIntList; Q_DECLARE_METATYPE(QImageWriter::ImageWriterError) Q_DECLARE_METATYPE(QImage::Format) @@ -227,8 +227,15 @@ void tst_QImageWriter::writeImage2_data() QTest::addColumn<QByteArray>("format"); QTest::addColumn<QImage>("image"); - const QStringList formats = QStringList() << "bmp" << "xpm" << "png" - << "ppm" << "ico"; //<< "jpeg"; + static const QLatin1String formats[] = { + QLatin1String("bmp"), + QLatin1String("xpm"), + QLatin1String("png"), + QLatin1String("ppm"), + QLatin1String("ico"), + // QLatin1String("jpeg"), + }; + QImage image0(70, 70, QImage::Format_ARGB32); image0.fill(QColor(Qt::red).rgb()); @@ -236,11 +243,11 @@ void tst_QImageWriter::writeImage2_data() while (imgFormat != QImage::NImageFormats) { QImage image = image0.convertToFormat(imgFormat); initializePadding(&image); - foreach (const QString format, formats) { + for (QLatin1String format : formats) { const QString fileName = QLatin1String("solidcolor_") + QString::number(imgFormat) + QLatin1Char('.') + format; QTest::newRow(fileName.toLatin1()) << writePrefix + fileName - << format.toLatin1() + << QByteArray(format.data(), format.size()) << image; } imgFormat = QImage::Format(int(imgFormat) + 1); @@ -304,41 +311,38 @@ void tst_QImageWriter::writeImage2() QVERIFY(QFile::remove(fileName)); } +namespace { +// C++98-library version of C++11 std::is_sorted +template <typename C> +bool is_sorted(const C &c) +{ + return std::adjacent_find(c.begin(), c.end(), std::greater_equal<typename C::value_type>()) == c.end(); +} +} + void tst_QImageWriter::supportedFormats() { QList<QByteArray> formats = QImageWriter::supportedImageFormats(); - QList<QByteArray> sortedFormats = formats; - std::sort(sortedFormats.begin(), sortedFormats.end()); // check that the list is sorted - QCOMPARE(formats, sortedFormats); - - QSet<QByteArray> formatSet; - foreach (QByteArray format, formats) - formatSet << format; + QVERIFY(is_sorted(formats)); // check that the list does not contain duplicates - QCOMPARE(formatSet.size(), formats.size()); + QVERIFY(std::unique(formats.begin(), formats.end()) == formats.end()); } void tst_QImageWriter::supportedMimeTypes() { QList<QByteArray> mimeTypes = QImageWriter::supportedMimeTypes(); - QList<QByteArray> sortedMimeTypes = mimeTypes; - std::sort(sortedMimeTypes.begin(), sortedMimeTypes.end()); // check that the list is sorted - QCOMPARE(mimeTypes, sortedMimeTypes); - - QSet<QByteArray> mimeTypeSet; - foreach (QByteArray mimeType, mimeTypes) - mimeTypeSet << mimeType; + QVERIFY(is_sorted(mimeTypes)); // check the list as a minimum contains image/bmp - QVERIFY(mimeTypeSet.contains("image/bmp")); + QVERIFY(mimeTypes.contains("image/bmp")); // check that the list does not contain duplicates - QCOMPARE(mimeTypeSet.size(), mimeTypes.size()); + QVERIFY(std::unique(mimeTypes.begin(), mimeTypes.end()) == mimeTypes.end()); } void tst_QImageWriter::writeToInvalidDevice() @@ -412,30 +416,27 @@ void tst_QImageWriter::supportsOption() QFETCH(QString, fileName); QFETCH(QIntList, options); - QSet<QImageIOHandler::ImageOption> allOptions; - allOptions << QImageIOHandler::Size - << QImageIOHandler::ClipRect - << QImageIOHandler::Description - << QImageIOHandler::ScaledClipRect - << QImageIOHandler::ScaledSize - << QImageIOHandler::CompressionRatio - << QImageIOHandler::Gamma - << QImageIOHandler::Quality - << QImageIOHandler::Name - << QImageIOHandler::SubType - << QImageIOHandler::IncrementalReading - << QImageIOHandler::Endianness - << QImageIOHandler::Animation - << QImageIOHandler::BackgroundColor; + static Q_CONSTEXPR QImageIOHandler::ImageOption allOptions[] = { + QImageIOHandler::Size, + QImageIOHandler::ClipRect, + QImageIOHandler::Description, + QImageIOHandler::ScaledClipRect, + QImageIOHandler::ScaledSize, + QImageIOHandler::CompressionRatio, + QImageIOHandler::Gamma, + QImageIOHandler::Quality, + QImageIOHandler::Name, + QImageIOHandler::SubType, + QImageIOHandler::IncrementalReading, + QImageIOHandler::Endianness, + QImageIOHandler::Animation, + QImageIOHandler::BackgroundColor, + }; QImageWriter writer(writePrefix + fileName); - for (int i = 0; i < options.size(); ++i) { - QVERIFY(writer.supportsOption(QImageIOHandler::ImageOption(options.at(i)))); - allOptions.remove(QImageIOHandler::ImageOption(options.at(i))); + for (auto option : allOptions) { + QCOMPARE(writer.supportsOption(option), options.contains(option)); } - - foreach (QImageIOHandler::ImageOption option, allOptions) - QVERIFY(!writer.supportsOption(option)); } void tst_QImageWriter::saveWithNoFormat_data() diff --git a/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp b/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp index ad7de09c48..e3bda6c2df 100644 --- a/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp +++ b/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp @@ -99,6 +99,7 @@ private slots: void task_51271(); void convertFromImageNoDetach(); + void convertFromImageNoDetach2(); void convertFromImageDetach(); void convertFromImageCacheKey(); @@ -118,6 +119,7 @@ private slots: void refUnref(); void copy(); + void deepCopyPreservesDpr(); void depthOfNullObjects(); void transformed(); @@ -766,6 +768,33 @@ void tst_QPixmap::convertFromImageNoDetach() QCOMPARE(constOrig.bits(), constCopy.bits()); } +void tst_QPixmap::convertFromImageNoDetach2() +{ + QPixmap randomPixmap(10, 10); + if (randomPixmap.handle()->classId() != QPlatformPixmap::RasterClass) + QSKIP("Test only valid for raster pixmaps"); + + //first get the screen format + QImage::Format screenFormat = randomPixmap.toImage().format(); + QVERIFY(screenFormat != QImage::Format_Invalid); + if (screenFormat != QImage::Format_RGB32 && + screenFormat != QImage::Format_ARGB32_Premultiplied) + QSKIP("Test only valid for platforms with RGB32 pixmaps"); + + QImage orig(100,100, QImage::Format_ARGB32_Premultiplied); + orig.fill(Qt::white); + + const uchar *origBits = orig.constBits(); + + QPixmap pix = QPixmap::fromImage(std::move(orig)); + QImage copy = pix.toImage(); + + QVERIFY(!copy.hasAlphaChannel()); + QCOMPARE(copy.format(), QImage::Format_RGB32); + + QCOMPARE(origBits, copy.constBits()); +} + void tst_QPixmap::convertFromImageDetach() { QImage img(10,10, QImage::Format_RGB32); @@ -1105,6 +1134,19 @@ void tst_QPixmap::copy() QCOMPARE(trans, transCopy); } +// QTBUG-58653: Force a deep copy of a pixmap by +// having a QPainter and check whether DevicePixelRatio is preserved +void tst_QPixmap::deepCopyPreservesDpr() +{ + const qreal dpr = 2; + QPixmap src(32, 32); + src.setDevicePixelRatio(dpr); + src.fill(Qt::red); + QPainter painter(&src); + const QPixmap dest = src.copy(); + QCOMPARE(dest.devicePixelRatio(), dpr); +} + void tst_QPixmap::depthOfNullObjects() { QBitmap b1; |