Use color look-up table for PNG conversion

This commit is contained in:
Jaime Passos 2020-09-10 02:10:31 -03:00
parent bf52125494
commit 15eb91be69
5 changed files with 36 additions and 21 deletions

View file

@ -499,20 +499,22 @@ static size_t gifframe_size = 8192;
// converts an RGB frame to a frame with a palette. // converts an RGB frame to a frame with a palette.
// //
#ifdef HWRENDER #ifdef HWRENDER
static colorlookup_t gif_colorlookup;
static void GIF_rgbconvert(UINT8 *linear, UINT8 *scr) static void GIF_rgbconvert(UINT8 *linear, UINT8 *scr)
{ {
UINT8 r, g, b; UINT8 r, g, b;
size_t src = 0, dest = 0; size_t src = 0, dest = 0;
size_t size = (vid.width * vid.height * 3); size_t size = (vid.width * vid.height * 3);
InitColorLUT(gif_framepalette, true); InitColorLUT(&gif_colorlookup, gif_framepalette, true);
while (src < size) while (src < size)
{ {
r = (UINT8)linear[src]; r = (UINT8)linear[src];
g = (UINT8)linear[src + 1]; g = (UINT8)linear[src + 1];
b = (UINT8)linear[src + 2]; b = (UINT8)linear[src + 2];
scr[dest] = GetColorLUTDirect(r, g, b); scr[dest] = GetColorLUTDirect(&gif_colorlookup, r, g, b);
src += (3 * scrbuf_downscaleamt); src += (3 * scrbuf_downscaleamt);
dest += scrbuf_downscaleamt; dest += scrbuf_downscaleamt;
} }

View file

@ -52,6 +52,10 @@
static unsigned char imgbuf[1<<26]; static unsigned char imgbuf[1<<26];
#ifdef PICTURE_PNG_USELOOKUP
static colorlookup_t png_colorlookup;
#endif
/** Converts a picture between two formats. /** Converts a picture between two formats.
* *
* \param informat Input picture format. * \param informat Input picture format.
@ -964,6 +968,11 @@ void *Picture_PNGConvert(
if (outbpp == PICDEPTH_8BPP) if (outbpp == PICDEPTH_8BPP)
memset(flat, TRANSPARENTPIXEL, (width * height)); memset(flat, TRANSPARENTPIXEL, (width * height));
#ifdef PICTURE_PNG_USELOOKUP
if (outbpp != PICDEPTH_32BPP)
InitColorLUT(&png_colorlookup, pMasterPalette, false);
#endif
for (y = 0; y < height; y++) for (y = 0; y < height; y++)
{ {
png_bytep row = row_pointers[y]; png_bytep row = row_pointers[y];
@ -988,7 +997,11 @@ void *Picture_PNGConvert(
} }
else else
{ {
#ifdef PICTURE_PNG_USELOOKUP
UINT8 palidx = GetColorLUT(&png_colorlookup, red, green, blue);
#else
UINT8 palidx = NearestColor(red, green, blue); UINT8 palidx = NearestColor(red, green, blue);
#endif
if (outbpp == PICDEPTH_16BPP) if (outbpp == PICDEPTH_16BPP)
{ {
UINT16 *outflat = (UINT16 *)flat; UINT16 *outflat = (UINT16 *)flat;

View file

@ -113,6 +113,8 @@ void *Picture_PNGConvert(
boolean Picture_PNGDimensions(UINT8 *png, INT16 *width, INT16 *height, size_t size); boolean Picture_PNGDimensions(UINT8 *png, INT16 *width, INT16 *height, size_t size);
#endif #endif
#define PICTURE_PNG_USELOOKUP
// SpriteInfo // SpriteInfo
extern spriteinfo_t spriteinfo[NUMSPRITES]; extern spriteinfo_t spriteinfo[NUMSPRITES];
void R_LoadSpriteInfoLumps(UINT16 wadnum, UINT16 numlumps); void R_LoadSpriteInfoLumps(UINT16 wadnum, UINT16 numlumps);

View file

@ -3667,21 +3667,19 @@ Unoptimized version
} }
// Generates a RGB565 color look-up table // Generates a RGB565 color look-up table
static colorlookup_t colorlookup; void InitColorLUT(colorlookup_t *lut, RGBA_t *palette, boolean makecolors)
void InitColorLUT(RGBA_t *palette, boolean makecolors)
{ {
size_t palsize = (sizeof(RGBA_t) * 256); size_t palsize = (sizeof(RGBA_t) * 256);
if (!colorlookup.init || memcmp(colorlookup.palette, palette, palsize)) if (!lut->init || memcmp(lut->palette, palette, palsize))
{ {
INT32 i; INT32 i;
colorlookup.init = true; lut->init = true;
memcpy(colorlookup.palette, palette, palsize); memcpy(lut->palette, palette, palsize);
for (i = 0; i < 0xFFFF; i++) for (i = 0; i < 0xFFFF; i++)
colorlookup.table[i] = 0xFFFF; lut->table[i] = 0xFFFF;
if (makecolors) if (makecolors)
{ {
@ -3692,25 +3690,25 @@ void InitColorLUT(RGBA_t *palette, boolean makecolors)
for (b = 0; b < 0xFF; b++) for (b = 0; b < 0xFF; b++)
{ {
i = CLUTINDEX(r, g, b); i = CLUTINDEX(r, g, b);
if (colorlookup.table[i] == 0xFFFF) if (lut->table[i] == 0xFFFF)
colorlookup.table[i] = NearestPaletteColor(r, g, b, palette); lut->table[i] = NearestPaletteColor(r, g, b, palette);
} }
} }
} }
} }
UINT8 GetColorLUT(UINT8 r, UINT8 g, UINT8 b) UINT8 GetColorLUT(colorlookup_t *lut, UINT8 r, UINT8 g, UINT8 b)
{ {
INT32 i = CLUTINDEX(r, g, b); INT32 i = CLUTINDEX(r, g, b);
if (colorlookup.table[i] == 0xFFFF) if (lut->table[i] == 0xFFFF)
colorlookup.table[i] = NearestPaletteColor(r << 3, g << 2, b << 3, colorlookup.palette); lut->table[i] = NearestPaletteColor(r, g, b, lut->palette);
return colorlookup.table[i]; return lut->table[i];
} }
UINT8 GetColorLUTDirect(UINT8 r, UINT8 g, UINT8 b) UINT8 GetColorLUTDirect(colorlookup_t *lut, UINT8 r, UINT8 g, UINT8 b)
{ {
INT32 i = CLUTINDEX(r, g, b); INT32 i = CLUTINDEX(r, g, b);
return colorlookup.table[i]; return lut->table[i];
} }
// V_Init // V_Init

View file

@ -40,10 +40,6 @@ void V_Init(void);
// Color look-up table // Color look-up table
#define CLUTINDEX(r, g, b) (((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3) #define CLUTINDEX(r, g, b) (((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)
void InitColorLUT(RGBA_t *palette, boolean makecolors);
UINT8 GetColorLUT(UINT8 r, UINT8 g, UINT8 b);
UINT8 GetColorLUTDirect(UINT8 r, UINT8 g, UINT8 b);
typedef struct typedef struct
{ {
boolean init; boolean init;
@ -51,6 +47,10 @@ typedef struct
UINT16 table[0xFFFF]; UINT16 table[0xFFFF];
} colorlookup_t; } colorlookup_t;
void InitColorLUT(colorlookup_t *lut, RGBA_t *palette, boolean makecolors);
UINT8 GetColorLUT(colorlookup_t *lut, UINT8 r, UINT8 g, UINT8 b);
UINT8 GetColorLUTDirect(colorlookup_t *lut, UINT8 r, UINT8 g, UINT8 b);
// Set the current RGB palette lookup to use for palettized graphics // Set the current RGB palette lookup to use for palettized graphics
void V_SetPalette(INT32 palettenum); void V_SetPalette(INT32 palettenum);