changeset 2049:a945a5f2fd70

Improve the cdump output format support of gfxconv. Also add some error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 03 Dec 2018 18:58:45 +0200
parents 0a1fe72be4a9
children 416af5a842ec
files tools/libgfx.c
diffstat 1 files changed, 59 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/tools/libgfx.c	Mon Dec 03 18:51:45 2018 +0200
+++ b/tools/libgfx.c	Mon Dec 03 18:58:45 2018 +0200
@@ -305,21 +305,23 @@
     DMResource *fp, const char *filename, const char *prefix,
     const DMImage *img, const DMImageConvSpec *spec, const int fmtid)
 {
-    dmfprintf(fp,
+    if (dmfprintf(fp,
         "%s_width: dw.w %d\n"
         "%s_height: dw.w %d\n"
         "%s_nplanes: dw.w %d\n",
         prefix, img->width * spec->scaleX,
         prefix, img->height * spec->scaleY,
-        prefix, spec->nplanes);
+        prefix, spec->nplanes) < 0)
+        return dmferror(fp);
 
     if (fmtid == DM_IMGFMT_ARAW)
     {
-        dmfprintf(fp,
+        if (dmfprintf(fp,
             "%s_ncolors: dw.w %d\n"
             "%s_palette:\n",
             prefix, img->ncolors,
-            prefix);
+            prefix) < 0)
+            return dmferror(fp);
 
         for (int i = 0; i < (1 << spec->nplanes); i++)
         {
@@ -333,14 +335,16 @@
             else
                 color = 0;
 
-            dmfprintf(fp,
+            if (dmfprintf(fp,
                 "\tdc.w $%04X\n",
-                color);
+                color) < 0)
+                return dmferror(fp);
         }
 
-        dmfprintf(fp,
+        if (dmfprintf(fp,
             "%s: incbin \"%s\"\n",
-            prefix, filename);
+            prefix, filename) < 0)
+            return dmferror(fp);
     }
 
     return DMERR_OK;
@@ -396,37 +400,65 @@
 }
 
 
-static int dmWriteCDumpRow(void *cbdata, const Uint8 *row, const size_t len)
+static BOOL dmPutByteCDump(DMBitStreamContext *ctx, const Uint8 val)
 {
-    DMResource *fp = (DMResource *) cbdata;
-    for (size_t n = 0; n < len; n++)
-    {
-        dmfprintf(fp, "0x%02x, ", row[n]);
-    }
-
-    dmfprintf(fp, "\n");
-
-    return DMERR_OK;
+    return dmfprintf((DMResource *) ctx->handle, "0x%02x, ", val) > 0;
 }
 
 
 int dmWriteCDumpImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
 {
     int res;
-
-    dmfprintf(fp,
+    DMBitStreamContext bs;
+
+    if (dmfprintf(fp,
         "#define SET_WIDTH %d\n"
         "#define SET_HEIGHT %d\n"
         "\n"
         "Uint8 img_data[] = {\n",
-
         img->width * spec->scaleX,
-        img->height * spec->scaleY);
-
-    res = dmWriteImageData(img, (void *) fp, dmWriteCDumpRow, spec);
-
-    dmfprintf(fp,
-        "};\n");
+        img->height * spec->scaleY) < 0)
+        return dmferror(fp);
+
+    dmInitBitStreamContext(&bs);
+    bs.putByte = dmPutByteCDump;
+    bs.handle = (void *) fp;
+
+    if (spec->planar)
+    {
+        // Output bitplanes in planar format
+        // (each plane of line sequentially)
+        for (int yc = 0; yc < img->height; yc++)
+        for (int yscale = 0; yscale < spec->scaleY; yscale++)
+        for (int plane = 0; plane < spec->nplanes; plane++)
+        {
+            if (!dmWriteRAWRow(&bs, img, spec, yc, plane))
+                return DMERR_FWRITE;
+
+            if (dmfprintf(fp, "\n") < 0)
+                return dmferror(fp);
+        }
+    }
+    else
+    {
+        // Output each bitplane in sequence
+        for (int plane = 0; plane < spec->nplanes; plane++)
+        for (int yc = 0; yc < img->height; yc++)
+        for (int yscale = 0; yscale < spec->scaleY; yscale++)
+        {
+            if (!dmWriteRAWRow(&bs, img, spec, yc, plane))
+                return DMERR_FWRITE;
+
+            if (dmfprintf(fp, "\n") < 0)
+                return dmferror(fp);
+        }
+    }
+
+    res = dmFlushBitStream(&bs);
+
+    if (res == DMERR_OK &&
+        dmfprintf(fp, "};\n") < 0)
+        res = dmferror(fp);
 
     return res;
 }