changeset 1505:3265175b24d2

Change the passing of RLE compression/decompression parameters to be in a dedicated struct.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 May 2018 20:18:14 +0300
parents 15c77c6fbb5e
children 4fd4e7a00db4
files tools/lib64fmts.c tools/lib64gfx.c tools/lib64gfx.h
diffstat 3 files changed, 75 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64fmts.c	Fri May 11 07:50:18 2018 +0300
+++ b/tools/lib64fmts.c	Fri May 11 20:18:14 2018 +0300
@@ -52,8 +52,12 @@
 {
     int res;
     DMGrowBuf mem;
+    DMCompParams cfg;
 
-    if ((res = dmDecodeGenericRLEAlloc(&mem, buf + 0x0e, buf + len, *(buf + 0x0d), 0, 0, DM_RLE_MARKER)) != DMERR_OK)
+    cfg.type = DM_COMP_RLE_MARKER;
+    cfg.rleMarker = *(buf + 0x0d);
+
+    if ((res = dmDecodeGenericRLEAlloc(&mem, buf + 0x0e, buf + len, &cfg)) != DMERR_OK)
         goto out;
 
     res = dmC64DecodeGenericBMP(img, mem.data, mem.len, fmt);
@@ -68,7 +72,7 @@
 {
     int res;
     DMGrowBuf tmp;
-    Uint8 rleMarker;
+    DMCompParams cfg;
     const char *magicID = (fmt->type & D64_FMT_ILACE) ? "DRAZLACE! 1.0" : "DRAZPAINT 2.0";
 
     // Encode the data to temp buffer
@@ -76,20 +80,22 @@
         goto out;
 
     // Analyze the data ..
-    dmGenericRLEAnalyze(&tmp, &rleMarker, DM_RLE_MARKER);
-    rleMarker = 0xff;
+    dmGenericRLEAnalyze(&tmp, &cfg.rleMarker, DM_COMP_RLE_MARKER);
+    cfg.rleMarker = 0xff;
 
     // Add the header bits
     if (!dmGrowBufPut(buf, magicID, strlen(magicID)) ||
-        !dmGrowBufPutU8(buf, rleMarker))
+        !dmGrowBufPutU8(buf, cfg.rleMarker))
     {
         res = DMERR_MALLOC;
         goto out;
     }
 
     // And now RLE compress the data to the existing buffer
-    res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len,
-        rleMarker, 3, 255, DM_RLE_MARKER);
+    cfg.type = DM_COMP_RLE_MARKER;
+    cfg.rleMinCount = 3;
+    cfg.rleMaxCount = 255;
+    res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len, &cfg);
 
 out:
     dmGrowBufFree(&tmp);
@@ -159,6 +165,7 @@
 {
     int res;
     DMGrowBuf mem, tmp;
+    DMCompParams cfg;
 
     // Amica Paint apparently is broken and stores one byte less than it should
     // so we need to do some crappy buffer expansion here ..
@@ -170,7 +177,10 @@
     tmp.data[tmp.len++] = 0;
 
     // Now do an RLE decode on the enlarged buffer
-    if ((res = dmDecodeGenericRLE(&mem, tmp.data, tmp.data + tmp.len, 0xC2, 0, 0, DM_RLE_MARKER)) != DMERR_OK)
+    cfg.type = DM_COMP_RLE_MARKER;
+    cfg.rleMarker = 0xC2;
+
+    if ((res = dmDecodeGenericRLE(&mem, tmp.data, tmp.data + tmp.len, &cfg)) != DMERR_OK)
         goto out;
 
     // And finally decode to bitmap struct
@@ -289,9 +299,13 @@
 {
     int res;
     DMGrowBuf mem;
+    DMCompParams cfg;
+
     dmGrowBufInit(&mem);
 
-    if ((res = dmDecodeGenericRLE(&mem, buf + FUNPAINT2_HEADER_SIZE, buf + len, *(buf + 15), 0, 0, DM_RLE_MARKER)) != DMERR_OK)
+    cfg.type = DM_COMP_RLE_MARKER;
+    cfg.rleMarker = *(buf + 15);
+    if ((res = dmDecodeGenericRLE(&mem, buf + FUNPAINT2_HEADER_SIZE, buf + len, &cfg)) != DMERR_OK)
         goto out;
 
     res = dmC64DecodeGenericBMP(img, mem.data, mem.len, fmt);
@@ -317,27 +331,29 @@
 {
     int res;
     DMGrowBuf tmp;
-    Uint8 rleMarker;
+    DMCompParams cfg;
 
     // Encode the data to temp buffer
     if ((res = dmC64EncodeGenericBMP(TRUE, &tmp, img, fmt)) != DMERR_OK)
         goto out;
 
     // Analyze the data ..
-    dmGenericRLEAnalyze(&tmp, &rleMarker, DM_RLE_MARKER);
-    rleMarker = 0xff;
+    dmGenericRLEAnalyze(&tmp, &cfg.rleMarker, DM_COMP_RLE_MARKER);
+    cfg.rleMarker = 0xff;
 
     // Add the header bits
     if (!dmGrowBufPut(buf, fmtFunPaint2MagicID, strlen(fmtFunPaint2MagicID)) ||
-        !dmGrowBufPutU8(buf, rleMarker))
+        !dmGrowBufPutU8(buf, cfg.rleMarker))
     {
         res = DMERR_MALLOC;
         goto out;
     }
 
     // And now RLE compress the data to the existing buffer
-    res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len,
-        rleMarker, 3, 255, DM_RLE_MARKER);
+    cfg.type = DM_COMP_RLE_MARKER;
+    cfg.rleMinCount = 3;
+    cfg.rleMaxCount = 255;
+    res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len, &cfg);
 
 out:
     dmGrowBufFree(&tmp);
--- a/tools/lib64gfx.c	Fri May 11 07:50:18 2018 +0300
+++ b/tools/lib64gfx.c	Fri May 11 20:18:14 2018 +0300
@@ -248,8 +248,7 @@
 }
 
 
-int dmDecodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd,
-    const Uint8 rleMarker, const Uint8 rleMask1, const Uint8 rleMask2, const int rleType)
+int dmDecodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg)
 {
     int res;
 
@@ -262,11 +261,11 @@
         Uint8 data = *src++;
         int count = 1;
 
-        switch (rleType)
+        switch (cfg->type)
         {
-            case DM_RLE_MARKER:
+            case DM_COMP_RLE_MARKER:
                 // A simple marker byte RLE variant: [Marker] [count] [data]
-                if (data == rleMarker)
+                if (data == cfg->rleMarker)
                 {
                     if (srcEnd - src < 2)
                     {
@@ -278,10 +277,10 @@
                 }
                 break;
 
-            case DM_RLE_MASK:
+            case DM_COMP_RLE_MASK:
                 // Mask marker RLE: usually high bit(s) of byte mark RLE sequence
                 // and the lower bits contain the count: [Mask + count] [data]
-                if ((data & rleMask1) == rleMarker)
+                if ((data & cfg->rleMask1) == cfg->rleMarker)
                 {
                     if (srcEnd - src < 1)
                     {
@@ -289,7 +288,7 @@
                         goto err;
                     }
 
-                    count = data & rleMask2;
+                    count = data & cfg->rleMask2;
                     data = *src++;
                 }
                 break;
@@ -312,27 +311,27 @@
 }
 
 
-int dmDecodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd,
-    const Uint8 rleMarker, const Uint8 rleMask1, const Uint8 rleMask2, const int rleType)
+int dmDecodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg)
 {
     int res;
     if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK)
         return res;
 
-    return dmDecodeGenericRLE(dst, src, srcEnd, rleMarker, rleMask1, rleMask2, rleType);
+    return dmDecodeGenericRLE(dst, src, srcEnd, cfg);
 }
 
 
-static BOOL dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, Uint8 count, const Uint8 rleMarker, const Uint8 rleMinCount, const int rleType)
+static BOOL dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, Uint8 count, const DMCompParams *cfg)
 {
     BOOL copyOnly = FALSE;
-    switch (rleType)
+
+    switch (cfg->type)
     {
-        case DM_RLE_MARKER:
-            if (count >= rleMinCount || data == rleMarker)
+        case DM_COMP_RLE_MARKER:
+            if (count >= cfg->rleMinCount || data == cfg->rleMarker)
             {
                 // A simple marker byte RLE variant: [Marker] [count] [data]
-                if (!dmGrowBufPutU8(dst, rleMarker) ||
+                if (!dmGrowBufPutU8(dst, cfg->rleMarker) ||
                     !dmGrowBufPutU8(dst, count) ||
                     !dmGrowBufPutU8(dst, data))
                     return FALSE;
@@ -341,12 +340,12 @@
                 copyOnly = TRUE;
             break;
 
-        case DM_RLE_MASK:
-            if (count >= rleMinCount || (data & rleMarker) == rleMarker)
+        case DM_COMP_RLE_MASK:
+            if (count >= cfg->rleMinCount || (data & cfg->rleMarker) == cfg->rleMarker)
             {
                 // Mask marker RLE: usually high bit(s) of byte mark RLE sequence
                 // and the lower bits contain the count: [Mask + count] [data]
-                if (!dmGrowBufPutU8(dst, rleMarker | count) ||
+                if (!dmGrowBufPutU8(dst, cfg->rleMarker | count) ||
                     !dmGrowBufPutU8(dst, data))
                     return FALSE;
             }
@@ -368,20 +367,22 @@
 }
 
 
-int dmEncodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd,
-    const Uint8 rleMarker, const Uint8 rleMinCount, const Uint8 rleMaxCount, const int rleType)
+int dmEncodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg)
 {
     int res;
 
+    fprintf(stderr, "encrle: len=%d, rleMarker=%02x, rleMinCount=%d, rleMaxCount=%d, type=%d\n",
+        srcEnd - src, cfg->rleMarker, cfg->rleMinCount, cfg->rleMaxCount, cfg->type);
+
     // Perform RLE encoding
     int count = 0, prev;
     while (src < srcEnd)
     {
         Uint8 data = *src++;
 
-        if (data != prev || count >= rleMaxCount)
+        if (data != prev || count >= cfg->rleMaxCount)
         {
-            if (!dmEncodeGenericRLESequence(dst, prev, count, rleMarker, rleMinCount, rleType))
+            if (!dmEncodeGenericRLESequence(dst, prev, count, cfg))
             {
                 res = dmError(DMERR_MALLOC,
                     "Could reallocate memory for RLE encoding buffer.\n");
@@ -403,14 +404,13 @@
 }
 
 
-int dmEncodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd,
-    const Uint8 rleMarker, const Uint8 rleMinCount, const Uint8 rleMaxCount, const int rleType)
+int dmEncodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg)
 {
     int res;
     if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK)
         return res;
 
-    return dmEncodeGenericRLE(dst, src, srcEnd, rleMarker, rleMinCount, rleMaxCount, rleType);
+    return dmEncodeGenericRLE(dst, src, srcEnd, cfg);
 }
 
 
--- a/tools/lib64gfx.h	Fri May 11 07:50:18 2018 +0300
+++ b/tools/lib64gfx.h	Fri May 11 20:18:14 2018 +0300
@@ -51,10 +51,6 @@
 #define C64_MAX_CHARS          256
 
 
-#define DM_RLE_MARKER    1
-#define DM_RLE_MASK      2
-
-
 #define DM_GET_ADDR_LO(addr) ((addr) & 0xff)
 #define DM_GET_ADDR_HI(addr) (((addr) >> 8) & 0xff)
 
@@ -157,8 +153,10 @@
     DT_LAST,
 };
 
+
 #define D64_MAX_ENCDEC_OPS 64
 
+
 typedef struct _DMC64ImageFormat DMC64ImageFormat;
 
 typedef struct _DMC64EncDecOp
@@ -198,6 +196,17 @@
 } DMC64ImageFormat;
 
 
+#define DM_COMP_RLE_MARKER    1
+#define DM_COMP_RLE_MASK      2
+
+
+typedef struct
+{
+    int type;
+    Uint8 rleMarker, rleMask1, rleMask2, rleMinCount, rleMaxCount;
+} DMCompParams;
+
+
 //
 // Global variables
 //
@@ -237,17 +246,13 @@
 int       dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src, const DMC64ImageFormat *fmt);
 int       dmC64ConvertGenericImage2BMP(DMC64Image *dst, const DMImage *src, const DMC64ImageFormat *fmt);
 
-void dmGenericRLEAnalyze(const DMGrowBuf *buf, Uint8 *rleMarker, const int rleType);
+void      dmGenericRLEAnalyze(const DMGrowBuf *buf, Uint8 *rleMarker, const int rleType);
 
-int dmDecodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd,
-    const Uint8 rleMarker, const Uint8 rleMask1, const Uint8 rleMask2, const int rleType);
-int dmDecodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd,
-    const Uint8 rleMarker, const Uint8 rleMask1, const Uint8 rleMask2, const int rleType);
+int       dmDecodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg);
+int       dmDecodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg);
 
-int dmEncodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd,
-    const Uint8 rleMarker, const Uint8 rleMinCount, const Uint8 rleMaxCount, const int rleType);
-int dmEncodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd,
-    const Uint8 rleMarker, const Uint8 rleMinCount, const Uint8 rleMaxCount, const int rleType);
+int       dmEncodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg);
+int       dmEncodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg);
 
 
 //