comparison tools/gfxconv.c @ 2208:90ec1ec89c56

Revamp the palette handling in lib64gfx somewhat, add helper functions to lib64util for handling external palette file options and add support for specifying one of the "internal" palettes or external (.act) palette file to gfxconv and 64vw.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Jun 2019 05:01:12 +0300
parents cbac4912992c
children f8bba7a82ec2
comparison
equal deleted inserted replaced
2207:1ea48084055e 2208:90ec1ec89c56
134 int optNRemapTable = 0, 134 int optNRemapTable = 0,
135 optScaleMode = SCALE_AUTO; 135 optScaleMode = SCALE_AUTO;
136 DMMapValue optRemapTable[DM_MAX_COLORS]; 136 DMMapValue optRemapTable[DM_MAX_COLORS];
137 int optColorMap[D64_NCOLORS]; 137 int optColorMap[D64_NCOLORS];
138 char *optCharROMFilename = NULL; 138 char *optCharROMFilename = NULL;
139 DMC64Palette *optC64Palette = NULL;
140 char *optC64PaletteFile = NULL;
139 141
140 142
141 DMImageWriteSpec optSpec = 143 DMImageWriteSpec optSpec =
142 { 144 {
143 .scaleX = 1, 145 .scaleX = 1,
146 .bpp = 8, 148 .bpp = 8,
147 .planar = FALSE, 149 .planar = FALSE,
148 .pixfmt = 0, 150 .pixfmt = 0,
149 .compression = FCMP_BEST, 151 .compression = FCMP_BEST,
150 }; 152 };
153
154 DMC64ImageConvSpec optC64Spec;
155
151 156
152 static const DMOptArg optList[] = 157 static const DMOptArg optList[] =
153 { 158 {
154 { 0, '?', "help", "Show this help", OPT_NONE }, 159 { 0, '?', "help", "Show this help", OPT_NONE },
155 { 15, 'v', "verbose", "Increase verbosity", OPT_NONE }, 160 { 15, 'v', "verbose", "Increase verbosity", OPT_NONE },
168 { 18, 'B', "bpp", "Bits per plane (some output formats)", OPT_ARGREQ }, 173 { 18, 'B', "bpp", "Bits per plane (some output formats)", OPT_ARGREQ },
169 { 14, 'I', "interleave", "Interleaved/planar output (some output formats)", OPT_NONE }, 174 { 14, 'I', "interleave", "Interleaved/planar output (some output formats)", OPT_NONE },
170 { 20, 'C', "compress", "Use compression -C <0-9>, 0 = disable, default is 9", OPT_ARGREQ }, 175 { 20, 'C', "compress", "Use compression -C <0-9>, 0 = disable, default is 9", OPT_ARGREQ },
171 { 16, 'R', "remap", "Remap output image colors (-R <(#RRGGBB|index):index>[,<..>][+remove] | -R @map.txt[+remove])", OPT_ARGREQ }, 176 { 16, 'R', "remap", "Remap output image colors (-R <(#RRGGBB|index):index>[,<..>][+remove] | -R @map.txt[+remove])", OPT_ARGREQ },
172 { 21, 0, "char-rom", "Set character ROM file to be used.", OPT_ARGREQ }, 177 { 21, 0, "char-rom", "Set character ROM file to be used.", OPT_ARGREQ },
178 { 22, 'p', "palette" , "Set C64 palette to be used (see list with -p help).", OPT_ARGREQ },
173 }; 179 };
174 180
175 static const int optListN = sizeof(optList) / sizeof(optList[0]); 181 static const int optListN = sizeof(optList) / sizeof(optList[0]);
176 182
177 183
956 962
957 case 21: 963 case 21:
958 optCharROMFilename = optArg; 964 optCharROMFilename = optArg;
959 break; 965 break;
960 966
967 case 22:
968 return argHandleC64PaletteOption(optArg, &optC64Palette, &optC64PaletteFile);
969
961 default: 970 default:
962 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg); 971 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg);
963 return FALSE; 972 return FALSE;
964 } 973 }
965 974
1829 outIHeight++; 1838 outIHeight++;
1830 1839
1831 outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight, DM_PIXFMT_PALETTE, -1); 1840 outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight, DM_PIXFMT_PALETTE, -1);
1832 } 1841 }
1833 1842
1834 if ((err = dmC64SetImagePalette(outImage, NULL, FALSE)) != DMERR_OK) 1843 if ((err = dmC64SetImagePalette(outImage, &optC64Spec, FALSE)) != DMERR_OK)
1835 { 1844 {
1836 dmErrorMsg("Could not allocate C64 palette for output image: %d\n", err); 1845 dmErrorMsg("Could not allocate C64 palette for output image: %d\n", err);
1837 goto error; 1846 goto error;
1838 } 1847 }
1839 1848
1912 1921
1913 1922
1914 int main(int argc, char *argv[]) 1923 int main(int argc, char *argv[])
1915 { 1924 {
1916 FILE *inFile = NULL; 1925 FILE *inFile = NULL;
1917 DMC64ImageConvSpec imageSpecC64;
1918 const DMC64ImageFormat *inC64Fmt = NULL; 1926 const DMC64ImageFormat *inC64Fmt = NULL;
1919 DMConvFormat inFormat, outFormat; 1927 DMConvFormat inFormat, outFormat;
1920 DMC64Image *inC64Image = NULL, *outC64Image = NULL; 1928 DMC64Image *inC64Image = NULL, *outC64Image = NULL;
1921 DMImage *inImage = NULL, *outImage = NULL; 1929 DMImage *inImage = NULL, *outImage = NULL;
1922 Uint8 *dataBuf = NULL, *dataBufOrig = NULL; 1930 Uint8 *dataBuf = NULL, *dataBufOrig = NULL;
1926 // Default color mapping 1934 // Default color mapping
1927 for (i = 0; i < D64_NCOLORS; i++) 1935 for (i = 0; i < D64_NCOLORS; i++)
1928 optColorMap[i] = i; 1936 optColorMap[i] = i;
1929 1937
1930 // Initialize c64 image conversion spec 1938 // Initialize c64 image conversion spec
1931 memset(&imageSpecC64, 0, sizeof(imageSpecC64)); 1939 memset(&optC64Spec, 0, sizeof(optC64Spec));
1932 1940
1933 // Initialize list of additional conversion formats 1941 // Initialize list of additional conversion formats
1934 if ((res = dmLib64GFXInit()) != DMERR_OK) 1942 if ((res = dmLib64GFXInit()) != DMERR_OK)
1935 { 1943 {
1936 dmErrorMsg("Could not initialize lib64gfx: %s\n", 1944 dmErrorMsg("Could not initialize lib64gfx: %s\n",
2139 2147
2140 case SCALE_RELATIVE: 2148 case SCALE_RELATIVE:
2141 optSpec.scaleX *= scaleX; 2149 optSpec.scaleX *= scaleX;
2142 optSpec.scaleY *= scaleY; 2150 optSpec.scaleY *= scaleY;
2143 break; 2151 break;
2152 }
2153 }
2154
2155 if (optC64PaletteFile != NULL)
2156 {
2157 if ((res = dmHandleExternalPalette(optC64PaletteFile, &optC64Spec.pal)) != DMERR_OK)
2158 goto exit;
2159 }
2160 else
2161 {
2162 // No palette file specified, use internal palette
2163 if (optC64Palette == NULL)
2164 optC64Palette = &dmC64DefaultPalettes[0];
2165
2166 dmMsg(1, "Using internal palette '%s' (%s).\n",
2167 optC64Palette->name, optC64Palette->desc);
2168
2169 optC64Spec.cpal = optC64Palette;
2170
2171 if ((res = dmC64PaletteFromC64Palette(&optC64Spec.pal, optC64Palette, FALSE)) != DMERR_OK)
2172 {
2173 dmErrorMsg("Could not setup palette: %s\n",
2174 dmErrorStr(res));
2175 goto exit;
2144 } 2176 }
2145 } 2177 }
2146 2178
2147 switch (optInType) 2179 switch (optInType)
2148 { 2180 {
2184 goto exit; 2216 goto exit;
2185 } 2217 }
2186 } 2218 }
2187 2219
2188 // Convert the image 2220 // Convert the image
2189 res = dmC64ConvertBMP2Image(&outImage, inC64Image, inC64Fmt, &imageSpecC64); 2221 res = dmC64ConvertBMP2Image(&outImage, inC64Image, inC64Fmt, &optC64Spec);
2190 2222
2191 if (res != DMERR_OK || outImage == NULL) 2223 if (res != DMERR_OK || outImage == NULL)
2192 { 2224 {
2193 dmErrorMsg("Error in bitmap to image conversion.\n"); 2225 dmErrorMsg("Error in bitmap to image conversion.\n");
2194 goto exit; 2226 goto exit;
2273 2305
2274 case FFMT_BITMAP: 2306 case FFMT_BITMAP:
2275 { 2307 {
2276 DMC64Image *tmpC64Image = NULL; 2308 DMC64Image *tmpC64Image = NULL;
2277 res = dmC64ConvertImage2BMP(&tmpC64Image, inImage, 2309 res = dmC64ConvertImage2BMP(&tmpC64Image, inImage,
2278 &dmC64ImageFormats[optOutFormat], &imageSpecC64); 2310 &dmC64ImageFormats[optOutFormat], &optC64Spec);
2279 2311
2280 if (res != DMERR_OK || tmpC64Image == NULL) 2312 if (res != DMERR_OK || tmpC64Image == NULL)
2281 { 2313 {
2282 dmC64ImageFree(tmpC64Image); 2314 dmC64ImageFree(tmpC64Image);
2283 dmErrorMsg("Error in image to bitmap conversion: %s\n", 2315 dmErrorMsg("Error in image to bitmap conversion: %s\n",