comparison tools/64vw.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 b1e392da8346
comparison
equal deleted inserted replaced
2207:1ea48084055e 2208:90ec1ec89c56
6 * Please read file 'COPYING' for information on license and distribution. 6 * Please read file 'COPYING' for information on license and distribution.
7 */ 7 */
8 #include "dmlib.h" 8 #include "dmlib.h"
9 #include "dmargs.h" 9 #include "dmargs.h"
10 #include "dmfile.h" 10 #include "dmfile.h"
11 #include "libgfx.h"
11 #include "lib64gfx.h" 12 #include "lib64gfx.h"
12 #include "lib64util.h" 13 #include "lib64util.h"
13 #include <SDL.h> 14 #include <SDL.h>
14 15
15 16
23 optProbeOnly = FALSE, 24 optProbeOnly = FALSE,
24 optListOnly = FALSE; 25 optListOnly = FALSE;
25 size_t noptFilenames1 = 0, noptFilenames2 = 0; 26 size_t noptFilenames1 = 0, noptFilenames2 = 0;
26 char **optFilenames = NULL; 27 char **optFilenames = NULL;
27 char *optCharROMFilename = NULL; 28 char *optCharROMFilename = NULL;
29 DMC64Palette *optC64Palette = NULL;
30 char *optC64PaletteFile = NULL;
28 31
29 DMC64MemBlock setCharROM; 32 DMC64MemBlock setCharROM;
30 33
31 34
32 static const DMOptArg optList[] = 35 static const DMOptArg optList[] =
39 { 5, 'F', "formats", "List supported input formats", OPT_NONE }, 42 { 5, 'F', "formats", "List supported input formats", OPT_NONE },
40 { 6, 'i', "info", "Print information only (no display)", OPT_NONE }, 43 { 6, 'i', "info", "Print information only (no display)", OPT_NONE },
41 { 7, 'l', "list", "Output list of files that were recognized (implies -i)", OPT_NONE }, 44 { 7, 'l', "list", "Output list of files that were recognized (implies -i)", OPT_NONE },
42 { 8, 'P', "probe", "Probe only (do not attempt to decode the image)", OPT_NONE }, 45 { 8, 'P', "probe", "Probe only (do not attempt to decode the image)", OPT_NONE },
43 { 9, 0, "char-rom", "Set character ROM file to be used.", OPT_ARGREQ }, 46 { 9, 0, "char-rom", "Set character ROM file to be used.", OPT_ARGREQ },
47 {10, 'p', "palette" , "Set C64 palette to be used (see -p list).", OPT_ARGREQ },
44 }; 48 };
45 49
46 const int optListN = sizeof(optList) / sizeof(optList[0]); 50 const int optListN = sizeof(optList) / sizeof(optList[0]);
47 51
48 52
162 166
163 case 9: 167 case 9:
164 optCharROMFilename = optArg; 168 optCharROMFilename = optArg;
165 break; 169 break;
166 170
171 case 10:
172 return argHandleC64PaletteOption(optArg, &optC64Palette, &optC64PaletteFile);
173
167 default: 174 default:
168 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg); 175 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg);
169 return FALSE; 176 return FALSE;
170 } 177 }
171 178
221 int dmDecodeC64Image(DMC64Image *cimage, const DMC64ImageFormat *fmt, 228 int dmDecodeC64Image(DMC64Image *cimage, const DMC64ImageFormat *fmt,
222 SDL_Surface *surf, const DMC64ImageConvSpec *spec) 229 SDL_Surface *surf, const DMC64ImageConvSpec *spec)
223 { 230 {
224 DMImage bmap; 231 DMImage bmap;
225 BOOL charDataSet; 232 BOOL charDataSet;
226 int ret; 233 int res;
227 234
228 memset(&bmap, 0, sizeof(bmap)); 235 memset(&bmap, 0, sizeof(bmap));
229 bmap.size = surf->pitch * surf->h; 236 bmap.size = surf->pitch * surf->h;
230 bmap.data = surf->pixels; 237 bmap.data = surf->pixels;
231 bmap.pitch = surf->pitch; 238 bmap.pitch = surf->pitch;
232 bmap.width = surf->w; 239 bmap.width = surf->w;
233 bmap.height = surf->h; 240 bmap.height = surf->h;
234 241
235 if ((ret = dmC64SetImagePalette(&bmap, NULL, FALSE)) != DMERR_OK) 242 if ((res = dmPaletteCopy(&bmap.pal, spec->pal)) != DMERR_OK)
236 return ret; 243 {
244 dmErrorMsg("Could not create copy of palette.\n");
245 return res;
246 }
237 247
238 if (cimage->charData[0].data == NULL) 248 if (cimage->charData[0].data == NULL)
239 { 249 {
240 memcpy(&cimage->charData[0], &setCharROM, sizeof(DMC64MemBlock)); 250 memcpy(&cimage->charData[0], &setCharROM, sizeof(DMC64MemBlock));
241 charDataSet = TRUE; 251 charDataSet = TRUE;
242 } 252 }
243 else 253 else
244 charDataSet = FALSE; 254 charDataSet = FALSE;
245 255
246 if (fmt->format->convertFrom != NULL) 256 if (fmt->format->convertFrom != NULL)
247 ret = fmt->format->convertFrom(&bmap, cimage, fmt, spec); 257 res = fmt->format->convertFrom(&bmap, cimage, fmt, spec);
248 else 258 else
249 ret = dmC64ConvertGenericBMP2Image(&bmap, cimage, fmt, spec); 259 res = dmC64ConvertGenericBMP2Image(&bmap, cimage, fmt, spec);
250 260
251 if (charDataSet) 261 if (charDataSet)
252 memset(&cimage->charData[0], 0, sizeof(DMC64MemBlock)); 262 memset(&cimage->charData[0], 0, sizeof(DMC64MemBlock));
253 263
254 SDL_SetPaletteColors(surf->format->palette, (SDL_Color *) bmap.pal->colors, 0, bmap.pal->ncolors); 264 SDL_SetPaletteColors(surf->format->palette, (SDL_Color *) bmap.pal->colors, 0, bmap.pal->ncolors);
255 265
256 dmPaletteFree(bmap.pal); 266 dmPaletteFree(bmap.pal);
257 267 return res;
258 return ret; 268 }
259 } 269
270 #if 0
271 DIR *dirh;
272 struct dirent *entry;
273
274 if ((dirh = opendir(npath)) == NULL)
275 {
276 int err = th_get_error();
277 THERR("Could not open directory '%s': %s\n",
278 path, th_error_str(err));
279 ret = FALSE;
280 goto out;
281 }
282
283 while ((entry = readdir(dirh)) != NULL)
284 if (entry->d_name[0] != '.')
285 {
286 if (!argHandleFileDir(npath, entry->d_name, pattern))
287 {
288 ret = FALSE;
289 goto out;
290 }
291 }
292
293 closedir(dirh);
294 #endif
260 295
261 296
262 int main(int argc, char *argv[]) 297 int main(int argc, char *argv[])
263 { 298 {
264 const DMC64ImageFormat *forced; 299 const DMC64ImageFormat *forced;
265 DMC64ImageConvSpec spec; 300 DMC64ImageConvSpec optSpec;
266 SDL_Window *window = NULL; 301 SDL_Window *window = NULL;
267 SDL_Renderer *renderer = NULL; 302 SDL_Renderer *renderer = NULL;
268 SDL_Texture *texture = NULL; 303 SDL_Texture *texture = NULL;
269 SDL_Surface *surf = NULL; 304 SDL_Surface *surf = NULL;
270 BOOL initSDL = FALSE, exitFlag, needRedraw; 305 BOOL initSDL = FALSE, exitFlag, needRedraw;
278 dmErrorStr(res)); 313 dmErrorStr(res));
279 goto exit; 314 goto exit;
280 } 315 }
281 316
282 dmSetScaleFactor(2.0); 317 dmSetScaleFactor(2.0);
283 memset(&spec, 0, sizeof(spec)); 318 memset(&optSpec, 0, sizeof(optSpec));
284 memset(&setCharROM, 0, sizeof(setCharROM)); 319 memset(&setCharROM, 0, sizeof(setCharROM));
285 320
286 dmInitProg("64vw", "Displayer for various C64 graphics formats", "0.4", NULL, NULL); 321 dmInitProg("64vw", "Displayer for various C64 graphics formats", "0.4", NULL, NULL);
287 322
288 // Parse arguments, round #1 323 // Parse arguments, round #1
347 } 382 }
348 383
349 dmC64ImageFree(cimage); 384 dmC64ImageFree(cimage);
350 } 385 }
351 goto exit; 386 goto exit;
387 }
388
389 if (optC64PaletteFile != NULL)
390 {
391 if ((res = dmHandleExternalPalette(optC64PaletteFile, &optSpec.pal)) != DMERR_OK)
392 goto exit;
393 }
394 else
395 {
396 // No palette file specified, use internal palette
397 if (optC64Palette == NULL)
398 optC64Palette = &dmC64DefaultPalettes[0];
399
400 dmMsg(1, "Using internal palette '%s' (%s).\n",
401 optC64Palette->name, optC64Palette->desc);
402
403 optSpec.cpal = optC64Palette;
404
405 if ((res = dmC64PaletteFromC64Palette(&optSpec.pal, optC64Palette, FALSE)) != DMERR_OK)
406 {
407 dmErrorMsg("Could not setup palette: %s\n",
408 dmErrorStr(res));
409 goto exit;
410 }
352 } 411 }
353 412
354 // Check character ROM filename 413 // Check character ROM filename
355 if (optCharROMFilename == NULL) 414 if (optCharROMFilename == NULL)
356 optCharROMFilename = DM_DEF_CHARGEN; 415 optCharROMFilename = DM_DEF_CHARGEN;
522 dmC64ImageFree(cimage); 581 dmC64ImageFree(cimage);
523 dmErrorMsg("Could not allocate surface.\n"); 582 dmErrorMsg("Could not allocate surface.\n");
524 goto exit; 583 goto exit;
525 } 584 }
526 585
527 if (dmDecodeC64Image(cimage, fmt, surf, &spec) == DMERR_OK) 586 if (dmDecodeC64Image(cimage, fmt, surf, &optSpec) == DMERR_OK)
528 { 587 {
529 title = dm_strdup_printf("%s - [%d / %d] %s (%dx%d @ %s)", 588 title = dm_strdup_printf("%s - [%d / %d] %s (%dx%d @ %s)",
530 dmProgName, 589 dmProgName,
531 currIndex + 1, 590 currIndex + 1,
532 noptFilenames2, 591 noptFilenames2,
606 SDL_FreeSurface(surf); 665 SDL_FreeSurface(surf);
607 666
608 if (initSDL) 667 if (initSDL)
609 SDL_Quit(); 668 SDL_Quit();
610 669
670 dmPaletteFree(optSpec.pal);
611 dmLib64GFXClose(); 671 dmLib64GFXClose();
612 672
613 return 0; 673 return 0;
614 } 674 }