changeset 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 455a3849b8ac
files tools/64vw.c tools/gfxconv.c tools/lib64fmts.c tools/lib64gfx.c tools/lib64gfx.h
diffstat 5 files changed, 55 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/tools/64vw.c	Fri Jun 14 00:00:27 2019 +0300
+++ b/tools/64vw.c	Fri Jun 14 00:42:36 2019 +0300
@@ -230,7 +230,7 @@
     bmap.width    = surf->w;
     bmap.height   = surf->h;
 
-    if ((ret = dmSetDefaultC64Palette(&bmap)) != DMERR_OK)
+    if ((ret = dmC64SetImagePalette(&bmap, NULL, FALSE)) != DMERR_OK)
         return ret;
 
     if (cimage->charData[0].data == NULL)
--- a/tools/gfxconv.c	Fri Jun 14 00:00:27 2019 +0300
+++ b/tools/gfxconv.c	Fri Jun 14 00:42:36 2019 +0300
@@ -1830,7 +1830,7 @@
             outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight, DM_PIXFMT_PALETTE, -1);
         }
 
-        if ((err = dmSetDefaultC64Palette(outImage)) != DMERR_OK)
+        if ((err = dmC64SetImagePalette(outImage, NULL, FALSE)) != DMERR_OK)
         {
             dmErrorMsg("Could not allocate C64 palette for output image: %d\n", err);
             goto error;
--- a/tools/lib64fmts.c	Fri Jun 14 00:00:27 2019 +0300
+++ b/tools/lib64fmts.c	Fri Jun 14 00:42:36 2019 +0300
@@ -1545,7 +1545,10 @@
 {
     int res;
 
-    if ((res = dmSetMixedColorC64Palette(dst)) != DMERR_OK)
+    if (spec->pal != NULL)
+        dst->pal = spec->pal;
+    else
+    if ((res = dmC64SetImagePalette(dst, spec->cpal, TRUE)) != DMERR_OK)
         return res;
 
     return dmC64ConvertGenericBMP2Image(dst, src, fmt, spec);
--- 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
--- a/tools/lib64gfx.h	Fri Jun 14 00:00:27 2019 +0300
+++ b/tools/lib64gfx.h	Fri Jun 14 00:42:36 2019 +0300
@@ -153,8 +153,18 @@
 
 typedef struct
 {
+    char *name;
+    DMColor colors[D64_NCOLORS];
+} DMC64Palette;
+
+
+typedef struct
+{
     int dither;      // Dither mode (D64_DITH_*)
     BOOL aspect;     // Correct pixel aspect ratio?
+
+    DMPalette *pal;     // Use this palette
+    DMC64Palette *cpal; // If ^pal == NULL, use this C64 palette
 } DMC64ImageConvSpec;
 
 
@@ -167,13 +177,6 @@
 
 typedef struct
 {
-    char *name;
-    DMColor colors[D64_NCOLORS];
-} DMC64Palette;
-
-
-typedef struct
-{
     BOOL multicolor, xexpand, yexpand;
     int color, xc, yc;
     Uint8 data[D64_SPR_HEIGHT_UT][D64_SPR_WIDTH_UT];
@@ -357,8 +360,7 @@
 char *    dmC64GetImageTypeString(char *buf, const size_t len, const int type, const BOOL lng);
 void      dmC64ImageDump(FILE *fh, const DMC64Image *img, const DMC64ImageFormat *fmt, const char *indent);
 
-int       dmSetDefaultC64Palette(DMImage *img);
-int       dmSetMixedColorC64Palette(DMImage *img);
+int       dmC64SetImagePalette(DMImage *img, const DMC64Palette *cpal, const BOOL mixed);
 
 BOOL      dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr);
 int       dmC64ImageGetNumBlocks(const DMC64ImageFormat *fmt);