diff tools/lib64fmts.c @ 1650:9233da9de92c

Refactor RLE encoding a bit, and add support for 16bit run counts and make things more configurable.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 30 May 2018 03:25:34 +0300
parents dbdff3d50a4e
children 6dd191d04ea8
line wrap: on
line diff
--- a/tools/lib64fmts.c	Wed May 30 01:29:09 2018 +0300
+++ b/tools/lib64fmts.c	Wed May 30 03:25:34 2018 +0300
@@ -51,8 +51,9 @@
     DMGrowBuf mem;
     DMCompParams cfg;
 
-    cfg.type = DM_COMP_RLE_MARKER2;
-    cfg.rleMarker = 0xfe;
+    cfg.type        = DM_COMP_RLE_MARKER;
+    cfg.flags       = DM_RLE_8BIT_RUNS | DM_RLE_ORDER_2;
+    cfg.rleMarker1  = 0xfe;
     if ((res = dmDecodeGenericRLEAlloc(&mem, buf, buf + len, &cfg)) != DMERR_OK)
         goto out;
 
@@ -75,8 +76,9 @@
         goto out;
 
     // And now RLE compress the data to the existing buffer
-    cfg.type = DM_COMP_RLE_MARKER2;
-    cfg.rleMarker = 0xfe;
+    cfg.type        = DM_COMP_RLE_MARKER;
+    cfg.flags       = DM_RLE_8BIT_RUNS | DM_RLE_ORDER_2;
+    cfg.rleMarker1  = 0xfe;
     cfg.rleMinCount = 3;
     cfg.rleMaxCount = 255;
     res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len, &cfg);
@@ -110,8 +112,9 @@
     DMGrowBuf mem;
     DMCompParams cfg;
 
-    cfg.type = DM_COMP_RLE_MARKER1;
-    cfg.rleMarker = buf[0x0d];
+    cfg.type        = DM_COMP_RLE_MARKER;
+    cfg.flags       = DM_RLE_8BIT_RUNS | DM_RLE_ORDER_1;
+    cfg.rleMarker1  = buf[0x0d];
 
     if ((res = dmDecodeGenericRLEAlloc(&mem, buf + 0x0e, buf + len, &cfg)) != DMERR_OK)
         goto out;
@@ -135,21 +138,22 @@
     if ((res = dmC64EncodeGenericBMP(TRUE, &tmp, img, fmt)) != DMERR_OK)
         goto out;
 
-    // Analyze the data ..
-    dmGenericRLEAnalyze(&tmp, &cfg.rleMarker, DM_COMP_RLE_MARKER1);
+    // Analyze and setup RLE
+    dmGenericRLEAnalyze(tmp.data, tmp.len, &cfg);
+    cfg.type        = DM_COMP_RLE_MARKER;
+    cfg.flags       = DM_RLE_8BIT_RUNS | DM_RLE_ORDER_1;
+    cfg.rleMinCount = 3;
+    cfg.rleMaxCount = 255;
 
     // Add the header bits
     if (!dmGrowBufPut(buf, magicID, strlen(magicID)) ||
-        !dmGrowBufPutU8(buf, cfg.rleMarker))
+        !dmGrowBufPutU8(buf, cfg.rleMarker1))
     {
         res = DMERR_MALLOC;
         goto out;
     }
 
     // And now RLE compress the data to the existing buffer
-    cfg.type = DM_COMP_RLE_MARKER1;
-    cfg.rleMinCount = 3;
-    cfg.rleMaxCount = 255;
     res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len, &cfg);
 
 out:
@@ -240,8 +244,10 @@
     tmp.len = len + 1;
 
     // Now do an RLE decode on the enlarged buffer
-    cfg.type = DM_COMP_RLE_MARKER1;
-    cfg.rleMarker = 0xC2;
+    cfg.type        = DM_COMP_RLE_MARKER;
+    cfg.flags       = DM_RLE_8BIT_RUNS | DM_RLE_ORDER_1;
+    cfg.rleMarker1  = 0xC2;
+
     if ((res = dmDecodeGenericRLEAlloc(&mem, tmp.data, tmp.data + tmp.len, &cfg)) != DMERR_OK)
         goto out;
 
@@ -266,10 +272,12 @@
         goto out;
 
     // And now RLE compress the data to the existing buffer
-    cfg.type = DM_COMP_RLE_MARKER1;
-    cfg.rleMarker = 0xC2;
+    cfg.type        = DM_COMP_RLE_MARKER;
+    cfg.flags       = DM_RLE_8BIT_RUNS | DM_RLE_ORDER_1;
+    cfg.rleMarker1  = 0xC2;
     cfg.rleMinCount = 3;
     cfg.rleMaxCount = 255;
+
     res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len, &cfg);
 
 out:
@@ -504,8 +512,9 @@
         DMGrowBuf mem;
         DMCompParams cfg;
 
-        cfg.type = DM_COMP_RLE_MARKER1;
-        cfg.rleMarker = buf[15];
+        cfg.type        = DM_COMP_RLE_MARKER;
+        cfg.flags       = DM_RLE_8BIT_RUNS | DM_RLE_ORDER_1;
+        cfg.rleMarker1  = buf[15];
 
         if ((res = dmDecodeGenericRLEAlloc(
             &mem, buf + FUNPAINT2_HEADER_SIZE, buf + len, &cfg)) == DMERR_OK)
@@ -543,21 +552,22 @@
     if ((res = dmC64EncodeGenericBMP(TRUE, &tmp, img, fmt)) != DMERR_OK)
         goto out;
 
-    // Analyze the data ..
-    dmGenericRLEAnalyze(&tmp, &cfg.rleMarker, DM_COMP_RLE_MARKER1);
+    // Analyze and setup RLE
+    dmGenericRLEAnalyze(tmp.data, tmp.len, &cfg);
+    cfg.type        = DM_COMP_RLE_MARKER;
+    cfg.flags       = DM_RLE_8BIT_RUNS | DM_RLE_ORDER_1;
+    cfg.rleMinCount = 3;
+    cfg.rleMaxCount = 255;
 
     // Add the header bits
     if (!dmGrowBufPut(buf, fmtFunPaint2MagicID, strlen(fmtFunPaint2MagicID)) ||
-        !dmGrowBufPutU8(buf, cfg.rleMarker))
+        !dmGrowBufPutU8(buf, cfg.rleMarker1))
     {
         res = DMERR_MALLOC;
         goto out;
     }
 
     // And now RLE compress the data to the existing buffer
-    cfg.type = DM_COMP_RLE_MARKER1;
-    cfg.rleMinCount = 3;
-    cfg.rleMaxCount = 255;
     res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len, &cfg);
 
 out: