Mercurial > hg > dmlib
diff gfxconv.c @ 482:6fdee3ec2894
Improve remapping.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Nov 2012 05:22:25 +0200 |
parents | c3f0fca5b596 |
children | f119ebe3ad25 |
line wrap: on
line diff
--- a/gfxconv.c Wed Nov 07 04:25:16 2012 +0200 +++ b/gfxconv.c Wed Nov 07 05:22:25 2012 +0200 @@ -766,12 +766,13 @@ int dmRemapImageColors(DMImage *image) { - dmMsg(1, "Remapping %d output image colors.\n", optNRemapTable); DMColor *npal = dmCalloc(image->ncolors, sizeof(DMColor)); int *mapping = dmMalloc(image->ncolors * sizeof(int)); BOOL *mapped = dmMalloc(image->ncolors * sizeof(BOOL)); BOOL *used = dmMalloc(image->ncolors * sizeof(BOOL)); - int n, index, xc, yc, ncolors = image->ncolors; + int n, index, xc, yc, ncolors; + + dmMsg(1, "Remapping %d output image colors of %d colors.\n", optNRemapTable, image->ncolors); if (npal == NULL || mapping == NULL || mapped == NULL || used == NULL) { @@ -782,21 +783,25 @@ for (index = 0; index < image->ncolors; index++) { mapping[index] = -1; - used[index] = mapped[index] = FALSE; + mapped[index] = used[index] = FALSE; } // Find used colors dmMsg(2, "Scanning image for used colors...\n"); - for (yc = 0; yc < image->height; yc++) + for (ncolors = yc = 0; yc < image->height; yc++) { Uint8 *dp = image->data + image->pitch * yc; for (xc = 0; xc < image->width; xc++) { Uint8 col = dp[xc]; - if (col < image->ncolors) + if (col < image->ncolors && !used[col]) + { used[col] = TRUE; + ncolors++; + } } } + dmMsg(2, "Found %d used colors, creating remap-table.\n", ncolors); // Match and mark mapped colors for (index = 0; index < optNRemapTable; index++) @@ -813,13 +818,10 @@ map->color.r, map->color.g, map->color.b, map->color.a, n, map->to); - - if (used[n]) - { - mapping[n] = map->to; - mapped[map->to] = TRUE; - found = TRUE; - } + + mapping[n] = map->to; + mapped[map->to] = TRUE; + found = TRUE; } } @@ -832,16 +834,14 @@ } else { - if (used[map->from]) - { - dmMsg(3, "Map index: %d -> %d\n", - map->from, map->to); + dmMsg(3, "Map index: %d -> %d\n", + map->from, map->to); - mapping[map->from] = map->to; - mapped[map->to] = TRUE; - } + mapping[map->from] = map->to; + mapped[map->to] = TRUE; } } + // Fill in the rest if (optRemapRemove) @@ -873,21 +873,26 @@ } } } + + for (index = 0; index < image->ncolors; index++) + { + fprintf(stderr, "mapping[%d] = %d\n", index, mapping[index]); + } // Calculate final number of palette colors ncolors = 0; for (index = 0; index < image->ncolors; index++) { - if (mapping[index] > ncolors) + if (mapping[index] + 1 > ncolors) ncolors = mapping[index] + 1; } // Copy palette entries - for (index = 0; index < ncolors; index++) + for (index = 0; index < image->ncolors; index++) { if (mapping[index] >= 0) { - memcpy(&npal[index], &(image->pal[mapping[index]]), sizeof(DMColor)); + memcpy(&npal[mapping[index]], &(image->pal[index]), sizeof(DMColor)); } }