diff libgfx.c @ 451:fdc91f2a0d27

Modularize palette reading and handling code.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 04 Nov 2012 12:06:26 +0200
parents 117f94b253af
children d1f7ddc84c7c
line wrap: on
line diff
--- a/libgfx.c	Sun Nov 04 09:41:07 2012 +0200
+++ b/libgfx.c	Sun Nov 04 12:06:26 2012 +0200
@@ -48,6 +48,25 @@
 }
 
 
+BOOL dmPaletteAlloc(DMColor **ppal, int ncolors)
+{
+    if (ppal == NULL)
+        return FALSE;
+
+    return (*ppal = dmCalloc(ncolors, sizeof(DMColor))) != NULL;
+}
+
+
+BOOL dmImageAllocPalette(DMImage *img, int ncolors)
+{
+    if (img == NULL)
+        return FALSE;
+    
+    img->ncolors = ncolors;
+    return dmPaletteAlloc(&(img->pal), ncolors);
+}
+
+
 int dmImageGetBytesPerPixel(int format)
 {
     switch (format)
@@ -64,6 +83,27 @@
 }
 
 
+static BOOL dmReadPaletteData(FILE *fp, DMColor *pal, int ncolors)
+{
+    int i;
+    
+    for (i = 0; i < ncolors; i++)
+    {
+        Uint8 colR, colG, colB;
+        if (!dm_fread_byte(fp, &colR) ||
+            !dm_fread_byte(fp, &colG) ||
+            !dm_fread_byte(fp, &colB))
+            return FALSE;
+
+        pal[i].r = colR;
+        pal[i].g = colG;
+        pal[i].b = colB;
+    }
+
+    return TRUE;
+}
+
+
 int dmWriteImageData(DMImage *img, void *cbdata, BOOL (*writeRowCB)(void *, Uint8 *, size_t), const DMImageSpec *spec)
 {
     int x, y, yscale, xscale, res = 0, rowSize, rowWidth;
@@ -843,22 +883,22 @@
     // Read VGA palette
     if (paletted)
     {
-        int i;
+        int i, ncolors;
         Uint8 tmpb;
         BOOL read;
 
         if (!dm_fread_byte(fp, &tmpb) || tmpb != 0x0C)
         {
             read = FALSE;
-            img->ncolors = 16;
+            ncolors = 16;
         }
         else
         {
             read = TRUE;
-            img->ncolors = 256;
+            ncolors = 256;
         }
 
-        if ((img->pal = dmCalloc(img->ncolors, sizeof(DMColor))) == NULL)
+        if (!dmImageAllocPalette(img, ncolors))
         {
             dmError("PCX: Could not allocate palette data!\n");
             res = DMERR_MALLOC;
@@ -867,17 +907,10 @@
         
         if (read)
         {
-            for (i = 0; i < img->ncolors; i++)
+            if (!dmReadPaletteData(fp, img->pal, ncolors))
             {
-                Uint8 tmpR, tmpG, tmpB;
-                if (!dm_fread_byte(fp, &tmpR) ||
-                    !dm_fread_byte(fp, &tmpG) ||
-                    !dm_fread_byte(fp, &tmpB))
-                    goto error;
-
-                img->pal[i].r = tmpR;
-                img->pal[i].g = tmpG;
-                img->pal[i].b = tmpB;
+                dmError("PCX: Error reading palette.\n");
+                return DMERR_FREAD;
             }
         }
         else
@@ -892,8 +925,6 @@
                 }
             }
         }
-        
-
     }
 
 error:
@@ -1265,31 +1296,19 @@
                 if (iff.bmhd.nplanes > 0 && iff.ncolors != 1 << iff.bmhd.nplanes)
                     dmMsg(2, "ILBM: Expected %d entries in CMAP.\n", 1 << iff.bmhd.nplanes);
                 
-                if (iff.ncolors == 0)
-                    break;
-                
-                // Allocate palette
-                if ((iff.pal = dmMalloc(sizeof(DMColor) * iff.ncolors)) == NULL)
+                // Read palette
+                if (iff.ncolors > 0)
                 {
-                    dmError("ILBM: Could not allocate memory for palette.\n");
-                    return DMERR_MALLOC;
-                }
-                
-                // Read palette
-                for (i = 0; i < iff.ncolors; i++)
-                {
-                    Uint8 colR, colG, colB;
-                    if (!dm_fread_byte(fp, &colR) ||
-                        !dm_fread_byte(fp, &colG) ||
-                        !dm_fread_byte(fp, &colB))
+                    if (!dmPaletteAlloc(&iff.pal, iff.ncolors))
                     {
-                        dmError("ILBM: Error reading CMAP entry #%d, broken file.\n", i);
+                        dmError("ILBM: Could not allocate palette data.\n");
+                        return DMERR_MALLOC;
+                    }
+                    if (!dmReadPaletteData(fp, iff.pal, iff.ncolors))
+                    {
+                        dmError("ILBM: Error reading CMAP.\n");
                         return DMERR_FREAD;
                     }
-
-                    iff.pal[i].r = colR;
-                    iff.pal[i].g = colG;
-                    iff.pal[i].b = colB;
                 }
 
                 if (iff.chBMHD.count && iff.chBODY.count)