comparison gfxconv.c @ 474:95d1facfdb77

Improve color matching, make it possible to ignore alpha values.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Nov 2012 01:01:15 +0200
parents 73bfe73553eb
children c0dedfef3edf
comparison
equal deleted inserted replaced
473:73bfe73553eb 474:95d1facfdb77
97 static const int nconvFormatList = sizeof(convFormatList) / sizeof(convFormatList[0]); 97 static const int nconvFormatList = sizeof(convFormatList) / sizeof(convFormatList[0]);
98 98
99 99
100 typedef struct 100 typedef struct
101 { 101 {
102 BOOL triplet; 102 BOOL triplet, alpha;
103 DMColor color; 103 DMColor color;
104 int from, to; 104 int from, to;
105 } DMMapValue; 105 } DMMapValue;
106 106
107 107
256 256
257 if (*opt == '#' || *opt == '%') 257 if (*opt == '#' || *opt == '%')
258 { 258 {
259 int colR, colG, colB, colA; 259 int colR, colG, colB, colA;
260 260
261 if (sscanf(opt + 1, "%2x%2x%2x%2x", &colR, &colG, &colB, &colA) != 4 && 261 if (sscanf(opt + 1, "%2x%2x%2x%2x", &colR, &colG, &colB, &colA) == 4 ||
262 sscanf(opt + 1, "%2X%2X%2X%2X", &colR, &colG, &colB, &colA) != 4) 262 sscanf(opt + 1, "%2X%2X%2X%2X", &colR, &colG, &colB, &colA) == 4)
263 { 263 {
264 colA = 0; 264 value->alpha = TRUE;
265 if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 && 265 value->color.a = colA;
266 sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3) 266 }
267 { 267 else
268 dmError("Invalid %s value '%s', expected a hex triplet after #.\n", msg, opt); 268 if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 &&
269 return FALSE; 269 sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3)
270 } 270 {
271 } 271 dmError("Invalid %s value '%s', expected a hex triplet after #.\n", msg, opt);
272 return FALSE;
273 }
274
272 value->color.r = colR; 275 value->color.r = colR;
273 value->color.g = colG; 276 value->color.g = colG;
274 value->color.b = colB; 277 value->color.b = colB;
275 value->color.a = colA;
276 value->triplet = TRUE; 278 value->triplet = TRUE;
277 } 279 }
278 else 280 else
279 { 281 {
280 if (!dmGetIntVal(opt, &value->from)) 282 if (!dmGetIntVal(opt, &value->from))
726 728
727 return 0; 729 return 0;
728 } 730 }
729 #endif 731 #endif
730 732
733 BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha)
734 {
735 if (c1->r == c2->r &&
736 c1->g == c2->g &&
737 c1->b == c2->b)
738 return alpha ? (c1->a == c2->a) : TRUE;
739 else
740 return FALSE;
741 }
742
731 743
732 int dmRemapImageColors(DMImage *image) 744 int dmRemapImageColors(DMImage *image)
733 { 745 {
734 dmMsg(1, "Remapping %d output image colors.\n", optNRemapTable); 746 dmMsg(1, "Remapping %d output image colors.\n", optNRemapTable);
735 DMColor *npal = dmCalloc(image->ncolors, sizeof(DMColor)); 747 DMColor *npal = dmCalloc(image->ncolors, sizeof(DMColor));
754 { 766 {
755 BOOL found = FALSE; 767 BOOL found = FALSE;
756 int n; 768 int n;
757 for (n = 0; n < image->ncolors; n++) 769 for (n = 0; n < image->ncolors; n++)
758 { 770 {
759 if (memcmp(&(image->pal[n]), &(map->color), sizeof(DMColor)) == 0) 771 if (dmCompareColor(&(image->pal[n]), &(map->color), map->alpha))
760 { 772 {
761 dmMsg(3, "RGBA match #%02x%02x%02x%02x: %d -> %d\n", 773 dmMsg(3, "RGBA match #%02x%02x%02x%02x: %d -> %d\n",
762 map->color.r, map->color.g, map->color.b, map->color.a, 774 map->color.r, map->color.g, map->color.b, map->color.a,
763 n, 775 n,
764 map->to); 776 map->to);