comparison gfxconv.c @ 478:7c7a57590236

Calculate number of remapped colors.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Nov 2012 03:03:01 +0200
parents 0fd0e301b6fc
children b768bfb0b364
comparison
equal deleted inserted replaced
477:0fd0e301b6fc 478:7c7a57590236
745 { 745 {
746 dmMsg(1, "Remapping %d output image colors.\n", optNRemapTable); 746 dmMsg(1, "Remapping %d output image colors.\n", optNRemapTable);
747 DMColor *npal = dmCalloc(image->ncolors, sizeof(DMColor)); 747 DMColor *npal = dmCalloc(image->ncolors, sizeof(DMColor));
748 int *dpal = dmMalloc(image->ncolors * sizeof(int)); 748 int *dpal = dmMalloc(image->ncolors * sizeof(int));
749 BOOL *spal = dmCalloc(image->ncolors, sizeof(BOOL)); 749 BOOL *spal = dmCalloc(image->ncolors, sizeof(BOOL));
750 int index, xc, yc; 750 int index, xc, yc, nncolors = image->ncolors;
751 751
752 if (npal == NULL || spal == NULL || dpal == NULL) 752 if (npal == NULL || spal == NULL || dpal == NULL)
753 { 753 {
754 dmError("Could not allocate memory for remapped palette.\n"); 754 dmError("Could not allocate memory for remapped palette.\n");
755 return DMERR_MALLOC; 755 return DMERR_MALLOC;
776 map->to); 776 map->to);
777 777
778 dpal[map->to] = n; 778 dpal[map->to] = n;
779 spal[n] = TRUE; 779 spal[n] = TRUE;
780 found = TRUE; 780 found = TRUE;
781 break; 781 // break;
782 } 782 }
783 } 783 }
784 784
785 if (!found) 785 if (!found)
786 { 786 {
820 820
821 // Copy palette entries 821 // Copy palette entries
822 dmMsg(3, "Creating new palette.\n"); 822 dmMsg(3, "Creating new palette.\n");
823 for (index = 0; index < image->ncolors; index++) 823 for (index = 0; index < image->ncolors; index++)
824 { 824 {
825 dmMsg(4, "%d -> %d\n", dpal[index], index);
826 if (dpal[index] >= 0 && dpal[index] < image->ncolors) 825 if (dpal[index] >= 0 && dpal[index] < image->ncolors)
827 { 826 {
828 memcpy(&npal[index], &(image->pal[dpal[index]]), sizeof(DMColor)); 827 memcpy(&npal[index], &(image->pal[dpal[index]]), sizeof(DMColor));
829 } 828 nncolors = index;
830 } 829 }
830 }
831
831 832
832 // Remap image 833 // Remap image
833 dmMsg(1, "Remapping image ...\n"); 834 dmMsg(1, "Remapping image ...\n");
834 for (yc = 0; yc < image->height; yc++) 835 for (yc = 0; yc < image->height; yc++)
835 { 836 {
839 Uint8 col = dp[xc]; 840 Uint8 col = dp[xc];
840 if (col < image->ncolors && dpal[col] >= 0 && dpal[col] < image->ncolors) 841 if (col < image->ncolors && dpal[col] >= 0 && dpal[col] < image->ncolors)
841 { 842 {
842 dp[xc] = dpal[col]; 843 dp[xc] = dpal[col];
843 } 844 }
845 else
846 dp[xc] = 0;
844 } 847 }
845 } 848 }
846 849
847 // Set new palette, free memory 850 // Set new palette, free memory
848 dmFree(image->pal); 851 dmFree(image->pal);
849 image->pal = npal; 852 image->pal = npal;
853 image->ncolors = nncolors;
850 dmFree(spal); 854 dmFree(spal);
851 dmFree(dpal); 855 dmFree(dpal);
852 return DMERR_OK; 856 return DMERR_OK;
853 } 857 }
854 858