changeset 1496:5c46004dad15

Work on the RLE encoding.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 May 2018 06:27:53 +0300
parents acb607e2c350
children ee01fdec1a7b
files tools/lib64gfx.c
diffstat 1 files changed, 35 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64gfx.c	Fri May 11 06:26:55 2018 +0300
+++ b/tools/lib64gfx.c	Fri May 11 06:27:53 2018 +0300
@@ -346,25 +346,45 @@
 }
 
 
-static BOOL dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, const Uint8 count, const Uint8 rleMarker, const int rleType)
+static BOOL dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, Uint8 count, const Uint8 rleMarker, const Uint8 rleMinCount, const int rleType)
 {
+    BOOL copyOnly = FALSE;
     switch (rleType)
     {
         case DM_RLE_MARKER:
-            // A simple marker byte RLE variant: [Marker] [count] [data]
-            if (!dmGrowBufPutU8(dst, rleMarker) ||
-                !dmGrowBufPutU8(dst, count) ||
-                !dmGrowBufPutU8(dst, data))
-                return FALSE;
+            if (count >= rleMinCount || data == rleMarker)
+            {
+                // A simple marker byte RLE variant: [Marker] [count] [data]
+                if (!dmGrowBufPutU8(dst, rleMarker) ||
+                    !dmGrowBufPutU8(dst, count) ||
+                    !dmGrowBufPutU8(dst, data))
+                    return FALSE;
+            }
+            else
+                copyOnly = TRUE;
             break;
 
         case DM_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 (!dmGrowBufPutU8(dst, rleMarker | count) ||
-                !dmGrowBufPutU8(dst, data))
+            if (count >= rleMinCount || (data & rleMarker) == 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) ||
+                    !dmGrowBufPutU8(dst, data))
+                    return FALSE;
+            }
+            else
+                copyOnly = TRUE;
+            break;
+    }
+
+    if (copyOnly)
+    {
+        while (count--)
+        {
+            if (!dmGrowBufPutU8(dst, data))
                 return FALSE;
-            break;
+        }
     }
 
     return TRUE;
@@ -382,9 +402,9 @@
     {
         Uint8 data = *src++;
 
-        if ((count >= rleMinCount && data != prev) || count >= rleMaxCount)
+        if (data != prev || count >= rleMaxCount)
         {
-            if (!dmEncodeGenericRLESequence(dst, prev, count + 1, rleMarker, rleType))
+            if (!dmEncodeGenericRLESequence(dst, prev, count, rleMarker, rleMinCount, rleType))
             {
                 res = dmError(DMERR_MALLOC,
                     "Could reallocate memory for RLE encoding buffer.\n");
@@ -393,6 +413,8 @@
 
             count = 1;
         }
+        else
+            count++;
 
         prev = data;
     }