changeset 523:d0c0c6baeb57

Split the RLE decoding from DrazPaint/Lace and Amica paint decoders to a generic function.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 21 Nov 2012 23:04:00 +0200
parents 2a70f5902b70
children 78edb9710ab7
files lib64gfx.c
diffstat 1 files changed, 28 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/lib64gfx.c	Wed Nov 21 21:20:42 2012 +0200
+++ b/lib64gfx.c	Wed Nov 21 23:04:00 2012 +0200
@@ -142,21 +142,15 @@
 }
 
 
-static int fmtDecodeDrazPaintPacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int dmDecodeGenericRLE(Uint8 **mem, Uint8 **pdstEnd, const Uint8 *src, const Uint8 *srcEnd, const Uint8 rleMarker)
 {
-    int res;
-    Uint8 rleMarker;
-    Uint8 *mem, *dst, *dstEnd;
-    const Uint8 *src, *srcEnd;
+    Uint8 *dst, *dstEnd;
 
-    if ((mem = dmMalloc(C64_RAM_SIZE)) == NULL)
-        return -1;
+    if ((*mem = dmMalloc(C64_RAM_SIZE)) == NULL)
+        return DMERR_MALLOC;
     
-    rleMarker = *(buf + 0x0d);
-    src       = buf + 0x0e;
-    srcEnd    = buf + len;
-    dst       = mem;
-    dstEnd    = mem + C64_RAM_SIZE;
+    dst       = *mem;
+    dstEnd    = *mem + C64_RAM_SIZE;
 
     while (src <= srcEnd && dst <= dstEnd)
     {
@@ -171,9 +165,24 @@
         else
             *dst++ = c;
     }
-    
-    res = dmC64DecodeGenericBMP(img, mem, dst - mem + 1, fmt);
+
+    *pdstEnd = dst;
+
+    return DMERR_OK;
+}
+
 
+static int fmtDecodeDrazPaintPacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+{
+    int res;
+    Uint8 *mem = NULL, *dstEnd;
+
+    if ((res = dmDecodeGenericRLE(&mem, &dstEnd, buf + 0x0e, buf + len, *(buf + 0x0d))) != DMERR_OK)
+        goto out;
+
+    res = dmC64DecodeGenericBMP(img, mem, dstEnd - mem + 1, fmt);
+
+out:
     dmFree(mem);
     return res;
 }
@@ -227,35 +236,15 @@
 static int fmtDecodeAmicaPaintPacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
 {
     int res;
-    Uint8 *mem, *dst, *dstEnd;
-    const Uint8 *src, *srcEnd;
+    Uint8 *mem = NULL, *dstEnd;
 
-    if ((mem = dmMalloc(C64_RAM_SIZE)) == NULL)
-        return -1;
-    
-    src    = buf;
-    srcEnd = buf + len;
-    dst    = mem;
-    dstEnd = mem + C64_RAM_SIZE;
+    if ((res = dmDecodeGenericRLE(&mem, &dstEnd, buf, buf + len, 0xC2)) != DMERR_OK)
+        goto out;
 
-    while (src <= srcEnd && dst <= dstEnd)
-    {
-        int c = *src++;
-        if (c == 0xC2 && src + 2 <= srcEnd)
-        {
-            int cnt = *src++;
-            c = *src++;
-            while (cnt-- && dst <= dstEnd)
-                *dst++ = c;
-        }
-        else
-            *dst++ = c;
-    }
+    res = dmC64DecodeGenericBMP(img, mem, dstEnd - mem + 1, fmt);
     
-    res = dmC64DecodeGenericBMP(img, mem, dst - mem + 1, fmt);
-    
+out:
     dmFree(mem);
-
     return res;
 }