]> git.sur5r.net Git - cc65/commitdiff
Use only as many palette entries as there are colors in the image.
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 26 Feb 2012 21:26:00 +0000 (21:26 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 26 Feb 2012 21:26:00 +0000 (21:26 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@5556 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/sp65/pcx.c

index 3a9ccb3b0c8f8bb579e9ede50a2dce43559c27d1..5ce35b5fbb7623b2ba37c4b5e66c3d18f842a13d 100644 (file)
@@ -242,6 +242,7 @@ Bitmap* ReadPCXFile (const char* Name)
     Bitmap* B;
     unsigned char* L;
     Pixel* Px;
+    unsigned MaxIdx = 0;
     unsigned X, Y;
 
 
@@ -313,6 +314,9 @@ Bitmap* ReadPCXFile (const char* Name)
 
                 /* Create pixels */
                 for (X = 0; X < P->Width; ++X, ++Px) {
+                    if (L[X] > MaxIdx) {
+                        MaxIdx = L[X];
+                    }
                     Px->Index = L[X];
                 }
             }
@@ -388,22 +392,26 @@ Bitmap* ReadPCXFile (const char* Name)
                 Error ("Invalid palette marker in PCX file `%s'", Name);
             }
 
-            /* Read the palette */
-            ReadData (F, Palette, sizeof (Palette));
-            Count = 256;
-
         } else if (EndPos == CurPos) {
 
             /* The palette is in the header */
             FileSetPos (F, 16);
-            ReadData (F, Palette, 48);
-            Count = 16;
+
+            /* Check the maximum index for safety */
+            if (MaxIdx > 15) {
+                Error ("PCX file `%s' contains more than 16 indexed colors "
+                       "but no extra palette", Name);
+            }
 
         } else {
             Error ("Error in PCX file `%s': %lu bytes at end of pixel data",
                    Name, EndPos - CurPos);
         }
 
+        /* Read the palette. We will just read what we need. */
+        Count = MaxIdx + 1;
+        ReadData (F, Palette, Count * sizeof (Palette[0]));
+
         /* Create the palette from the data */
         B->Pal = NewPalette (Count);
         for (I = 0; I < Count; ++I) {
@@ -412,6 +420,7 @@ Bitmap* ReadPCXFile (const char* Name)
             B->Pal->Entries[I].B = Palette[I][2];
             B->Pal->Entries[I].A = 0;
         }
+
     }
 
     /* Close the file */