changeset 2065:451980580189

Refactor how paletted/indexed formats are handled in libgfx.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Dec 2018 13:33:03 +0200
parents 3617ef01c1de
children 63284e9ad95e
files tools/gfxconv.c tools/libgfx.c tools/libgfx.h
diffstat 3 files changed, 48 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Wed Dec 05 13:31:59 2018 +0200
+++ b/tools/gfxconv.c	Wed Dec 05 13:33:03 2018 +0200
@@ -127,7 +127,8 @@
 BOOL    optInMulticolor = FALSE,
         optSequential = FALSE,
         optRemapColors = FALSE,
-        optRemapRemove = FALSE;
+        optRemapRemove = FALSE,
+        optUsePalette = FALSE;
 int     optNRemapTable = 0,
         optScaleMode = SCALE_AUTO;
 DMMapValue optRemapTable[DM_MAX_COLORS];
@@ -140,7 +141,6 @@
     .nplanes = 4,
     .bpp = 8,
     .planar = FALSE,
-    .paletted = FALSE,
     .format = 0,
     .compression = FCMP_BEST,
 };
@@ -816,7 +816,7 @@
             break;
 
         case 12:
-            optSpec.paletted = TRUE;
+            optUsePalette = TRUE;
             break;
 
         case 13:
@@ -1327,7 +1327,7 @@
 int dmWriteIFFMasterRAWHeaderFile(
     const char *hdrFilename, const char *dataFilename,
     const char *prefix, const DMImage *img,
-    const DMImageConvSpec *spec, const int fmtid)
+    const DMImageConvSpec *spec)
 {
     DMResource *fp;
     int res;
@@ -1339,7 +1339,7 @@
             hdrFilename);
     }
 
-    res = dmWriteIFFMasterRAWHeader(fp, dataFilename, prefix, img, spec, fmtid);
+    res = dmWriteIFFMasterRAWHeader(fp, dataFilename, prefix, img, spec);
 
     dmf_close(fp);
     return res;
@@ -1378,11 +1378,29 @@
         allocated = TRUE;
     }
 
+    // Determine number of planes, if paletted
+    if (spec->format == DM_COLFMT_PALETTE)
+    {
+        spec->nplanes = 0;
+        for (int n = 8; n >= 0;)
+        {
+            if ((image->ncolors - 1) & (1 << n))
+            {
+                spec->nplanes = n + 1;
+                break;
+            }
+            else
+                n--;
+        }
+    }
+
+    spec->fmtid = fmt->fmtid;
+
     // Do some format-specific adjustments and other things
     switch (fmt->fmtid)
     {
         case DM_IMGFMT_PNG:
-            spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGBA;
+            spec->format = optUsePalette ? DM_COLFMT_PALETTE : DM_COLFMT_RGBA;
             break;
 
         case DM_IMGFMT_PPM:
@@ -1416,29 +1434,16 @@
                 }
 
                 res = dmWriteIFFMasterRAWHeaderFile(
-                    hdrFilename, filename, prefix, image, spec, fmt->fmtid);
+                    hdrFilename, filename, prefix, image, spec);
 
                 dmFree(prefix);
                 dmFree(hdrFilename);
             }
             break;
 
-        case DM_IMGFMT_IFF:
-            spec->nplanes = 0;
-            for (int n = 8; n >= 0;)
-            {
-                if ((image->ncolors - 1) & (1 << n))
-                {
-                    spec->nplanes = n + 1;
-                    break;
-                }
-                else
-                    n--;
-            }
+        default:
+            spec->format = optUsePalette ? DM_COLFMT_PALETTE : DM_COLFMT_RGB;
             break;
-
-        default:
-            spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGB;
     }
 
     // If no error has occured thus far, write the image
--- a/tools/libgfx.c	Wed Dec 05 13:31:59 2018 +0200
+++ b/tools/libgfx.c	Wed Dec 05 13:33:03 2018 +0200
@@ -303,7 +303,7 @@
 
 int dmWriteIFFMasterRAWHeader(
     DMResource *fp, const char *filename, const char *prefix,
-    const DMImage *img, const DMImageConvSpec *spec, const int fmtid)
+    const DMImage *img, const DMImageConvSpec *spec)
 {
     if (dmfprintf(fp,
         "%s_width: dw.w %d\n"
@@ -314,7 +314,7 @@
         prefix, spec->nplanes) < 0)
         return dmferror(fp);
 
-    if (fmtid == DM_IMGFMT_ARAW)
+    if (spec->fmtid == DM_IMGFMT_ARAW)
     {
         if (dmfprintf(fp,
             "%s_ncolors: dw.w %d\n"
@@ -999,13 +999,13 @@
     spec.planar = TRUE;
 
     // XXX: 24bit PCX does not work yet ..
-    if (!spec.paletted)
+    if (spec.format != DM_COLFMT_PALETTE)
     {
         return dmError(DMERR_NOT_SUPPORTED,
             "24bit PCX not supported yet.\n");
     }
 
-    if (spec.paletted && img->pal == NULL)
+    if (spec.format == DM_COLFMT_PALETTE && img->pal == NULL)
     {
         return dmError(DMERR_NULLPTR,
             "Image spec says paletted/indexed image, but palette pointer is NULL.\n");
@@ -1018,7 +1018,7 @@
 
     // Create PCX header
     dmMemset(&hdr, 0, sizeof(hdr));
-    if (spec.paletted)
+    if (spec.format == DM_COLFMT_PALETTE)
     {
         const int ncolors = img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors;
         for (int i = 0; i < ncolors; i++)
@@ -1055,9 +1055,9 @@
         hdr.hres, hdr.vres,
         hdr.hScreenSize, hdr.vScreenSize);
 
-    dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s, planar=%s\n",
+    dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, colfmt=%d, planar=%s\n",
         hdr.nplanes, hdr.bitsPerPlane, hdr.bpl,
-        spec.paletted ? "yes" : "no",
+        spec.format,
         spec.planar ? "yes" : "no"
         );
 
@@ -1118,7 +1118,7 @@
     res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, &spec);
 
     // Write VGA palette
-    if (spec.paletted)
+    if (spec.format == DM_COLFMT_PALETTE)
     {
         int i;
         dmMsg(2, "PCX: Writing palette of %d active entries.\n", img->ncolors);
@@ -2199,22 +2199,22 @@
     Uint8 *buf = NULL;
     size_t bufLen;
     int res = DMERR_OK;
-    //DMImageConvSpec pspec, *spec = &pspec;
-
-    //memcpy(&pspec, cpspec, sizeof(DMImageConvSpec));
 
     // XXX: Non-paletted IFF not supported!
-    if (!spec->paletted)
+    if (spec->format != DM_COLFMT_PALETTE)
     {
         return dmError(DMERR_NOT_SUPPORTED,
             "Non-paletted IFF is not supported.\n");
     }
 
-    switch (spec->format)
+    switch (spec->fmtid)
     {
         case DM_IMGFMT_IFF_ILBM: iff.idsig = IFF_ID_ILBM; iff.idstr = "ILBM"; break;
         case DM_IMGFMT_IFF_PBM : iff.idsig = IFF_ID_PBM; iff.idstr = "PBM"; break;
         case DM_IMGFMT_IFF_ACBM: iff.idsig = IFF_ID_ACBM; iff.idstr = "ACBM"; break;
+        default:
+            return dmError(DMERR_NOT_SUPPORTED,
+                "Invalid IFF format.\n");
     }
 
     // Setup headers
@@ -2277,7 +2277,7 @@
     //
     // CMAP
     //
-    if (img->ncolors > 0 && spec->paletted)
+    if (img->ncolors > 0 && spec->format == DM_COLFMT_PALETTE)
     {
         if ((res = dmWriteIFFChunkHdr(fp, &iff.chCMAP, IFF_ID_CMAP)) != DMERR_OK)
             goto out;
--- a/tools/libgfx.h	Wed Dec 05 13:31:59 2018 +0200
+++ b/tools/libgfx.h	Wed Dec 05 13:33:03 2018 +0200
@@ -90,11 +90,12 @@
 
 typedef struct
 {
-    int format;
-    int scaleX, scaleY;
-    int nplanes, bpp, mask;
-    BOOL planar, paletted;
-    int compression;
+    int  fmtid;               // DM_IMGFMT_* of target format (a bit of a kludge here)
+    int  format;              // Target color format DM_COLFMT_* 
+    int  scaleX, scaleY;      // Scale factors (1..)
+    int  nplanes, bpp, mask;
+    BOOL planar;
+    int  compression;         // Use compression/compression level (0 = none, 9 = max)
 } DMImageConvSpec;
 
 
@@ -129,7 +130,7 @@
     int (*writeRowCB)(void *, const Uint8 *, const size_t), const DMImageConvSpec *spec);
 
 
-int dmWriteIFFMasterRAWHeader(DMResource *fp, const char *filename, const char *prefix, const DMImage *img, const DMImageConvSpec *spec, const int fmtid);
+int dmWriteIFFMasterRAWHeader(DMResource *fp, const char *filename, const char *prefix, const DMImage *img, const DMImageConvSpec *spec);
 int dmWriteRAWImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
 
 int dmWritePPMImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);