changeset 556:44d1e0d4acf3

Improve DMC64Image -> DMImage conversion facilities.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 23 Nov 2012 18:37:09 +0200
parents 31652085440a
children 1993cd341079
files lib64gfx.c lib64gfx.h
diffstat 2 files changed, 53 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/lib64gfx.c	Fri Nov 23 17:33:55 2012 +0200
+++ b/lib64gfx.c	Fri Nov 23 18:37:09 2012 +0200
@@ -700,12 +700,22 @@
 
 // Convert a generic "C64" format bitmap in DMC64Image struct to
 // a indexed/paletted bitmap image.
-int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src)
+int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src, const BOOL doubleMC)
 {
-    int yc;
     Uint8 *dp = dst->data;
+    int divisor = doubleMC ? 4 : 8,
+        wdivisor = doubleMC ? 2 : 1,
+        yc;
 
-    for (yc = 0; yc < C64_SCR_HEIGHT; yc++)
+    // Sanity check arguments
+    if (dst == NULL || src == NULL)
+        return DMERR_NULLPTR;
+
+    if (dst->width < 8)
+        return DMERR_INVALID_ARGS;
+
+    // Perform generic conversion
+    for (yc = 0; yc < dst->height; yc++)
     {
         Uint8 *d = dp;
         const int y = yc / 8, yb = yc & 7;
@@ -715,14 +725,14 @@
 
         if ((src->type & D64_FMT_MC) == D64_FMT_HIRES)
         {
-            for (xc = 0; xc < C64_SCR_WIDTH; xc++)
+            for (xc = 0; xc < dst->width; xc++)
             {
                 const int x = xc / 8;
                 const int scroffs = scroffsy + x;
-                const int b = src->bitmap[0][bmoffsy + (x * 8) + yb];
+                const int bmoffs = bmoffsy + (x * 8) + yb;
                 const int v = 7 - (xc & 7);
 
-                if ((b >> v) & 1)
+                if ((src->bitmap[0][bmoffs] >> v) & 1)
                     *d++ = src->screen[0][scroffs] >> 4;
                 else
                     *d++ = src->screen[0][scroffs] & 15;
@@ -730,9 +740,9 @@
         }
         else
         {
-            for (xc = 0; xc < C64_SCR_WIDTH / 2; xc++)
+            for (xc = 0; xc < dst->width / wdivisor; xc++)
             {
-                const int x = xc / 4;
+                const int x = xc / divisor;
                 const int scroffs = scroffsy + x;
                 const int bmoffs = bmoffsy + (x * 8) + yb;
                 const int v = 6 - ((xc * 2) & 6);
@@ -771,7 +781,6 @@
                 }
             }
         }
-
         dp += dst->pitch;
     }
 
@@ -779,6 +788,38 @@
 }
 
 
+int dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageFormat *fmt, const BOOL doubleMC)
+{
+    int width, res;
+    DMImage *dst;
+
+    if (pdst == NULL || src == NULL)
+        return DMERR_NULLPTR;
+
+    // Calculate output image width
+    if ((src->type & D64_FMT_MC) && !doubleMC)
+        width = C64_SCR_WIDTH / 2;
+    else
+        width = C64_SCR_WIDTH;
+
+    // Allocate image structure
+    if ((*pdst = dst = dmImageAlloc(width, C64_SCR_HEIGHT)) == NULL)
+        return DMERR_MALLOC;
+
+    // Set palette
+    dst->pal      = (DMColor *) &dmC64Palette;
+    dst->ncolors  = C64_NCOLORS;
+    dst->constpal = TRUE;
+
+    // Convert
+    if (fmt->convertFrom != NULL)
+        res = fmt->convertFrom(dst, src, doubleMC);
+    else
+        res = dmC64ConvertGenericBMP2Image(dst, src, doubleMC);
+
+    return res;
+}
+
 
 int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize)
 {
--- a/lib64gfx.h	Fri Nov 23 17:33:55 2012 +0200
+++ b/lib64gfx.h	Fri Nov 23 18:37:09 2012 +0200
@@ -161,7 +161,7 @@
     int  (*probe)(const Uint8 *buf, const size_t len, const struct _DMC64ImageFormat *fmt);
     int  (*decode)(DMC64Image *img, const Uint8 *buf, const size_t len, const struct _DMC64ImageFormat *fmt);
     int  (*encode)(DMC64Image *img, Uint8 **buf, size_t *len, const struct _DMC64ImageFormat *fmt);
-    int  (*convertFrom)(DMImage *, DMC64Image *);
+    int  (*convertFrom)(DMImage *, const DMC64Image *, const BOOL doubleMC);
     int  (*convertTo)(DMImage *, DMC64Image *);
 
     int nencdecOps;
@@ -180,8 +180,9 @@
 
 int       dmC64DecodeGenericBMP(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt);
 int       dmC64EncodeGenericBMP(Uint8 **pbuf, size_t *plen, const DMC64Image *img, const DMC64ImageFormat *fmt);
-int       dmC64ConvertGenericBMP2Image(DMImage *screen, const DMC64Image *img);
+int       dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src, const BOOL doubleMC);
 
+int       dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageFormat *fmt, const BOOL doubleMC);
 int       dmC64ProbeBMP(const Uint8 *buf, const size_t len, const DMC64ImageFormat **fmt);
 int       dmC64DecodeBMP(DMC64Image *img, const Uint8 *buf, const size_t len, const size_t probeOffs, const size_t loadOffs, const DMC64ImageFormat **fmt, const DMC64ImageFormat *forced);