diff libgfx.c @ 449:117f94b253af

Add support for CAMG chunk reading and possibly half-brite handling in IFF ILBM reader.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 04 Nov 2012 09:11:57 +0200
parents f1aab48a76fe
children fdc91f2a0d27
line wrap: on
line diff
--- a/libgfx.c	Sun Nov 04 08:41:15 2012 +0200
+++ b/libgfx.c	Sun Nov 04 09:11:57 2012 +0200
@@ -925,6 +925,7 @@
 #define IFF_ID_BMHD        0x424D4844 // "BMHD"
 #define IFF_ID_CMAP        0x434D4150 // "CMAP"
 #define IFF_ID_BODY        0x424F4459 // "BODY"
+#define IFF_ID_CAMG        0x43414D47 // "CAMG"
 
 #define IFF_MASK_NONE      0
 #define IFF_MASK_HAS_MASK  1
@@ -934,6 +935,9 @@
 #define IFF_COMP_NONE      0
 #define IFF_COMP_BYTERUN1  1
 
+#define IFF_CAMG_LACE      0x00000004
+#define IFF_CAMG_HALFBRITE 0x00000080
+#define IFF_CAMG_HAM       0x00000800
 
 typedef struct
 {
@@ -962,6 +966,7 @@
 {
     DMIFFChunk chBMHD, chCMAP, chBODY;
     DMIFFBMHD bmhd;
+    Uint32 camg;
     int ncolors;
     DMColor *pal;
     BOOL paletted;
@@ -1315,6 +1320,27 @@
                 if (iff.chCMAP.count)
                     parsed = TRUE;
                 break;
+
+
+            case IFF_ID_CAMG:
+                if (!dm_fread_be32(fp, &iff.camg))
+                {
+                    dmError("ILBM: Error reading CAMG chunk.\n");
+                    return DMERR_FREAD;
+                }
+                
+                dmMsg(2, "ILBM: CAMG value 0x%08x\n", iff.camg);
+
+                if ((iff.camg & IFF_CAMG_HAM))
+                {
+                    dmError("ILBM: HAM files are not supported.\n");
+                    return DMERR_INVALID_DATA;
+                }
+
+                if (!dmSkipIFFChunkRest(fp, &chunk, 4))
+                    return DMERR_FREAD;
+                break;
+
             
             default:
                 {
@@ -1337,6 +1363,27 @@
     // Set colormap after finishing
     if (iff.pal != NULL && iff.ncolors > 0 && *pimg != NULL)
     {
+        // If halfbrite is used, duplicate the palette
+        if (iff.camg & IFF_CAMG_HALFBRITE)
+        {
+            if (iff.ncolors > 128)
+            {
+                dmError("ILBM: Halfbrite enabled, but ncolors > 128.\n");
+                return DMERR_INVALID_DATA;
+            }
+
+            if ((iff.pal = dmRealloc(iff.pal, sizeof(DMColor) * iff.ncolors * 2)) == NULL)
+                return DMERR_MALLOC;
+            
+            for (i = 0; i < iff.ncolors; i++)
+            {
+                int i2 = iff.ncolors + i;
+                iff.pal[i2].r = iff.pal[i].r / 2;
+                iff.pal[i2].g = iff.pal[i].g / 2;
+                iff.pal[i2].b = iff.pal[i].b / 2;
+            }
+        }
+    
         (*pimg)->ncolors = iff.ncolors;
         (*pimg)->pal = iff.pal;
     }