From 0378bd67e12c9e7602c70f7d64a1216a76bd2e38 Mon Sep 17 00:00:00 2001 From: Mirco Miranda Date: Thu, 24 Oct 2024 15:07:44 +0000 Subject: [PATCH] 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. --- autotests/read/tga/bw.tga | Bin 3090 -> 485 bytes autotests/read/tga/bwa.tga | Bin 4114 -> 737 bytes src/imageformats/tga.cpp | 27 +++++++++++++++++++-------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/autotests/read/tga/bw.tga b/autotests/read/tga/bw.tga index 0ae195a82cb03a65938966091140981f20d258b7..2cd375eab6e7d865ed07faed78f060d7fa282a7f 100644 GIT binary patch literal 485 zcmZQz;AVgT1qKl0P?-N81e^ae{`u1Qk?Y;_XYby>dhzVhljn^un4Uj<*7$_^`HQ!| z8vi%_XZ>?~c6)P4V=#M3RZ^6{y=Pcs6j!KEpoeEjOwp-dAQfMxYegNA{k4`w!yK){oquf4nN;VGn!!>-XP3jsKW`|N8x}@qh1s z*3X@SY(7uALCha(l-Wa{gPFe%8gfFJf6rO6_&kM(9yaEHivHSbz!~)dCQ3lR0#wzW>bszJL7H1CBCiJTim?g}R1$27CJZ=|;GD`nc*b003Yd B_{snP literal 3090 zcmeH}&r4!a6vxfX(UOs($f(5wH;s#a(576B1O*9!#8iuP746!kt6GeZ8Lev9qJO|e z>7r%2h=Nckw@N$Hia=vfP)eKm@MPlQ2+tR}Xy`2Doa4FYo_o&seeYGN)T+NCs05Ws z`28sjcc5G@7Yc=6qU3V9ySuyF+gp^!$H#|W z)6-MUwj{s4zCJiO*xugW+}!kdJU>K1SzljYUS3{WT5>oXHk-|Ew=XU(Vj>F*3$wGc zb8~YuGc)+-=jUB6*V@`zEEcQTg39;z_Xh_D`}+ENdwV-OJHLp6(%s$N-rla)>)YDe z)M|BWYb*a?Rz;)HVB$p4>2ySPb#-~YUfLi%f;7L$Wa{bZ`6>!Ze}DfsQH(}oOG^vc z1VI=Y8X6fH85hB&m)mYG#X_DFE20B{OHp8R;OJ3 z&FAx_Qi%~W3xmNx{q+Cl=7tVGKR;J06&8{W!K^ybYm&>F?6R}7!#FqsGX*WOOB4lg zgu}x_v)N4jzy@S0S}q&R*-uYT86U7|Mt=Hyb#*l{F#!(g51*B6Ns0ofAGCl#K;De{ z0S4Fu^Yn)=7jjTko^0@I{h$4>+bjBia&j^}JPd<5oz8dfKi?ND1kZJJbU@e8Mcp#q z?1Ue7cXtN{2B7PWjSViHH>aw*5U%t4{ZKdJ^Z9D%z3vi4P7=b~)%++QF@ID0!+g2O zxGS49U+@_W1}zo~_*_|8d3SumKNlAlQ&Usizizji8&(lKWP`a<$;rcjx5vNF8`*c86hKN1dosr z_>O}?kk)bho!ftGODj!JCi|r)pVIXIkY-AiD}9!B(h-Xq-|qcX#E8rA2NB%_KRxs0Qm_esOEH?9Yq%rr8yNtIf&#^&JE!G8ZRo>H9oi3 TckBDj&F1Rz^x=GSu|Crmh=Y>Oe*UjbHoxrvu0{6)??puwi3Z_%b_|}wIU5zO zqmS6ZHe3sSr>LkU(L4Nw`A6Y75EZUJ_cHTa18Y%-sBou3cTU BfVltw diff --git a/src/imageformats/tga.cpp b/src/imageformats/tga.cpp index ccb08c2..b1193e6 100644 --- a/src/imageformats/tga.cpp +++ b/src/imageformats/tga.cpp @@ -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(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.