changeset 2132:6528a1398e8e

Add char map helper functions and use them.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 28 May 2019 10:32:12 +0300
parents 781b1d63109a
children 898c1edadbc6
files tools/lib64gfx.c tools/lib64gfx.h
diffstat 2 files changed, 134 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64gfx.c	Tue May 28 07:25:20 2019 +0300
+++ b/tools/lib64gfx.c	Tue May 28 10:32:12 2019 +0300
@@ -1277,124 +1277,70 @@
 }
 
 
-static int fmtGetGenericSCPixel(Uint8 *col,
-    const DMC64Image *img, const int rasterX, const int rasterY)
+static int fmtGetGenericSCPixel(Uint8 *col, const DMC64Image *img,
+    const int rasterX, const int rasterY)
 {
     DM_C64_GENERIC_SC_PIXEL_DEFS(img)
 
     return dmC64GetGenericSCPixel(
-        col, img, bmoffs, scroffs, vshift, 0, 0);
+        col, img,
+        bmoffs, scroffs,
+        vshift, 0, 0);
 }
 
 
-static int fmtGetGenericMCPixel(Uint8 *col,
-    const DMC64Image *img, const int rasterX, const int rasterY)
+static int fmtGetGenericMCPixel(Uint8 *col, const DMC64Image *img,
+    const int rasterX, const int rasterY)
 {
     DM_C64_GENERIC_MC_PIXEL_DEFS(img)
 
     return dmC64GetGenericMCPixel(
-        col, img, bmoffs, scroffs, vshift, 0, 0, 0, img->bgcolor);
+        col, img,
+        bmoffs, scroffs,
+        vshift, 0,
+        0, 0, img->bgcolor);
 }
 
 
-static int fmtGetGenericCharSCPixel(Uint8 *col,
-    const DMC64Image *img, const int rasterX, const int rasterY)
+static int fmtGetGenericCharSCPixel(Uint8 *col, const DMC64Image *img,
+    const int rasterX, const int rasterY)
 {
-    const int
-        x = rasterX / 8,
-        y = rasterY / 8,
-        scroffs = y * img->fmt->chWidth + x,
-        vshift = 7 - (rasterX & 7),
-        chr = img->screen[0].data[scroffs];
+    DM_C64_GENERIC_CHAR_PIXEL(img)
+    const int chr = img->screen[0].data[scroffs];
 
-    const size_t chroffs = (chr * D64_CHR_SIZE) + (rasterY & 7);
-    if (chroffs >= img->charData[0].size)
-    {
-        return dmError(DMERR_INVALID_DATA,
-            "Character map index #%d out of bounds for char ROM data.\n",
-            chr);
-    }
-
-    if ((img->charData[0].data[chroffs] >> vshift) & 1)
-        *col = img->color[0].data[scroffs];
-    else
-        *col = img->bgcolor;
-
-    return DMERR_OK;
+    return dmC64GetGenericCharSCPixel(
+        col, img,
+        scroffs, rasterX,
+        0, (chr * D64_CHR_SIZE) + (rasterY & 7), chr,
+        0, img->bgcolor);
 }
 
 
-static int fmtGetGenericCharMCPixel(Uint8 *col,
-    const DMC64Image *img, const int rasterX, const int rasterY)
+static int fmtGetGenericCharMCPixel(Uint8 *col, const DMC64Image *img,
+    const int rasterX, const int rasterY)
 {
-    const int
-        y = rasterY / 8,
-        scroffs = y * img->fmt->chWidth + (rasterX / 8),
-        chr = img->screen[0].data[scroffs],
-        ccol = img->color[0].data[scroffs];
-
-    const size_t chroffs = (chr * D64_CHR_SIZE) + (rasterY & 7);
-    if (chroffs >= img->charData[0].size)
-    {
-        return dmError(DMERR_INVALID_DATA,
-            "Character map index #%d out of bounds for char ROM data.\n",
-            chr);
-    }
+    DM_C64_GENERIC_CHAR_PIXEL(img)
+    const int chr = img->screen[0].data[scroffs];
 
-    if (ccol & 8)
-    {
-        const int vshift = 6 - (rasterX & 6);
-        switch ((img->charData[0].data[chroffs] >> vshift) & 3)
-        {
-            case 0: *col = img->bgcolor; break;
-            case 1: *col = img->d022; break;
-            case 2: *col = img->d023; break;
-            case 3: *col = ccol & 15;
-        }
-    }
-    else
-    {
-        const int vshift = 7 - (rasterX & 7);
-        if ((img->charData[0].data[chroffs] >> vshift) & 1)
-            *col = ccol & 7;
-        else
-            *col = img->bgcolor;
-    }
-
-    return DMERR_OK;
+    return dmC64GetGenericCharMCPixel(
+        col, img,
+        scroffs, rasterX,
+        0, (chr * D64_CHR_SIZE) + (rasterY & 7), chr,
+        0, img->bgcolor, img->d022, img->d023);
 }
 
 
-static int fmtGetGenericCharECMPixel(Uint8 *col,
-    const DMC64Image *img, const int rasterX, const int rasterY)
+static int fmtGetGenericCharECMPixel(Uint8 *col, const DMC64Image *img,
+    const int rasterX, const int rasterY)
 {
-    const int
-        x = rasterX / 8,
-        y = rasterY / 8,
-        scroffs = y * img->fmt->chWidth + x,
-        vshift = 7 - (rasterX & 7),
-        chr = img->screen[0].data[scroffs];
+    DM_C64_GENERIC_CHAR_PIXEL(img)
+    const int chr = img->screen[0].data[scroffs];
 
-    const size_t chroffs = ((chr & 0x3f) * D64_CHR_SIZE) + (rasterY & 7);
-    if (chroffs >= img->charData[0].size)
-    {
-        return dmError(DMERR_INVALID_DATA,
-            "Character map index #%d out of bounds for char ROM data.\n",
-            chr);
-    }
-
-    if ((img->charData[0].data[chroffs] >> vshift) & 1)
-        *col = img->color[0].data[scroffs] & 15;
-    else
-    switch ((chr >> 6) & 3)
-    {
-        case 0: *col = img->bgcolor; break;
-        case 1: *col = img->d022; break;
-        case 2: *col = img->d023; break;
-        case 3: *col = img->d024; break;
-    }
-
-    return DMERR_OK;
+    return dmC64GetGenericCharECMPixel(
+        col, img,
+        scroffs, rasterX,
+        0, ((chr & 0x3f) * D64_CHR_SIZE) + (rasterY & 7), chr,
+        0, img->bgcolor, img->d022, img->d023, img->d024);
 }
 
 
--- a/tools/lib64gfx.h	Tue May 28 07:25:20 2019 +0300
+++ b/tools/lib64gfx.h	Tue May 28 10:32:12 2019 +0300
@@ -390,6 +390,13 @@
         vshift = 6 - ((rasterX * 2) & 6);
 
 
+#define DM_C64_GENERIC_CHAR_PIXEL(ximg) \
+    const int \
+        x = rasterX / 8, \
+        y = rasterY / 8, \
+        scroffs = y * (ximg)->fmt->chWidth + x; \
+
+
 //
 // Inline helper functions for pixel format decoding
 //
@@ -423,6 +430,95 @@
 }
 
 
+static inline int dmC64GetGenericCharSCPixel(Uint8 *col,
+    const DMC64Image *img, const int scroffs, const int rasterX,
+    const int chrbank, const size_t chroffs, const int chr,
+    const int cbank, const int bgcolor)
+{
+    if (chroffs >= img->charData[chrbank].size)
+    {
+        return dmError(DMERR_INVALID_DATA,
+            "Character map index #%d out of bounds for char ROM data.\n",
+            chr);
+    }
+
+    const int vshift = 7 - (rasterX & 7);
+    if ((img->charData[chrbank].data[chroffs] >> vshift) & 1)
+        *col = img->color[cbank].data[scroffs];
+    else
+        *col = bgcolor;
+
+    return DMERR_OK;
+}
+
+
+static inline int dmC64GetGenericCharMCPixel(Uint8 *col,
+    const DMC64Image *img, const int scroffs, const int rasterX,
+    const int chrbank, const size_t chroffs, const int chr,
+    const int cbank, const int bgcolor,
+    const int bgd022, const int bgd023)
+{
+    if (chroffs >= img->charData[chrbank].size)
+    {
+        return dmError(DMERR_INVALID_DATA,
+            "Character map index #%d out of bounds for char ROM data.\n",
+            chr);
+    }
+
+    const int ccol = img->color[cbank].data[scroffs];
+    if (ccol & 8)
+    {
+        const int vshift = 6 - (rasterX & 6);
+        switch ((img->charData[chrbank].data[chroffs] >> vshift) & 3)
+        {
+            case 0: *col = bgcolor; break;
+            case 1: *col = bgd022; break;
+            case 2: *col = bgd023; break;
+            case 3: *col = ccol & 15;
+        }
+    }
+    else
+    {
+        const int vshift = 7 - (rasterX & 7);
+        if ((img->charData[chrbank].data[chroffs] >> vshift) & 1)
+            *col = ccol & 7;
+        else
+            *col = bgcolor;
+    }
+
+    return DMERR_OK;
+}
+
+
+static inline int dmC64GetGenericCharECMPixel(Uint8 *col,
+    const DMC64Image *img, const int scroffs, const int rasterX,
+    const int chrbank, const size_t chroffs, const int chr,
+    const int cbank, const int bgcolor,
+    const int bgd022, const int bgd023, const int bgd024)
+{
+    if (chroffs >= img->charData[0].size)
+    {
+        return dmError(DMERR_INVALID_DATA,
+            "Character map index #%d out of bounds for char ROM data.\n",
+            chr);
+    }
+
+    const int vshift = 7 - (rasterX & 7);
+    if ((img->charData[chrbank].data[chroffs] >> vshift) & 1)
+        *col = img->color[cbank].data[scroffs] & 15;
+    else
+    switch ((chr >> 6) & 3)
+    {
+        case 0: *col = bgcolor; break;
+        case 1: *col = bgd022; break;
+        case 2: *col = bgd023; break;
+        case 3: *col = bgd024; break;
+    }
+
+    return DMERR_OK;
+}
+
+
 static inline const DMC64EncDecOp * fmtGetEncDecOp(const DMC64ImageFormat *fmt, const int index)
 {
     return &fmt->format->encdecOps[index];