changeset 1811:4f141426eb31

Clean up the image format output stuff in gfxconv.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 21 Jun 2018 13:01:57 +0300
parents c9197a038e8e
children 7460a9b804e9
files tools/gfxconv.c
diffstat 1 files changed, 113 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Thu Jun 21 13:00:24 2018 +0300
+++ b/tools/gfxconv.c	Thu Jun 21 13:01:57 2018 +0300
@@ -1066,57 +1066,84 @@
 }
 
 
-void dmOutputImageBitFormat(const int format, const BOOL info)
+#define DMCOL(x) (((x) >> 4) & 0xf)
+
+int dmWriteIFFMasterRAW(const char *filename, const char *prefix, const DMImage *img, const DMImageConvSpec *spec, const int fmtid)
 {
-    if (info)
+    // Open data file for writing
+    FILE *fp = NULL;
+    int res = DMERR_OK;
+
+    if ((fp = fopen(filename, "w")) == NULL)
     {
-        char *str;
-        switch (format)
+        res = dmError(DMERR_FOPEN,
+            "Could not create file '%s'.\n", filename);
+        goto err;
+    }
+
+
+    fprintf(fp,
+        "%s_width: dw.w %d\n"
+        "%s_height: dw.w %d\n"
+        "%s_nplanes: dw.w %d\n",
+        prefix, img->width,
+        prefix, img->height,
+        prefix, spec->nplanes);
+
+    if (fmtid == DM_IMGFMT_ARAW)
+    {
+        fprintf(fp,
+            "%s_ncolors: dw.w %d\n"
+            "%s_palette:\n",
+            prefix, img->ncolors,
+            prefix);
+
+        for (int i = 0; i < (1 << spec->nplanes); i++)
         {
-            case DM_COLFMT_PALETTE : str = "Indexed 8bpp"; break;
-            case DM_COLFMT_RGB     : str = "24bit RGB"; break;
-            case DM_COLFMT_RGBA    : str = "32bit RGBA"; break;
-            default                : str = "???"; break;
+            Uint32 color;
+            if (i < img->ncolors)
+            {
+                color = (DMCOL(img->pal[i].r) << 8) |
+                        (DMCOL(img->pal[i].g) << 4) |
+                        (DMCOL(img->pal[i].b));
+            }
+            else
+                color = 0;
+
+            fprintf(fp,
+                "\tdc.w $%04X\n",
+                color);
         }
-        dmMsg(2, "%s output.\n", str);
+
+        fprintf(fp,
+            "%s: incbin \"%s\"\n",
+            prefix, filename);
     }
+
+err:
+    if (fp != NULL)
+        fclose(fp);
+
+    return res;
 }
 
 
-#define DMCOL(x) (((x) >> 4) & 0xf)
-
-void dmWriteIFFMasterRAWPalette(FILE *fp,
-    const DMImage *img, int ncolors,
-    const char *indent, const char *type)
-{
-    for (int i = 0; i < ncolors; i++)
-    {
-        int color;
-        if (i < img->ncolors)
-        {
-            color = (DMCOL(img->pal[i].r) << 8) |
-                    (DMCOL(img->pal[i].g) << 4) |
-                    (DMCOL(img->pal[i].b));
-        }
-        else
-            color = 0;
-
-        fprintf(fp, "%s%s $%04X\n",
-            indent != NULL ? indent : "\t",
-            type != NULL ? type : "dc.w",
-            color);
-    }
-}
-
-
-int dmWriteImage(const char *filename, DMImage *pimage, DMImageConvSpec *spec, int iformat, BOOL info)
+int dmWriteImage(const char *filename, DMImage *pimage, DMImageConvSpec *spec, const DMImageFormat *fmt, BOOL info)
 {
     int res = DMERR_OK;
 
+    // Check if writing is even supported
+    if (fmt->write == NULL || (fmt->flags & DM_FMT_WR) == 0)
+    {
+        return dmError(DMERR_NOT_SUPPORTED,
+            "Writing of '%s' format is not supported.\n",
+            fmt->name);
+    }
+
     if (info)
     {
-        dmMsg(1, "Outputting %s image %d x %d -> %d x %d [%d x %d]\n",
-            dmImageFormatList[iformat].fext,
+        dmMsg(1, "Outputting '%s' image %d x %d -> %d x %d [%d x %d]\n",
+            fmt->fext,
             pimage->width, pimage->height,
             pimage->width * spec->scaleX, pimage->height * spec->scaleY,
             spec->scaleX, spec->scaleY);
@@ -1127,111 +1154,76 @@
     BOOL allocated = FALSE;
     if (optRemapColors)
     {
-        int res;
         if ((res = dmRemapImageColors(&image, pimage)) != DMERR_OK)
             return res;
 
         allocated = TRUE;
     }
 
-    switch (iformat)
+    // Do some format-specific adjustments and other things
+    switch (fmt->fmtid)
     {
-#ifdef DM_USE_LIBPNG
         case DM_IMGFMT_PNG:
             spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGBA;
-            dmOutputImageBitFormat(spec->format, info);
-            res = dmWritePNGImage(filename, image, spec);
             break;
-#endif
 
         case DM_IMGFMT_PPM:
             spec->format = DM_COLFMT_RGB;
-            dmOutputImageBitFormat(spec->format, info);
-            res = dmWritePPMImage(filename, image, spec);
-            break;
-
-        case DM_IMGFMT_PCX:
-            spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGB;
-            dmOutputImageBitFormat(spec->format, info);
-            res = dmWritePCXImage(filename, image, spec);
             break;
 
         case DM_IMGFMT_RAW:
         case DM_IMGFMT_ARAW:
             {
-                // Open data file for writing
-                FILE *fp;
-                char * dataFilename = dm_strdup_fext(filename, "%s.inc");
-                if ((fp = fopen(dataFilename, "w")) == NULL)
+                char *prefix = NULL, *dataFilename = NULL;
+                if ((dataFilename = dm_strdup_fext(filename, "%s.inc")) == NULL ||
+                    (prefix = dm_strdup_fext(filename, "img_%s")) == NULL)
                 {
-                    res = dmError(DMERR_FOPEN,
-                        "Could not create '%s'.\n", dataFilename);
+                    res = dmError(DMERR_MALLOC,
+                        "Could not allocate memory for filename strings? :O\n");
                     goto err;
                 }
 
-                dmFree(dataFilename);
-
-                if (fp != NULL)
-                {
-                    // Strip extension
-                    char *palID = dm_strdup_fext(filename, "img_%s");
-
-                    // Replace any non-alphanumerics
-                    for (int i = 0; palID[i]; i++)
-                    {
-                        if (isalnum(palID[i]))
-                            palID[i] = tolower(palID[i]);
-                        else
-                            palID[i] = '_';
-                    }
-
-                    if (iformat == DM_IMGFMT_ARAW)
-                    {
-                        fprintf(fp,
-                        "%s_width: dw.w %d\n"
-                        "%s_height: dw.w %d\n"
-                        "%s_nplanes: dw.w %d\n"
-                        "%s_ncolors: dw.w %d\n"
-                        "%s_palette:\n",
-                        palID, image->width,
-                        palID, image->height,
-                        palID, spec->nplanes,
-                        palID, image->ncolors,
-                        palID);
-
-                        dmWriteIFFMasterRAWPalette(fp, image, 1 << optSpec.nplanes, NULL, NULL);
-
-                        fprintf(fp,
-                        "%s: incbin \"%s\"\n",
-                        palID, filename);
-                    }
-                    else
-                    {
-                        fprintf(fp,
-                        "%s_width: dw.w %d\n"
-                        "%s_height: dw.w %d\n"
-                        "%s_nplanes: dw.w %d\n",
-                        palID, image->width,
-                        palID, image->height,
-                        palID, spec->nplanes);
-                    }
-
-                    fclose(fp);
-                    dmFree(palID);
-                }
+                // Replace any non-alphanumerics in palette ID
+                for (int i = 0; prefix[i]; i++)
+                    prefix[i] = isalnum(prefix[i]) ? tolower(prefix[i]) : '_';
 
                 if (info)
                 {
-                    dmMsg(2, "%d bitplanes, %s planes.\n",
+                    dmMsg(2, "%d bitplanes, %s planes output.\n",
                         spec->nplanes,
                         spec->planar ? "planar/interleaved" : "non-interleaved");
+                    dmMsg(2, "%s datafile '%s', ID prefix '%s'.\n",
+                        fmt->fmtid == DM_IMGFMT_ARAW ? "ARAW" : "RAW",
+                        dataFilename, prefix);
                 }
-                res = dmWriteRAWImage(filename, image, spec);
+                res = dmWriteIFFMasterRAW(dataFilename, prefix, image, spec, fmt->fmtid);
+
+                dmFree(prefix);
+                dmFree(dataFilename);
             }
             break;
 
         default:
-            res = DMERR_INVALID_DATA;
+            spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGB;
+    }
+
+    // If no error has occured thus far, write the image
+    if (res == DMERR_OK)
+    {
+        if (info)
+        {
+            char *str;
+            switch (spec->format)
+            {
+                case DM_COLFMT_PALETTE : str = "indexed/paletted"; break;
+                case DM_COLFMT_RGB     : str = "24bit RGB"; break;
+                case DM_COLFMT_RGBA    : str = "32bit RGBA"; break;
+                default                : str = "???"; break;
+            }
+            dmMsg(2, "Using %s output.\n", str);
+        }
+
+        res = fmt->write(filename, image, spec);
     }
 
 err:
@@ -1541,7 +1533,8 @@
                     goto error;
                 }
 
-                ret = dmWriteImage(outFilename, outImage, &optSpec, optOutSubFormat, TRUE);
+                ret = dmWriteImage(outFilename, outImage, &optSpec,
+                    &dmImageFormatList[optOutSubFormat], TRUE);
                 if (ret != DMERR_OK)
                 {
                     dmErrorMsg("Error writing output image '%s': %s.\n",
@@ -1565,7 +1558,8 @@
 
         if (!optSequential)
         {
-            ret = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE);
+            ret = dmWriteImage(optOutFilename, outImage, &optSpec,
+                &dmImageFormatList[optOutSubFormat], TRUE);
             if (ret != DMERR_OK)
             {
                 dmError(ret, "Error writing output image '%s': %s.\n",
@@ -1781,11 +1775,13 @@
                             goto error;
                         }
 
-                        res = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE);
+                        res = dmWriteImage(optOutFilename, outImage, &optSpec,
+                            &dmImageFormatList[optOutSubFormat], TRUE);
                         break;
 
                     case FFMT_BITMAP:
-                        if ((res = dmConvertC64Bitmap(&outC64Image, inC64Image, &dmC64ImageFormats[optOutSubFormat], inC64Fmt)) != DMERR_OK)
+                        if ((res = dmConvertC64Bitmap(&outC64Image, inC64Image,
+                            &dmC64ImageFormats[optOutSubFormat], inC64Fmt)) != DMERR_OK)
                         {
                             dmErrorMsg("Error in bitmap format conversion.\n");
                             goto error;
@@ -1848,7 +1844,8 @@
                 switch (optOutFormat)
                 {
                     case FFMT_IMAGE:
-                        res = dmWriteImage(optOutFilename, inImage, &optSpec, optOutSubFormat, TRUE);
+                        res = dmWriteImage(optOutFilename, inImage, &optSpec,
+                            &dmImageFormatList[optOutSubFormat], TRUE);
                         break;
 
                     case FFMT_CHAR: