diff tools/64vw.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 b1e392da8346
line wrap: on
line diff
--- a/tools/64vw.c	Fri Jun 14 03:28:46 2019 +0300
+++ b/tools/64vw.c	Fri Jun 14 05:01:12 2019 +0300
@@ -8,6 +8,7 @@
 #include "dmlib.h"
 #include "dmargs.h"
 #include "dmfile.h"
+#include "libgfx.h"
 #include "lib64gfx.h"
 #include "lib64util.h"
 #include <SDL.h>
@@ -25,6 +26,8 @@
 size_t  noptFilenames1 = 0, noptFilenames2 = 0;
 char    **optFilenames = NULL;
 char    *optCharROMFilename = NULL;
+DMC64Palette *optC64Palette = NULL;
+char    *optC64PaletteFile = NULL;
 
 DMC64MemBlock setCharROM;
 
@@ -41,6 +44,7 @@
     { 7, 'l', "list",       "Output list of files that were recognized (implies -i)", OPT_NONE },
     { 8, 'P', "probe",      "Probe only (do not attempt to decode the image)", OPT_NONE },
     { 9,   0, "char-rom",   "Set character ROM file to be used.", OPT_ARGREQ },
+    {10, 'p', "palette" ,   "Set C64 palette to be used (see -p list).", OPT_ARGREQ },
 };
 
 const int optListN = sizeof(optList) / sizeof(optList[0]);
@@ -164,6 +168,9 @@
             optCharROMFilename = optArg;
             break;
 
+        case 10:
+            return argHandleC64PaletteOption(optArg, &optC64Palette, &optC64PaletteFile);
+
         default:
             dmErrorMsg("Unimplemented option argument '%s'.\n", currArg);
             return FALSE;
@@ -223,7 +230,7 @@
 {
     DMImage bmap;
     BOOL charDataSet;
-    int ret;
+    int res;
 
     memset(&bmap, 0, sizeof(bmap));
     bmap.size     = surf->pitch * surf->h;
@@ -231,9 +238,12 @@
     bmap.pitch    = surf->pitch;
     bmap.width    = surf->w;
     bmap.height   = surf->h;
-
-    if ((ret = dmC64SetImagePalette(&bmap, NULL, FALSE)) != DMERR_OK)
-        return ret;
+    
+    if ((res = dmPaletteCopy(&bmap.pal, spec->pal)) != DMERR_OK)
+    {
+        dmErrorMsg("Could not create copy of palette.\n");
+        return res;
+    }
 
     if (cimage->charData[0].data == NULL)
     {
@@ -244,9 +254,9 @@
         charDataSet = FALSE;
 
     if (fmt->format->convertFrom != NULL)
-        ret = fmt->format->convertFrom(&bmap, cimage, fmt, spec);
+        res = fmt->format->convertFrom(&bmap, cimage, fmt, spec);
     else
-        ret = dmC64ConvertGenericBMP2Image(&bmap, cimage, fmt, spec);
+        res = dmC64ConvertGenericBMP2Image(&bmap, cimage, fmt, spec);
 
     if (charDataSet)
         memset(&cimage->charData[0], 0, sizeof(DMC64MemBlock));
@@ -254,15 +264,40 @@
     SDL_SetPaletteColors(surf->format->palette, (SDL_Color *) bmap.pal->colors, 0, bmap.pal->ncolors);
 
     dmPaletteFree(bmap.pal);
+    return res;
+}
 
-    return ret;
-}
+#if 0
+        DIR *dirh;
+        struct dirent *entry;
+
+        if ((dirh = opendir(npath)) == NULL)
+        {
+            int err = th_get_error();
+            THERR("Could not open directory '%s': %s\n",
+                path, th_error_str(err));
+            ret = FALSE;
+            goto out;
+        }
+
+        while ((entry = readdir(dirh)) != NULL)
+        if (entry->d_name[0] != '.')
+        {
+            if (!argHandleFileDir(npath, entry->d_name, pattern))
+            {
+                ret = FALSE;
+                goto out;
+            }
+        }
+
+        closedir(dirh);
+#endif
 
 
 int main(int argc, char *argv[])
 {
     const DMC64ImageFormat *forced;
-    DMC64ImageConvSpec spec;
+    DMC64ImageConvSpec optSpec;
     SDL_Window *window = NULL;
     SDL_Renderer *renderer = NULL;
     SDL_Texture *texture = NULL;
@@ -280,7 +315,7 @@
     }
 
     dmSetScaleFactor(2.0);
-    memset(&spec, 0, sizeof(spec));
+    memset(&optSpec, 0, sizeof(optSpec));
     memset(&setCharROM, 0, sizeof(setCharROM));
 
     dmInitProg("64vw", "Displayer for various C64 graphics formats", "0.4", NULL, NULL);
@@ -351,6 +386,30 @@
         goto exit;
     }
 
+    if (optC64PaletteFile != NULL)
+    {
+        if ((res = dmHandleExternalPalette(optC64PaletteFile, &optSpec.pal)) != DMERR_OK)
+            goto exit;
+    }
+    else
+    {
+        // No palette file specified, use internal palette
+        if (optC64Palette == NULL)
+            optC64Palette = &dmC64DefaultPalettes[0];
+            
+        dmMsg(1, "Using internal palette '%s' (%s).\n",
+            optC64Palette->name, optC64Palette->desc);
+
+        optSpec.cpal = optC64Palette;
+
+        if ((res = dmC64PaletteFromC64Palette(&optSpec.pal, optC64Palette, FALSE)) != DMERR_OK)
+        {
+            dmErrorMsg("Could not setup palette: %s\n",
+                dmErrorStr(res));
+            goto exit;
+        }
+    }
+
     // Check character ROM filename
     if (optCharROMFilename == NULL)
         optCharROMFilename = DM_DEF_CHARGEN;
@@ -524,7 +583,7 @@
                 goto exit;
             }
 
-            if (dmDecodeC64Image(cimage, fmt, surf, &spec) == DMERR_OK)
+            if (dmDecodeC64Image(cimage, fmt, surf, &optSpec) == DMERR_OK)
             {
                 title = dm_strdup_printf("%s - [%d / %d] %s (%dx%d @ %s)",
                     dmProgName,
@@ -608,6 +667,7 @@
     if (initSDL)
         SDL_Quit();
 
+    dmPaletteFree(optSpec.pal);
     dmLib64GFXClose();
 
     return 0;