changeset 1909:a323ca23f449

Rename several variables to lessen the confusion about input/output "format" and "subformat" by using the more proper terms "type" and "format".
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 26 Jun 2018 17:30:51 +0300
parents 2977baab68db
children 1a198212009e
files tools/gfxconv.c
diffstat 1 files changed, 97 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Tue Jun 26 16:55:27 2018 +0300
+++ b/tools/gfxconv.c	Tue Jun 26 17:30:51 2018 +0300
@@ -37,6 +37,18 @@
 };
 
 
+static const char *formatTypeList[FFMT_LAST] =
+{
+    "AUTO",
+    "ASCII text",
+    "ANSI text",
+    "C64 bitmap image",
+    "C64 character/font data",
+    "C64 sprite data",
+    "Image",
+};
+
+
 enum
 {
     CROP_NONE = 0,
@@ -50,8 +62,8 @@
     char *name;       // Descriptive name of the format
     char *fext;       // File extension
     int flags;        // DM_FMT_* flags, see libgfx.h
-    int format;       // Format identifier
-    int subformat;    // Subformat identifier
+    int type;         // Format type
+    int format;       // Subformat identifier
 } DMConvFormat;
 
 
@@ -83,10 +95,10 @@
 char    *optInFilename = NULL,
         *optOutFilename = NULL;
 
-int     optInFormat = FFMT_AUTO,
-        optOutFormat = FFMT_ASCII,
-        optInSubFormat = -1,
-        optOutSubFormat = -1,
+int     optInType = FFMT_AUTO,
+        optOutType = FFMT_AUTO,
+        optInFormat = -1,
+        optOutFormat = -1,
         optItemCount = -1,
         optPlanedWidth = 1,
         optForcedInSubFormat = -1;
@@ -253,13 +265,13 @@
 }
 
 
-BOOL dmGetConvFormat(const int format, const int subformat, DMConvFormat *pfmt)
+BOOL dmGetConvFormat(const int type, const int format, DMConvFormat *pfmt)
 {
     for (int i = 0; i < nconvFormatList; i++)
     {
         const DMConvFormat *fmt = &convFormatList[i];
-        if (fmt->format == format &&
-            fmt->subformat == subformat)
+        if (fmt->type == type &&
+            fmt->format == format)
         {
             memcpy(pfmt, fmt, sizeof(DMConvFormat));
             return TRUE;
@@ -269,10 +281,10 @@
     for (int i = 0; i < nconvFormatList; i++)
     {
         const DMConvFormat *fmt = &convFormatList[i];
-        if (fmt->format == format && format == FFMT_BITMAP)
+        if (fmt->type == type && type == FFMT_BITMAP)
         {
             const DMConvFormat *fmt = &convFormatList[i];
-            const DMC64ImageFormat *cfmt = &dmC64ImageFormats[subformat];
+            const DMC64ImageFormat *cfmt = &dmC64ImageFormats[format];
             memcpy(pfmt, fmt, sizeof(DMConvFormat));
             pfmt->fext = cfmt->name;
             return TRUE;
@@ -283,7 +295,7 @@
 }
 
 
-BOOL dmGetC64FormatByExt(const char *fext, int *format, int *subformat)
+BOOL dmGetC64FormatByExt(const char *fext, int *type, int *format)
 {
     if (fext == NULL)
         return FALSE;
@@ -294,8 +306,8 @@
         if (fmt->fext != NULL &&
             strcasecmp(fext, fmt->fext) == 0)
         {
-            *format = FFMT_BITMAP;
-            *subformat = i;
+            *type   = FFMT_BITMAP;
+            *format = i;
             return TRUE;
         }
     }
@@ -304,7 +316,7 @@
 }
 
 
-BOOL dmGetFormatByExt(const char *fext, int *format, int *subformat)
+BOOL dmGetFormatByExt(const char *fext, int *type, int *format)
 {
     if (fext == NULL)
         return FALSE;
@@ -315,8 +327,8 @@
         if (fmt->fext != NULL &&
             strcasecmp(fext, fmt->fext) == 0)
         {
+            *type   = fmt->type;
             *format = fmt->format;
-            *subformat = fmt->subformat;
             return TRUE;
         }
     }
@@ -623,10 +635,10 @@
             {
                 DMConvFormat fmt;
 
-                if (!dmParseFormatOption("input", "Input", optArg, &optInFormat, &optForcedInSubFormat))
+                if (!dmParseFormatOption("input", "Input", optArg, &optInType, &optForcedInSubFormat))
                     return FALSE;
 
-                dmGetConvFormat(optInFormat, optForcedInSubFormat, &fmt);
+                dmGetConvFormat(optInType, optForcedInSubFormat, &fmt);
                 if ((fmt.flags & DM_FMT_RD) == 0)
                 {
                     dmErrorMsg("Invalid input format '%s', does not support reading.\n",
@@ -652,10 +664,10 @@
             {
                 DMConvFormat fmt;
 
-                if (!dmParseFormatOption("output", "Output", optArg, &optOutFormat, &optOutSubFormat))
+                if (!dmParseFormatOption("output", "Output", optArg, &optOutType, &optOutFormat))
                     return FALSE;
 
-                dmGetConvFormat(optOutFormat, optOutSubFormat, &fmt);
+                dmGetConvFormat(optOutType, optOutFormat, &fmt);
                 if ((fmt.flags & DM_FMT_WR) == 0)
                 {
                     dmErrorMsg("Invalid output format '%s', does not support writing.\n",
@@ -1593,7 +1605,7 @@
     int ret = DMERR_OK, itemCount, outWidth, outWidthPX, outHeight;
     size_t offs, outSize;
 
-    switch (optInFormat)
+    switch (optInType)
     {
         case FFMT_CHAR:
             outSize    = C64_CHR_SIZE;
@@ -1611,13 +1623,13 @@
 
         default:
             return dmError(DMERR_INTERNAL,
-                "Invalid input format %d, internal error.\n", optInFormat);
+                "Invalid input format %d, internal error.\n", optInType);
     }
 
     offs = 0;
     itemCount = 0;
 
-    if (optOutFormat == FFMT_ANSI || optOutFormat == FFMT_ASCII)
+    if (optOutType == FFMT_ANSI || optOutType == FFMT_ASCII)
     {
         BOOL error = FALSE;
         FILE *outFile;
@@ -1637,13 +1649,13 @@
         {
             fprintf(outFile, "---- : -------------- #%d\n", itemCount);
 
-            switch (optInFormat)
+            switch (optInType)
             {
                 case FFMT_CHAR:
-                    dmDumpCharASCII(outFile, dataBuf + offs, realOffs + offs, optOutFormat, optInMulticolor);
+                    dmDumpCharASCII(outFile, dataBuf + offs, realOffs + offs, optOutType, optInMulticolor);
                     break;
                 case FFMT_SPRITE:
-                    dmDumpSpriteASCII(outFile, dataBuf + offs, realOffs + offs, optOutFormat, optInMulticolor);
+                    dmDumpSpriteASCII(outFile, dataBuf + offs, realOffs + offs, optOutType, optInMulticolor);
                     break;
             }
             offs += outSize;
@@ -1653,7 +1665,7 @@
         fclose(outFile);
     }
     else
-    if (optOutFormat == FFMT_IMAGE)
+    if (optOutType == FFMT_IMAGE)
     {
         DMImage *outImage = NULL;
         char *outFilename = NULL;
@@ -1706,7 +1718,7 @@
                 outFilename = dm_strdup_printf("%s%04d.%s",
                     optOutFilename,
                     itemCount,
-                    convFormatList[optOutFormat].fext);
+                    convFormatList[optOutType].fext);
 
                 if (outFilename == NULL)
                 {
@@ -1715,7 +1727,7 @@
                 }
 
                 ret = dmWriteImage(outFilename, outImage, &optSpec,
-                    &dmImageFormatList[optOutSubFormat], TRUE);
+                    &dmImageFormatList[optOutFormat], TRUE);
                 if (ret != DMERR_OK)
                 {
                     dmErrorMsg("Error writing output image '%s': %s.\n",
@@ -1740,7 +1752,7 @@
         if (!optSequential)
         {
             ret = dmWriteImage(optOutFilename, outImage, &optSpec,
-                &dmImageFormatList[optOutSubFormat], TRUE);
+                &dmImageFormatList[optOutFormat], TRUE);
             if (ret != DMERR_OK)
             {
                 dmError(ret, "Error writing output image '%s': %s.\n",
@@ -1751,7 +1763,7 @@
         dmImageFree(outImage);
     }
     else
-    if (optOutFormat == FFMT_BITMAP)
+    if (optOutType == FFMT_BITMAP)
     {
         if (optSequential)
         {
@@ -1789,11 +1801,11 @@
     {
         const DMImageFormat *sfmt = &dmImageFormatList[i];
         DMConvFormat *dfmt = &convFormatList[n++];
-        dfmt->name = sfmt->name;
-        dfmt->fext = sfmt->fext;
-        dfmt->flags = sfmt->flags;
-        dfmt->format = FFMT_IMAGE;
-        dfmt->subformat = sfmt->fmtid;
+        dfmt->name   = sfmt->name;
+        dfmt->fext   = sfmt->fext;
+        dfmt->flags  = sfmt->flags;
+        dfmt->type   = FFMT_IMAGE;
+        dfmt->format = sfmt->fmtid;
     }
 
     for (i = 0; i < nbaseFormatList; i++)
@@ -1806,21 +1818,24 @@
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
         exit(1);
 
-    // Determine input format, if not specified'
-    if (optInFormat == FFMT_AUTO && optInFilename != NULL)
+    // Determine input format, if not specified
+    if (optInType == FFMT_AUTO && optInFilename != NULL)
     {
         char *dext = strrchr(optInFilename, '.');
-        dmMsg(4, "Trying to determine file format by extension.\n");
         if (dext)
         {
-            if (!dmGetFormatByExt(dext + 1, &optInFormat, &optInSubFormat))
-                dmGetC64FormatByExt(dext + 1, &optInFormat, &optInSubFormat);
+            if (dmGetFormatByExt(dext + 1, &optInType, &optInFormat) ||
+                dmGetC64FormatByExt(dext + 1, &optInType, &optInFormat))
+            {
+                dmMsg(3, "Guessed input type as %s\n",
+                    formatTypeList[optInType]);
+            }
         }
     }
 
     if (optInFilename == NULL)
     {
-        if (optInFormat == FFMT_AUTO)
+        if (optInType == FFMT_AUTO)
         {
             dmErrorMsg("Standard input cannot be used without specifying input format.\n");
             dmErrorMsg("Perhaps you should try using --help\n");
@@ -1838,6 +1853,23 @@
         goto error;
     }
 
+    // Determine output format, if not specified
+    if (optOutType == FFMT_AUTO && optOutFilename != NULL)
+    {
+        char *dext = strrchr(optOutFilename, '.');
+        if (dext)
+        {
+            if (dmGetFormatByExt(dext + 1, &optOutType, &optOutFormat) ||
+                dmGetC64FormatByExt(dext + 1, &optOutType, &optOutFormat))
+            {
+                dmMsg(3, "Guessed output type as %s\n",
+                    formatTypeList[optOutType]);
+            }
+        }
+    }
+    else
+        optOutType = FFMT_ASCII;
+
     // Read the input ..
     dmMsg(1, "Reading input from '%s'.\n", optInFilename);
 
@@ -1857,12 +1889,12 @@
     dataBuf = dataBufOrig + optInSkip;
     dataSize = dataSizeOrig - optInSkip;
 
-    // Check for forced input subformat here
+    // Check for forced input format here
     if (optForcedInSubFormat >= 0)
-        optInSubFormat = optForcedInSubFormat;
+        optInFormat = optForcedInSubFormat;
 
     // Perform probing, if required
-    if (optInFormat == FFMT_AUTO || optInFormat == FFMT_BITMAP)
+    if (optInType == FFMT_AUTO || optInType == FFMT_BITMAP)
     {
         // Probe for format
         const DMC64ImageFormat *forced = NULL;
@@ -1885,44 +1917,44 @@
             dmMsg(1, "Probed '%s' format image, type %d, %s\n",
                 inC64Fmt->name, inC64Fmt->format->type, inC64Fmt->fext);
 
-            optInFormat = FFMT_BITMAP;
+            optInType = FFMT_BITMAP;
         }
         else
-        if (res != DMERR_OK && (forced != NULL || optInFormat == FFMT_BITMAP))
+        if (res != DMERR_OK && (forced != NULL || optInType == FFMT_BITMAP))
         {
             dmErrorMsg("Could not decode input image: %s.\n", dmErrorStr(res));
             goto error;
         }
     }
 
-    if (optInFormat == FFMT_AUTO || optInFormat == FFMT_IMAGE)
+    if (optInType == FFMT_AUTO || optInType == FFMT_IMAGE)
     {
         const DMImageFormat *ifmt = NULL;
         int index;
         dmMsg(4, "Trying to probe image formats.\n");
         if (dmImageProbeGeneric(dataBuf, dataSize, &ifmt, &index) > 0)
         {
-            optInFormat = FFMT_IMAGE;
-            optInSubFormat = index;
+            optInType = FFMT_IMAGE;
+            optInFormat = index;
             dmMsg(1, "Probed '%s' format image.\n", ifmt->name);
         }
     }
 
-    if (optInFormat == FFMT_AUTO)
+    if (optInType == FFMT_AUTO)
     {
         dmErrorMsg("No input format specified, and could not be determined automatically.\n");
         goto error;
     }
 
-    if (dmGetConvFormat(optInFormat, optInSubFormat, &inFormat) &&
-        dmGetConvFormat(optOutFormat, optOutSubFormat, &outFormat))
+    if (dmGetConvFormat(optInType, optInFormat, &inFormat) &&
+        dmGetConvFormat(optOutType, optOutFormat, &outFormat))
     {
         dmMsg(1, "Attempting conversion %s (%s) -> %s (%s)\n",
             inFormat.name, inFormat.fext,
             outFormat.name, outFormat.fext);
     }
 
-    switch (optInFormat)
+    switch (optInType)
     {
         case FFMT_SPRITE:
         case FFMT_CHAR:
@@ -1940,7 +1972,7 @@
                     goto error;
                 }
 
-                switch (optOutFormat)
+                switch (optOutType)
                 {
                     case FFMT_IMAGE:
                     case FFMT_CHAR:
@@ -1953,24 +1985,24 @@
                             goto error;
                         }
 
-                        switch (optOutFormat)
+                        switch (optOutType)
                         {
                             case FFMT_IMAGE:
                                 res = dmWriteImage(optOutFilename, outImage, &optSpec,
-                                    &dmImageFormatList[optOutSubFormat], TRUE);
+                                    &dmImageFormatList[optOutFormat], TRUE);
                                 break;
 
                             case FFMT_CHAR:
                             case FFMT_SPRITE:
                                 res = dmWriteSpritesAndChars(optOutFilename, outImage,
-                                    optOutFormat, optInMulticolor);
+                                    optOutType, optInMulticolor);
                                 break;
                         }
                         break;
 
                     case FFMT_BITMAP:
                         if ((res = dmConvertC64Bitmap(&outC64Image, inC64Image,
-                            &dmC64ImageFormats[optOutSubFormat], inC64Fmt)) != DMERR_OK)
+                            &dmC64ImageFormats[optOutFormat], inC64Fmt)) != DMERR_OK)
                         {
                             dmErrorMsg("Error in bitmap format conversion.\n");
                             goto error;
@@ -1978,9 +2010,9 @@
                         if (dmVerbosity >= 2)
                         {
                             dmPrint(0, "INPUT:\n");  dmC64ImageDump(stderr, inC64Image, inC64Fmt, "  ");
-                            dmPrint(0, "OUTPUT:\n"); dmC64ImageDump(stderr, outC64Image, &dmC64ImageFormats[optOutSubFormat], "  ");
+                            dmPrint(0, "OUTPUT:\n"); dmC64ImageDump(stderr, outC64Image, &dmC64ImageFormats[optOutFormat], "  ");
                         }
-                        res = dmWriteBitmap(optOutFilename, outC64Image, &dmC64ImageFormats[optOutSubFormat]);
+                        res = dmWriteBitmap(optOutFilename, outC64Image, &dmC64ImageFormats[optOutFormat]);
                         break;
 
                     default:
@@ -1994,7 +2026,7 @@
 
         case FFMT_IMAGE:
             {
-                const DMImageFormat *ifmt = &dmImageFormatList[optInSubFormat];
+                const DMImageFormat *ifmt = &dmImageFormatList[optInFormat];
                 DMImage *inImage = NULL;
                 int res = DMERR_OK;
                 DMResource *fp;
@@ -2022,16 +2054,16 @@
                 if (res != DMERR_OK || inImage == NULL)
                     goto error;
 
-                switch (optOutFormat)
+                switch (optOutType)
                 {
                     case FFMT_IMAGE:
                         res = dmWriteImage(optOutFilename, inImage, &optSpec,
-                            &dmImageFormatList[optOutSubFormat], TRUE);
+                            &dmImageFormatList[optOutFormat], TRUE);
                         break;
 
                     case FFMT_CHAR:
                     case FFMT_SPRITE:
-                        res = dmWriteSpritesAndChars(optOutFilename, inImage, optOutFormat, optInMulticolor);
+                        res = dmWriteSpritesAndChars(optOutFilename, inImage, optOutType, optInMulticolor);
                         break;
 
                     default: