changeset 1716:9731b0bdec64

Improve error handling of dmEncodeGenericRLESequence() and use dmGenericRLEOutputRun() here as well.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 06 Jun 2018 14:34:58 +0300
parents c0c6fd8b288a
children 7de37c01dbd4
files tools/lib64gfx.c
diffstat 1 files changed, 23 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64gfx.c	Wed Jun 06 14:33:30 2018 +0300
+++ b/tools/lib64gfx.c	Wed Jun 06 14:34:58 2018 +0300
@@ -469,9 +469,10 @@
 }
 
 
-static BOOL dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, unsigned int count, const DMCompParams *cfg)
+int dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, const unsigned int count, const DMCompParams *cfg)
 {
     BOOL copyOnly = FALSE;
+    int res;
 
     switch (cfg->type)
     {
@@ -481,20 +482,20 @@
             {
                 // A simple marker byte RLE variant: [Marker] [count] [data]
                 if (!dmGrowBufPutU8(dst, cfg->rleMarkerW))
-                    return FALSE;
+                    goto err;
 
                 switch (cfg->flags & DM_RLE_ORDER_MASK)
                 {
                     case DM_RLE_ORDER_1:
                         if (!dmGrowBufPutU16LE(dst, count) ||
                             !dmGrowBufPutU8(dst, data))
-                            return FALSE;
+                            goto err;
                         break;
 
                     case DM_RLE_ORDER_2:
                         if (!dmGrowBufPutU8(dst, data) ||
                             !dmGrowBufPutU16LE(dst, count))
-                            return FALSE;
+                            goto err;
                         break;
                 }
             }
@@ -504,20 +505,20 @@
             {
                 // A simple marker byte RLE variant: [Marker] [count] [data]
                 if (!dmGrowBufPutU8(dst, cfg->rleMarkerB))
-                    return FALSE;
+                    goto err;
 
                 switch (cfg->flags & DM_RLE_ORDER_MASK)
                 {
                     case DM_RLE_ORDER_1:
                         if (!dmGrowBufPutU8(dst, count) ||
                             !dmGrowBufPutU8(dst, data))
-                            return FALSE;
+                            goto err;
                         break;
 
                     case DM_RLE_ORDER_2:
                         if (!dmGrowBufPutU8(dst, data) ||
                             !dmGrowBufPutU8(dst, count))
-                            return FALSE;
+                            goto err;
                         break;
                 }
             }
@@ -532,23 +533,22 @@
                 // and the lower bits contain the count: [Mask + count] [data]
                 if (!dmGrowBufPutU8(dst, cfg->rleMarkerBits | count) ||
                     !dmGrowBufPutU8(dst, data))
-                    return FALSE;
+                    goto err;
             }
             else
                 copyOnly = TRUE;
             break;
     }
 
-    if (copyOnly)
-    {
-        while (count--)
-        {
-            if (!dmGrowBufPutU8(dst, data))
-                return FALSE;
-        }
-    }
+    if (copyOnly && (res = dmGenericRLEOutputRun(dst, cfg, data, count)) != DMERR_OK)
+        return res;
+
+    return DMERR_OK;
 
-    return TRUE;
+err:
+    return dmError(DMERR_MALLOC,
+        "%s: RLE: Could not output RLE sequence %d x 0x%02x.\n",
+        cfg->func, count, data);
 }
 
 
@@ -556,7 +556,7 @@
 {
     DMGrowBuf src;
     unsigned int count = 0;
-    int prev = -1;
+    int prev = -1, res = DMERR_OK;
     Uint8 data;
 
     // As we need to modify the offs, etc. but not the data,
@@ -572,7 +572,7 @@
             ((cfg->flags & DM_RLE_WORD_RUNS) && count >= cfg->rleMaxCountW) ||
             (((cfg->flags & DM_RLE_RUNS_MASK) == DM_RLE_BYTE_RUNS) && count >= cfg->rleMaxCountB))
         {
-            if (!dmEncodeGenericRLESequence(dst, prev, count, cfg))
+            if ((res = dmEncodeGenericRLESequence(dst, prev, count, cfg)) != DMERR_OK)
                 goto err;
 
             count = 1;
@@ -583,14 +583,14 @@
         prev = data;
     }
 
-    if (!dmEncodeGenericRLESequence(dst, prev, count, cfg))
+    // If there is anything left in the output queue ..
+    if ((res = dmEncodeGenericRLESequence(dst, prev, count, cfg)) != DMERR_OK)
         goto err;
 
-    return DMERR_OK;
+    dmFinishRLEBuffers(dst, &src, cfg);
 
 err:
-     return dmError(DMERR_MALLOC,
-        "Could not reallocate memory for RLE encoding buffer.\n");
+    return res;
 }