comparison 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
comparison
equal deleted inserted replaced
2207:1ea48084055e 2208:90ec1ec89c56
86 }; 86 };
87 87
88 const int ndmC64DefaultPalettes = sizeof(dmC64DefaultPalettes) / sizeof(dmC64DefaultPalettes[0]); 88 const int ndmC64DefaultPalettes = sizeof(dmC64DefaultPalettes) / sizeof(dmC64DefaultPalettes[0]);
89 89
90 90
91 int dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *pcpal, const BOOL mixed) 91 int dmC64PaletteFromC64Colors(DMPalette **ppal, const DMColor *colors, const BOOL mixed)
92 { 92 {
93 const DMC64Palette *cpal = pcpal;
94 int res; 93 int res;
95 94
96 if (cpal == NULL) 95 if (ppal == NULL || colors == NULL)
97 cpal = &dmC64DefaultPalettes[0]; 96 return DMERR_NULLPTR;
98 97
99 // Allocate and create new 98 // Allocate and create new
100 if (mixed) 99 if (mixed)
101 { 100 {
102 // Mixed 256 color palette 101 // Mixed 256 color palette
103 if ((res = dmPaletteAlloc(ppal, D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK) 102 if ((res = dmPaletteAlloc(ppal, D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK)
104 return res; 103 return res;
105 104
106 for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++) 105 for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++)
107 { 106 {
108 const DMColor *col1 = &cpal->colors[n1]; 107 const DMColor *col1 = &colors[n1];
109 for (int n2 = 0; n2 < D64_NCOLORS; n2++) 108 for (int n2 = 0; n2 < D64_NCOLORS; n2++)
110 { 109 {
111 const DMColor *col2 = &cpal->colors[n2]; 110 const DMColor *col2 = &colors[n2];
112 (*ppal)->colors[n].r = (col1->r + col2->r) / 2; 111 (*ppal)->colors[n].r = (col1->r + col2->r) / 2;
113 (*ppal)->colors[n].g = (col1->g + col2->g) / 2; 112 (*ppal)->colors[n].g = (col1->g + col2->g) / 2;
114 (*ppal)->colors[n].b = (col1->b + col2->b) / 2; 113 (*ppal)->colors[n].b = (col1->b + col2->b) / 2;
115 n++; 114 n++;
116 } 115 }
117 } 116 }
118 } 117 }
119 else 118 else
120 { 119 {
121 // Standard palette, just copy it 120 // Standard palette, just copy it
122 if ((res = dmPaletteAlloc(ppal, 256, 255)) != DMERR_OK) 121 if ((res = dmPaletteAlloc(ppal, D64_NCOLORS, 255)) != DMERR_OK)
123 return res; 122 return res;
124 123
125 memcpy((*ppal)->colors, cpal->colors, D64_NCOLORS * sizeof(DMColor)); 124 memcpy((*ppal)->colors, colors, D64_NCOLORS * sizeof(DMColor));
126 } 125 }
127 126
128 return DMERR_OK; 127 return DMERR_OK;
129 } 128 }
130 129
131 130
132 int dmC64SetImagePalette(DMImage *img, const DMC64Palette *cpal, const BOOL mixed) 131 int dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *cpal, const BOOL mixed)
133 { 132 {
134 if (img == NULL) 133 if (ppal == NULL || cpal == NULL)
134 return DMERR_NULLPTR;
135
136 return dmC64PaletteFromC64Colors(ppal, cpal->colors, mixed);
137 }
138
139
140 int dmC64SetImagePalette(DMImage *img, const DMC64ImageConvSpec *spec, const BOOL mixed)
141 {
142 if (img == NULL || spec == NULL)
135 return DMERR_NULLPTR; 143 return DMERR_NULLPTR;
136 144
137 // Free previous palette 145 // Free previous palette
138 if (!img->constpal) 146 if (!img->constpal)
139 dmPaletteFree(img->pal); 147 dmPaletteFree(img->pal);
140 148
141 img->constpal = FALSE; 149 img->constpal = FALSE;
142 150
143 return dmC64PaletteFromC64Palette(&img->pal, cpal, mixed); 151 // If specific palette is wanted, use it
152 if (spec->pal != NULL)
153 {
154 if (spec->pal->ncolors > D64_NCOLORS)
155 return dmPaletteCopy(&img->pal, spec->pal);
156 else
157 if (spec->pal->ncolors == D64_NCOLORS)
158 return dmC64PaletteFromC64Colors(&img->pal, spec->pal->colors, mixed);
159 }
160
161 // Else, use the c64 palette specified
162 return dmC64PaletteFromC64Palette(&img->pal, spec->cpal, mixed);
144 } 163 }
145 164
146 165
147 BOOL dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr) 166 BOOL dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr)
148 { 167 {
1427 if ((*pdst = dst = dmImageAlloc( 1446 if ((*pdst = dst = dmImageAlloc(
1428 src->fmt->width, src->fmt->height, DM_PIXFMT_PALETTE, -1)) == NULL) 1447 src->fmt->width, src->fmt->height, DM_PIXFMT_PALETTE, -1)) == NULL)
1429 return DMERR_MALLOC; 1448 return DMERR_MALLOC;
1430 1449
1431 // Set palette information 1450 // Set palette information
1432 if (spec->pal != NULL) 1451 if ((res = dmC64SetImagePalette(dst, spec, FALSE)) != DMERR_OK)
1433 dst->pal = spec->pal;
1434 else
1435 if ((res = dmC64SetImagePalette(dst, spec->cpal, FALSE)) != DMERR_OK)
1436 return res; 1452 return res;
1437 1453
1438 // Convert 1454 // Convert
1439 if (fmt->format->convertFrom != NULL) 1455 if (fmt->format->convertFrom != NULL)
1440 res = fmt->format->convertFrom(dst, src, fmt, spec); 1456 res = fmt->format->convertFrom(dst, src, fmt, spec);