diff lib64gfx.c @ 510:43ea59887c69

Start work on making C64 formats encoding possible by changing DMDecodeOps to DMEncDecOps and adding fields and op enums for custom encode functions, renaming, etc. Split generic op sanity checking into a separate function in preparation for its use in generic encoding function.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 19 Nov 2012 15:06:01 +0200
parents 1ed5025c2538
children 4cdcaeb68b54
line wrap: on
line diff
--- a/lib64gfx.c	Mon Nov 19 14:04:56 2012 +0200
+++ b/lib64gfx.c	Mon Nov 19 15:06:01 2012 +0200
@@ -197,7 +197,7 @@
 }
 
 
-static BOOL fmtDrazLaceSetLaceType(DMC64Image *img, const struct _DMDecodeOp *op, const Uint8 *buf, const size_t len)
+static BOOL fmtDrazLaceSetLaceType(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len)
 {
     (void) len;
 
@@ -282,7 +282,7 @@
 }
 
 
-static BOOL fmtTruePaintSetLaceType(DMC64Image *img, const struct _DMDecodeOp *op, const Uint8 *buf, const size_t len)
+static BOOL fmtTruePaintSetLaceType(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len)
 {
     (void) op;
     (void) buf;
@@ -301,10 +301,10 @@
         NULL, NULL, NULL,
         4,
         {
-            { DT_COLOR_RAM,  0x0000, 0,  0, NULL },
-            { DT_BITMAP,     0x0800, 0,  0, NULL },
-            { DT_SCREEN_RAM, 0x0400, 0,  0, NULL },
-            { DT_BGCOLOR,    0x2740, 0,  0, NULL },
+            { DT_COLOR_RAM,    0x0000, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x0800, 0,  0, NULL, NULL },
+            { DT_SCREEN_RAM,   0x0400, 0,  0, NULL, NULL },
+            { DT_BGCOLOR,      0x2740, 0,  0, NULL, NULL },
         }
     },
 
@@ -314,12 +314,12 @@
         NULL, NULL, NULL,
         6,
         {
-            { DT_COLOR_RAM,  0x0000, 0,  0, NULL },
-            { DT_BITMAP,     0x0800, 0,  0, NULL },
-            { DT_SCREEN_RAM, 0x0400, 0,  0, NULL },
-            { DT_BGCOLOR,    0x2740, 0,  0, NULL },
-            { DT_BITMAP,     0x2800, 1,  0, NULL },
-            { DT_FUNCTION,   0x2742, 0,  1, fmtDrazLaceSetLaceType },
+            { DT_COLOR_RAM,    0x0000, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x0800, 0,  0, NULL, NULL },
+            { DT_SCREEN_RAM,   0x0400, 0,  0, NULL, NULL },
+            { DT_BGCOLOR,      0x2740, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x2800, 1,  0, NULL, NULL },
+            { DT_DEC_FUNCTION, 0x2742, 0,  1, fmtDrazLaceSetLaceType, NULL },
         }
     },
     
@@ -329,10 +329,10 @@
         NULL, NULL, NULL,
         4,
         {
-            { DT_COLOR_RAM,  0x0000, 0,  0, NULL },
-            { DT_BITMAP,     0x0800, 0,  0, NULL },
-            { DT_SCREEN_RAM, 0x0400, 0,  0, NULL },
-            { DT_BGCOLOR,    0x2740, 0,  0, NULL },
+            { DT_COLOR_RAM,    0x0000, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x0800, 0,  0, NULL, NULL },
+            { DT_SCREEN_RAM,   0x0400, 0,  0, NULL, NULL },
+            { DT_BGCOLOR,      0x2740, 0,  0, NULL, NULL },
         }
     },
 
@@ -342,12 +342,12 @@
         NULL, NULL, NULL,
         6,
         {
-            { DT_COLOR_RAM,  0x0000, 0,  0, NULL },
-            { DT_BITMAP,     0x0800, 0,  0, NULL },
-            { DT_SCREEN_RAM, 0x0400, 0,  0, NULL },
-            { DT_BGCOLOR,    0x2740, 0,  0, NULL },
-            { DT_BITMAP,     0x2800, 1,  0, NULL },
-            { DT_FUNCTION,   0x2742, 0,  1, fmtDrazLaceSetLaceType },
+            { DT_COLOR_RAM,    0x0000, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x0800, 0,  0, NULL, NULL },
+            { DT_SCREEN_RAM,   0x0400, 0,  0, NULL, NULL },
+            { DT_BGCOLOR,      0x2740, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x2800, 1,  0, NULL, NULL },
+            { DT_DEC_FUNCTION, 0x2742, 0,  1, fmtDrazLaceSetLaceType, NULL },
         }
     },
     
@@ -357,13 +357,13 @@
         NULL, NULL, NULL,
         6,
         {
-            { DT_SCREEN_RAM, 0x0000, 0,  0, NULL },
-            { DT_BGCOLOR,    0x03e8, 0,  0, NULL },
-            { DT_BITMAP,     0x0400, 0,  0, NULL },
-            { DT_BITMAP,     0x2400, 1,  0, NULL },
-            { DT_SCREEN_RAM, 0x4400, 1,  0, NULL },
-            { DT_COLOR_RAM,  0x4800, 0,  0, NULL },
-            { DT_FUNCTION,   0x0000, 0,  0, fmtTruePaintSetLaceType },
+            { DT_SCREEN_RAM,   0x0000, 0,  0, NULL, NULL },
+            { DT_BGCOLOR,      0x03e8, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x0400, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x2400, 1,  0, NULL, NULL },
+            { DT_SCREEN_RAM,   0x4400, 1,  0, NULL, NULL },
+            { DT_COLOR_RAM,    0x4800, 0,  0, NULL, NULL },
+            { DT_DEC_FUNCTION, 0x0000, 0,  0, fmtTruePaintSetLaceType, NULL },
         }
     },
     
@@ -373,10 +373,10 @@
         NULL, NULL, NULL,
         4,
         {
-            { DT_COLOR_RAM,  0x2328, 0,  0, NULL },
-            { DT_BITMAP,     0x0000, 0,  0, NULL },
-            { DT_SCREEN_RAM, 0x1f40, 0,  0, NULL },
-            { DT_BGCOLOR,    0x2710, 0,  0, NULL },
+            { DT_COLOR_RAM,    0x2328, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x0000, 0,  0, NULL, NULL },
+            { DT_SCREEN_RAM,   0x1f40, 0,  0, NULL, NULL },
+            { DT_BGCOLOR,      0x2710, 0,  0, NULL, NULL },
         }
     },
 
@@ -386,10 +386,10 @@
         NULL, NULL, NULL,
         4,
         {
-            { DT_COLOR_RAM,  0x2328, 0,  0, NULL },
-            { DT_BITMAP,     0x0000, 0,  0, NULL },
-            { DT_SCREEN_RAM, 0x1f40, 0,  0, NULL },
-            { DT_BGCOLOR,    0x2710, 0,  0, NULL },
+            { DT_COLOR_RAM,    0x2328, 0,  0, NULL, NULL },
+            { DT_BITMAP,       0x0000, 0,  0, NULL, NULL },
+            { DT_SCREEN_RAM,   0x1f40, 0,  0, NULL, NULL },
+            { DT_BGCOLOR,      0x2710, 0,  0, NULL, NULL },
         }
     },
     
@@ -423,6 +423,26 @@
 }
 
 
+static int dmC64SanityCheckEncDecOp(const int i, const DMC64EncDecOp *op)
+{
+    if (op->bank < 0 || op->bank >= C64_SCR_MAX_BANK)
+    {
+        dmError("Invalid bank %d definition in generic encode/decode operator %d @ #%d.\n",
+            op->bank, op->type, i);
+        return DMERR_INTERNAL;
+    }
+
+    if (op->type < 0 || op->type >= DT_LAST)
+    {
+        dmError("Invalid encode/decode operator type %d @ #%d.\n",
+            op->type, i);
+        return DMERR_INTERNAL;
+    }
+
+    return DMERR_OK;
+}
+
+
 int dmC64DecodeGenericBMP(DMC64Image *img, const Uint8 *buf,
     const size_t len, const DMC64ImageFormat *fmt)
 {
@@ -431,31 +451,22 @@
     memset(img, 0, sizeof(*img));
     img->type = fmt->type;
 
-    for (i = 0; i < fmt->ndecodeOps; i++)
+    for (i = 0; i < fmt->ndecencOps; i++)
     {
-        const DMDecodeOp *op = &fmt->decodeOps[i];
+        const DMC64EncDecOp *op = &fmt->decencOps[i];
         const Uint8 *src;
         size_t size;
+        int res;
 
-        if (op->bank < 0 || op->bank >= C64_SCR_MAX_BANK)
-        {
-            dmError("Invalid bank %d definition in generic decode operator %d @ #%d.\n",
-                op->bank, op->type, i);
-            return DMERR_INTERNAL;
-        }
-        
-        if (op->type < 0 || op->type >= DT_LAST)
-        {
-            dmError("Invalid decode operator type %d @ #%d.\n",
-                op->type, i);
-            return DMERR_INTERNAL;
-        }
+        if ((res = dmC64SanityCheckEncDecOp(i, op)) != DMERR_OK)
+            return res;
         
         size = (op->size == 0) ? dmC64DefaultSizes[op->type] : op->size;
         
         if (op->offs + size > len)
         {
-            dmError("Decode out of bounds, op #%d type=%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x\n",
+            dmError("Decode out of bounds, op #%d type=%d, offs=%d ($%04x), "
+                "bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
                 i, op->type, op->offs, op->offs, op->bank, size, size, len, len);
             return DMERR_INVALID_DATA;
         }
@@ -469,16 +480,18 @@
             case DT_SCREEN_RAM:  memcpy(img->screen[op->bank], src, size); break;
             case DT_BGCOLOR:     img->bgcolor = *src; break;
             case DT_EXTRADATA:   memcpy(img->extradata, src, size); break;
-            case DT_FUNCTION:
-                if (op->function == NULL)
+            case DT_DEC_FUNCTION:
+                if (op->decfunction == NULL)
                 {
-                    dmError("Decode op is a function, but function ptr is NULL: op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x\n",
+                    dmError("Decode op is a function, but function ptr is NULL: "
+                        "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
                         i, op->offs, op->offs, op->bank, size, size, len, len);
                     return DMERR_INTERNAL;
                 }
-                if (!op->function(img, op, buf, len))
+                if (!op->decfunction(img, op, buf, len))
                 {
-                    dmError("Decode op custom function failed: op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x\n",
+                    dmError("Decode op custom function failed: op #%d, "
+                        "offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
                         i, op->offs, op->offs, op->bank, size, size, len, len);
                     return DMERR_INTERNAL;
                 }