瀏覽代碼

fix bug reading colormapped tga files

David Rose 17 年之前
父節點
當前提交
99221e46ec
共有 2 個文件被更改,包括 17 次插入7 次删除
  1. 15 2
      panda/src/pnmimagetypes/pnmFileTypeTGA.cxx
  2. 2 5
      panda/src/pnmimagetypes/pnmFileTypeTGA.h

+ 15 - 2
panda/src/pnmimagetypes/pnmFileTypeTGA.cxx

@@ -80,6 +80,9 @@ struct ImageHeader {
 
 typedef char ImageIDField[256];
 
+/* Max number of colors allowed for colormapped output. */
+#define TGA_MAXCOLORS 257
+
 /* Definitions for image types. */
 #define TGA_Null 0
 #define TGA_Map 1
@@ -190,6 +193,8 @@ Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
   tga_head = new ImageHeader;
   RLE_count = 0;
   RLE_flag = 0;
+  ColorMap = NULL;
+  AlphaMap = NULL;
 
     /* Read the Targa file header. */
     readtga( file, tga_head, magic_number );
@@ -288,8 +293,10 @@ Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
         unsigned int temp1, temp2;
         temp1 = tga_head->Index_lo + tga_head->Index_hi * 256;
         temp2 = tga_head->Length_lo + tga_head->Length_hi * 256;
-        if ( ( temp1 + temp2 + 1 ) >= TGA_MAXCOLORS )
-            pm_error( "too many colors - %d", ( temp1 + temp2 + 1 ) );
+        int num_colors = temp1 + temp2 + 1;
+        nassertv(ColorMap == NULL && AlphaMap == NULL);
+        ColorMap = (pixel *)PANDA_MALLOC_ARRAY(num_colors * sizeof(pixel));
+        AlphaMap = (gray *)PANDA_MALLOC_ARRAY(num_colors * sizeof(gray));
         for ( unsigned int i = temp1; i < ( temp1 + temp2 ); ++i )
             get_map_entry( file, &ColorMap[i], (int) tga_head->CoSize,
                        &AlphaMap[i]);
@@ -317,6 +324,12 @@ Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
 PNMFileTypeTGA::Reader::
 ~Reader() {
   delete tga_head;
+  if (ColorMap != NULL) {
+    PANDA_FREE_ARRAY(ColorMap);
+  }
+  if (AlphaMap != NULL) {
+    PANDA_FREE_ARRAY(AlphaMap);
+  }
 }
 
 ////////////////////////////////////////////////////////////////////

+ 2 - 5
panda/src/pnmimagetypes/pnmFileTypeTGA.h

@@ -26,9 +26,6 @@
 struct ImageHeader;
 
 
-/* Max number of colors allowed for colormapped output. */
-#define TGA_MAXCOLORS 257
-
 ////////////////////////////////////////////////////////////////////
 //       Class : PNMFileTypeTGA
 // Description : For reading and writing Targa image files.
@@ -64,8 +61,8 @@ public:
 
     int rows, cols, rlencoded, mapped;
     struct ImageHeader *tga_head;
-    pixel ColorMap[TGA_MAXCOLORS];
-    gray AlphaMap[TGA_MAXCOLORS];
+    pixel *ColorMap;
+    gray *AlphaMap;
     int RLE_count, RLE_flag;
   };