From ee3ac3a3bfb4c4a0802f6530f61995297c573646 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Wed, 12 Apr 2017 16:38:40 +0200 Subject: [PATCH] Fix PNGs saved from QImage transform of 8-bit images Fixes two separate errors. QImage::transform was incorrectly adding colors to the color-table of the returned image when the converted image would not be indexed, and qpnghandler was looking at non-empty color- table instead of color format. Task-number: QTBUG-43708 Change-Id: Ife14b6428ca65ac7d3a0b36a89a73e56d64586b4 Reviewed-by: Eirik Aavitsland --- src/gui/image/qimage.cpp | 2 +- src/gui/image/qpnghandler.cpp | 2 +- tests/auto/gui/image/qimage/tst_qimage.cpp | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index 23b05fa874..9696584f36 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -4671,7 +4671,7 @@ QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode } // initizialize the data - if (d->format == QImage::Format_Indexed8) { + if (target_format == QImage::Format_Indexed8) { if (dImage.d->colortable.size() < 256) { // colors are left in the color table, so pick that one as transparent dImage.d->colortable.append(0x0); diff --git a/src/gui/image/qpnghandler.cpp b/src/gui/image/qpnghandler.cpp index 1d19c165fc..9506a95506 100644 --- a/src/gui/image/qpnghandler.cpp +++ b/src/gui/image/qpnghandler.cpp @@ -830,7 +830,7 @@ bool QPNGImageWriter::writeImage(const QImage& image, volatile int quality_in, c int color_type = 0; - if (image.colorCount()) { + if (image.format() <= QImage::Format_Indexed8) { if (image.isGrayscale()) color_type = PNG_COLOR_TYPE_GRAY; else diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index 21481e374d..fac785ac86 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -211,6 +211,8 @@ private slots: void reinterpretAsFormat2(); + void complexTransform8bit(); + #ifdef Q_OS_DARWIN void toCGImage_data(); void toCGImage(); @@ -3366,6 +3368,18 @@ void tst_QImage::reinterpretAsFormat2() } } +void tst_QImage::complexTransform8bit() +{ + QImage img1(100, 100, QImage::Format_RGB32); + img1.fill(Qt::green); + img1 = img1.convertToFormat(QImage::Format_Indexed8); + QImage img2 = img1.transformed(QTransform().rotate(45), Qt::SmoothTransformation); + // Currently the format is always QImage::Format_ARGB32_Premultiplied, but it + // doesn't have to be, and if it becomes indexed this test is no longer be valid. + QVERIFY(img2.format() > QImage::Format_Indexed8); + QCOMPARE(img2.colorCount(), 0); +} + #ifdef Q_OS_DARWIN void tst_QImage::toCGImage_data()