Mercurial > hg > dmlib
comparison 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 |
comparison
equal
deleted
inserted
replaced
2200:dcd26cdc395e | 2201:9f3fb4004c20 |
---|---|
167 indent, fmt->format->chWidth, fmt->format->chHeight); | 167 indent, fmt->format->chWidth, fmt->format->chHeight); |
168 } | 168 } |
169 } | 169 } |
170 | 170 |
171 | 171 |
172 int dmSetDefaultC64Palette(DMImage *img) | 172 int dmC64SetImagePalette(DMImage *img, const DMC64Palette *ppal, const BOOL mixed) |
173 { | 173 { |
174 const DMC64Palette *cpal = ppal; | |
174 int res; | 175 int res; |
176 | |
177 if (img == NULL) | |
178 return DMERR_NULLPTR; | |
179 | |
180 if (cpal == NULL) | |
181 cpal = &dmC64DefaultPalettes[0]; | |
175 | 182 |
176 // Free previous palette | 183 // Free previous palette |
177 if (!img->constpal) | 184 if (!img->constpal) |
178 dmPaletteFree(img->pal); | 185 dmPaletteFree(img->pal); |
179 | 186 |
180 img->constpal = FALSE; | 187 img->constpal = FALSE; |
181 | 188 |
182 // Allocate new | 189 // Allocate and create new |
183 if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS, 255)) != DMERR_OK) | 190 if (mixed) |
184 return res; | 191 { |
185 | 192 // Mixed 256 color palette |
186 memcpy(img->pal->colors, dmC64DefaultPalettes[0].colors, img->pal->ncolors * sizeof(DMColor)); | 193 if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK) |
187 | 194 return res; |
188 return DMERR_OK; | 195 |
189 } | 196 for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++) |
190 | 197 { |
191 | 198 const DMColor *col1 = &cpal->colors[n1]; |
192 int dmSetMixedColorC64Palette(DMImage *img) | 199 for (int n2 = 0; n2 < D64_NCOLORS; n2++) |
193 { | 200 { |
194 int res; | 201 const DMColor *col2 = &cpal->colors[n2]; |
195 | 202 img->pal->colors[n].r = (col1->r + col2->r) / 2; |
196 // Free previous palette | 203 img->pal->colors[n].g = (col1->g + col2->g) / 2; |
197 if (!img->constpal) | 204 img->pal->colors[n].b = (col1->b + col2->b) / 2; |
198 dmPaletteFree(img->pal); | 205 n++; |
199 | 206 } |
200 img->constpal = FALSE; | 207 } |
201 | 208 } |
202 // Allocate new | 209 else |
203 if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK) | 210 { |
204 return res; | 211 // Standard palette, just copy it |
205 | 212 if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS, 255)) != DMERR_OK) |
206 for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++) | 213 return res; |
207 { | 214 |
208 const DMColor *col1 = &dmC64DefaultPalettes[0].colors[n1]; | 215 memcpy(img->pal->colors, cpal->colors, img->pal->ncolors * sizeof(DMColor)); |
209 for (int n2 = 0; n2 < D64_NCOLORS; n2++) | |
210 { | |
211 const DMColor *col2 = &dmC64DefaultPalettes[0].colors[n2]; | |
212 img->pal->colors[n].r = (col1->r + col2->r) / 2; | |
213 img->pal->colors[n].g = (col1->g + col2->g) / 2; | |
214 img->pal->colors[n].b = (col1->b + col2->b) / 2; | |
215 n++; | |
216 } | |
217 } | 216 } |
218 | 217 |
219 return DMERR_OK; | 218 return DMERR_OK; |
220 } | 219 } |
221 | 220 |
1502 // Allocate image structure | 1501 // Allocate image structure |
1503 if ((*pdst = dst = dmImageAlloc( | 1502 if ((*pdst = dst = dmImageAlloc( |
1504 src->fmt->width, src->fmt->height, DM_PIXFMT_PALETTE, -1)) == NULL) | 1503 src->fmt->width, src->fmt->height, DM_PIXFMT_PALETTE, -1)) == NULL) |
1505 return DMERR_MALLOC; | 1504 return DMERR_MALLOC; |
1506 | 1505 |
1507 // Set partial palette information | 1506 // Set palette information |
1508 if ((res = dmSetDefaultC64Palette(dst)) != DMERR_OK) | 1507 if (spec->pal != NULL) |
1508 dst->pal = spec->pal; | |
1509 else | |
1510 if ((res = dmC64SetImagePalette(dst, spec->cpal, FALSE)) != DMERR_OK) | |
1509 return res; | 1511 return res; |
1510 | 1512 |
1511 // Convert | 1513 // Convert |
1512 if (fmt->format->convertFrom != NULL) | 1514 if (fmt->format->convertFrom != NULL) |
1513 res = fmt->format->convertFrom(dst, src, fmt, spec); | 1515 res = fmt->format->convertFrom(dst, src, fmt, spec); |