From 606d8fbe55eb990fb34100980495c92990082a39 Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 27 Feb 2024 19:59:09 +0100 Subject: [PATCH] optimize gif palette creation --- src/d_netcmd.c | 1 + src/m_anigif.c | 88 +++++++++++++++++++++++++++++++++++++++++--------- src/m_anigif.h | 2 +- src/v_video.c | 8 ++--- src/v_video.h | 2 +- 5 files changed, 80 insertions(+), 21 deletions(-) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index eb1a5deb5..b387e5a6e 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -929,6 +929,7 @@ void D_RegisterClientCommands(void) CV_RegisterVar(&cv_gif_optimize); CV_RegisterVar(&cv_gif_downscale); CV_RegisterVar(&cv_gif_dynamicdelay); + CV_RegisterVar(&cv_gif_localcolortable); #ifdef WALLSPLATS CV_RegisterVar(&cv_splats); diff --git a/src/m_anigif.c b/src/m_anigif.c index a4d4b0399..94535a0b3 100644 --- a/src/m_anigif.c +++ b/src/m_anigif.c @@ -20,6 +20,7 @@ #include "i_video.h" #include "i_system.h" // I_GetPreciseTime #include "doomstat.h" // singletics +#include "st_stuff.h" // st_palette #ifdef HWRENDER #include "hardware/hw_main.h" @@ -37,12 +38,18 @@ CV_PossibleValue_t gif_dynamicdelay_cons_t[] = { consvar_t cv_gif_optimize = {"gif_optimize", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_gif_downscale = {"gif_downscale", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_gif_dynamicdelay = {"gif_dynamicdelay", "On", CV_SAVE, gif_dynamicdelay_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_gif_localcolortable = {"gif_localcolortable", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; #ifdef HAVE_ANIGIF static boolean gif_optimize = false; // So nobody can do something dumb static boolean gif_downscale = false; // like changing cvars mid output static UINT8 gif_dynamicdelay = (UINT8)0; // and messing something up +// Palette handling +static boolean gif_localcolortable = false; +static RGBA_t *gif_headerpalette = NULL; +static RGBA_t *gif_framepalette = NULL; + static FILE *gif_out = NULL; static INT32 gif_frames = 0; static precise_t gif_prevframetime = 0; @@ -50,7 +57,6 @@ static UINT32 gif_delayus = 0; // "us" is microseconds static UINT8 gif_writeover = 0; - // OPTIMIZE gif output // --- @@ -402,18 +408,47 @@ const UINT8 gifhead_base[6] = {0x47,0x49,0x46,0x38,0x39,0x61}; // GIF89a const UINT8 gifhead_nsid[19] = {0x21,0xFF,0x0B, // extension block + size 0x4E,0x45,0x54,0x53,0x43,0x41,0x50,0x45,0x32,0x2E,0x30, // NETSCAPE2.0 0x03,0x01,0xFF,0xFF,0x00}; // sub-block, repetitions + +// +// GIF_getpalette +// determine the palette for the current frame. +// +static RGBA_t *GIF_getpalette(size_t palnum) +{ + // In hardware mode, always returns the local palette +#ifdef HWRENDER + if (rendermode == render_opengl && !HWR_ShouldUsePaletteRendering()) + return pLocalPalette; + else +#endif + return (&pLocalPalette[palnum*256]); +} + +// +// GIF_palwrite +// writes the gif palette. +// used both for the header and local color tables. +// +static UINT8 *GIF_palwrite(UINT8 *p, RGBA_t *pal) +{ + INT32 i; + for (i = 0; i < 256; i++) + { + WRITEUINT8(p, pal[i].s.red); + WRITEUINT8(p, pal[i].s.green); + WRITEUINT8(p, pal[i].s.blue); + } + return p; +} // // GIF_headwrite // writes the gif header to the currently open output file. -// NOTE that this code does not accomodate for palette changes. // static void GIF_headwrite(void) { UINT8 *gifhead = Z_Malloc(800, PU_STATIC, NULL); UINT8 *p = gifhead; - RGBA_t *c; - INT32 i; UINT16 rwidth, rheight; if (!gif_out) @@ -434,22 +469,17 @@ static void GIF_headwrite(void) rwidth = vid.width; rheight = vid.height; } + WRITEUINT16(p, rwidth); WRITEUINT16(p, rheight); // colors, aspect, etc - WRITEUINT8(p, 0xF7); + WRITEUINT8(p, 0xF7); // (0xF7 = 1111 0111) WRITEUINT8(p, 0x00); WRITEUINT8(p, 0x00); // write color table - for (i = 0; i < 256; ++i) - { - c = &pLocalPalette[i]; - WRITEUINT8(p, c->s.red); - WRITEUINT8(p, c->s.green); - WRITEUINT8(p, c->s.blue); - } + p = GIF_palwrite(p, gif_headerpalette); // write extension block WRITEMEM(p, gifhead_nsid, sizeof(gifhead_nsid)); @@ -480,7 +510,7 @@ static void GIF_rgbconvert(UINT8 *linear, UINT8 *scr) size_t src, dest; int x, y; - InitColorLUT(); + InitColorLUT(gif_framepalette); for (x = 0; x < vid.width; x += scrbuf_downscaleamt) { @@ -507,6 +537,7 @@ static void GIF_framewrite(void) UINT8 *p; UINT8 *movie_screen = screens[2]; INT32 blitx, blity, blitw, blith; + boolean palchanged; if (!gifframe_data) gifframe_data = Z_Malloc(gifframe_size, PU_STATIC, NULL); @@ -514,9 +545,19 @@ static void GIF_framewrite(void) if (!gif_out) return; + + // Lactozilla: Compare the header's palette with the current frame's palette and see if it changed. + if (gif_localcolortable) + { + gif_framepalette = GIF_getpalette(max(st_palette, 0)); + palchanged = memcmp(gif_headerpalette, gif_framepalette, sizeof(RGBA_t) * 256); + } + else + palchanged = false; // Compare image data (for optimizing GIF) - if (gif_optimize && gif_frames > 0) + // If the palette has changed, the entire frame is considered to be different. + if (gif_optimize && gif_frames > 0 && (!palchanged)) { // before blit movie_screen points to last frame, cur_screen points to this frame UINT8 *cur_screen = screens[0]; @@ -613,7 +654,20 @@ static void GIF_framewrite(void) WRITEUINT16(p, (UINT16)(blity / scrbuf_downscaleamt)); WRITEUINT16(p, (UINT16)(blitw / scrbuf_downscaleamt)); WRITEUINT16(p, (UINT16)(blith / scrbuf_downscaleamt)); - WRITEUINT8(p, 0); // no local table of colors + + if (!gif_localcolortable) + WRITEUINT8(p, 0); // no local table of colors + else + { + if (palchanged) + { + // The palettes are different, so write the Local Color Table! + WRITEUINT8(p, 0x87); // (0x87 = 1000 0111) + p = GIF_palwrite(p, gif_framepalette); + } + else + WRITEUINT8(p, 0); // They are equal, no Local Color Table needed. + } scrbuf_pos = movie_screen + blitx + (blity * vid.width); scrbuf_writeend = scrbuf_pos + (blitw - 1) + ((blith - 1) * vid.width); @@ -679,6 +733,10 @@ INT32 GIF_open(const char *filename) gif_optimize = (!!cv_gif_optimize.value); gif_downscale = (!!cv_gif_downscale.value); gif_dynamicdelay = (UINT8)cv_gif_dynamicdelay.value; + + gif_localcolortable = (!!cv_gif_localcolortable.value); + gif_headerpalette = GIF_getpalette(0); + GIF_headwrite(); gif_frames = 0; gif_prevframetime = I_GetPreciseTime(); diff --git a/src/m_anigif.h b/src/m_anigif.h index 70c1aac8d..7d89ae86a 100644 --- a/src/m_anigif.h +++ b/src/m_anigif.h @@ -27,6 +27,6 @@ void GIF_frame(void); INT32 GIF_close(void); #endif -extern consvar_t cv_gif_optimize, cv_gif_downscale, cv_gif_dynamicdelay; +extern consvar_t cv_gif_optimize, cv_gif_downscale, cv_gif_dynamicdelay, cv_gif_localcolortable; #endif diff --git a/src/v_video.c b/src/v_video.c index 25d7a1dbd..3599c5f02 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -3200,19 +3200,19 @@ Unoptimized version UINT8 colorlookup[CLUTSIZE][CLUTSIZE][CLUTSIZE]; -void InitColorLUT(void) +void InitColorLUT(RGBA_t *palette) { UINT8 r, g, b; static boolean clutinit = false; static RGBA_t *lastpalette = NULL; - if ((!clutinit) || (lastpalette != pLocalPalette)) + if ((!clutinit) || (lastpalette != palette)) { for (r = 0; r < CLUTSIZE; r++) for (g = 0; g < CLUTSIZE; g++) for (b = 0; b < CLUTSIZE; b++) - colorlookup[r][g][b] = NearestColor(r << SHIFTCOLORBITS, g << SHIFTCOLORBITS, b << SHIFTCOLORBITS); + colorlookup[r][g][b] = NearestPaletteColor(r << SHIFTCOLORBITS, g << SHIFTCOLORBITS, b << SHIFTCOLORBITS, palette); clutinit = true; - lastpalette = pLocalPalette; + lastpalette = palette; } } diff --git a/src/v_video.h b/src/v_video.h index 4767b4d36..3b011a10f 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -47,7 +47,7 @@ void V_Init(void); extern UINT8 colorlookup[CLUTSIZE][CLUTSIZE][CLUTSIZE]; -void InitColorLUT(void); +void InitColorLUT(RGBA_t *palette); // Set the current RGB palette lookup to use for palettized graphics void V_SetPalette(INT32 palettenum);