diff tools/lib64gfx.c @ 2201:9f3fb4004c20

Improvements to the lib64gfx palette handling.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Jun 2019 00:42:36 +0300
parents dcd26cdc395e
children b259312ddb59
line wrap: on
line diff
--- a/tools/lib64gfx.c	Fri Jun 14 00:00:27 2019 +0300
+++ b/tools/lib64gfx.c	Fri Jun 14 00:42:36 2019 +0300
@@ -169,51 +169,50 @@
 }
 
 
-int dmSetDefaultC64Palette(DMImage *img)
+int dmC64SetImagePalette(DMImage *img, const DMC64Palette *ppal, const BOOL mixed)
 {
+    const DMC64Palette *cpal = ppal;
     int res;
 
+    if (img == NULL)
+        return DMERR_NULLPTR;
+
+    if (cpal == NULL)
+        cpal = &dmC64DefaultPalettes[0];
+
     // Free previous palette
     if (!img->constpal)
         dmPaletteFree(img->pal);
 
     img->constpal = FALSE;
 
-    // Allocate new
-    if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS, 255)) != DMERR_OK)
-        return res;
-
-    memcpy(img->pal->colors, dmC64DefaultPalettes[0].colors, img->pal->ncolors * sizeof(DMColor));
-
-    return DMERR_OK;
-}
-
-
-int dmSetMixedColorC64Palette(DMImage *img)
-{
-    int res;
-
-    // Free previous palette
-    if (!img->constpal)
-        dmPaletteFree(img->pal);
+    // Allocate and create new
+    if (mixed)
+    {
+        // Mixed 256 color palette
+        if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK)
+            return res;
 
-    img->constpal = FALSE;
-
-    // Allocate new
-    if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK)
-        return res;
-
-    for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++)
+        for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++)
+        {
+            const DMColor *col1 = &cpal->colors[n1];
+            for (int n2 = 0; n2 < D64_NCOLORS; n2++)
+            {
+                const DMColor *col2 = &cpal->colors[n2];
+                img->pal->colors[n].r = (col1->r + col2->r) / 2;
+                img->pal->colors[n].g = (col1->g + col2->g) / 2;
+                img->pal->colors[n].b = (col1->b + col2->b) / 2;
+                n++;
+            }
+        }
+    }
+    else
     {
-        const DMColor *col1 = &dmC64DefaultPalettes[0].colors[n1];
-        for (int n2 = 0; n2 < D64_NCOLORS; n2++)
-        {
-            const DMColor *col2 = &dmC64DefaultPalettes[0].colors[n2];
-            img->pal->colors[n].r = (col1->r + col2->r) / 2;
-            img->pal->colors[n].g = (col1->g + col2->g) / 2;
-            img->pal->colors[n].b = (col1->b + col2->b) / 2;
-            n++;
-        }
+        // Standard palette, just copy it
+        if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS, 255)) != DMERR_OK)
+            return res;
+
+        memcpy(img->pal->colors, cpal->colors, img->pal->ncolors * sizeof(DMColor));
     }
 
     return DMERR_OK;
@@ -1504,8 +1503,11 @@
         src->fmt->width, src->fmt->height, DM_PIXFMT_PALETTE, -1)) == NULL)
         return DMERR_MALLOC;
 
-    // Set partial palette information
-    if ((res = dmSetDefaultC64Palette(dst)) != DMERR_OK)
+    // Set palette information
+    if (spec->pal != NULL)
+        dst->pal = spec->pal;
+    else
+    if ((res = dmC64SetImagePalette(dst, spec->cpal, FALSE)) != DMERR_OK)
         return res;
 
     // Convert