changeset 1626:1793fc1496da

Remove some code duplication.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 16 May 2018 15:04:38 +0300
parents c8afa3e6c413
children d0e626e039bf
files tools/libgfx.c
diffstat 1 files changed, 17 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/tools/libgfx.c	Wed May 16 14:44:49 2018 +0300
+++ b/tools/libgfx.c	Wed May 16 15:04:38 2018 +0300
@@ -1607,31 +1607,21 @@
 }
 
 
-void dmDecodeBitPlane(Uint8 *dp, const Uint8 *src, const int width, const int nplane)
+static void dmDecodeBitPlane(Uint8 *dp, const Uint8 *src, const int width, const int nplane)
 {
     for (int xc = 0; xc < width; xc++)
         dp[xc] |= dmDecodeBit(src, xc) << nplane;
 }
 
 
-int dmDecodeILBMBody(DMResource *fp, DMIFF *iff, DMImage **pimg, Uint32 *read)
+static int dmDecodeILBMBody(DMResource *fp, DMIFF *iff, DMImage *img, Uint32 *read)
 {
-    DMImage *img;
     Uint8 *buf;
     size_t bufLen;
     int res = DMERR_OK;
 
     *read = 0;
 
-    // Allocate image
-    if ((*pimg = img = dmImageAlloc(iff->bmhd.w, iff->bmhd.h,
-        iff->bmhd.nplanes <= 8 ? DM_IFMT_PALETTE : DM_IFMT_RGBA,
-        // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
-        //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1
-        -1
-        )) == NULL)
-        return DMERR_MALLOC;
-
     // Allocate planar decoding buffer
     bufLen = ((img->width + 15) / 16) * 2;
     if ((buf = dmMalloc(bufLen)) == NULL)
@@ -1695,24 +1685,14 @@
 }
 
 
-int dmDecodePBMBody(DMResource *fp, DMIFF *iff, DMImage **pimg, Uint32 *read)
+static int dmDecodePBMBody(DMResource *fp, DMIFF *iff, DMImage *img, Uint32 *read)
 {
-    DMImage *img;
-    int yc, res = DMERR_OK;
+    int res = DMERR_OK;
 
     *read = 0;
 
-    // Allocate image
-    if ((*pimg = img = dmImageAlloc(iff->bmhd.w, iff->bmhd.h,
-        iff->bmhd.nplanes <= 8 ? DM_IFMT_PALETTE : DM_IFMT_RGBA,
-        // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
-        //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1
-        -1
-        )) == NULL)
-        return DMERR_MALLOC;
-
     // Decode the chunk
-    for (yc = 0; yc < img->height; yc++)
+    for (int yc = 0; yc < img->height; yc++)
     {
         Uint8 *dp = img->data + (yc * img->pitch);
 
@@ -1871,15 +1851,25 @@
 
                 dmMsg(2, "ILBM: BODY chunk size %d bytes\n", chunk.size);
 
+                // Allocate image
+                if ((*pimg = dmImageAlloc(iff.bmhd.w, iff.bmhd.h,
+                    iff.bmhd.nplanes <= 8 ? DM_IFMT_PALETTE : DM_IFMT_RGBA,
+                    // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
+                    //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1
+                    -1
+                    )) == NULL)
+                    return DMERR_MALLOC;
+
+
                 // Decode the body
                 if (iff.planar)
                 {
-                    if ((res = dmDecodeILBMBody(fp, &iff, pimg, &read)) != DMERR_OK)
+                    if ((res = dmDecodeILBMBody(fp, &iff, *pimg, &read)) != DMERR_OK)
                         return res;
                 }
                 else
                 {
-                    if ((res = dmDecodePBMBody(fp, &iff, pimg, &read)) != DMERR_OK)
+                    if ((res = dmDecodePBMBody(fp, &iff, *pimg, &read)) != DMERR_OK)
                         return res;
                 }