changeset 2333:8ad08ab4975b

Unify 64vw image decoding.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 19 Sep 2019 11:33:11 +0300
parents 46b929f822f8
children 0e355702435d
files tools/64vw.c
diffstat 1 files changed, 25 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/tools/64vw.c	Mon Sep 16 11:42:32 2019 +0300
+++ b/tools/64vw.c	Thu Sep 19 11:33:11 2019 +0300
@@ -211,47 +211,36 @@
 }
 
 
-int dmConvertC64ImageToSDLSurface(DMC64Image *cimage, SDL_Surface *surf, const DMC64ImageConvSpec *spec)
+int dmConvertC64ImageToSDLSurface(DMImage **bimage, SDL_Surface **psurf, DMC64Image *cimage, const DMC64ImageConvSpec *spec)
 {
-    DMImage bmap;
-    BOOL charDataSet, mixedPalette;
+    BOOL charDataSet = FALSE;
     int res;
 
-    memset(&bmap, 0, sizeof(bmap));
-    bmap.size     = surf->pitch * surf->h;
-    bmap.data     = surf->pixels;
-    bmap.pitch    = surf->pitch;
-    bmap.width    = surf->w;
-    bmap.height   = surf->h;
-
-    mixedPalette  = (cimage->extraInfo[D64_EI_MODE] & D64_FMT_ILACE) &&
-        cimage->extraInfo[D64_EI_ILACE_TYPE] == D64_ILACE_COLOR;
-
-    if ((res = dmC64SetImagePalette(&bmap, spec, mixedPalette)) != DMERR_OK)
-    {
-        dmErrorMsg("Could not create copy of palette.\n");
-        return res;
-    }
-
     if (cimage->charData[0].data == NULL)
     {
         memcpy(&cimage->charData[0], &setCharROM, sizeof(DMC64MemBlock));
         charDataSet = TRUE;
     }
-    else
-        charDataSet = FALSE;
 
-    if (cimage->fmt->convertFrom != NULL)
-        res = cimage->fmt->convertFrom(&bmap, cimage, spec);
-    else
-        res = dmC64ConvertGenericBMP2Image(&bmap, cimage, spec);
+    res = dmC64ConvertBMP2Image(bimage, cimage, spec);
 
     if (charDataSet)
         memset(&cimage->charData[0], 0, sizeof(DMC64MemBlock));
 
-    SDL_SetPaletteColors(surf->format->palette, (SDL_Color *) bmap.pal->colors, 0, bmap.pal->ncolors);
+    if (res == DMERR_OK)
+    {
+        *psurf = SDL_CreateRGBSurfaceWithFormatFrom(
+            (*bimage)->data, (*bimage)->width, (*bimage)->height,
+            8, (*bimage)->pitch, SDL_PIXELFORMAT_INDEX8);
 
-    dmPaletteFree(bmap.pal);
+        if (*psurf != NULL)
+        {
+            SDL_SetPaletteColors((*psurf)->format->palette,
+                (SDL_Color *) (*bimage)->pal->colors, 0,
+                (*bimage)->pal->ncolors);
+        }
+    }
+
     return res;
 }
 
@@ -264,6 +253,7 @@
     SDL_Renderer *renderer = NULL;
     SDL_Texture *texture = NULL;
     SDL_Surface *surf = NULL;
+    DMImage *bimage = NULL;
     BOOL initSDL = FALSE, exitFlag, needRedraw;
     size_t currIndex, prevIndex;
     int res;
@@ -526,6 +516,12 @@
                 surf = NULL;
             }
 
+            if (bimage != NULL)
+            {
+                dmImageFree(bimage);
+                bimage = NULL;
+            }
+
             if ((res = dmReadC64Image(filename, forced, &fmt, &cimage)) != DMERR_OK)
             {
                 if (res != DMERR_NOT_SUPPORTED)
@@ -543,16 +539,7 @@
             }
 
             // Create surface (we are lazy and ugly)
-            if ((surf = SDL_CreateRGBSurfaceWithFormat(0,
-                cimage->fmt->width, cimage->fmt->height,
-                8, SDL_PIXELFORMAT_INDEX8)) == NULL)
-            {
-                dmC64ImageFree(cimage);
-                dmErrorMsg("Could not allocate surface.\n");
-                goto exit;
-            }
-
-            if (dmConvertC64ImageToSDLSurface(cimage, surf, &optSpec) == DMERR_OK)
+            if (dmConvertC64ImageToSDLSurface(&bimage, &surf, cimage, &optSpec) == DMERR_OK)
             {
                 title = dm_strdup_printf("%s - [%d / %d] %s (%dx%d @ %s)",
                     dmProgName,
@@ -579,7 +566,6 @@
                 goto exit;
             }
 
-
             if (texture != NULL)
                 SDL_DestroyTexture(texture);
 
@@ -636,6 +622,7 @@
     if (initSDL)
         SDL_Quit();
 
+    dmImageFree(bimage);
     dmPaletteFree(optSpec.pal);
     dmLib64GFXClose();