diff tools/lib64util.c @ 2208:90ec1ec89c56

Revamp the palette handling in lib64gfx somewhat, add helper functions to lib64util for handling external palette file options and add support for specifying one of the "internal" palettes or external (.act) palette file to gfxconv and 64vw.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Jun 2019 05:01:12 +0300
parents cbac4912992c
children fa5e74384d87
line wrap: on
line diff
--- a/tools/lib64util.c	Fri Jun 14 03:28:46 2019 +0300
+++ b/tools/lib64util.c	Fri Jun 14 05:01:12 2019 +0300
@@ -6,6 +6,7 @@
  * Please read file 'COPYING' for information on license and distribution.
  */
 #include "lib64util.h"
+#include "dmfile.h"
 
 
 char * dmC64GetImageTypeString(char *buf, const size_t len, const int type, const BOOL lng)
@@ -104,3 +105,93 @@
     }
     fprintf(stdout, "\n");
 }
+
+
+BOOL argHandleC64PaletteOption(char *optArg, DMC64Palette **ppal, char **palFile)
+{
+    if (strcasecmp(optArg, "help") == 0 ||
+        strcasecmp(optArg, "list") == 0)
+    {
+        argShowC64PaletteHelp();
+        return FALSE;
+    }
+
+    for (int n = 0; n < ndmC64DefaultPalettes; n++)
+    {
+        DMC64Palette *pal = &dmC64DefaultPalettes[n];
+        if (strcasecmp(pal->name, optArg) == 0)
+        {
+            *ppal = pal;
+            return TRUE;
+        }
+    }
+
+    *palFile = optArg;
+    return TRUE;
+}
+
+
+int dmHandleExternalPalette(const char *filename, DMPalette **ppal)
+{
+    DMResource *fp = NULL;
+    const DMImageFormat *ifmt = NULL;
+    const DMPaletteFormat *pfmt = NULL;
+    DMImage *inImage = NULL;
+    Uint8 *dataBuf = NULL;
+    size_t dataSize;
+    int index, res;
+
+    dmMsg(1, "Probing file '%s' for palette data.\n", filename);
+
+    if ((res = dmReadDataFile(NULL, filename, &dataBuf, &dataSize)) != DMERR_OK)
+    {
+        dmErrorMsg("No such palette '%s', and no such file found (%s).\n",
+            filename, dmErrorStr(res));
+        goto done;
+    }
+
+    if ((res = dmf_open_memio(NULL, filename, dataBuf, dataSize, &fp)) != DMERR_OK)
+    {
+        dmErrorMsg("Could not create MemIO handle for input.\n");
+        goto done;
+    }
+
+    if (dmImageProbeGeneric(dataBuf, dataSize, &ifmt, &index) > 0 &&
+        ifmt->read != NULL)
+    {
+        dmMsg(1, "Probed image format %s (%s)\n",
+            ifmt->name, ifmt->fext);
+
+        res = ifmt->read(fp, &inImage);
+        if (res != DMERR_OK)
+        {
+            dmErrorMsg("Could not read image file: %s\n",
+                dmErrorStr(res));
+            goto done;
+        }
+
+        res = dmPaletteCopy(ppal, inImage->pal);
+    }
+    else
+    if (dmPaletteProbeGeneric(dataBuf, dataSize, &pfmt, &index) > 0 &&
+        pfmt->read != NULL)
+    {
+        dmMsg(1, "Probed palette format %s (%s)\n",
+            pfmt->name, pfmt->fext);
+
+        res = pfmt->read(fp, ppal);
+    }
+    else
+    {
+        res = DMERR_NOT_SUPPORTED;
+        dmErrorMsg("Not an internal palette or recognized palette file '%s'.\n",
+            filename);
+    }
+
+done:
+    dmf_close(fp);
+    dmImageFree(inImage);
+    dmFree(dataBuf);
+
+    return res;
+}