From 809b687969bcb9bafc1fee7edf71946ab98470e8 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 28 Jan 2020 10:31:59 +0100 Subject: [PATCH] - 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. --- source/CMakeLists.txt | 1 - source/blood/src/screen.cpp | 4 - source/blood/src/view.cpp | 1 - source/build/include/colmatch.h | 24 ---- source/build/src/colmatch.cpp | 199 -------------------------------- source/build/src/defs.cpp | 3 - source/build/src/engine.cpp | 41 +------ source/build/src/palette.cpp | 16 +-- source/duke3d/src/game.cpp | 37 ------ source/duke3d/src/gameexec.cpp | 4 +- source/duke3d/src/screens.cpp | 1 - source/rr/src/game.cpp | 40 +------ source/rr/src/gameexec.cpp | 1 - source/rr/src/screens.cpp | 1 - 14 files changed, 10 insertions(+), 363 deletions(-) delete mode 100644 source/build/include/colmatch.h delete mode 100644 source/build/src/colmatch.cpp diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 7dcf888b6..c799685e8 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -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 diff --git a/source/blood/src/screen.cpp b/source/blood/src/screen.cpp index 03b6e897d..8f81d2b75 100644 --- a/source/blood/src/screen.cpp +++ b/source/blood/src/screen.cpp @@ -25,7 +25,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #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(); } diff --git a/source/blood/src/view.cpp b/source/blood/src/view.cpp index 78d431304..12daf066a 100644 --- a/source/blood/src/view.cpp +++ b/source/blood/src/view.cpp @@ -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" diff --git a/source/build/include/colmatch.h b/source/build/include/colmatch.h deleted file mode 100644 index 7a9077737..000000000 --- a/source/build/include/colmatch.h +++ /dev/null @@ -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); -} diff --git a/source/build/src/colmatch.cpp b/source/build/src/colmatch.cpp deleted file mode 100644 index 401c532f5..000000000 --- a/source/build/src/colmatch.cpp +++ /dev/null @@ -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<=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). -// 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; -} diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index 8589a6461..2c2e85c66 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -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; } } diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index 852397bdb..84a413145 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -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>3]&(1<= 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=0 && daumost[x]=0 && dadmost[x]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)); } diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index d96f89fd0..f0b13d644 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -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) { diff --git a/source/duke3d/src/gameexec.cpp b/source/duke3d/src/gameexec.cpp index ac2806c20..fef43960e 100644 --- a/source/duke3d/src/gameexec.cpp +++ b/source/duke3d/src/gameexec.cpp @@ -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(); diff --git a/source/duke3d/src/screens.cpp b/source/duke3d/src/screens.cpp index fc9ef491b..92f41b92a 100644 --- a/source/duke3d/src/screens.cpp +++ b/source/duke3d/src/screens.cpp @@ -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" diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index 697e05fa7..05907273b 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -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; diff --git a/source/rr/src/gameexec.cpp b/source/rr/src/gameexec.cpp index c5129ba6c..3fac38e7e 100644 --- a/source/rr/src/gameexec.cpp +++ b/source/rr/src/gameexec.cpp @@ -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" diff --git a/source/rr/src/screens.cpp b/source/rr/src/screens.cpp index f08033783..3d8985e90 100644 --- a/source/rr/src/screens.cpp +++ b/source/rr/src/screens.cpp @@ -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"