changeset 1542:69fa95707e65

Implement dmGenericRLEAnalyze() and use it where appropriate.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 12 May 2018 05:31:40 +0300
parents 203e00a4dfc0
children 416d7b3ba3b2
files tools/lib64fmts.c tools/lib64gfx.c
diffstat 2 files changed, 32 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64fmts.c	Sat May 12 05:17:00 2018 +0300
+++ b/tools/lib64fmts.c	Sat May 12 05:31:40 2018 +0300
@@ -81,7 +81,6 @@
 
     // Analyze the data ..
     dmGenericRLEAnalyze(&tmp, &cfg.rleMarker, DM_COMP_RLE_MARKER);
-    cfg.rleMarker = 0xff;
 
     // Add the header bits
     if (!dmGrowBufPut(buf, magicID, strlen(magicID)) ||
@@ -367,7 +366,6 @@
 
     // Analyze the data ..
     dmGenericRLEAnalyze(&tmp, &cfg.rleMarker, DM_COMP_RLE_MARKER);
-    cfg.rleMarker = 0xff;
 
     // Add the header bits
     if (!dmGrowBufPut(buf, fmtFunPaint2MagicID, strlen(fmtFunPaint2MagicID)) ||
--- a/tools/lib64gfx.c	Sat May 12 05:17:00 2018 +0300
+++ b/tools/lib64gfx.c	Sat May 12 05:31:40 2018 +0300
@@ -244,7 +244,39 @@
 
 void dmGenericRLEAnalyze(const DMGrowBuf *buf, Uint8 *rleMarker, const int rleType)
 {
+#define DM_STAT_MAX 256
+    size_t *stats;
 
+    // Allocate statistics counts buffer
+    if ((stats = dmMalloc0(DM_STAT_MAX * sizeof(size_t))) == NULL)
+        return;
+
+    // Get statistics on the data
+    for (size_t offs = 0; offs < buf->len; offs++)
+        stats[buf->data[offs]]++;
+
+    // According to compression type ..
+    if (rleType == DM_COMP_RLE_MARKER)
+    {
+        size_t selected = 0,
+            smallest = buf->len;
+
+        // Find least used byte value
+        for (size_t n = 0; n < DM_STAT_MAX; n++)
+        {
+            if (stats[n] < smallest)
+            {
+                selected = n;
+                smallest = stats[n];
+            }
+        }
+
+        *rleMarker = selected;
+    }
+    else
+        *rleMarker = 0xC0;
+
+    dmFree(stats);
 }