diff tools/lib64fmts.c @ 1722:de8e0a404c06

Refactor fmtDecodeTruePaintPacked() to use more generic DMGrowBuf functions now that they support backwards growing buffers, and get rid of dmReverseGetByte() and dmReversePutByte() helpers.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 06 Jun 2018 15:22:15 +0300
parents 95317672ff00
children 13fe4010623f
line wrap: on
line diff
--- a/tools/lib64fmts.c	Wed Jun 06 15:20:31 2018 +0300
+++ b/tools/lib64fmts.c	Wed Jun 06 15:22:15 2018 +0300
@@ -423,56 +423,54 @@
 // some kind of "improved RLE" variant with different modes and a
 // simplistic "codebook".
 //
-static BOOL fmtTruePaintGetByte(const Uint8 *src, size_t *srcOffs, Uint8 *data, int *res, const int mode)
+static int fmtTruePaintGetByte(DMGrowBuf *src, Uint8 *data, const int mode)
 {
-    if (!dmReverseGetByte(src, srcOffs, data))
+    if (!dmGrowBufGetU8(src, data))
     {
-        *res = dmError(DMERR_INVALID_DATA,
+        return dmError(DMERR_INVALID_DATA,
             "TruePaintRLE: Out of input data (N=%d)\n", mode);
-        return FALSE;
     }
     else
-        return TRUE;
+        return DMERR_OK;
 }
 
 
-static int fmtDecodeTruePaintPacked(DMC64Image *img, const DMGrowBuf *src, const DMC64ImageFormat *fmt)
+static int fmtDecodeTruePaintPacked(DMC64Image *img, const DMGrowBuf *psrc, const DMC64ImageFormat *fmt)
 {
     int res = DMERR_OK;
-    Uint8 *dst = NULL;
-    DMGrowBuf dstTmp;
     const Uint8 *codeBook1, *codeBook2;
-    size_t
-        srcOffs, dstOffs,
-        dstLen  = 0x4be8;
-        // 1b7e-67e8 decoded by original depacker
-        // 1c00-67e8 is the actual area used tho
+    DMGrowBuf dst, src;
+    DMCompParams cfg;
+    Uint8 data;
+
+    cfg.func         = fmt->name;
+    cfg.type         = DM_COMP_RLE_MARKER;
+    cfg.flags        = DM_RLE_BACKWARDS_OUTPUT | DM_RLE_BACKWARDS_INPUT;
+    cfg.rleMarkerB   = 0xfe;
+
+    // 1b7e-67e8 decoded by original depacker
+    // 1c00-67e8 is the actual area used tho
+    cfg.flags |= DM_OUT_CROP_END;
+    cfg.cropOutLen   = 0x67e8 - 0x1c00;
 
     // Codebooks: #1 is trampoline table markers, #2 is RLE data table
-    codeBook1 = src->data + 0x81 - 2;
-    codeBook2 = src->data + 0x85 - 2;
+    codeBook1 = psrc->data + 0x81 - 2;
+    codeBook2 = psrc->data + 0x85 - 2;
 
     // Allocate output buffer
-    if ((dst = dmMalloc0(dstLen)) == NULL)
-    {
-        res = dmError(DMERR_MALLOC,
-            "Could not allocate memory for temporary decompression buffer.\n");
+    if ((res = dmGrowBufAlloc(&dst, 64*1024, 4*1024)) != DMERR_OK)
         goto out;
-    }
 
-    // Begin decompression
-    srcOffs = src->len;
-    dstOffs = dstLen;
+    // As we need to modify the offs, etc. but not the data,
+    // we will just make a shallow copy of the DMGrowBuf struct
+    dmGrowBufConstCopy(&src, psrc);
+    dmSetupRLEBuffers(&dst, &src, &cfg);
 
-    while (srcOffs > 0 && dstOffs > 0)
+    while ((res = fmtTruePaintGetByte(&src, &data, -1)) == DMERR_OK)
     {
-        Uint8 data;
-        int count = 1, scount;
+        unsigned int count = 1;
         BOOL found = FALSE;
 
-        if (!fmtTruePaintGetByte(src->data, &srcOffs, &data, &res, -1))
-            goto out;
-
         for (int n = 0; n < 8; n++)
         if (codeBook1[n] == data && !found)
         {
@@ -480,7 +478,7 @@
             switch (n)
             {
                 case 4: // Y = 4, JTO = $0B
-                    if (!fmtTruePaintGetByte(src->data, &srcOffs, &data, &res, n))
+                    if ((res = fmtTruePaintGetByte(&src, &data, n)) != DMERR_OK)
                         goto out;
 
                     count = data;
@@ -494,12 +492,12 @@
                     // fallthrough
 
                 case 0: // Y = 0, JTO = $19
-                    if (!fmtTruePaintGetByte(src->data, &srcOffs, &data, &res, n))
+                    if ((res = fmtTruePaintGetByte(&src, &data, n)) != DMERR_OK)
                         goto out;
                     break;
 
                 case 2: // Y = 2, JTO = $07
-                    if (!fmtTruePaintGetByte(src->data, &srcOffs, &data, &res, n))
+                    if ((res = fmtTruePaintGetByte(&src, &data, n)) != DMERR_OK)
                         goto out;
 
                     count = data;
@@ -517,23 +515,16 @@
             }
         }
 
-        for (scount = count; count; count--)
-        {
-            if (!dmReversePutByte(dst, &dstOffs, data))
-            {
-                res = dmError(DMERR_INVALID_DATA,
-                    "TruePaintRLE: Out of output space for run: %d x $%02x!\n",
-                    scount, data);
-                goto out;
-            }
-        }
+        if ((res = dmGenericRLEOutputRun(&dst, &cfg, data, count)) != DMERR_OK)
+            goto out;
     }
 
 finish:
-    res = dmC64DecodeGenericBMP(img, dmGrowBufCreateFrom(&dstTmp, dst, dstLen), fmt);
+    dmFinishRLEBuffers(&dst, &src, &cfg);
+    res = dmC64DecodeGenericBMP(img, &dst, fmt);
 
 out:
-    dmFree(dst);
+    dmGrowBufFree(&dst);
     return res;
 }