changeset 2206:7694b5c8edc1

Add dmC64PaletteFromC64Palette(), used by dmC64SetImagePalette().
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Jun 2019 03:27:50 +0300
parents 3a25e85f0203
children 1ea48084055e
files tools/lib64gfx.c tools/lib64gfx.h
diffstat 2 files changed, 24 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64gfx.c	Fri Jun 14 03:27:25 2019 +0300
+++ b/tools/lib64gfx.c	Fri Jun 14 03:27:50 2019 +0300
@@ -88,28 +88,19 @@
 const int ndmC64DefaultPalettes = sizeof(dmC64DefaultPalettes) / sizeof(dmC64DefaultPalettes[0]);
 
 
-int dmC64SetImagePalette(DMImage *img, const DMC64Palette *ppal, const BOOL mixed)
+int dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *pcpal, const BOOL mixed)
 {
-    const DMC64Palette *cpal = ppal;
+    const DMC64Palette *cpal = pcpal;
     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 and create new
     if (mixed)
     {
         // Mixed 256 color palette
-        if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK)
+        if ((res = dmPaletteAlloc(ppal, D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK)
             return res;
 
         for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++)
@@ -118,9 +109,9 @@
             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;
+                (*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;
                 n++;
             }
         }
@@ -128,16 +119,31 @@
     else
     {
         // Standard palette, just copy it
-        if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS, 255)) != DMERR_OK)
+        if ((res = dmPaletteAlloc(ppal, 256, 255)) != DMERR_OK)
             return res;
 
-        memcpy(img->pal->colors, cpal->colors, img->pal->ncolors * sizeof(DMColor));
+        memcpy((*ppal)->colors, cpal->colors, D64_NCOLORS * sizeof(DMColor));
     }
 
     return DMERR_OK;
 }
 
 
+int dmC64SetImagePalette(DMImage *img, const DMC64Palette *cpal, const BOOL mixed)
+{
+    if (img == NULL)
+        return DMERR_NULLPTR;
+
+    // Free previous palette
+    if (!img->constpal)
+        dmPaletteFree(img->pal);
+
+    img->constpal = FALSE;
+
+    return dmC64PaletteFromC64Palette(&img->pal, cpal, mixed);
+}
+
+
 BOOL dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr)
 {
     return
--- a/tools/lib64gfx.h	Fri Jun 14 03:27:25 2019 +0300
+++ b/tools/lib64gfx.h	Fri Jun 14 03:27:50 2019 +0300
@@ -352,6 +352,7 @@
 //
 int       dmC64ProbeBMP(const DMGrowBuf *buf, const DMC64ImageFormat **fmt);
 
+int       dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *pcpal, const BOOL mixed);
 int       dmC64SetImagePalette(DMImage *img, const DMC64Palette *cpal, const BOOL mixed);
 
 BOOL      dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr);