Skip to content

Commit

Permalink
- allow passing a remap table to BestColor.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Aug 21, 2021
1 parent 13bdd32 commit 4614ce4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/common/engine/palettecontainer.cpp
Expand Up @@ -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;
Clear();
Expand All @@ -63,6 +63,7 @@ void PaletteContainer::Init(int numslots) // This cannot be a constructor!!!
TranslationTables.Resize(numslots);
StoreTranslation(0, &remap); // make sure that translation ID 0 is the identity.
ColorMatcher.SetPalette(BaseColors);
ColorMatcher.SetIndexMap(indexmap);
}

void PaletteContainer::SetPalette(const uint8_t* colors, int transparent_index)
Expand Down
2 changes: 1 addition & 1 deletion src/common/engine/palettecontainer.h
Expand Up @@ -116,7 +116,7 @@ class PaletteContainer
FMemArena remapArena;
TArray<TAutoGrowArray<FRemapTablePtr, FRemapTable*>> TranslationTables;
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 Clear();
int DetermineTranslucency(FileReader& file);
Expand Down
2 changes: 1 addition & 1 deletion src/common/models/voxels.cpp
Expand Up @@ -74,7 +74,7 @@ static uint8_t *GetVoxelRemap(const uint8_t *pal)
{
// The voxel palette uses VGA colors, so we have to expand it
// from 6 to 8 bits per component.
remap[i] = BestColor((uint32_t *)GPalette.BaseColors,
remap[i] = ColorMatcher.Pick(
(oldpal[i*3 + 0] << 2) | (oldpal[i*3 + 0] >> 4),
(oldpal[i*3 + 1] << 2) | (oldpal[i*3 + 1] >> 4),
(oldpal[i*3 + 2] << 2) | (oldpal[i*3 + 2] >> 4));
Expand Down
14 changes: 6 additions & 8 deletions src/common/utility/colormatcher.h
Expand Up @@ -42,34 +42,32 @@

#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
{
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 (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)
{
if (Pal == nullptr)
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)
{
return Pick(pe.r, pe.g, pe.b);
}

FColorMatcher &operator= (const FColorMatcher &other) = default;

private:
const PalEntry *Pal;
const PalEntry *Pal = nullptr;
const uint8_t* indexmap = nullptr;
int startindex = 1;
};

extern FColorMatcher ColorMatcher;
Expand Down
13 changes: 7 additions & 6 deletions src/common/utility/palette.cpp
Expand Up @@ -46,25 +46,26 @@
/* 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;
int bestcolor = first;
int bestdist = 257 * 257 + 257 * 257 + 257 * 257;

for (int color = first; color < num; color++)
{
int x = r - pal[color].r;
int y = g - pal[color].g;
int z = b - pal[color].b;
int co = indexmap ? indexmap[color] : color;
int x = r - pal[co].r;
int y = g - pal[co].g;
int z = b - pal[co].b;
int dist = x*x + y*y + z*z;
if (dist < bestdist)
{
if (dist == 0)
return color;
return co;

bestdist = dist;
bestcolor = color;
bestcolor = co;
}
}
return bestcolor;
Expand Down
2 changes: 1 addition & 1 deletion src/common/utility/palutil.h
Expand Up @@ -7,7 +7,7 @@
struct FScriptPosition;
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);
void DoBlending(const PalEntry* from, PalEntry* to, int count, int r, int g, int b, int a);

Expand Down

1 comment on commit 4614ce4

@drfrag666
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't compile:

D:\a\gzdoom\gzdoom\src\r_data\v_palette.cpp(58,38): error C2660: 'PaletteContainer::Init': function does not take 1 arguments [D:\a\gzdoom\gzdoom\build\src\zdoom.vcxproj]
D:\a\gzdoom\gzdoom\src\common\engine\palettecontainer.h(119,7): message : see declaration of 'PaletteContainer::Init' [D:\a\gzdoom\gzdoom\build\src\zdoom.vcxproj]

Please sign in to comment.