From 10d0477cbd22c61a3fa607603ee82e744b7079c4 Mon Sep 17 00:00:00 2001
From: FunkyFr3sh <cc.red.alert.1@googlemail.com>
Date: Fri, 9 Sep 2022 10:44:04 +0200
Subject: [PATCH] add color conversion blitters

---
 inc/blt.h        |  24 ++++++++++++
 src/blt.c        | 100 ++++++++++++++++++++++++++++++++++++++++-------
 src/screenshot.c |  67 ++++++++++++++++---------------
 3 files changed, 143 insertions(+), 48 deletions(-)

diff --git a/inc/blt.h b/inc/blt.h
index f216988..363139f 100644
--- a/inc/blt.h
+++ b/inc/blt.h
@@ -75,6 +75,30 @@ void blt_colorfill(
     unsigned int color,
     int bpp);
 
+void blt_rgb565_to_rgba8888(
+    unsigned int* dst,
+    int dst_x,
+    int dst_y,
+    int dst_w,
+    int dst_h,
+    int dst_p,
+    unsigned short* src,
+    int src_x,
+    int src_y,
+    int src_p);
+
+void blt_bgra8888_to_rgba8888(
+    unsigned int* dst,
+    int dst_x,
+    int dst_y,
+    int dst_w,
+    int dst_h,
+    int dst_p,
+    unsigned int* src,
+    int src_x,
+    int src_y,
+    int src_p);
+
 void blt_stretch(
     unsigned char* dst_buf,
     int dst_x,
diff --git a/src/blt.c b/src/blt.c
index f5c9eac..9bafb50 100644
--- a/src/blt.c
+++ b/src/blt.c
@@ -95,8 +95,8 @@ void blt_colorkey(
 {
     int bytes_pp = bpp / 8;
 
-    size_t d_p = (dst_p / bytes_pp) - dst_w;
-    size_t s_p = (src_p / bytes_pp) - dst_w;
+    size_t s_a = (src_p / bytes_pp) - dst_w;
+    size_t d_a = (dst_p / bytes_pp) - dst_w;
 
     src += (src_x * bytes_pp) + (src_p * src_y);
     dst += (dst_x * bytes_pp) + (dst_p * dst_y);
@@ -122,8 +122,8 @@ void blt_colorkey(
                     dst++;
                 }
 
-                src += s_p;
-                dst += d_p;
+                src += s_a;
+                dst += d_a;
             }
         }
         else
@@ -142,8 +142,8 @@ void blt_colorkey(
                     dst++;
                 }
 
-                src += s_p;
-                dst += d_p;
+                src += s_a;
+                dst += d_a;
             }
         }
     }
@@ -171,8 +171,8 @@ void blt_colorkey(
                     d++;
                 }
 
-                s += s_p;
-                d += d_p;
+                s += s_a;
+                d += d_a;
             }
         }
         else
@@ -191,8 +191,8 @@ void blt_colorkey(
                     d++;
                 }
 
-                s += s_p;
-                d += d_p;
+                s += s_a;
+                d += d_a;
             }
         }
     }
@@ -220,8 +220,8 @@ void blt_colorkey(
                     d++;
                 }
 
-                s += s_p;
-                d += d_p;
+                s += s_a;
+                d += d_a;
             }
         }
         else
@@ -240,8 +240,8 @@ void blt_colorkey(
                     d++;
                 }
 
-                s += s_p;
-                d += d_p;
+                s += s_a;
+                d += d_a;
             }
         }
     }
@@ -449,6 +449,78 @@ void blt_colorfill(
     }
 }
 
+void blt_rgb565_to_rgba8888(
+    unsigned int* dst,
+    int dst_x,
+    int dst_y,
+    int dst_w,
+    int dst_h,
+    int dst_p,
+    unsigned short* src,
+    int src_x,
+    int src_y,
+    int src_p)
+{
+    size_t s_a = (src_p / sizeof(unsigned short)) - dst_w;
+    size_t d_a = (dst_p / sizeof(unsigned int))   - dst_w;
+
+    src += (src_x * sizeof(unsigned short)) + (src_p * src_y);
+    dst += (dst_x * sizeof(unsigned int))   + (dst_p * dst_y);
+
+    for (int y = 0; y < dst_h; y++)
+    {
+        for (int x = 0; x < dst_w; x++)
+        {
+            unsigned short pixel = *src++;
+
+            BYTE r = ((pixel & 0xF800) >> 11) << 3;
+            BYTE g = ((pixel & 0x07E0) >> 5) << 2;
+            BYTE b = ((pixel & 0x001F)) << 3;
+
+            *dst++ = (0xFF << 24) | (b << 16) | (g << 8) | r;
+        }
+
+        src += s_a;
+        dst += d_a;
+    }
+}
+
+void blt_bgra8888_to_rgba8888(
+    unsigned int* dst,
+    int dst_x,
+    int dst_y,
+    int dst_w,
+    int dst_h,
+    int dst_p,
+    unsigned int* src,
+    int src_x,
+    int src_y,
+    int src_p)
+{
+    size_t s_a = (src_p / sizeof(unsigned int)) - dst_w;
+    size_t d_a = (dst_p / sizeof(unsigned int)) - dst_w;
+
+    src += (src_x * sizeof(unsigned int)) + (src_p * src_y);
+    dst += (dst_x * sizeof(unsigned int)) + (dst_p * dst_y);
+
+    for (int y = 0; y < dst_h; y++)
+    {
+        for (int x = 0; x < dst_w; x++)
+        {
+            unsigned int pixel = *src++;
+
+            BYTE r = pixel >> 16;
+            BYTE g = pixel >> 8;
+            BYTE b = pixel;
+
+            *dst++ = (0xFF << 24) | (b << 16) | (g << 8) | r;
+        }
+
+        src += s_a;
+        dst += d_a;
+    }
+}
+
 void blt_stretch(
     unsigned char* dst_buf,
     int dst_x,
diff --git a/src/screenshot.c b/src/screenshot.c
index ee9a3d4..457afb9 100644
--- a/src/screenshot.c
+++ b/src/screenshot.c
@@ -6,6 +6,7 @@
 #include "ddpalette.h"
 #include "ddsurface.h"
 #include "lodepng.h"
+#include "blt.h"
 
 
 static BOOL ss_screenshot_8bit(char* filename, IDirectDrawSurfaceImpl* src)
@@ -29,7 +30,15 @@ static BOOL ss_screenshot_8bit(char* filename, IDirectDrawSurfaceImpl* src)
 
     unsigned char* dst_buf = NULL;
     size_t dst_buf_size = 0;
-    unsigned int error = lodepng_encode(&dst_buf, &dst_buf_size, dds_GetBuffer(src), src->width, src->height, &state);
+
+    unsigned int error = 
+        lodepng_encode(
+            &dst_buf, 
+            &dst_buf_size, 
+            dds_GetBuffer(src), 
+            src->l_pitch / src->lx_pitch, /* can't specify pitch so we use bitmap real width */
+            src->height,
+            &state);
 
     if (!error && dst_buf)
         lodepng_save_file(dst_buf, dst_buf_size, filename);
@@ -49,22 +58,17 @@ static BOOL ss_screenshot_16bit(char* filename, IDirectDrawSurfaceImpl* src)
 
     if (buf)
     {
-        unsigned short* src_buf = (unsigned short*)dds_GetBuffer(src);
-        unsigned int* dst_buf = buf;
-
-        for (int y = 0; y < src->height; y++)
-        {
-            for (int x = 0; x < src->width; x++)
-            {
-                unsigned short pixel = *src_buf++;
-
-                BYTE red = ((pixel & 0xF800) >> 11) << 3;
-                BYTE green = ((pixel & 0x07E0) >> 5) << 2;
-                BYTE blue = ((pixel & 0x001F)) << 3;
-
-                *dst_buf++ = (0xFF << 24) | (blue << 16) | (green << 8) | red;
-            }
-        }
+        blt_rgb565_to_rgba8888(
+            buf,
+            0,
+            0,
+            src->width,
+            src->height,
+            src->width * 4,
+            dds_GetBuffer(src),
+            0,
+            0,
+            src->l_pitch);
 
         error = lodepng_encode32_file(filename, (unsigned char*)buf, src->width, src->height);
 
@@ -81,22 +85,17 @@ static BOOL ss_screenshot_32bit(char* filename, IDirectDrawSurfaceImpl* src)
 
     if (buf)
     {
-        unsigned int* src_buf = (unsigned int*)dds_GetBuffer(src);
-        unsigned int* dst_buf = buf;
-
-        for (int y = 0; y < src->height; y++)
-        {
-            for (int x = 0; x < src->width; x++)
-            {
-                unsigned int pixel = *src_buf++;
-
-                BYTE red = (pixel >> 16) & 0xFF;
-                BYTE green = (pixel >> 8) & 0xFF;
-                BYTE blue = pixel & 0xFF;
-
-                *dst_buf++ = (0xFF << 24) | (blue << 16) | (green << 8) | red;
-            }
-        }
+        blt_bgra8888_to_rgba8888(
+            buf,
+            0,
+            0,
+            src->width,
+            src->height,
+            src->width * 4,
+            dds_GetBuffer(src),
+            0,
+            0,
+            src->l_pitch);
 
         error = lodepng_encode32_file(filename, (unsigned char*)buf, src->width, src->height);
 
@@ -108,7 +107,7 @@ static BOOL ss_screenshot_32bit(char* filename, IDirectDrawSurfaceImpl* src)
 
 BOOL ss_take_screenshot(IDirectDrawSurfaceImpl* src)
 {
-    if (!src || !dds_GetBuffer(src))
+    if (!src || !dds_GetBuffer(src) || !src->width || !src->height)
         return FALSE;
 
     char title[128];