- merged FPalette and PaletteContainer.

This commit is contained in:
Christoph Oelckers 2020-04-11 18:19:05 +02:00
parent f94e4a908c
commit ac610d87e5
28 changed files with 162 additions and 181 deletions

View file

@ -43,6 +43,8 @@
#include "v_palette.h"
PaletteContainer GPalette;
//----------------------------------------------------------------------------
//
//
@ -60,6 +62,22 @@ void PaletteContainer::Init(int numslots) // This cannot be a constructor!!!
TranslationTables.Resize(numslots);
}
void PaletteContainer::SetPalette(const uint8_t* colors)
{
for (int i = 0; i < 256; i++, colors += 3)
{
BaseColors[i] = PalEntry(colors[0], colors[1], colors[2]);
Remap[i] = i;
}
// Find white and black from the original palette so that they can be
// used to make an educated guess of the translucency % for a BOOM
// translucency map.
WhiteIndex = BestColor((uint32_t*)BaseColors, 255, 255, 255, 0, 255);
BlackIndex = BestColor((uint32_t*)BaseColors, 0, 0, 0, 0, 255);
}
//----------------------------------------------------------------------------
//
//
@ -168,7 +186,7 @@ int PaletteContainer::StoreTranslation(int slot, FRemapTable *remap)
auto size = NumTranslations(slot);
for (i = 0; i < size; i++)
{
if (*remap == *palMgr.TranslationToTable(TRANSLATION(slot, i)))
if (*remap == *TranslationToTable(TRANSLATION(slot, i)))
{
// A duplicate of this translation already exists
return TRANSLATION(slot, i);

View file

@ -67,11 +67,19 @@ inline int GetTranslationIndex(uint32_t trans)
class PaletteContainer
{
public:
PalEntry BaseColors[256]; // non-gamma corrected palette
uint8_t Remap[256]; // remap original palette indices to in-game indices
uint8_t WhiteIndex; // white in original palette index
uint8_t BlackIndex; // black in original palette index
private:
FMemArena remapArena;
TArray<FRemapTable*> uniqueRemaps;
TArray<TAutoGrowArray<FRemapTablePtr, FRemapTable*>> TranslationTables;
public:
void Init(int numslots); // This cannot be a constructor!!!
void SetPalette(const uint8_t* colors);
void Clear();
FRemapTable* AddRemap(FRemapTable* remap);
void UpdateTranslation(int trans, FRemapTable* remap);
@ -102,5 +110,5 @@ public:
};
extern PaletteContainer palMgr;
extern PaletteContainer GPalette;

View file

@ -40,6 +40,7 @@
#include "filesystem.h"
#include "printf.h"
#include "templates.h"
#include "m_png.h"
/****************************/
/* Palette management stuff */
@ -703,6 +704,7 @@ int V_GetColor(const uint32_t* palette, FScanner& sc)
TArray<FSpecialColormap> SpecialColormaps;
uint8_t DesaturateColormap[31][256];
// These default tables are needed for texture composition.
static FSpecialColormapParameters SpecialColormapParms[] =
@ -820,5 +822,91 @@ void InitSpecialColormaps(PalEntry *pe)
SpecialColormapParms[i].Start[2], SpecialColormapParms[i].End[0],
SpecialColormapParms[i].End[1], SpecialColormapParms[i].End[2]);
}
// desaturated colormaps. These are used for texture composition
for (int m = 0; m < 31; m++)
{
uint8_t* shade = DesaturateColormap[m];
for (int c = 0; c < 256; c++)
{
int intensity = pe[c].Luminance();
int r = (pe[c].r * (31 - m) + intensity * m) / 31;
int g = (pe[c].g * (31 - m) + intensity * m) / 31;
int b = (pe[c].b * (31 - m) + intensity * m) / 31;
shade[c] = BestColor((uint32_t*)pe, r, g, b);
}
}
}
//==========================================================================
//
//
//
//==========================================================================
int ReadPalette(int lumpnum, uint8_t* buffer)
{
if (lumpnum < 0)
{
return 0;
}
FileData lump = fileSystem.ReadFile(lumpnum);
uint8_t* lumpmem = (uint8_t*)lump.GetMem();
memset(buffer, 0, 768);
FileReader fr;
fr.OpenMemory(lumpmem, lump.GetSize());
auto png = M_VerifyPNG(fr);
if (png)
{
uint32_t id, len;
fr.Seek(33, FileReader::SeekSet);
fr.Read(&len, 4);
fr.Read(&id, 4);
bool succeeded = false;
while (id != MAKE_ID('I', 'D', 'A', 'T') && id != MAKE_ID('I', 'E', 'N', 'D'))
{
len = BigLong((unsigned int)len);
if (id != MAKE_ID('P', 'L', 'T', 'E'))
fr.Seek(len, FileReader::SeekCur);
else
{
int PaletteSize = MIN<int>(len, 768);
fr.Read(buffer, PaletteSize);
return PaletteSize / 3;
}
fr.Seek(4, FileReader::SeekCur); // Skip CRC
fr.Read(&len, 4);
id = MAKE_ID('I', 'E', 'N', 'D');
fr.Read(&id, 4);
}
I_Error("%s contains no palette", fileSystem.GetFileFullName(lumpnum));
}
if (memcmp(lumpmem, "JASC-PAL", 8) == 0)
{
FScanner sc;
sc.OpenMem(fileSystem.GetFileFullName(lumpnum), (char*)lumpmem, int(lump.GetSize()));
sc.MustGetString();
sc.MustGetNumber(); // version - ignore
sc.MustGetNumber();
int colors = MIN(256, sc.Number) * 3;
for (int i = 0; i < colors; i++)
{
sc.MustGetNumber();
if (sc.Number < 0 || sc.Number > 255)
{
sc.ScriptError("Color %d value out of range.", sc.Number);
}
buffer[i] = sc.Number;
}
return colors / 3;
}
else
{
memcpy(buffer, lumpmem, MIN<size_t>(768, lump.GetSize()));
return 256;
}
}

View file

@ -55,7 +55,9 @@ struct FSpecialColormap
};
extern TArray<FSpecialColormap> SpecialColormaps;
extern uint8_t DesaturateColormap[31][256];
int AddSpecialColormap(PalEntry *pe, float r1, float g1, float b1, float r2, float g2, float b2);
void InitSpecialColormaps(PalEntry* pe);
void UpdateSpecialColormap(PalEntry* BaseColors, unsigned int index, float r1, float g1, float b1, float r2, float g2, float b2);
int ReadPalette(int lumpnum, uint8_t* buffer);