comparison tools/gfxconv.c @ 2143:693c3e73eb34

Fix palette remapping.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 02 Jun 2019 21:08:06 +0300
parents 894781b7310a
children 0bb1d0ad178f
comparison
equal deleted inserted replaced
2142:894781b7310a 2143:693c3e73eb34
1053 } 1053 }
1054 1054
1055 1055
1056 int dmRemapImageColors(DMImage **pdst, const DMImage *src) 1056 int dmRemapImageColors(DMImage **pdst, const DMImage *src)
1057 { 1057 {
1058 DMPalette *npal = NULL; 1058 DMPalette *tmpPal = NULL;
1059 int *mapping = dmMalloc(src->pal->ncolors * sizeof(int)); 1059 int *mapping = dmMalloc(src->pal->ncolors * sizeof(int));
1060 BOOL *mapped = dmMalloc(src->pal->ncolors * sizeof(BOOL)); 1060 BOOL *mapped = dmMalloc(src->pal->ncolors * sizeof(BOOL));
1061 BOOL *used = dmMalloc(src->pal->ncolors * sizeof(BOOL)); 1061 BOOL *used = dmMalloc(src->pal->ncolors * sizeof(BOOL));
1062 int n, index, xc, yc, ncolors, res = DMERR_OK; 1062 int n, index, xc, yc, ncolors, res = DMERR_OK;
1063 DMImage *dst; 1063 DMImage *dst;
1064 1064
1065 if ((res = dmPaletteAlloc(&npal, src->pal->ncolors, -1)) != DMERR_OK) 1065 if ((res = dmPaletteAlloc(&tmpPal, src->pal->ncolors, -1)) != DMERR_OK)
1066 { 1066 {
1067 dmErrorMsg("Could not allocate memory for remap palette.\n"); 1067 dmErrorMsg("Could not allocate memory for remap palette.\n");
1068 goto error; 1068 goto error;
1069 } 1069 }
1070 1070
1176 // Copy palette entries 1176 // Copy palette entries
1177 for (index = 0; index < src->pal->ncolors; index++) 1177 for (index = 0; index < src->pal->ncolors; index++)
1178 { 1178 {
1179 if (mapping[index] >= 0) 1179 if (mapping[index] >= 0)
1180 { 1180 {
1181 memcpy(&npal[mapping[index]], &(src->pal[index]), sizeof(DMColor)); 1181 memcpy(&tmpPal->colors[mapping[index]], &(src->pal->colors[index]), sizeof(DMColor));
1182 } 1182 }
1183 } 1183 }
1184 1184
1185 // Remap image 1185 // Remap image
1186 dmMsg(1, "Remapping image to %d colors...\n", ncolors); 1186 dmMsg(1, "Remapping image to %d colors...\n", ncolors);
1199 dp[xc] = 0; 1199 dp[xc] = 0;
1200 } 1200 }
1201 } 1201 }
1202 1202
1203 // Set new palette, free memory 1203 // Set new palette, free memory
1204 if ((res = dmPaletteCopy(&dst->pal, npal)) != DMERR_OK) 1204 if ((res = dmPaletteCopy(&dst->pal, tmpPal)) != DMERR_OK)
1205 { 1205 {
1206 res = dmError(DMERR_MALLOC, 1206 res = dmError(DMERR_MALLOC,
1207 "Could not allocate memory for final remapped palette.\n"); 1207 "Could not allocate memory for final remapped palette.\n");
1208 goto error; 1208 goto error;
1209 } 1209 }