mirror of
https://invent.kde.org/frameworks/kimageformats.git
synced 2026-11-18 12:08:48 -05:00
PCX: fix writing of indexed images with few colors
The palette was filled with 16 (1 bit, 4 planes) or 256 (8 bits) entries read with QImage::color(), reading past the end of the color table of images with fewer colors. Entries past the color table (all of them for an image without one) are now written as black, as the DDS plugin does. The 4-plane writer set BytesPerLine to width / 8, so for widths not a multiple of 8 the last pixels of each line were set one byte past the end of the line buffers (an assertion in debug builds) and dropped from the file, which the plugin then could not read back. Images narrower than 8 pixels could not be saved at all. It is now (width + 7) / 8. The 1-bit and 8-bit writers copied whole scanlines of the image into the file, including their uninitialized padding bytes. Only the bytes used by the pixels are copied now. Added indexed images with 3, 5, 16 and 40 colors (6, 13, 16 and 17 pixels wide) to autotests/write/basic, with the .qoi and .rgb files of the formats whose output is checked byte for byte. writetest now compares indexed images as RGB32 or ARGB32, since fuzzyeq() only takes 24, 32 and 64 bit images.
This commit is contained in:
committed by
Mirco Miranda
parent
2e36a53947
commit
f68108cfa8
Binary file not shown.
|
After Width: | Height: | Size: 165 B |
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 119 B |
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 275 B |
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 155 B |
Binary file not shown.
Binary file not shown.
@@ -230,6 +230,10 @@ int basicTest(const QString &suffix, bool lossless, bool ignoreDataCheck, bool s
|
||||
reReadImage.convertToColorSpace(QColorSpace(QColorSpace::SRgb));
|
||||
}
|
||||
}
|
||||
// fuzzyeq() compares images of 24, 32 or 64 bits per pixel only (e.g. not the indexed ones)
|
||||
if (pngImage.depth() < 24) {
|
||||
pngImage.convertTo(pngImage.hasAlphaChannel() ? QImage::Format_ARGB32 : QImage::Format_RGB32);
|
||||
}
|
||||
reReadImage = reReadImage.convertToFormat(pngImage.format());
|
||||
}
|
||||
|
||||
|
||||
@@ -645,12 +645,14 @@ static bool writeImage1(const QImage &image, QDataStream &s, PCXHEADER &header)
|
||||
|
||||
auto rgb = image.color(0);
|
||||
auto minIsBlack = (qRed(rgb) + qGreen(rgb) + qBlue(rgb)) / 3 < 127;
|
||||
// only the bytes used by the pixels are copied: the scanline padding is not initialized
|
||||
const int bpl = std::min(int(header.BytesPerLine), (header.width() + 7) / 8);
|
||||
QByteArray buf(header.BytesPerLine, 0);
|
||||
for (int y = 0; y < header.height(); ++y) {
|
||||
auto p = scl.convertedScanLine(image, y);
|
||||
|
||||
// Invert as QImage uses reverse palette for monochrome images?
|
||||
for (int i = 0; i < header.BytesPerLine; ++i) {
|
||||
for (int i = 0; i < bpl; ++i) {
|
||||
buf[i] = minIsBlack ? p[i] : ~p[i];
|
||||
}
|
||||
|
||||
@@ -665,13 +667,14 @@ static bool writeImage4(const QImage &image, QDataStream &s, PCXHEADER &header)
|
||||
{
|
||||
header.Bpp = 1;
|
||||
header.NPlanes = 4;
|
||||
header.BytesPerLine = header.width() / 8;
|
||||
header.BytesPerLine = (header.width() + 7) / 8;
|
||||
if (header.BytesPerLine == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto colorTable = image.colorTable();
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
header.ColorMap.setColor(i, image.color(i));
|
||||
header.ColorMap.setColor(i, i < colorTable.size() ? colorTable.at(i) : qRgb(0, 0, 0));
|
||||
}
|
||||
|
||||
s << header;
|
||||
@@ -729,12 +732,14 @@ static bool writeImage8(const QImage &image, QDataStream &s, PCXHEADER &header)
|
||||
|
||||
s << header;
|
||||
|
||||
// only the bytes used by the pixels are copied: the scanline padding is not initialized
|
||||
const int bpl = std::min(int(header.BytesPerLine), header.width());
|
||||
QByteArray buf(header.BytesPerLine, 0);
|
||||
|
||||
for (int y = 0; y < header.height(); ++y) {
|
||||
auto p = scl.convertedScanLine(image, y);
|
||||
|
||||
for (int i = 0; i < header.BytesPerLine; ++i) {
|
||||
for (int i = 0; i < bpl; ++i) {
|
||||
buf[i] = p[i];
|
||||
}
|
||||
|
||||
@@ -748,11 +753,12 @@ static bool writeImage8(const QImage &image, QDataStream &s, PCXHEADER &header)
|
||||
s << byte;
|
||||
|
||||
// Write palette
|
||||
const auto colorTable = image.colorTable();
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
if (tfmt != QImage::Format_Indexed8)
|
||||
s << RGB::from(qRgb(i, i, i));
|
||||
else
|
||||
s << RGB::from(image.color(i));
|
||||
s << RGB::from(i < colorTable.size() ? colorTable.at(i) : qRgb(0, 0, 0));
|
||||
}
|
||||
|
||||
return (s.status() == QDataStream::Ok);
|
||||
|
||||
Reference in New Issue
Block a user