TGA: Fixed GrayA image loading error

Gray TGA images with alpha were loading incorrectly and tests did not detect the error since the BW(A).TGA images were actually RGB(A) images.
This commit is contained in:
Mirco Miranda 2024-10-24 15:07:44 +00:00 committed by Albert Astals Cid
parent 7d696a81d2
commit 0378bd67e1
3 changed files with 19 additions and 8 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 485 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.0 KiB

After

Width:  |  Height:  |  Size: 737 B

View File

@ -174,15 +174,21 @@ static QImage::Format imageFormat(const TgaHeader &head)
{
auto format = QImage::Format_Invalid;
if (IsSupported(head)) {
TgaHeaderInfo info(head);
// Bits 0-3 are the numbers of alpha bits (can be zero!)
const int numAlphaBits = head.flags & 0xf;
// However alpha exists only in the 32 bit format.
if ((head.pixel_size == 32) && (head.flags & 0xf)) {
// However alpha should exists only in the 32 bit format.
if ((head.pixel_size == 32) && (numAlphaBits)) {
if (numAlphaBits <= 8) {
format = QImage::Format_ARGB32;
}
}
else {
// Anyway, GIMP also saves gray images with alpha in TGA format
} else if((info.grey) && (head.pixel_size == 16) && (numAlphaBits)) {
if (numAlphaBits == 8) {
format = QImage::Format_ARGB32;
}
} else {
format = QImage::Format_RGB32;
}
}
@ -348,8 +354,7 @@ static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img)
uchar *src = image;
for (int y = y_start; y != y_end; y += y_step) {
QRgb *scanline = (QRgb *)img.scanLine(y);
auto scanline = reinterpret_cast<QRgb *>(img.scanLine(y));
if (info.pal) {
// Paletted.
for (int x = 0; x < tga.width; x++) {
@ -359,8 +364,14 @@ static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img)
} else if (info.grey) {
// Greyscale.
for (int x = 0; x < tga.width; x++) {
scanline[x] = qRgb(*src, *src, *src);
src++;
if (tga.pixel_size == 16) {
scanline[x] = qRgba(*src, *src, *src, *(src + 1));
src += 2;
}
else {
scanline[x] = qRgb(*src, *src, *src);
src++;
}
}
} else {
// True Color.