diff tools/lib64gfx.c @ 1465:88845f95e791

Change dmC64EncodeGenericBMP() to use DMGrowBuf, and make the necessary changes in gfxconv as well.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 10 May 2018 21:29:52 +0300
parents bde6a66bc2f6
children bc75be0546fc
line wrap: on
line diff
--- a/tools/lib64gfx.c	Thu May 10 21:28:14 2018 +0300
+++ b/tools/lib64gfx.c	Thu May 10 21:29:52 2018 +0300
@@ -1338,27 +1338,19 @@
 }
 
 
-int dmC64EncodeGenericBMP(Uint8 **pbuf, size_t *plen, const DMC64Image *img, const DMC64ImageFormat *fmt)
+int dmC64EncodeGenericBMP(DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt)
 {
     int res = DMERR_OK;
-    Uint8 *buf;
-    size_t allocated;
 
-    if (pbuf == NULL || plen == NULL || img == NULL || fmt == NULL)
+    if (buf == NULL || img == NULL || fmt == NULL)
         return DMERR_NULLPTR;
 
     // Allocate the output buffer
-    *plen = 0;
-    if (fmt->size > 0)
-        *plen = allocated = fmt->size;
-    else
-        allocated = 16 * 1024;
-
-    if ((buf = dmMalloc(allocated)) == NULL)
+    if ((res = dmGrowBufAlloc(buf, fmt->size, BUF_SIZE_GROW)) != DMERR_OK)
     {
-        return dmError(DMERR_MALLOC,
+        return dmError(res,
             "Could not allocate %d bytes of memory for C64 image encoding buffer.\n",
-            allocated);
+            fmt->size);
         goto err;
     }
 
@@ -1366,7 +1358,6 @@
     for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++)
     {
         const DMC64EncDecOp *op = &fmt->encdecOps[i];
-        Uint8 *dst = buf + op->offs;
         size_t size, chksize;
 
         // Check for last operator
@@ -1389,26 +1380,19 @@
 
         // Do we need to reallocate some more space?
         chksize = op->offs + size;
-        if (chksize > allocated)
+        if (!dmGrowBufCheckGrow(buf, chksize))
         {
-            size_t diff = allocated - chksize,
-                   grow = (diff / (BUF_SIZE_GROW - 1)) * BUF_SIZE_GROW;
-
-            allocated += grow;
-
-            if ((buf = dmRealloc(buf, allocated)) == NULL)
-            {
-                res = dmError(DMERR_MALLOC,
-                    "Could not re-allocate %d bytes of memory for C64 image encoding buffer.\n",
-                    allocated);
-                goto err;
-            }
+            res = dmError(DMERR_MALLOC,
+                "Could not re-allocate %d bytes of memory for C64 image encoding buffer.\n",
+                chksize);
+            goto err;
         }
 
-        if (fmt->size == 0 && chksize > *plen)
-            *plen = chksize;
+        if (fmt->size == 0 && chksize > buf->len)
+            buf->len = chksize;
 
         // Perform operation
+        Uint8 *dst = buf->data + op->offs;
         switch (op->type)
         {
             case DT_COLOR_RAM:   memcpy(dst, img->color[op->bank], size); break;
@@ -1421,7 +1405,7 @@
                     res = dmError(DMERR_NULLPTR,
                         "DT_EXTRA_DATA block is NULL in ",
                         "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
-                        i, op->offs, op->offs, op->bank, size, size, *plen, *plen);
+                        i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len);
                     goto err;
                 }
                 if (size > img->extraDataSizes[op->bank])
@@ -1429,7 +1413,7 @@
                     res = dmError(DMERR_INTERNAL,
                         "DT_EXTRA_DATA size mismatch %d <> %d in ",
                         "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
-                        op->size, img->extraDataSizes[op->bank], i, op->offs, op->offs, op->bank, size, size, *plen, *plen);
+                        op->size, img->extraDataSizes[op->bank], i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len);
                     goto err;
                 }
                 memcpy(dst, img->extraData[op->bank], size);
@@ -1448,7 +1432,7 @@
                         res = dmError(DMERR_INTERNAL,
                             "Unhandled DT_COLOR_REG mode %d in ",
                             "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
-                            op->size, i, op->offs, op->offs, op->bank, size, size, *plen, *plen);
+                            op->size, i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len);
                         goto err;
                 }
                 break;
@@ -1459,7 +1443,7 @@
                     res = dmError(DMERR_INTERNAL,
                         "Encode op is a function, but function ptr is NULL: "
                         "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
-                        i, op->offs, op->offs, op->bank, size, size, *plen, *plen);
+                        i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len);
                     goto err;
                 }
                 /*
@@ -1479,7 +1463,6 @@
     res = DMERR_OK;
 
 err:
-    *pbuf = buf;
     return res;
 }