diff --git a/source/build/include/colmatch.h b/source/build/include/colmatch.h index f0ab75529..7a9077737 100644 --- a/source/build/include/colmatch.h +++ b/source/build/include/colmatch.h @@ -5,10 +5,19 @@ extern void paletteInitClosestColorScale(int32_t rscale, int32_t gscale, int32_t extern void paletteInitClosestColorMap(uint8_t const * pal) ATTRIBUTE((nonnull(1))); extern void paletteInitClosestColorGrid(void); -extern int32_t paletteGetClosestColorUpToIndex(int32_t r, int32_t g, int32_t b, int32_t lastokcol); -extern int32_t paletteGetClosestColorUpToIndexNoCache(int32_t r, int32_t g, int32_t b, int32_t lastokcol); +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 index 3e5ab9aca..401c532f5 100644 --- a/source/build/src/colmatch.cpp +++ b/source/build/src/colmatch.cpp @@ -80,9 +80,11 @@ void paletteFlushClosestColor(void) 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 paletteGetClosestColorUpToIndex(int32_t const r, int32_t const g, int32_t const b, int32_t const lastokcol) +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); @@ -113,16 +115,20 @@ int32_t paletteGetClosestColorUpToIndex(int32_t const r, int32_t const g, int32_ for (; i < k; i++) if (col == (colmatchresults[i] & 0x00ffffff)) { mindist = i; break; } - if (mindist != -1 && colmatchresults[mindist]>>24 <= (unsigned)lastokcol) - return colmatchresults[mindist]>>24; + if (mindist != -1) + { + uint32_t const idx = colmatchresults[mindist]>>24; + if (idx <= (unsigned)lastokcol && (blacklist == nullptr || !checkbitfield(blacklist, idx))) + return idx; + } skip: - i = paletteGetClosestColorUpToIndexNoCache(r, g, b, lastokcol); + i = paletteGetClosestColorWithBlacklistNoCache(r, g, b, lastokcol, blacklist); colmatchresults[numcolmatchresults++ & (COLRESULTSIZ-1)] = col | (i << 24); return i; } -int32_t paletteGetClosestColorUpToIndexNoCache(int32_t r, int32_t g, int32_t b, int32_t const lastokcol) +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); @@ -158,7 +164,7 @@ int32_t paletteGetClosestColorUpToIndexNoCache(int32_t r, int32_t g, int32_t b, char const * const pal1 = (char const *)&colmatch_palette[i*3]; int dist = gdist[pal1[1]+g]; - if (dist >= mindist || i > lastokcol) continue; + 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; @@ -175,6 +181,9 @@ int32_t paletteGetClosestColorUpToIndexNoCache(int32_t r, int32_t g, int32_t b, 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]; diff --git a/source/build/src/palette.cpp b/source/build/src/palette.cpp index ce571eb46..cc9fa9bf3 100644 --- a/source/build/src/palette.cpp +++ b/source/build/src/palette.cpp @@ -401,7 +401,7 @@ void palettePostLoadTables(void) continue; palette_t *edcol = (palette_t *) &vgapal16[4*i]; - editorcolors[i] = paletteGetClosestColorUpToIndex(edcol->b, edcol->g, edcol->r, 239); + editorcolors[i] = paletteGetClosestColorWithBlacklist(edcol->b, edcol->g, edcol->r, 254, PaletteIndexFullbrights); } }