Fix convertToFormat with color-tables

The function was only well defined from RGB32 and ARGB32PM formats,
this patch fixes it so it behaves well from all formats.

Task-number: QTBUG-63163
Change-Id: Id892531d9aaf997b707b430196c1166493792a2a
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2017-09-13 11:07:58 +02:00
parent b6e99ec056
commit 590e71a69c
2 changed files with 19 additions and 17 deletions
src/gui/image
tests/auto/gui/image/qimage

View File

@ -2096,8 +2096,8 @@ static QImage convertWithPalette(const QImage &src, QImage::Format format,
Returns a copy of the image converted to the given \a format,
using the specified \a colorTable.
Conversion from 32 bit to 8 bit indexed is a slow operation and
will use a straightforward nearest color approach, with no
Conversion from RGB formats to indexed formats is a slow operation
and will use a straightforward nearest color approach, with no
dithering.
*/
QImage QImage::convertToFormat(Format format, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags) const
@ -2105,23 +2105,12 @@ QImage QImage::convertToFormat(Format format, const QVector<QRgb> &colorTable, Q
if (!d || d->format == format)
return *this;
if (format <= QImage::Format_Indexed8 && depth() == 32) {
return convertWithPalette(*this, format, colorTable);
}
const Image_Converter *converterPtr = &qimage_converter_map[d->format][format];
Image_Converter converter = *converterPtr;
if (!converter)
if (format == QImage::Format_Invalid)
return QImage();
if (format <= QImage::Format_Indexed8)
return convertWithPalette(convertToFormat(QImage::Format_ARGB32, flags), format, colorTable);
QImage image(d->width, d->height, format);
QIMAGE_SANITYCHECK_MEMORY(image);
image.d->offset = offset();
copyMetadata(image.d, d);
converter(image.d, d, flags);
return image;
return convertToFormat(format, flags);
}
/*!

View File

@ -69,6 +69,7 @@ private slots:
void convertToFormat_data();
void convertToFormat();
void convertToFormatWithColorTable();
void convertToFormatRgb888ToRGB32();
@ -958,6 +959,18 @@ void tst_QImage::convertToFormat()
QFile::remove(QLatin1String("expected2.xpm"));
}
void tst_QImage::convertToFormatWithColorTable()
{
QVector<QRgb> colors(2);
colors[0] = 0xFF000000;
colors[1] = 0xFFFFFFFF;
for (int format = QImage::Format_RGB32; format < QImage::Format_Alpha8; ++format) {
QImage fromImage(10, 10, (QImage::Format)format);
QImage bitmap = fromImage.convertToFormat(QImage::Format_Mono, colors);
QVERIFY(!bitmap.isNull());
}
}
void tst_QImage::convertToFormatRgb888ToRGB32()
{
// 545 so width % 4 != 0. This ensure there is padding at the end of the scanlines