- removed the Build color matching code

For the handful of lookups it got used for the needed data is too large and the lookup not precise enough. ZDoom's BestColor yields better results, does not need any tables and for the small amount of lookups being performed is more than adequate.
This commit is contained in:
Christoph Oelckers 2020-01-28 10:31:59 +01:00
parent c0bd8f80e7
commit 809b687969
14 changed files with 10 additions and 363 deletions

View file

@ -710,7 +710,6 @@ set (PCH_SOURCES
build/src/a-c.cpp
build/src/animvpx.cpp
build/src/clip.cpp
build/src/colmatch.cpp
build/src/common.cpp
build/src/compat.cpp
build/src/defs.cpp

View file

@ -25,7 +25,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <string.h>
#include "a.h"
#include "build.h"
#include "colmatch.h"
#include "common_game.h"
#include "globals.h"
@ -148,8 +147,6 @@ void scrLoadPalette(void)
x = bloodglblend;
#endif
paletteInitClosestColorScale(30, 59, 11);
paletteInitClosestColorGrid();
paletteloaded = 0;
initprintf("Loading palettes\n");
for (int i = 0; i < 5; i++)
@ -172,7 +169,6 @@ void scrLoadPalette(void)
blendtable[0] = (char*)gSysRes.Lock(pTrans);
paletteloaded |= PALETTE_TRANSLUC;
paletteInitClosestColorMap(palette);
palettePostLoadTables();
}

View file

@ -28,7 +28,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "compat.h"
#include "a.h"
#include "build.h"
#include "colmatch.h"
#include "pragmas.h"
#include "mmulti.h"
#include "osd.h"

View file

@ -1,24 +0,0 @@
#include "compat.h"
extern void paletteInitClosestColorScale(int32_t rscale, int32_t gscale, int32_t bscale);
extern void paletteInitClosestColorMap(uint8_t const * pal) ATTRIBUTE((nonnull(1)));
extern void paletteInitClosestColorGrid(void);
extern int32_t paletteGetClosestColorWithBlacklist(int32_t r, int32_t g, int32_t b, int32_t lastokcol, uint8_t const * blacklist);
extern int32_t paletteGetClosestColorWithBlacklistNoCache(int32_t r, int32_t g, int32_t b, int32_t lastokcol, uint8_t const * blacklist);
extern void paletteFlushClosestColor(void);
static FORCE_INLINE int32_t paletteGetClosestColorUpToIndex(int32_t r, int32_t g, int32_t b, int32_t lastokcol)
{
return paletteGetClosestColorWithBlacklist(r, g, b, lastokcol, NULL);
}
static FORCE_INLINE int32_t paletteGetClosestColorUpToIndexNoCache(int32_t r, int32_t g, int32_t b, int32_t lastokcol)
{
return paletteGetClosestColorWithBlacklistNoCache(r, g, b, lastokcol, NULL);
}
static FORCE_INLINE int32_t paletteGetClosestColor(int32_t r, int32_t g, int32_t b)
{
return paletteGetClosestColorUpToIndex(r, g, b, 255);
}

View file

@ -1,199 +0,0 @@
#include "colmatch.h"
#define FASTPALCOLDEPTH 256
#define FASTPALRIGHTSHIFT 3
#define FASTPALRGBDIST (FASTPALCOLDEPTH*2+1)
static int32_t rdist[FASTPALRGBDIST], gdist[FASTPALRGBDIST], bdist[FASTPALRGBDIST];
#define FASTPALGRIDSIZ (FASTPALCOLDEPTH>>FASTPALRIGHTSHIFT)
static char colhere[((FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)+7)>>3];
static char colhead[(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)];
static int32_t colnext[256];
#define FASTPALCOLDIST (1<<FASTPALRIGHTSHIFT)
#define FASTPALCOLDISTMASK (FASTPALCOLDIST-1)
static uint8_t coldist[FASTPALCOLDIST];
static int32_t colscan[27];
static uint8_t const * colmatch_palette;
#define pow2char(x) (1u << (x))
//
// paletteInitClosestColor
//
void paletteInitClosestColorScale(int32_t rscale, int32_t gscale, int32_t bscale)
{
int32_t j = 0;
for (bssize_t i=256; i>=0; i--)
{
//j = (i-64)*(i-64);
rdist[i] = rdist[FASTPALCOLDEPTH*2-i] = j*rscale;
gdist[i] = gdist[FASTPALCOLDEPTH*2-i] = j*gscale;
bdist[i] = bdist[FASTPALCOLDEPTH*2-i] = j*bscale;
j += FASTPALRGBDIST-(i<<1);
}
}
void paletteInitClosestColorMap(uint8_t const * const pal)
{
Bmemset(colhere,0,sizeof(colhere));
Bmemset(colhead,0,sizeof(colhead));
colmatch_palette = pal;
char const *pal1 = (char const *)&pal[768-3];
for (bssize_t i=255; i>=0; i--,pal1-=3)
{
int32_t const j = (pal1[0]>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ*FASTPALGRIDSIZ
+ (pal1[1]>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ + (pal1[2]>>FASTPALRIGHTSHIFT)
+ FASTPALGRIDSIZ*FASTPALGRIDSIZ + FASTPALGRIDSIZ + 1;
if (colhere[j>>3]&pow2char(j&7)) colnext[i] = colhead[j]; else colnext[i] = -1;
colhead[j] = i;
colhere[j>>3] |= pow2char(j&7);
}
paletteFlushClosestColor();
}
void paletteInitClosestColorGrid(void)
{
int i = 0;
int32_t x, y, z;
for (x=-FASTPALGRIDSIZ*FASTPALGRIDSIZ; x<=FASTPALGRIDSIZ*FASTPALGRIDSIZ; x+=FASTPALGRIDSIZ*FASTPALGRIDSIZ)
for (y=-FASTPALGRIDSIZ; y<=FASTPALGRIDSIZ; y+=FASTPALGRIDSIZ)
for (z=-1; z<=1; z++)
colscan[i++] = x+y+z;
i = colscan[13]; colscan[13] = colscan[26]; colscan[26] = i;
for (i = 0; i < FASTPALCOLDIST/2; i++)
coldist[i] = i;
for (; i < FASTPALCOLDIST; i++)
coldist[i] = FASTPALCOLDIST-i;
}
#define COLRESULTSIZ 4096
static uint32_t colmatchresults[COLRESULTSIZ];
static int32_t numcolmatchresults;
void paletteFlushClosestColor(void)
{
Bmemset(colmatchresults, 0, COLRESULTSIZ * sizeof(uint32_t));
numcolmatchresults = 0;
}
#define checkbitfield(field, idx) ((field)[(idx)>>3] & (1u<<((idx)&7)))
// Finds a color index in [0 .. lastokcol] closest to (r, g, b).
// <lastokcol> must be in [0 .. 255].
int32_t paletteGetClosestColorWithBlacklist(int32_t const r, int32_t const g, int32_t const b, int32_t const lastokcol, uint8_t const * const blacklist)
{
#ifdef DEBUGGINGAIDS
Bassert(lastokcol >= 0 && lastokcol <= 255);
#endif
uint32_t const col = r | (g<<8) | (b<<16);
int mindist = -1;
int const k = (numcolmatchresults > COLRESULTSIZ) ? COLRESULTSIZ : numcolmatchresults;
if (!numcolmatchresults) goto skip;
if (col == (colmatchresults[(numcolmatchresults-1) & (COLRESULTSIZ-1)] & 0x00ffffff))
return colmatchresults[(numcolmatchresults-1) & (COLRESULTSIZ-1)]>>24;
int i;
for (i = 0; i <= k-4; i+=4)
{
if (col == (colmatchresults[i] & 0x00ffffff)) { mindist = i; break; }
if (col == (colmatchresults[i+1] & 0x00ffffff)) { mindist = i+1; break; }
if (col == (colmatchresults[i+2] & 0x00ffffff)) { mindist = i+2; break; }
if (col == (colmatchresults[i+3] & 0x00ffffff)) { mindist = i+3; break; }
}
if (mindist == -1)
for (; i < k; i++)
if (col == (colmatchresults[i] & 0x00ffffff)) { mindist = i; break; }
if (mindist != -1)
{
uint32_t const idx = colmatchresults[mindist]>>24;
if (idx <= (unsigned)lastokcol && (blacklist == nullptr || !checkbitfield(blacklist, idx)))
return idx;
}
skip:
i = paletteGetClosestColorWithBlacklistNoCache(r, g, b, lastokcol, blacklist);
colmatchresults[numcolmatchresults++ & (COLRESULTSIZ-1)] = col | (i << 24);
return i;
}
int32_t paletteGetClosestColorWithBlacklistNoCache(int32_t r, int32_t g, int32_t b, int32_t const lastokcol, uint8_t const * const blacklist)
{
#ifdef DEBUGGINGAIDS
Bassert(lastokcol >= 0 && lastokcol <= 255);
#endif
int const j = (r>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ*FASTPALGRIDSIZ
+ (g>>FASTPALRIGHTSHIFT)*FASTPALGRIDSIZ + (b>>FASTPALRIGHTSHIFT)
+ FASTPALGRIDSIZ*FASTPALGRIDSIZ
+ FASTPALGRIDSIZ + 1;
int const minrdist = rdist[coldist[r&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
int const mingdist = gdist[coldist[g&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
int const minbdist = bdist[coldist[b&FASTPALCOLDISTMASK]+FASTPALCOLDEPTH];
int mindist = min(minrdist, mingdist);
mindist = min(mindist, minbdist) + 1;
r = FASTPALCOLDEPTH-r, g = FASTPALCOLDEPTH-g, b = FASTPALCOLDEPTH-b;
int retcol = -1;
for (bssize_t k=26; k>=0; k--)
{
int i = colscan[k]+j;
if ((colhere[i>>3]&pow2char(i&7)) == 0)
continue;
i = colhead[i];
do
{
char const * const pal1 = (char const *)&colmatch_palette[i*3];
int dist = gdist[pal1[1]+g];
if (dist >= mindist || i > lastokcol || (blacklist != nullptr && checkbitfield(blacklist, i))) continue;
if ((dist += rdist[pal1[0]+r]) >= mindist) continue;
if ((dist += bdist[pal1[2]+b]) >= mindist) continue;
mindist = dist;
retcol = i;
}
while ((i = colnext[i]) >= 0);
}
if (retcol >= 0)
return retcol;
mindist = INT32_MAX;
for (bssize_t i = 0; i <= lastokcol; ++i)
{
if (blacklist != nullptr && checkbitfield(blacklist, i))
continue;
char const * const pal1 = (char const *)&colmatch_palette[i*3];
int dist = gdist[pal1[1]+g];
if (dist >= mindist) continue;
if ((dist += rdist[pal1[0]+r]) >= mindist) continue;
if ((dist += bdist[pal1[2]+b]) >= mindist) continue;
mindist = dist;
retcol = i;
}
return retcol;
}

View file

@ -13,7 +13,6 @@
#include "common.h"
#include "mdsprite.h" // md3model_t
#include "colmatch.h"
#include "textures.h"
#include "bitmap.h"
#include "m_argv.h"
@ -2711,8 +2710,6 @@ static int32_t defsparser(scriptfile *script)
if (didLoadPal && id == 0)
{
paletteInitClosestColorMap(palette);
paletteloaded |= PALETTE_MAIN;
}
}

View file

@ -12,7 +12,7 @@
#include "baselayer.h"
#include "build.h"
#include "colmatch.h"
#include "imagehelpers.h"
#include "common.h"
#include "compat.h"
#include "engine_priv.h"
@ -220,8 +220,6 @@ uint8_t graysectbitmap[(MAXSECTORS+7)>>3];
uint8_t graywallbitmap[(MAXWALLS+7)>>3];
int32_t autogray = 0, showinnergray = 1;
//#define YAX_DEBUG_YMOSTS
#ifdef YAX_DEBUG
// XXX: This could be replaced with the use of gethiticks().
double u64tickspersec;
@ -1082,34 +1080,6 @@ void yax_drawrooms(void (*SpriteAnimFunc)(int32_t,int32_t,int32_t,int32_t,int32_
engine_screenshot = 0;
#endif
#ifdef YAX_DEBUG_YMOSTS
if (videoGetRenderMode() == REND_CLASSIC && numyaxbunches>0)
{
char purple = paletteGetClosestColor(255, 0, 255);
char yellow = paletteGetClosestColor(255, 255, 0);
videoBeginDrawing();
for (i=0; i<numyaxbunches; i++)
{
int32_t x, x1;
if ((haveymost[i>>3]&(1<<i&7))==0)
continue;
x1 = i*xdimen;
for (x=x1; x<x1+xdimen; x++)
{
if (yumost[x] >= 0 && yumost[x] < ydim && (x&1))
*((char *)frameplace + yumost[x]*bytesperline + x-x1) = purple;
if (ydmost[x]-1 >= 0 && ydmost[x]-1 < ydim && !(x&1))
*((char *)frameplace + (ydmost[x]-1)*bytesperline + x-x1) = yellow;
}
}
videoEndDrawing();
}
#endif
#ifdef USE_OPENGL
if (videoGetRenderMode() == REND_POLYMOST)
yax_polymostclearzbuffer = 1;
@ -4948,15 +4918,6 @@ static void classicDrawVoxel(int32_t dasprx, int32_t daspry, int32_t dasprz, int
}
}
#if 0
for (x=0; x<xdimen; x++)
{
if (daumost[x]>=0 && daumost[x]<ydimen)
*(char *)(frameplace + x + bytesperline*daumost[x]) = editorcolors[13];
if (dadmost[x]>=0 && dadmost[x]<ydimen)
*(char *)(frameplace + x + bytesperline*dadmost[x]) = editorcolors[14];
}
#endif
videoEndDrawing(); //}}}
}

View file

@ -10,7 +10,7 @@
#include "build.h"
#include "engine_priv.h"
#include "baselayer.h"
#include "colmatch.h"
#include "imagehelpers.h"
#include "palette.h"
#include "a.h"
@ -112,8 +112,6 @@ inline bool read_and_test(FileReader& handle, void* buffer, int32_t leng)
//
void paletteLoadFromDisk(void)
{
paletteInitClosestColorScale(30, 59, 11);
paletteInitClosestColorGrid();
#ifdef USE_OPENGL
for (auto & x : glblend)
@ -139,8 +137,6 @@ void paletteLoadFromDisk(void)
for (unsigned char & k : palette)
k <<= 2;
paletteInitClosestColorMap(palette);
paletteloaded |= PALETTE_MAIN;
@ -302,9 +298,9 @@ void palettePostLoadTables(void)
palookup0[i] = palookup0[i+(16<<8)];
#endif
blackcol = paletteGetClosestColor(0, 0, 0);
whitecol = paletteGetClosestColor(255, 255, 255);
redcol = paletteGetClosestColor(255, 0, 0);
blackcol = ImageHelpers::BestColor(0, 0, 0);
whitecol = ImageHelpers::BestColor(255, 255, 255);
redcol = ImageHelpers::BestColor(255, 0, 0);
// Bmemset(PaletteIndexFullbrights, 0, sizeof(PaletteIndexFullbrights));
for (bssize_t c = 0; c < 255; ++c) // skipping transparent color
@ -349,7 +345,7 @@ void palettePostLoadTables(void)
continue;
palette_t *edcol = (palette_t *) &vgapal16[4*i];
editorcolors[i] = paletteGetClosestColorWithBlacklist(edcol->b, edcol->g, edcol->r, 254, PaletteIndexFullbrights);
editorcolors[i] = ImageHelpers::BestColor(edcol->b, edcol->g, edcol->r, 254);
}
}
@ -658,7 +654,7 @@ void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uin
for (j=0; j<256; j++)
{
const char *ptr = (const char *) &palette[remapbuf[j]*3];
*ptr2++ = paletteGetClosestColor(ptr[0] + mulscale16(r-ptr[0], palscale),
*ptr2++ = ImageHelpers::BestColor(ptr[0] + mulscale16(r-ptr[0], palscale),
ptr[1] + mulscale16(g-ptr[1], palscale),
ptr[2] + mulscale16(b-ptr[2], palscale));
}

View file

@ -36,7 +36,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "anim.h"
#include "demo.h"
#include "colmatch.h"
#include "cheats.h"
#include "sbar.h"
#include "screens.h"
@ -624,42 +623,6 @@ static void G_ClearGotMirror()
}
}
#ifdef USE_OPENGL
static void G_ReadGLFrame(void)
{
// Save OpenGL screenshot with Duke3D palette
palette_t *const frame = (palette_t *)Xcalloc(xdim * ydim, sizeof(palette_t));
int32_t x, y;
const int32_t xf = divscale16(ydim*4/3, 320);
const int32_t yf = divscale16(ydim, 200); // (ydim<<16)/200
auto pic = TileFiles.tileCreate(TILE_SAVESHOT, 200, 320);
if (!frame)
{
Bmemset(pic, 0, 320 * 200);
return;
}
videoBeginDrawing();
getScreen((uint8_t*)frame);
videoEndDrawing();
for (y = 0; y < 200; y++)
{
const int32_t base = mulscale16(200 - y - 1, yf)*xdim;
for (x = 0; x < 320; x++)
{
const palette_t *pix = &frame[base + mulscale16(x, xf) + (xdim-(ydim*4/3))/2];
pic[320 * y + x] = paletteGetClosestColor(pix->r, pix->g, pix->b);
}
}
Xfree(frame);
}
#endif
void G_DrawRooms(int32_t playerNum, int32_t smoothRatio)
{

View file

@ -24,7 +24,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "anim.h"
#include "cmdline.h"
#include "colmatch.h"
#include "imagehelpers.h"
#include "compat.h"
#include "duke3d.h"
@ -5828,7 +5828,7 @@ badindex:
{
tw = *insptr++;
int32_t const rgb = Gv_GetVar(*insptr++);
Gv_SetVar(tw, paletteGetClosestColorUpToIndex(rgb & 0xFF, (rgb >> 8) & 0xFF, (rgb >> 16) & 0xFF, Gv_GetVar(*insptr++)));
Gv_SetVar(tw, ImageHelpers::BestColor(rgb & 0xFF, (rgb >> 8) & 0xFF, (rgb >> 16) & 0xFF, Gv_GetVar(*insptr++)));
}
dispatch();

View file

@ -23,7 +23,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "ns.h" // Must come before everything else!
#include "anim.h"
#include "colmatch.h"
#include "compat.h"
#include "demo.h"
#include "duke3d.h"

View file

@ -1,4 +1,4 @@
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
/*
Copyright (C) 2016 EDuke32 developers and contributors
@ -34,7 +34,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "anim.h"
#include "demo.h"
#include "colmatch.h"
#include "cheats.h"
#include "sbar.h"
#include "screens.h"
@ -840,43 +839,6 @@ void G_HandleMirror(int32_t x, int32_t y, int32_t z, fix16_t a, fix16_t q16horiz
}
}
#ifdef USE_OPENGL
static void G_ReadGLFrame(void)
{
// Save OpenGL screenshot with Duke3D palette
palette_t *const frame = (palette_t *)Xcalloc(xdim * ydim, sizeof(palette_t));
int32_t x, y;
const int32_t xf = divscale16(ydim*4/3, 320);
const int32_t yf = divscale16(ydim, 200); // (ydim<<16)/200
auto pic = TileFiles.tileCreate(TILE_SAVESHOT, 200, 320);
if (!frame)
{
Bmemset(pic, 0, 320 * 200);
return;
}
videoBeginDrawing();
getScreen((uint8_t*)frame);
videoEndDrawing();
for (y = 0; y < 200; y++)
{
const int32_t base = mulscale16(200 - y - 1, yf)*xdim;
for (x = 0; x < 320; x++)
{
const palette_t *pix = &frame[base + mulscale16(x, xf) + (xdim-(ydim*4/3))/2];
pic[320 * y + x] = paletteGetClosestColor(pix->r, pix->g, pix->b);
}
}
Xfree(frame);
}
#endif
void G_DrawRooms(int32_t playerNum, int32_t smoothRatio)
{
DukePlayer_t *const pPlayer = g_player[playerNum].ps;

View file

@ -22,7 +22,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "ns.h" // Must come before everything else!
#include "colmatch.h"
#include "compat.h"
#include "duke3d.h"

View file

@ -24,7 +24,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "duke3d.h"
#include "compat.h"
#include "screens.h"
#include "colmatch.h"
#include "anim.h"
#include "sbar.h"