Re: [AD] Bug in TGA loading code?

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Rikard Ljungstrand wrote:

Hello

I think there is a bug in Allegro's TGA loading code causing it to crash on loading some TGAs and to load others incorrectly. In the below archive are my example TGAs and a text document describing how Allegro reacts to the different files. I unfortunately don't know the internals of TGA, thus I don't have the skill to fix the bug myself.
I'm using Allegro WIP 4.1.16, built for Windows, Mingw/GCC.

The archive can be found (at least for a few weeks) at:
http://stor.no-ip.org/~rlj/tga_crash.zip

Thanks for the test cases. It appears PSP is violating the TGA spec (according to the spec, the Color Map Length field "should" be set to 0 if the Color Map Type field is set to zero, i.e. no palette). Nevertheless it is something Allegro ought to be able to handle just fine, so a fix is attached.

Peter
Index: src/tga.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/tga.c,v
retrieving revision 1.14
diff -u -c -r1.14 tga.c
cvs diff: conflicting specifications of output style
*** src/tga.c	14 Feb 2004 20:22:34 -0000	1.14
--- src/tga.c	29 Nov 2004 00:00:05 -0000
***************
*** 290,316 ****
  
     pack_fread(image_id, id_length, f);
  
!    for (i = 0; i < palette_colors; i++) {
  
!       switch (palette_entry_size) {
  
! 	 case 16: 
! 	    c = pack_igetw(f);
! 	    image_palette[i][0] = (c & 0x1F) << 3;
! 	    image_palette[i][1] = ((c >> 5) & 0x1F) << 3;
! 	    image_palette[i][2] = ((c >> 10) & 0x1F) << 3;
! 	    break;
  
! 	 case 24:
! 	 case 32:
! 	    image_palette[i][0] = pack_getc(f);
! 	    image_palette[i][1] = pack_getc(f);
! 	    image_palette[i][2] = pack_getc(f);
! 	    if (palette_entry_size == 32)
! 	       pack_getc(f);
! 	    break;
        }
     }
  
     /* Image type:
      *    0 = no image data
--- 290,323 ----
  
     pack_fread(image_id, id_length, f);
  
!    if (palette_type == 1) {
  
!       for (i = 0; i < palette_colors; i++) {
  
! 	 switch (palette_entry_size) {
  
! 	    case 16: 
! 	       c = pack_igetw(f);
! 	       image_palette[i][0] = (c & 0x1F) << 3;
! 	       image_palette[i][1] = ((c >> 5) & 0x1F) << 3;
! 	       image_palette[i][2] = ((c >> 10) & 0x1F) << 3;
! 	       break;
! 
! 	    case 24:
! 	    case 32:
! 	       image_palette[i][0] = pack_getc(f);
! 	       image_palette[i][1] = pack_getc(f);
! 	       image_palette[i][2] = pack_getc(f);
! 	       if (palette_entry_size == 32)
! 		  pack_getc(f);
! 	       break;
! 	 }
        }
     }
+    else if (palette_type != 0) {
+       pack_fclose(f);
+       return NULL;
+    }
  
     /* Image type:
      *    0 = no image data


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/