changeset 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 73e00bf7531c
children bc75be0546fc
files tools/gfxconv.c tools/lib64gfx.c tools/lib64gfx.h
diffstat 3 files changed, 27 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Thu May 10 21:28:14 2018 +0300
+++ b/tools/gfxconv.c	Thu May 10 21:29:52 2018 +0300
@@ -1034,8 +1034,7 @@
 int dmWriteBitmap(const char *filename, const DMC64Image *image, const DMC64ImageFormat *fmt, const BOOL enableFixUps)
 {
     FILE *outFile = NULL;
-    Uint8 *buf = NULL;
-    size_t bufSize;
+    DMGrowBuf buf;
     int res = DMERR_OK;
 
     dmMsg(1, "Converting to %s format bitmap.\n", fmt->name);
@@ -1054,11 +1053,11 @@
         }
     }
 
-
-    if ((res = dmC64EncodeGenericBMP(&buf, &bufSize, image, fmt)) != DMERR_OK)
+    // Encode it, XXX TODO: Use proper abstraction function when it works dmC64EncodeBMP()
+    if ((res = dmC64EncodeGenericBMP(&buf, image, fmt)) != DMERR_OK)
         goto error;
 
-    dmMsg(2, "Result: %d bytes\n", bufSize);
+    dmMsg(2, "Result: %d bytes\n", buf.len);
 
     if ((outFile = fopen(filename, "wb")) == NULL)
     {
@@ -1068,7 +1067,7 @@
         goto error;
     }
 
-    if (!dm_fwrite_str(outFile, buf, bufSize))
+    if (!dm_fwrite_str(outFile, buf.data, buf.len))
     {
         res = dmGetErrno();
         dmError(res, "Error writing image data to '%s', %d: %s\n",
@@ -1078,7 +1077,8 @@
 error:
     if (outFile != NULL)
         fclose(outFile);
-    dmFree(buf);
+
+    dmGrowBufFree(&buf);
     return res;
 }
 
--- 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;
 }
 
--- a/tools/lib64gfx.h	Thu May 10 21:28:14 2018 +0300
+++ b/tools/lib64gfx.h	Thu May 10 21:29:52 2018 +0300
@@ -10,6 +10,7 @@
 #define LIB64GFX_H 1
 
 #include "libgfx.h"
+#include "dmgrowbuf.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -172,7 +173,7 @@
     int  (*probe)(const Uint8 *buf, const size_t len, const struct _DMC64ImageFormat *fmt);
 
     int  (*decode)(DMC64Image *img, const Uint8 *buf, const size_t len, const struct _DMC64ImageFormat *fmt);
-    int  (*encode)(Uint8 **buf, size_t *len, const DMC64Image *img, const struct _DMC64ImageFormat *fmt);
+    int  (*encode)(DMGrowBuf *buf, const DMC64Image *img, const struct _DMC64ImageFormat *fmt);
 
     int  (*convertFrom)(DMImage *, const DMC64Image *, const struct _DMC64ImageFormat *fmt);
     int  (*convertTo)(DMC64Image *, const DMImage *, const struct _DMC64ImageFormat *fmt);
@@ -200,7 +201,7 @@
 int       dmC64ProbeBMP(const Uint8 *buf, const size_t len, const DMC64ImageFormat **fmt);
 
 int       dmC64DecodeGenericBMP(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt);
-int       dmC64EncodeGenericBMP(Uint8 **pbuf, size_t *plen, const DMC64Image *img, const DMC64ImageFormat *fmt);
+int       dmC64EncodeGenericBMP(DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt);
 
 int       dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageFormat *fmt);