- changed palette lookup to consider the remapping of color 0 to 255.

This is to ensure that black maps to the proper index, even if there's duplicates.
This commit is contained in:
Christoph Oelckers 2021-08-14 10:26:04 +02:00
parent bad2c2e55f
commit a5e3a85c98
6 changed files with 24 additions and 18 deletions

View file

@ -51,7 +51,7 @@ extern uint8_t IcePalette[16][3];
// //
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void PaletteContainer::Init(int numslots) // This cannot be a constructor!!! void PaletteContainer::Init(int numslots, const uint8_t* indexmap) // This cannot be a constructor!!!
{ {
if (numslots < 1) numslots = 1; if (numslots < 1) numslots = 1;
Clear(); Clear();
@ -63,6 +63,7 @@ void PaletteContainer::Init(int numslots) // This cannot be a constructor!!!
TranslationTables.Resize(numslots); TranslationTables.Resize(numslots);
StoreTranslation(0, &remap); // make sure that translation ID 0 is the identity. StoreTranslation(0, &remap); // make sure that translation ID 0 is the identity.
ColorMatcher.SetPalette(BaseColors); ColorMatcher.SetPalette(BaseColors);
ColorMatcher.SetIndexMap(indexmap);
} }
void PaletteContainer::SetPalette(const uint8_t* colors, int transparent_index) void PaletteContainer::SetPalette(const uint8_t* colors, int transparent_index)

View file

@ -116,7 +116,7 @@ private:
FMemArena remapArena; FMemArena remapArena;
TArray<TAutoGrowArray<FRemapTablePtr, FRemapTable*>> TranslationTables; TArray<TAutoGrowArray<FRemapTablePtr, FRemapTable*>> TranslationTables;
public: public:
void Init(int numslots); // This cannot be a constructor!!! void Init(int numslots, const uint8_t *indexmap); // This cannot be a constructor!!!
void SetPalette(const uint8_t* colors, int transparent_index = -1); void SetPalette(const uint8_t* colors, int transparent_index = -1);
void Clear(); void Clear();
int DetermineTranslucency(FileReader& file); int DetermineTranslucency(FileReader& file);

View file

@ -42,23 +42,21 @@
#include "palutil.h" #include "palutil.h"
int BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num); int BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num, const uint8_t* indexmap);
class FColorMatcher class FColorMatcher
{ {
public: public:
FColorMatcher () = default;
FColorMatcher (const uint32_t *palette) { Pal = reinterpret_cast<const PalEntry*>(palette); }
FColorMatcher (const FColorMatcher &other) = default;
void SetPalette(PalEntry* palette) { Pal = palette; } void SetPalette(PalEntry* palette) { Pal = palette; }
void SetPalette (const uint32_t *palette) { Pal = reinterpret_cast<const PalEntry*>(palette); } void SetPalette (const uint32_t *palette) { Pal = reinterpret_cast<const PalEntry*>(palette); }
void SetIndexMap(const uint8_t* index) { indexmap = index; startindex = index ? 0 : 1; }
uint8_t Pick (int r, int g, int b) uint8_t Pick (int r, int g, int b)
{ {
if (Pal == nullptr) if (Pal == nullptr)
return 1; return 1;
return (uint8_t)BestColor ((uint32_t *)Pal, r, g, b, 1, 255); return (uint8_t)BestColor ((uint32_t *)Pal, r, g, b, startindex, 255, indexmap);
} }
uint8_t Pick (PalEntry pe) uint8_t Pick (PalEntry pe)
@ -66,10 +64,10 @@ public:
return Pick(pe.r, pe.g, pe.b); return Pick(pe.r, pe.g, pe.b);
} }
FColorMatcher &operator= (const FColorMatcher &other) = default;
private: private:
const PalEntry *Pal; const PalEntry *Pal = nullptr;
const uint8_t* indexmap = nullptr;
int startindex = 1;
}; };
extern FColorMatcher ColorMatcher; extern FColorMatcher ColorMatcher;

View file

@ -46,7 +46,7 @@
/* Palette management stuff */ /* Palette management stuff */
/****************************/ /****************************/
int BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num) int BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num, const uint8_t* indexmap)
{ {
const PalEntry *pal = (const PalEntry *)pal_in; const PalEntry *pal = (const PalEntry *)pal_in;
int bestcolor = first; int bestcolor = first;
@ -54,17 +54,18 @@ int BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num)
for (int color = first; color < num; color++) for (int color = first; color < num; color++)
{ {
int x = r - pal[color].r; int co = indexmap ? indexmap[color] : color;
int y = g - pal[color].g; int x = r - pal[co].r;
int z = b - pal[color].b; int y = g - pal[co].g;
int z = b - pal[co].b;
int dist = x*x + y*y + z*z; int dist = x*x + y*y + z*z;
if (dist < bestdist) if (dist < bestdist)
{ {
if (dist == 0) if (dist == 0)
return color; return co;
bestdist = dist; bestdist = dist;
bestcolor = color; bestcolor = co;
} }
} }
return bestcolor; return bestcolor;

View file

@ -7,7 +7,7 @@
struct FScriptPosition; struct FScriptPosition;
class FScanner; class FScanner;
int BestColor(const uint32_t* pal, int r, int g, int b, int first = 1, int num = 255); int BestColor(const uint32_t* pal, int r, int g, int b, int first = 1, int num = 255, const uint8_t* indexmap = nullptr);
int PTM_BestColor(const uint32_t* pal_in, int r, int g, int b, bool reverselookup, float powtable, int first = 1, int num = 255); int PTM_BestColor(const uint32_t* pal_in, int r, int g, int b, bool reverselookup, float powtable, int first = 1, int num = 255);
void DoBlending(const PalEntry* from, PalEntry* to, int count, int r, int g, int b, int a); void DoBlending(const PalEntry* from, PalEntry* to, int count, int r, int g, int b, int a);

View file

@ -914,6 +914,8 @@ static void InitTextures()
// //
//========================================================================== //==========================================================================
static uint8_t palindexmap[256];
int RunGame() int RunGame()
{ {
GameStartupInfo.FgColor = 0xffffff; GameStartupInfo.FgColor = 0xffffff;
@ -1023,7 +1025,11 @@ int RunGame()
} }
GameTicRate = 30; GameTicRate = 30;
CheckUserMap(); CheckUserMap();
GPalette.Init(MAXPALOOKUPS + 2); // one slot for each translation, plus a separate one for the base palettes and the internal one
palindexmap[0] = 255;
for (int i = 1; i <= 255; i++) palindexmap[i] = i;
GPalette.Init(MAXPALOOKUPS + 2, palindexmap); // one slot for each translation, plus a separate one for the base palettes and the internal one
int v = ColorMatcher.Pick(0, 0, 0);
gi->loadPalette(); gi->loadPalette();
StartScreen->Progress(); StartScreen->Progress();
InitTextures(); InitTextures();