1
0
mirror of https://github.com/FunkyFr3sh/cnc-ddraw.git synced 2025-03-25 01:57:47 +01:00

add color conversion blitters

This commit is contained in:
FunkyFr3sh 2022-09-09 10:44:04 +02:00
parent 8df3d5dcb4
commit 10d0477cbd
3 changed files with 143 additions and 48 deletions

View File

@ -75,6 +75,30 @@ void blt_colorfill(
unsigned int color, unsigned int color,
int bpp); 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( void blt_stretch(
unsigned char* dst_buf, unsigned char* dst_buf,
int dst_x, int dst_x,

100
src/blt.c
View File

@ -95,8 +95,8 @@ void blt_colorkey(
{ {
int bytes_pp = bpp / 8; int bytes_pp = bpp / 8;
size_t d_p = (dst_p / bytes_pp) - dst_w; size_t s_a = (src_p / bytes_pp) - dst_w;
size_t s_p = (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); src += (src_x * bytes_pp) + (src_p * src_y);
dst += (dst_x * bytes_pp) + (dst_p * dst_y); dst += (dst_x * bytes_pp) + (dst_p * dst_y);
@ -122,8 +122,8 @@ void blt_colorkey(
dst++; dst++;
} }
src += s_p; src += s_a;
dst += d_p; dst += d_a;
} }
} }
else else
@ -142,8 +142,8 @@ void blt_colorkey(
dst++; dst++;
} }
src += s_p; src += s_a;
dst += d_p; dst += d_a;
} }
} }
} }
@ -171,8 +171,8 @@ void blt_colorkey(
d++; d++;
} }
s += s_p; s += s_a;
d += d_p; d += d_a;
} }
} }
else else
@ -191,8 +191,8 @@ void blt_colorkey(
d++; d++;
} }
s += s_p; s += s_a;
d += d_p; d += d_a;
} }
} }
} }
@ -220,8 +220,8 @@ void blt_colorkey(
d++; d++;
} }
s += s_p; s += s_a;
d += d_p; d += d_a;
} }
} }
else else
@ -240,8 +240,8 @@ void blt_colorkey(
d++; d++;
} }
s += s_p; s += s_a;
d += d_p; 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( void blt_stretch(
unsigned char* dst_buf, unsigned char* dst_buf,
int dst_x, int dst_x,

View File

@ -6,6 +6,7 @@
#include "ddpalette.h" #include "ddpalette.h"
#include "ddsurface.h" #include "ddsurface.h"
#include "lodepng.h" #include "lodepng.h"
#include "blt.h"
static BOOL ss_screenshot_8bit(char* filename, IDirectDrawSurfaceImpl* src) 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; unsigned char* dst_buf = NULL;
size_t dst_buf_size = 0; 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) if (!error && dst_buf)
lodepng_save_file(dst_buf, dst_buf_size, filename); lodepng_save_file(dst_buf, dst_buf_size, filename);
@ -49,22 +58,17 @@ static BOOL ss_screenshot_16bit(char* filename, IDirectDrawSurfaceImpl* src)
if (buf) if (buf)
{ {
unsigned short* src_buf = (unsigned short*)dds_GetBuffer(src); blt_rgb565_to_rgba8888(
unsigned int* dst_buf = buf; buf,
0,
for (int y = 0; y < src->height; y++) 0,
{ src->width,
for (int x = 0; x < src->width; x++) src->height,
{ src->width * 4,
unsigned short pixel = *src_buf++; dds_GetBuffer(src),
0,
BYTE red = ((pixel & 0xF800) >> 11) << 3; 0,
BYTE green = ((pixel & 0x07E0) >> 5) << 2; src->l_pitch);
BYTE blue = ((pixel & 0x001F)) << 3;
*dst_buf++ = (0xFF << 24) | (blue << 16) | (green << 8) | red;
}
}
error = lodepng_encode32_file(filename, (unsigned char*)buf, src->width, src->height); 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) if (buf)
{ {
unsigned int* src_buf = (unsigned int*)dds_GetBuffer(src); blt_bgra8888_to_rgba8888(
unsigned int* dst_buf = buf; buf,
0,
for (int y = 0; y < src->height; y++) 0,
{ src->width,
for (int x = 0; x < src->width; x++) src->height,
{ src->width * 4,
unsigned int pixel = *src_buf++; dds_GetBuffer(src),
0,
BYTE red = (pixel >> 16) & 0xFF; 0,
BYTE green = (pixel >> 8) & 0xFF; src->l_pitch);
BYTE blue = pixel & 0xFF;
*dst_buf++ = (0xFF << 24) | (blue << 16) | (green << 8) | red;
}
}
error = lodepng_encode32_file(filename, (unsigned char*)buf, src->width, src->height); 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) BOOL ss_take_screenshot(IDirectDrawSurfaceImpl* src)
{ {
if (!src || !dds_GetBuffer(src)) if (!src || !dds_GetBuffer(src) || !src->width || !src->height)
return FALSE; return FALSE;
char title[128]; char title[128];