changeset 1499:32640e1934d5

Simplify some encoding bits.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 May 2018 07:07:40 +0300
parents 9217fcc727e8
children 3223d01ac8e8
files tools/lib64gfx.c tools/lib64gfx.h
diffstat 2 files changed, 16 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64gfx.c	Fri May 11 07:06:36 2018 +0300
+++ b/tools/lib64gfx.c	Fri May 11 07:07:40 2018 +0300
@@ -522,16 +522,12 @@
     const char *magicID = (fmt->type & D64_FMT_ILACE) ? "DRAZLACE! 1.0" : "DRAZPAINT 2.0";
 
     // Encode the data to temp buffer
-    if ((res = dmC64EncodeGenericBMP(&tmp, img, fmt)) != DMERR_OK)
+    if ((res = dmC64EncodeGenericBMP(TRUE, &tmp, img, fmt)) != DMERR_OK)
         goto out;
 
     // Analyze the data ..
     rleMarker = 0xff;
 
-    // Allocate the output buffer
-    if ((res = dmGrowBufAlloc(buf, tmp.len + BUF_SIZE_GROW, BUF_SIZE_GROW)) != DMERR_OK)
-        goto out;
-
     // Add the header bits
     if (!dmGrowBufPut(buf, magicID, strlen(magicID)) ||
         !dmGrowBufPutU8(buf, rleMarker))
@@ -540,7 +536,7 @@
         goto out;
     }
 
-    // And now RLE compress it to the existing buffer
+    // And now RLE compress the data to the existing buffer
     res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len,
         rleMarker, 3, 255, DM_RLE_MARKER);
 
@@ -753,11 +749,6 @@
 }
 
 
-{
-
-}
-
-
 static Uint8 fmtGetPixelFunPaint2(
     const DMC64Image *img, const int bmoffs, const int scroffs,
     const int vshift, const int vbitmap, const int raster)
@@ -1530,15 +1521,15 @@
 }
 
 
-int dmC64EncodeGenericBMP(DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt)
+int dmC64EncodeGenericBMP(const BOOL allocate, DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt)
 {
     int res = DMERR_OK;
 
     if (img == NULL || fmt == NULL)
         return DMERR_NULLPTR;
 
-    // Allocate the output buffer
-    if ((res = dmGrowBufAlloc(buf, fmt->size, BUF_SIZE_GROW)) != DMERR_OK)
+    // Allocate the output buffer if requested
+    if (allocate && (res = dmGrowBufAlloc(buf, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK)
     {
         dmError(res,
             "Could not allocate %d bytes of memory for C64 image encoding buffer.\n",
@@ -1897,31 +1888,24 @@
     if (img == NULL || fmt == NULL)
         return DMERR_NULLPTR;
 
+    // Allocate a buffer
+    if ((res = dmGrowBufAlloc(buf, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK)
+        goto err;
+
+    // Add the loading address
+    if (!dmGrowBufPutU8(buf, DM_GET_ADDR_LO(fmt->addr)) ||
+        !dmGrowBufPutU8(buf, DM_GET_ADDR_HI(fmt->addr)))
+        goto err;
+
     // Attempt to encode the image to a buffer
-    if ((res = dmGrowBufInit(buf)) != DMERR_OK)
-        return res;
-
     if (fmt->encode != NULL)
         res = fmt->encode(buf, img, fmt);
     else
-        res = dmC64EncodeGenericBMP(buf, img, fmt);
+        res = dmC64EncodeGenericBMP(FALSE, buf, img, fmt);
 
     if (res != DMERR_OK)
         goto err;
 
-    // Add the loading address
-    if (!dmGrowBufGrow(buf, 2))
-    {
-        res = DMERR_MALLOC;
-        goto err;
-    }
-
-    memmove(buf->data + 2, buf->data, buf->len);
-
-    buf->data[0] = DM_GET_ADDR_LO(fmt->addr);
-    buf->data[1] = DM_GET_ADDR_HI(fmt->addr);
-    buf->len += 2;
-
     return DMERR_OK;
 
 err:
--- a/tools/lib64gfx.h	Fri May 11 07:06:36 2018 +0300
+++ b/tools/lib64gfx.h	Fri May 11 07:07:40 2018 +0300
@@ -209,7 +209,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(DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt);
+int       dmC64EncodeGenericBMP(const BOOL allocate, DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt);
 
 int       dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageFormat *fmt);
 int       dmC64ConvertImage2BMP(DMC64Image **pdst, const DMImage *src, const DMC64ImageFormat *fmt);