changeset 1616:36d073c45327

Refactor the format handling a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 15 May 2018 14:29:32 +0300
parents 2f8f2f7a42c8
children 625a7867877e
files tools/gfxconv.c tools/libgfx.c tools/libgfx.h
diffstat 3 files changed, 58 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Tue May 15 12:38:55 2018 +0300
+++ b/tools/gfxconv.c	Tue May 15 14:29:32 2018 +0300
@@ -55,22 +55,20 @@
 } DMConvFormat;
 
 
-static const DMConvFormat convFormatList[] =
+static const DMConvFormat baseFormatList[] =
 {
     { "ASCII text"                           , "asc"   , DM_FMT_WR   , FFMT_ASCII  , 0 },
     { "ANSI colored text"                    , "ansi"  , DM_FMT_WR   , FFMT_ANSI   , 0 },
-    { "PNG image"                            , "png"   , DM_FMT_RDWR , FFMT_IMAGE  , DM_IMGFMT_PNG },
-    { "PPM image"                            , "ppm"   , DM_FMT_WR   , FFMT_IMAGE  , DM_IMGFMT_PPM },
-    { "PCX image"                            , "pcx"   , DM_FMT_RDWR , FFMT_IMAGE  , DM_IMGFMT_PCX },
-    { "IFF ILBM"                             , "lbm"   , DM_FMT_RD   , FFMT_IMAGE  , DM_IMGFMT_ILBM },
-    { "Bitplaned RAW (intl/non-intl) image"  , "raw"   , DM_FMT_WR   , FFMT_IMAGE  , DM_IMGFMT_RAW },
-    { "IFFMaster RAW image"                  , "araw"  , DM_FMT_WR   , FFMT_IMAGE  , DM_IMGFMT_ARAW },
     { "C64 bitmap image"                     , NULL    , DM_FMT_RDWR , FFMT_BITMAP , -1  },
     { "C64 character/font data"              , "chr"   , DM_FMT_RDWR , FFMT_CHAR   , 0 },
     { "C64 sprite data"                      , "spr"   , DM_FMT_RDWR , FFMT_SPRITE , 0 },
 };
 
-static const int nconvFormatList = sizeof(convFormatList) / sizeof(convFormatList[0]);
+static const int nbaseFormatList = sizeof(baseFormatList) / sizeof(baseFormatList[0]);
+
+
+static DMConvFormat *convFormatList = NULL;
+static int nconvFormatList = 0;
 
 
 typedef struct
@@ -87,8 +85,8 @@
 
 int     optInFormat = FFMT_AUTO,
         optOutFormat = FFMT_ASCII,
-        optInSubFormat = DM_IMGFMT_PNG,
-        optOutSubFormat = DM_IMGFMT_PNG,
+        optInSubFormat = -1,
+        optOutSubFormat = -1,
         optItemCount = -1,
         optPlanedWidth = 1,
         optForcedFormat = -1;
@@ -1606,12 +1604,30 @@
     DMC64Image *inC64Image = NULL, *outC64Image = NULL;
     Uint8 *dataBuf = NULL, *dataBufOrig = NULL;
     size_t dataSize, dataSizeOrig;
-    int i;
+    int i, n;
 
     // Default colors
     for (i = 0; i < C64_NCOLORS; i++)
         optColors[i] = i;
 
+    // Initialize list of additional conversion formats
+    nconvFormatList = ndmImageFormatList + nbaseFormatList;
+    convFormatList = dmCalloc(nconvFormatList, sizeof(DMConvFormat));
+
+    for (n = i = 0; i < ndmImageFormatList; i++)
+    {
+        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;
+    }
+
+    for (i = 0; i < nbaseFormatList; i++)
+        memcpy(&convFormatList[n++], &baseFormatList[i], sizeof(DMConvFormat));
+
     // Initialize and parse commandline
     dmInitProg("gfxconv", "Simple graphics converter", "0.91", NULL, NULL);
 
@@ -1619,14 +1635,6 @@
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
         exit(1);
 
-#ifndef DM_USE_LIBPNG
-    if (optOutFormat == DM_IMGFMT_PNG)
-    {
-        dmErrorMsg("PNG output format support not compiled in, sorry.\n");
-        goto error;
-    }
-#endif
-
     // Determine input format, if not specified'
     if (optInFormat == FFMT_AUTO && optInFilename != NULL)
     {
@@ -1709,7 +1717,7 @@
 
     if (optInFormat == FFMT_AUTO || optInFormat == FFMT_IMAGE)
     {
-        DMImageFormat *ifmt = NULL;
+        const DMImageFormat *ifmt = NULL;
         int index;
         dmMsg(4, "Trying to probe image formats.\n");
         if (dmImageProbeGeneric(dataBuf + optInSkip, dataSize - optInSkip, &ifmt, &index) > 0)
@@ -1731,7 +1739,8 @@
 
     if (inFormat != -1 && outFormat != -1)
     {
-        char *inFmtName = convFormatList[inFormat].name,
+        const char
+             *inFmtName = convFormatList[inFormat].name,
              *inFmtExt = convFormatList[inFormat].fext,
              *outFmtName = convFormatList[outFormat].name,
              *outFmtExt = convFormatList[outFormat].fext;
@@ -1810,6 +1819,7 @@
 
         case FFMT_IMAGE:
             {
+                const DMImageFormat *ifmt = &dmImageFormatList[optInSubFormat];
                 DMImage *inImage = NULL;
                 int res = DMERR_OK;
                 DMResource *fp;
@@ -1827,7 +1837,6 @@
                 }
 
                 // Read input
-                DMImageFormat *ifmt = &dmImageFormatList[optInSubFormat];
                 if (ifmt->readFILE != NULL)
                     res = ifmt->readFILE(fp, &inImage);
                 else
--- a/tools/libgfx.c	Tue May 15 12:38:55 2018 +0300
+++ b/tools/libgfx.c	Tue May 15 14:29:32 2018 +0300
@@ -1988,59 +1988,64 @@
 //
 // List of formats
 //
-DMImageFormat dmImageFormatList[DM_IMGFMT_LAST] =
+const DMImageFormat dmImageFormatList[] =
 {
+#ifdef DM_USE_LIBPNG
     {
-        "PNG", "Portable Network Graphics",
+        "png", "Portable Network Graphics",
+        DM_IMGFMT_PNG, DM_FMT_RDWR,
         fmtProbePNG,
-#ifdef DM_USE_LIBPNG
         dmReadPNGImage, dmReadPNGImageFILE,
         dmWritePNGImage, dmWritePNGImageFILE,
-#else
-        NULL, NULL,
-        NULL, NULL,
+    },
 #endif
-    },
     {
-        "PPM", "Portable PixMap",
+        "ppm", "Portable PixMap",
+        DM_IMGFMT_PPM, DM_FMT_WR,
         NULL,
         NULL, NULL,
         dmWritePPMImage, dmWritePPMImageFILE,
     },
     {
-        "PCX", "Z-Soft Paintbrush",
+        "pcx", "Z-Soft Paintbrush",
+        DM_IMGFMT_PCX, DM_FMT_RDWR,
         fmtProbePCX,
         dmReadPCXImage, dmReadPCXImageFILE,
         dmWritePCXImage, dmWritePCXImageFILE,
     },
     {
-        "ILBM", "IFF ILBM",
+        "lbm", "IFF ILBM / PBM",
+        DM_IMGFMT_ILBM, DM_FMT_RD,
         fmtProbeILBM,
         dmReadILBMImage, dmReadILBMImageFILE,
         NULL, NULL,
     },
     {
-        "RAW", "Plain bitplaned (planar or non-planar) RAW",
+        "raw", "Plain bitplaned (planar or non-planar) RAW",
+        DM_IMGFMT_RAW, DM_FMT_WR,
         NULL,
         NULL, NULL,
         dmWriteRAWImage, dmWriteRAWImageFILE,
     },
     {
-        "ARAW", "IFFMaster Amiga RAW",
+        "araw", "IFFMaster Amiga RAW",
+        DM_IMGFMT_ARAW, DM_FMT_WR,
         NULL,
         NULL, NULL,
         dmWriteRAWImage, dmWriteRAWImageFILE,
     }
 };
 
+const int ndmImageFormatList = sizeof(dmImageFormatList) / sizeof(dmImageFormatList[0]);
 
-int dmImageProbeGeneric(const Uint8 *buf, const size_t len, DMImageFormat **pfmt, int *index)
+
+int dmImageProbeGeneric(const Uint8 *buf, const size_t len, const DMImageFormat **pfmt, int *index)
 {
-    int i, scoreMax = DM_PROBE_SCORE_FALSE, scoreIndex = -1;
+    int scoreMax = DM_PROBE_SCORE_FALSE, scoreIndex = -1;
 
-    for (i = 0; i < DM_IMGFMT_LAST; i++)
+    for (int i = 0; i < ndmImageFormatList; i++)
     {
-        DMImageFormat *fmt = &dmImageFormatList[i];
+        const DMImageFormat *fmt = &dmImageFormatList[i];
         if (fmt->probe != NULL)
         {
             int score = fmt->probe(buf, len);
--- a/tools/libgfx.h	Tue May 15 12:38:55 2018 +0300
+++ b/tools/libgfx.h	Tue May 15 14:29:32 2018 +0300
@@ -25,8 +25,6 @@
     DM_IMGFMT_ILBM,
     DM_IMGFMT_RAW,
     DM_IMGFMT_ARAW,
-
-    DM_IMGFMT_LAST
 };
 
 
@@ -97,7 +95,9 @@
 typedef struct
 {
     char *fext;
-    char *desc;
+    char *name;
+    int  fmtid;  // DM_IMGFMT_*
+    int  flags;  // DM_FMT_* flags
     int  (*probe)(const Uint8 *buf, const size_t len);
     int  (*read)(const char *filename, DMImage **pimg);
     int  (*readFILE)(DMResource *fp, DMImage **pimg);
@@ -106,14 +106,15 @@
 } DMImageFormat;
 
 
-extern DMImageFormat dmImageFormatList[DM_IMGFMT_LAST];
+extern const DMImageFormat dmImageFormatList[];
+extern const int ndmImageFormatList;
 extern int dmGFXErrorMode;
 
 
 DMImage * dmImageAlloc(const int width, const int height, const int format, const int bpp);
 void      dmImageFree(DMImage *img);
 int       dmImageGetBytesPerPixel(const int format);
-int       dmImageProbeGeneric(const Uint8 *buf, const size_t len, DMImageFormat **fmt, int *index);
+int       dmImageProbeGeneric(const Uint8 *buf, const size_t len, const DMImageFormat **fmt, int *index);
 
 BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha);