Mercurial > hg > dmlib
comparison tools/64vw.c @ 2333:8ad08ab4975b
Unify 64vw image decoding.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 19 Sep 2019 11:33:11 +0300 |
parents | 48b48251610a |
children | 2afe501cfed2 |
comparison
equal
deleted
inserted
replaced
2332:46b929f822f8 | 2333:8ad08ab4975b |
---|---|
209 dmFree(dataBuf); | 209 dmFree(dataBuf); |
210 return ret; | 210 return ret; |
211 } | 211 } |
212 | 212 |
213 | 213 |
214 int dmConvertC64ImageToSDLSurface(DMC64Image *cimage, SDL_Surface *surf, const DMC64ImageConvSpec *spec) | 214 int dmConvertC64ImageToSDLSurface(DMImage **bimage, SDL_Surface **psurf, DMC64Image *cimage, const DMC64ImageConvSpec *spec) |
215 { | 215 { |
216 DMImage bmap; | 216 BOOL charDataSet = FALSE; |
217 BOOL charDataSet, mixedPalette; | |
218 int res; | 217 int res; |
219 | |
220 memset(&bmap, 0, sizeof(bmap)); | |
221 bmap.size = surf->pitch * surf->h; | |
222 bmap.data = surf->pixels; | |
223 bmap.pitch = surf->pitch; | |
224 bmap.width = surf->w; | |
225 bmap.height = surf->h; | |
226 | |
227 mixedPalette = (cimage->extraInfo[D64_EI_MODE] & D64_FMT_ILACE) && | |
228 cimage->extraInfo[D64_EI_ILACE_TYPE] == D64_ILACE_COLOR; | |
229 | |
230 if ((res = dmC64SetImagePalette(&bmap, spec, mixedPalette)) != DMERR_OK) | |
231 { | |
232 dmErrorMsg("Could not create copy of palette.\n"); | |
233 return res; | |
234 } | |
235 | 218 |
236 if (cimage->charData[0].data == NULL) | 219 if (cimage->charData[0].data == NULL) |
237 { | 220 { |
238 memcpy(&cimage->charData[0], &setCharROM, sizeof(DMC64MemBlock)); | 221 memcpy(&cimage->charData[0], &setCharROM, sizeof(DMC64MemBlock)); |
239 charDataSet = TRUE; | 222 charDataSet = TRUE; |
240 } | 223 } |
241 else | 224 |
242 charDataSet = FALSE; | 225 res = dmC64ConvertBMP2Image(bimage, cimage, spec); |
243 | |
244 if (cimage->fmt->convertFrom != NULL) | |
245 res = cimage->fmt->convertFrom(&bmap, cimage, spec); | |
246 else | |
247 res = dmC64ConvertGenericBMP2Image(&bmap, cimage, spec); | |
248 | 226 |
249 if (charDataSet) | 227 if (charDataSet) |
250 memset(&cimage->charData[0], 0, sizeof(DMC64MemBlock)); | 228 memset(&cimage->charData[0], 0, sizeof(DMC64MemBlock)); |
251 | 229 |
252 SDL_SetPaletteColors(surf->format->palette, (SDL_Color *) bmap.pal->colors, 0, bmap.pal->ncolors); | 230 if (res == DMERR_OK) |
253 | 231 { |
254 dmPaletteFree(bmap.pal); | 232 *psurf = SDL_CreateRGBSurfaceWithFormatFrom( |
233 (*bimage)->data, (*bimage)->width, (*bimage)->height, | |
234 8, (*bimage)->pitch, SDL_PIXELFORMAT_INDEX8); | |
235 | |
236 if (*psurf != NULL) | |
237 { | |
238 SDL_SetPaletteColors((*psurf)->format->palette, | |
239 (SDL_Color *) (*bimage)->pal->colors, 0, | |
240 (*bimage)->pal->ncolors); | |
241 } | |
242 } | |
243 | |
255 return res; | 244 return res; |
256 } | 245 } |
257 | 246 |
258 | 247 |
259 int main(int argc, char *argv[]) | 248 int main(int argc, char *argv[]) |
262 DMC64ImageConvSpec optSpec; | 251 DMC64ImageConvSpec optSpec; |
263 SDL_Window *window = NULL; | 252 SDL_Window *window = NULL; |
264 SDL_Renderer *renderer = NULL; | 253 SDL_Renderer *renderer = NULL; |
265 SDL_Texture *texture = NULL; | 254 SDL_Texture *texture = NULL; |
266 SDL_Surface *surf = NULL; | 255 SDL_Surface *surf = NULL; |
256 DMImage *bimage = NULL; | |
267 BOOL initSDL = FALSE, exitFlag, needRedraw; | 257 BOOL initSDL = FALSE, exitFlag, needRedraw; |
268 size_t currIndex, prevIndex; | 258 size_t currIndex, prevIndex; |
269 int res; | 259 int res; |
270 | 260 |
271 // Initialize pre-requisites | 261 // Initialize pre-requisites |
524 { | 514 { |
525 SDL_FreeSurface(surf); | 515 SDL_FreeSurface(surf); |
526 surf = NULL; | 516 surf = NULL; |
527 } | 517 } |
528 | 518 |
519 if (bimage != NULL) | |
520 { | |
521 dmImageFree(bimage); | |
522 bimage = NULL; | |
523 } | |
524 | |
529 if ((res = dmReadC64Image(filename, forced, &fmt, &cimage)) != DMERR_OK) | 525 if ((res = dmReadC64Image(filename, forced, &fmt, &cimage)) != DMERR_OK) |
530 { | 526 { |
531 if (res != DMERR_NOT_SUPPORTED) | 527 if (res != DMERR_NOT_SUPPORTED) |
532 { | 528 { |
533 dmErrorMsg("Could not decode file '%s': %s\n", | 529 dmErrorMsg("Could not decode file '%s': %s\n", |
541 dmErrorMsg("Probing could not find any matching image format. Perhaps try forcing a format via -f.\n"); | 537 dmErrorMsg("Probing could not find any matching image format. Perhaps try forcing a format via -f.\n"); |
542 goto fail; | 538 goto fail; |
543 } | 539 } |
544 | 540 |
545 // Create surface (we are lazy and ugly) | 541 // Create surface (we are lazy and ugly) |
546 if ((surf = SDL_CreateRGBSurfaceWithFormat(0, | 542 if (dmConvertC64ImageToSDLSurface(&bimage, &surf, cimage, &optSpec) == DMERR_OK) |
547 cimage->fmt->width, cimage->fmt->height, | |
548 8, SDL_PIXELFORMAT_INDEX8)) == NULL) | |
549 { | |
550 dmC64ImageFree(cimage); | |
551 dmErrorMsg("Could not allocate surface.\n"); | |
552 goto exit; | |
553 } | |
554 | |
555 if (dmConvertC64ImageToSDLSurface(cimage, surf, &optSpec) == DMERR_OK) | |
556 { | 543 { |
557 title = dm_strdup_printf("%s - [%d / %d] %s (%dx%d @ %s)", | 544 title = dm_strdup_printf("%s - [%d / %d] %s (%dx%d @ %s)", |
558 dmProgName, | 545 dmProgName, |
559 currIndex + 1, | 546 currIndex + 1, |
560 noptFilenames2, | 547 noptFilenames2, |
577 { | 564 { |
578 dmErrorMsg("Could not allocate surface.\n"); | 565 dmErrorMsg("Could not allocate surface.\n"); |
579 goto exit; | 566 goto exit; |
580 } | 567 } |
581 | 568 |
582 | |
583 if (texture != NULL) | 569 if (texture != NULL) |
584 SDL_DestroyTexture(texture); | 570 SDL_DestroyTexture(texture); |
585 | 571 |
586 if ((texture = SDL_CreateTextureFromSurface(renderer, surf)) == NULL) | 572 if ((texture = SDL_CreateTextureFromSurface(renderer, surf)) == NULL) |
587 { | 573 { |
634 SDL_FreeSurface(surf); | 620 SDL_FreeSurface(surf); |
635 | 621 |
636 if (initSDL) | 622 if (initSDL) |
637 SDL_Quit(); | 623 SDL_Quit(); |
638 | 624 |
625 dmImageFree(bimage); | |
639 dmPaletteFree(optSpec.pal); | 626 dmPaletteFree(optSpec.pal); |
640 dmLib64GFXClose(); | 627 dmLib64GFXClose(); |
641 | 628 |
642 return 0; | 629 return 0; |
643 } | 630 } |