changeset 1476:f0232684857a

Cleanup the RLE decoder.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 May 2018 02:26:09 +0300
parents 7729fefe8e57
children e8fe529f4341
files tools/lib64gfx.c
diffstat 1 files changed, 13 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64gfx.c	Fri May 11 02:25:40 2018 +0300
+++ b/tools/lib64gfx.c	Fri May 11 02:26:09 2018 +0300
@@ -231,35 +231,34 @@
     int res;
 
     if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK)
-    {
-        dmError(res,
-            "Could not allocate RLE decoding buffer.\n");
         goto err;
-    }
 
     // Perform RLE decode
     while (src < srcEnd)
     {
-        Uint8 c = *src++;
-        int cnt = 1;
+        Uint8 data = *src++;
+        int count = 1;
 
         switch (rleType)
         {
             case DM_RLE_MARKER:
-                if (c == rleMarker)
+                // A simple marker byte RLE variant: [Marker] [count] [data]
+                if (data == rleMarker)
                 {
                     if (srcEnd - src < 2)
                     {
                         res = DMERR_INVALID_DATA;
                         goto err;
                     }
-                    cnt = *src++;
-                    c = *src++;
+                    count = *src++;
+                    data = *src++;
                 }
                 break;
 
             case DM_RLE_MASK:
-                if ((c & rleMask1) == rleMarker)
+                // 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 (srcEnd - src < 1)
                     {
@@ -267,16 +266,15 @@
                         goto err;
                     }
 
-                    // XXX TODO actually we probably want another mask here
-                    cnt = c & rleMask2;
-                    c = *src++;
+                    count = data & rleMask2;
+                    data = *src++;
                 }
                 break;
         }
 
-        while (cnt--)
+        while (count--)
         {
-            if (!dmGrowBufPutU8(dst, c))
+            if (!dmGrowBufPutU8(dst, data))
             {
                 res = DMERR_MALLOC;
                 goto err;
@@ -284,10 +282,6 @@
         }
     }
 
-    // Reallocate the memory
-    if ((res = dmGrowBufResize(dst)) != DMERR_OK)
-        goto err;
-
     res = DMERR_OK;
 
 err: