Bug loading TGA with 32-bit palette

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
deltaflux
Posts: 1
Joined: 2011-01-20T09:38:13-07:00
Authentication code: 8675308

Bug loading TGA with 32-bit palette

Post by deltaflux »

I have a TGA file with a 32-bit palette (so 256 colours, but each colour is RGBA) which was hanging during loading (link below). It loads fine in GIMP so I'm fairly confident that the file itself is ok.
http://rapidshare.com/#!download|616l34 ... stem.tga|1


I found a section of code in tga.c which looks wrong, namely in ReadTGAImage it does:

Code: Select all

case 24:
case 32:
{
    /*
    8 bits each of blue, green and red.
    */
    pixel.blue=ScaleCharToQuantum((unsigned char)
        ReadBlobByte(image));
    pixel.green=ScaleCharToQuantum((unsigned char)
        ReadBlobByte(image));
    pixel.red=ScaleCharToQuantum((unsigned char)
        ReadBlobByte(image));
    break;
}
In the 32-bit case it's only reading 3 chars when the image contains 4 chars per colour (I've checked this in a hex editor). I made a local change which splits off the 32 bit case and adds an extra line:

Code: Select all

pixel.opacity=ScaleCharToQuantum((unsigned char)
                ReadBlobByte(image));
The image now loads succesfully so it looks like a genuine bug. Can anyone confirm this?
kayama
Posts: 1
Joined: 2013-06-28T05:50:58-07:00
Authentication code: 6789

Re: Bug loading TGA with 32-bit palette

Post by kayama »

I had successfully converted 32bit-palette TGA with following patch.
Thanks.

Code: Select all

--- ImageMagick-6.8.6/coders/tga.c.orig	2013-04-08 01:20:50.000000000 +0900
+++ ImageMagick-6.8.6/coders/tga.c	2013-06-27 22:13:27.000000000 +0900
@@ -229,7 +229,8 @@
   image->columns=tga_info.width;
   image->rows=tga_info.height;
   alpha_bits=(tga_info.attributes & 0x0FU);
-  image->matte=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) ?
+  image->matte=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) ||
+	  (tga_info.colormap_size == 32) ?
     MagickTrue : MagickFalse;
   if ((tga_info.image_type != TGAColormap) &&
       (tga_info.image_type != TGARLEColormap))
@@ -332,6 +333,15 @@
             break;
           }
           case 24:
+          {
+            /*
+              8 bits each of blue, green and red.
+            */
+            pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
+            pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
+            pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
+            break;
+          }
           case 32:
           {
             /*
@@ -340,6 +350,7 @@
             pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
             pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
             pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
+            pixel.opacity=(Quantum) (QuantumRange-ScaleCharToQuantum((unsigned char)ReadBlobByte(image)));
             break;
           }
         }
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Bug loading TGA with 32-bit palette

Post by magick »

We can reproduce the problem you posted and have a patch in ImageMagick 6.8.6-3 Beta available by sometime tomorrow. Thanks.
Post Reply