optimize gif palette creation

This commit is contained in:
Alug committed 2024-02-27 20:01:56 +01:00
1 parent edfa116a40
commit 606d8fbe55
5 files changed
+80 -21

No files matched your search

+1
View File
@@ -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);
+73 -15
View File
@@ -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();
+1 -1
View File
@@ -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
+4 -4
View File
@@ -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;
}
}
+1 -1
View File
@@ -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);