changeset 530:5b37a2e427b7

Greatly simplify and also improve the multicolor/hires/lace bitmap->image conversion functionality. This breaks the API, however, adjust gfxconv and view64 accordingly.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 22 Nov 2012 15:28:27 +0200
parents 1bce06b5026f
children 2ac364d0ace9
files gfxconv.c lib64gfx.c lib64gfx.h view64.c
diffstat 4 files changed, 108 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/gfxconv.c	Thu Nov 22 03:23:05 2012 +0200
+++ b/gfxconv.c	Thu Nov 22 15:28:27 2012 +0200
@@ -182,9 +182,10 @@
     for (i = 0; i < ndmC64ImageFormats; i++)
     {
         const DMC64ImageFormat *fmt = &dmC64ImageFormats[i];
+        char buf[64];
         printf("%3d | %-5s | %-15s | %s\n",
             i, fmt->extension,
-            dmC64ImageTypeNames[fmt->type],
+            dmC64GetImageTypeString(buf, sizeof(buf), fmt->type),
             fmt->name);
     }
 }
--- a/lib64gfx.c	Thu Nov 22 03:23:05 2012 +0200
+++ b/lib64gfx.c	Thu Nov 22 15:28:27 2012 +0200
@@ -12,15 +12,17 @@
 #define BUF_SIZE_GROW      (4*1024)
 
 
-const char *dmC64ImageTypeNames[DM_C64IFMT_LAST_TYPE] =
+char * dmC64GetImageTypeString(char *buf, const size_t len, const int type)
 {
-    "hires",
-    "multicolor",
-    "hires interlace",
-    "mc interlace",
-    "hires fli",
-    "mc fli",
-};
+    snprintf(buf, len,
+        "%s%s%s",
+        (type & D64_FMT_FLI) ? "FLI " : "",
+        (type & D64_FMT_MC) ? "MCol" : "HiRes",
+        (type & D64_FMT_ILACE) ? " Ilace" : ""
+        );
+
+    return buf;
+}
 
 
 // Based on Pepto's palette, stolen from VICE
@@ -204,8 +206,8 @@
 {
     (void) len;
 
-    img->laceType = buf[op->offs] ? DM_C64ILACE_RES : DM_C64ILACE_COLOR;
-    img->laceBank2 = 0;
+    img->laceType = buf[op->offs] ? D64_ILACE_RES : D64_ILACE_COLOR;
+    img->laceBank1 = img->laceBank2 = 0;
     return TRUE;
 }
 
@@ -254,7 +256,8 @@
     (void) op;
     (void) buf;
     (void) len;
-    img->laceType = DM_C64ILACE_RES;
+    img->laceType = D64_ILACE_RES;
+    img->laceBank1 = 0;
     img->laceBank2 = 1;
     return TRUE;
 }
@@ -263,7 +266,7 @@
 const DMC64ImageFormat dmC64ImageFormats[] =
 {
     {
-        DM_C64IFMT_MC, ".drp", "DrazPaint 2.0 (packed)", 0x5800, -1,
+        D64_FMT_MC, ".drp", "DrazPaint 2.0 (packed)", 0x5800, -1,
         fmtProbeDrazPaint20Packed, fmtDecodeDrazPaintPacked,
         NULL, NULL, NULL,
         4,
@@ -276,7 +279,7 @@
     },
 
     {
-        DM_C64IFMT_MC_ILACE, ".dlp", "DrazLace 1.0 (packed)", 0x5800, -1,
+        D64_FMT_MC | D64_FMT_ILACE, ".dlp", "DrazLace 1.0 (packed)", 0x5800, -1,
         fmtProbeDrazLace10Packed, fmtDecodeDrazPaintPacked,
         NULL, NULL, NULL,
         6,
@@ -291,7 +294,7 @@
     },
     
     {
-        DM_C64IFMT_MC, ".drp", "DrazPaint (unpacked)", 0x5800, 10051,
+        D64_FMT_MC, ".drp", "DrazPaint (unpacked)", 0x5800, 10051,
         NULL, NULL,
         NULL, NULL, NULL,
         4,
@@ -304,7 +307,7 @@
     },
 
     {
-        DM_C64IFMT_MC_ILACE, ".drl", "DrazLace 1.0 (unpacked)", 0x5800, 18242,
+        D64_FMT_MC | D64_FMT_ILACE, ".drl", "DrazLace 1.0 (unpacked)", 0x5800, 18242,
         NULL, NULL,
         NULL, NULL, NULL,
         6,
@@ -319,7 +322,7 @@
     },
     
     {
-        DM_C64IFMT_MC_ILACE, ".mci", "Truepaint (unpacked)", 0x9c00, 19434,
+        D64_FMT_MC | D64_FMT_ILACE, ".mci", "Truepaint (unpacked)", 0x9c00, 19434,
         NULL, NULL,
         NULL, NULL, NULL,
         6,
@@ -335,7 +338,7 @@
     },
     
     {
-        DM_C64IFMT_MC, ".kla", "Koala Paint (unpacked)", 0x6000, 10003,
+        D64_FMT_MC, ".kla", "Koala Paint (unpacked)", 0x6000, 10003,
         NULL, NULL,
         NULL, NULL, NULL,
         4,
@@ -348,7 +351,7 @@
     },
 
     {
-        DM_C64IFMT_MC, ".ocp", "Advanced Art Studio (unpacked)", 0x2000, 10018,
+        D64_FMT_MC, ".ocp", "Advanced Art Studio (unpacked)", 0x2000, 10018,
         NULL, NULL,
         NULL, NULL, NULL,
         4,
@@ -361,7 +364,7 @@
     },
 
     {
-        DM_C64IFMT_MC, ".ami", "Amica Paint (packed)", 0x4000, -1,
+        D64_FMT_MC, ".ami", "Amica Paint (packed)", 0x4000, -1,
         fmtProbeAmicaPaintPacked, fmtDecodeAmicaPaintPacked,
         NULL, NULL, NULL,
         4,
@@ -374,7 +377,7 @@
     },
 
     {
-        DM_C64IFMT_MC, ".rpm", "Run Paint (unpacked)", 0x6000, 10006,
+        D64_FMT_MC, ".rpm", "Run Paint (unpacked)", 0x6000, 10006,
         NULL, NULL,
         NULL, NULL, NULL,
         4,
@@ -387,7 +390,7 @@
     },
 
     {
-        DM_C64IFMT_HIRES, ".art", "Art Studio (unpacked)", 0x2000, 9009,
+        D64_FMT_HIRES, ".art", "Art Studio (unpacked)", 0x2000, 9009,
         NULL, NULL,
         NULL, NULL, NULL,
         2,
@@ -398,7 +401,7 @@
     },
 
     {
-        DM_C64IFMT_HIRES, ".iph", "Interpaint (unpacked)", 0x4000, 9002,
+        D64_FMT_HIRES, ".iph", "Interpaint (unpacked)", 0x4000, 9002,
         NULL, NULL,
         NULL, NULL, NULL,
         2,
@@ -409,7 +412,7 @@
     },
 
     {
-        DM_C64IFMT_HIRES, ".dd", "Doodle (unpacked)", 0x1c00, 9218,
+        D64_FMT_HIRES, ".dd", "Doodle (unpacked)", 0x1c00, 9218,
         NULL, NULL,
         NULL, NULL, NULL,
         2,
@@ -547,42 +550,6 @@
 }
 
 
-static int dmC64ConvertHiResBMP(DMImage *screen, const DMC64Image *img)
-{
-    int yc;
-    Uint8 *dp = screen->data;
-    
-    for (yc = 0; yc < C64_SCR_HEIGHT; yc++)
-    {
-        Uint8 *d = dp;
-        const int y = yc / 8, yb = yc & 7;
-        const int scroffsy = y * C64_SCR_CH_WIDTH;
-        const int bmoffsy = y * C64_SCR_WIDTH;
-        int xc;
-
-        for (xc = 0; xc < C64_SCR_WIDTH; xc++)
-        {
-            const int x = xc / 8;
-            const int scroffs = scroffsy + x;
-            const int b = img->bitmap[0][bmoffsy + (x * 8) + yb];
-            const int v = 7 - (xc & 7);
-            Uint8 c;
-
-            if ((b >> v) & 1)
-                c = img->screen[0][scroffs] >> 4;
-            else
-                c = img->screen[0][scroffs] & 15;
-            
-            *d++ = c;
-        }
-
-        dp += screen->pitch;
-    }
-
-    return DMERR_OK;
-}
-
-
 static inline Uint8 dmC64GetMCColor(const DMC64Image *img, const int bits,  const int bank, const int scroffs)
 {
     switch (bits)
@@ -595,11 +562,13 @@
 }
 
 
-static int dmC64ConvertMultiColorBMP(DMImage *screen, const DMC64Image *img)
+// Convert a generic "C64" format bitmap in DMC64Image struct to
+// a indexed/paletted bitmap image.
+int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src)
 {
     int yc;
-    Uint8 *dp = screen->data;
-    
+    Uint8 *dp = dst->data;
+
     for (yc = 0; yc < C64_SCR_HEIGHT; yc++)
     {
         Uint8 *d = dp;
@@ -608,54 +577,72 @@
         const int bmoffsy = y * C64_SCR_WIDTH;
         int xc;
 
-        for (xc = 0; xc < C64_SCR_WIDTH / 2; xc++)
+        if ((src->type & D64_FMT_MC) == D64_FMT_HIRES)
+        {
+            for (xc = 0; xc < C64_SCR_WIDTH; xc++)
+            {
+                const int x = xc / 8;
+                const int scroffs = scroffsy + x;
+                const int b = src->bitmap[0][bmoffsy + (x * 8) + yb];
+                const int v = 7 - (xc & 7);
+
+                if ((b >> v) & 1)
+                    *d++ = src->screen[0][scroffs] >> 4;
+                else
+                    *d++ = src->screen[0][scroffs] & 15;
+            }
+        }
+        else
         {
-            const int x = xc / 4;
-            const int scroffs = scroffsy + x;
-            const int bmoffs = bmoffsy + (x * 8) + yb;
-            const int v = 6 - ((xc * 2) & 6);
-            switch (img->type)
+            for (xc = 0; xc < C64_SCR_WIDTH / 2; xc++)
             {
-                case DM_C64IFMT_MC:
+                const int x = xc / 4;
+                const int scroffs = scroffsy + x;
+                const int bmoffs = bmoffsy + (x * 8) + yb;
+                const int v = 6 - ((xc * 2) & 6);
+                Uint8 c;
+
+                if (src->type & D64_FMT_FLI)
+                {
+                    int bank = 0;
+                    switch (src->fliType)
                     {
-                    const Uint8 c = dmC64GetMCColor(img, (img->bitmap[0][bmoffs] >> v) & 3, 0, scroffs);
+                        case D64_FLI_2BANK:
+                            bank = yb / 4;
+                            break;
+                        case D64_FLI_4BANK:
+                            bank = yb / 2;
+                            break;
+                        case D64_FLI_8BANK:
+                            bank = yb;
+                            break;
+                    }
+                    c = dmC64GetMCColor(src, (src->bitmap[0][bmoffs] >> v) & 3, bank, scroffs);
                     *d++ = c;
                     *d++ = c;
-                    }
-                    break;
-                case DM_C64IFMT_MC_ILACE:
-                    *d++ = dmC64GetMCColor(img, (img->bitmap[0][bmoffs] >> v) & 3, 0, scroffs);
-                    *d++ = dmC64GetMCColor(img, (img->bitmap[1][bmoffs] >> v) & 3, img->laceBank2, scroffs);
-                    break;
+                }
+                else
+                if (src->type & D64_FMT_ILACE)
+                {
+                    *d++ = dmC64GetMCColor(src, (src->bitmap[0][bmoffs] >> v) & 3, src->laceBank1, scroffs);
+                    *d++ = dmC64GetMCColor(src, (src->bitmap[1][bmoffs] >> v) & 3, src->laceBank2, scroffs);
+                }
+                else
+                {
+                    c = dmC64GetMCColor(src, (src->bitmap[0][bmoffs] >> v) & 3, 0, scroffs);
+                    *d++ = c;
+                    *d++ = c;
+                }
             }
         }
 
-        dp += screen->pitch;
+        dp += dst->pitch;
     }
 
     return DMERR_OK;
 }
 
 
-// Convert a generic "C64" format bitmap in DMC64Image struct to
-// a indexed/paletted bitmap image.
-int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src)
-{
-    switch (src->type)
-    {
-        case DM_C64IFMT_HIRES:
-            return dmC64ConvertHiResBMP(dst, src);
-        
-        case DM_C64IFMT_MC:
-        case DM_C64IFMT_MC_ILACE:
-            return dmC64ConvertMultiColorBMP(dst, src);
-
-        default:
-            return DMERR_NOT_SUPPORTED;
-    }
-}
-
-
 
 int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize)
 {
--- a/lib64gfx.h	Thu Nov 22 03:23:05 2012 +0200
+++ b/lib64gfx.h	Thu Nov 22 15:28:27 2012 +0200
@@ -53,21 +53,26 @@
 // Different supported C64 bitmap "modes"
 enum
 {
-    DM_C64IFMT_HIRES,
-    DM_C64IFMT_MC,
-    DM_C64IFMT_HIRES_ILACE,
-    DM_C64IFMT_MC_ILACE,
-    DM_C64IFMT_HIRES_FLI,
-    DM_C64IFMT_MC_FLI,
+    D64_FMT_HIRES        = 0x0000,
+    D64_FMT_MC           = 0x0001,
+    D64_FMT_ILACE        = 0x0002,
+    D64_FMT_FLI          = 0x0004,
 
-    DM_C64IFMT_LAST_TYPE
+    D64_FMT_MODE_MASK    = 0x000f,
+
 };
 
-
 enum
 {
-    DM_C64ILACE_COLOR,
-    DM_C64ILACE_RES,
+    D64_FLI_2BANK,
+    D64_FLI_4BANK,
+    D64_FLI_8BANK,
+};
+
+enum
+{
+    D64_ILACE_COLOR,
+    D64_ILACE_RES,
 };
 
 typedef struct
@@ -79,9 +84,9 @@
 
 enum
 {
-    DM_C64CHR_GLOBAL,		// use font-global setting
-    DM_C64CHR_MULTICOLOR,	// character is multicolor
-    DM_C64CHR_HIRES,
+    D64_CHR_GLOBAL,		// use font-global setting
+    D64_CHR_MULTICOLOR,	// character is multicolor
+    D64_CHR_HIRES,
 };
 
 typedef struct
@@ -104,9 +109,9 @@
 typedef struct
 {
     int type,     // Image type (DM_C64IFMT_*)
+        fliType,  // FLI type (if FLI used)
         laceType, // Interlace type (DM_C64ILACE_*)
-        fliType,  // FLI type (if FLI used)
-        fliLines, // FLI on every # line
+        laceBank1,
         laceBank2;
 
     Uint8 color[C64_SCR_MAX_BANK][C64_SCR_COLOR_SIZE],
@@ -168,9 +173,9 @@
 extern DMColor           dmC64Palette[C64_NCOLORS];
 extern const DMC64ImageFormat  dmC64ImageFormats[];
 extern const int         ndmC64ImageFormats;
-extern const char *      dmC64ImageTypeNames[];
 
 
+char *    dmC64GetImageTypeString(char *buf, const size_t len, const int type);
 int       dmC64ConvertCSData(DMImage *img, int xoffs, int yoffs, const Uint8 *inBuf, int width, int height, BOOL multicolor, int *colors);
 int       dmC64ProbeGeneric(const Uint8 *buf, const size_t len, const DMC64ImageFormat **fmt);
 int       dmC64DecodeGenericBMP(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt);
--- a/view64.c	Thu Nov 22 03:23:05 2012 +0200
+++ b/view64.c	Thu Nov 22 15:28:27 2012 +0200
@@ -48,9 +48,10 @@
     for (i = 0; i < ndmC64ImageFormats; i++)
     {
         const DMC64ImageFormat *fmt = &dmC64ImageFormats[i];
+        char buf[64];
         printf("%3d | %-5s | %-15s | %s\n",
             i, fmt->extension,
-            dmC64ImageTypeNames[fmt->type],
+            dmC64GetImageTypeString(buf, sizeof(buf), fmt->type),
             fmt->name);
     }
 }