changeset 1904:5930ff7879b5

Cleanup IFF writer a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 26 Jun 2018 13:22:20 +0300
parents 8ad98bc7402c
children 425259977bc5
files tools/libgfx.c
diffstat 1 files changed, 71 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/tools/libgfx.c	Tue Jun 26 13:01:25 2018 +0300
+++ b/tools/libgfx.c	Tue Jun 26 13:22:20 2018 +0300
@@ -2044,85 +2044,12 @@
 }
 
 
-int dmEncodeILBMBody(DMResource *fp, DMIFF *iff, const DMImage *img)
-{
-    Uint8 *buf;
-    size_t bufLen;
-    int res = DMERR_OK;
-    const int nplanes = iff->bmhd.nplanes;
-
-    // Allocate planar encoding buffer
-    bufLen = ((img->width + 15) / 16) * 2;
-    if ((buf = dmMalloc(bufLen)) == NULL)
-        return DMERR_MALLOC;
-
-    dmMsg(2, "IFF: plane row size %d bytes.\n", bufLen);
-
-    // Encode the chunk
-    for (int yc = 0; yc < img->height; yc++)
-    {
-        const Uint8 *sp = img->data + (yc * img->pitch);
-
-        for (int plane = 0; plane < nplanes; plane++)
-        {
-            // Encode bitplane
-            dmMemset(buf, 0, bufLen);
-
-            for (int xc = 0; xc < img->width; xc++)
-                buf[xc / 8] |= ((sp[xc] >> plane) & 1) << (7 - (xc & 7));
-
-            // Compress / write data
-            if (!dmIFFWriteOneRow(fp, iff, buf, bufLen))
-            {
-                res = dmError(DMERR_FWRITE,
-                    "IFF: Error in writing image plane #%d @ %d.\n",
-                    plane, yc);
-                goto error;
-            }
-        }
-
-        // Write mask data, if any
-        if (iff->bmhd.masking == IFF_MASK_HAS_MASK)
-        {
-            dmMemset(buf, 0, bufLen);
-
-            for (int xc = 0; xc < img->width; xc++)
-                buf[xc / 8] |= (sp[xc] == img->ctransp) << (7 - (xc & 7));
-
-            if (!dmIFFWriteOneRow(fp, iff, buf, bufLen))
-            {
-                res = dmError(DMERR_FWRITE,
-                    "IFF: Error in writing mask plane.\n");
-                goto error;
-            }
-        }
-    }
-
-error:
-    dmFree(buf);
-    return res;
-}
-
-
-int dmEncodePBMBody(DMResource *fp, DMIFF *iff, const DMImage *img)
-{
-    for (int yc = 0; yc < img->height; yc++)
-    {
-        if (!dmIFFWriteOneRow(fp, iff, img->data + (yc * img->pitch), img->width))
-        {
-            return dmError(DMERR_FWRITE,
-                "IFF: Error writing PBM image row #%d.\n", yc);
-        }
-    }
-
-    return DMERR_OK;
-}
-
-
 int dmWriteIFFImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
 {
     Uint32 idsig;
     DMIFF iff;
+    Uint8 *buf = NULL;
+    size_t bufLen;
     int res = DMERR_OK;
 
     // XXX: Non-paletted ILBM not supported!
@@ -2135,10 +2062,10 @@
     // Setup headers
     iff.bmhd.x           = 0;
     iff.bmhd.y           = 0;
-    iff.bmhd.w           = img->width;
-    iff.bmhd.h           = img->height;
-    iff.bmhd.pagew       = img->width;
-    iff.bmhd.pageh       = img->height;
+    iff.bmhd.w           = img->width * spec->scaleX;
+    iff.bmhd.h           = img->height * spec->scaleY;
+    iff.bmhd.pagew       = img->width * spec->scaleX;
+    iff.bmhd.pageh       = img->height * spec->scaleY;
     iff.bmhd.pad1        = 0;
     iff.bmhd.xasp        = 1; // XXX TODO: compute the xasp/yasp from the img->aspect
     iff.bmhd.yasp        = 1;
@@ -2239,15 +2166,72 @@
     if ((res = dmWriteIFFChunkHdr(fp, &iff.chBODY, IFF_ID_BODY)) != DMERR_OK)
         goto out;
 
+
+    // Allocate encoding buffer
     if (spec->planar)
+        bufLen = (((img->width * spec->scaleX) + 15) / 16) * 2;
+    else
+        bufLen = img->width * spec->scaleX;
+
+    dmMsg(2, "IFF: Line/plane row size %d bytes.\n", bufLen);
+
+    if ((buf = dmMalloc(bufLen)) == NULL)
+        return DMERR_MALLOC;
+
+    // Encode the body
+    for (int yc = 0; yc < img->height; yc++)
+    for (int yscale = 0; yscale < spec->scaleY; yscale++)
     {
-        if ((res = dmEncodeILBMBody(fp, &iff, img)) != DMERR_OK)
-            goto out;
-    }
-    else
-    {
-        if ((res = dmEncodePBMBody(fp, &iff, img)) != DMERR_OK)
-            goto out;
+        const Uint8 *sp = img->data + (yc * img->pitch);
+
+        if (spec->planar)
+        {
+            for (int plane = 0; plane < spec->nplanes; plane++)
+            {
+                // Encode bitplane
+                dmMemset(buf, 0, bufLen);
+
+                for (int xc = 0; xc < img->width * spec->scaleX; xc++)
+                    buf[xc / 8] |= ((sp[xc / spec->scaleX] >> plane) & 1) << (7 - (xc & 7));
+
+                // Compress / write data
+                if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen))
+                {
+                    res = dmError(DMERR_FWRITE,
+                        "IFF: Error writing image plane #%d @ %d.\n",
+                        plane, yc);
+                    goto out;
+                }
+            }
+
+            // Write mask data, if any
+            if (iff.bmhd.masking == IFF_MASK_HAS_MASK)
+            {
+                dmMemset(buf, 0, bufLen);
+
+                for (int xc = 0; xc < img->width * spec->scaleX; xc++)
+                    buf[xc / 8] |= (sp[xc / spec->scaleX] == img->ctransp) << (7 - (xc & 7));
+
+                if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen))
+                {
+                    res = dmError(DMERR_FWRITE,
+                        "IFF: Error writing mask plane %d.\n", yc);
+                    goto out;
+                }
+            }
+        }
+        else
+        {
+            for (int xc = 0; xc < img->width * spec->scaleX; xc++)
+                buf[xc] = sp[xc / spec->scaleX];
+
+            if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen))
+            {
+                res = dmError(DMERR_FWRITE,
+                    "IFF: Error writing PBM image row #%d.\n", yc);
+                goto out;
+            }
+        }
     }
 
     if ((res = dmWriteIFFChunkFinish(fp, &iff.chBODY)) != DMERR_OK)
@@ -2258,6 +2242,7 @@
         goto out;
 
 out:
+    dmFree(buf);
     return res;
 }