diff tools/lib64gfx.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 7694b5c8edc1
children 5477e792def3
line wrap: on
line diff
--- a/tools/lib64gfx.c	Fri Jun 14 03:28:46 2019 +0300
+++ b/tools/lib64gfx.c	Fri Jun 14 05:01:12 2019 +0300
@@ -88,13 +88,12 @@
 const int ndmC64DefaultPalettes = sizeof(dmC64DefaultPalettes) / sizeof(dmC64DefaultPalettes[0]);
 
 
-int dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *pcpal, const BOOL mixed)
+int dmC64PaletteFromC64Colors(DMPalette **ppal, const DMColor *colors, const BOOL mixed)
 {
-    const DMC64Palette *cpal = pcpal;
     int res;
 
-    if (cpal == NULL)
-        cpal = &dmC64DefaultPalettes[0];
+    if (ppal == NULL || colors == NULL)
+        return DMERR_NULLPTR;
 
     // Allocate and create new
     if (mixed)
@@ -105,10 +104,10 @@
 
         for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++)
         {
-            const DMColor *col1 = &cpal->colors[n1];
+            const DMColor *col1 = &colors[n1];
             for (int n2 = 0; n2 < D64_NCOLORS; n2++)
             {
-                const DMColor *col2 = &cpal->colors[n2];
+                const DMColor *col2 = &colors[n2];
                 (*ppal)->colors[n].r = (col1->r + col2->r) / 2;
                 (*ppal)->colors[n].g = (col1->g + col2->g) / 2;
                 (*ppal)->colors[n].b = (col1->b + col2->b) / 2;
@@ -119,19 +118,28 @@
     else
     {
         // Standard palette, just copy it
-        if ((res = dmPaletteAlloc(ppal, 256, 255)) != DMERR_OK)
+        if ((res = dmPaletteAlloc(ppal, D64_NCOLORS, 255)) != DMERR_OK)
             return res;
 
-        memcpy((*ppal)->colors, cpal->colors, D64_NCOLORS * sizeof(DMColor));
+        memcpy((*ppal)->colors, colors, D64_NCOLORS * sizeof(DMColor));
     }
 
     return DMERR_OK;
 }
 
 
-int dmC64SetImagePalette(DMImage *img, const DMC64Palette *cpal, const BOOL mixed)
+int dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *cpal, const BOOL mixed)
 {
-    if (img == NULL)
+    if (ppal == NULL || cpal == NULL)
+        return DMERR_NULLPTR;
+
+    return dmC64PaletteFromC64Colors(ppal, cpal->colors, mixed);
+}
+
+
+int dmC64SetImagePalette(DMImage *img, const DMC64ImageConvSpec *spec, const BOOL mixed)
+{
+    if (img == NULL || spec == NULL)
         return DMERR_NULLPTR;
 
     // Free previous palette
@@ -140,7 +148,18 @@
 
     img->constpal = FALSE;
 
-    return dmC64PaletteFromC64Palette(&img->pal, cpal, mixed);
+    // If specific palette is wanted, use it
+    if (spec->pal != NULL)
+    {
+        if (spec->pal->ncolors > D64_NCOLORS)
+            return dmPaletteCopy(&img->pal, spec->pal);
+        else
+        if (spec->pal->ncolors == D64_NCOLORS)
+            return dmC64PaletteFromC64Colors(&img->pal, spec->pal->colors, mixed);
+    }
+
+    // Else, use the c64 palette specified
+    return dmC64PaletteFromC64Palette(&img->pal, spec->cpal, mixed);
 }
 
 
@@ -1429,10 +1448,7 @@
         return DMERR_MALLOC;
 
     // Set palette information
-    if (spec->pal != NULL)
-        dst->pal = spec->pal;
-    else
-    if ((res = dmC64SetImagePalette(dst, spec->cpal, FALSE)) != DMERR_OK)
+    if ((res = dmC64SetImagePalette(dst, spec, FALSE)) != DMERR_OK)
         return res;
 
     // Convert