From 4d1d90d7123dc0c259b763f16e13289535ee0170 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 12 Apr 2020 00:18:12 +0200 Subject: [PATCH] - use a global constant for the transparent palette index instead of hardcoding it to the 255 literal everywhere. - added the needed glue to allow palettecontainer.cpp to compile. --- source/CMakeLists.txt | 2 +- source/build/src/voxmodel.cpp | 4 +- source/common/engine/palettecontainer.cpp | 35 +++++++- source/common/engine/palettecontainer.h | 10 ++- source/common/utility/palette.cpp | 88 +++++++++++++++++++++ source/common/utility/palutil.h | 2 + source/core/textures/formats/arttexture.cpp | 2 +- source/core/textures/texture.cpp | 3 +- source/rr/src/rrdh.cpp | 2 +- 9 files changed, 140 insertions(+), 8 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 8fc69ec30..b2584bb3c 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -756,7 +756,7 @@ set (PCH_SOURCES common/filesystem/file_directory.cpp common/filesystem/resourcefile.cpp common/engine/sc_man.cpp - #common/engine/palettecontainer.cpp // not yet operational. + common/engine/palettecontainer.cpp common/engine/stringtable.cpp core/utility/stats.cpp diff --git a/source/build/src/voxmodel.cpp b/source/build/src/voxmodel.cpp index d29832f5d..265d5bc28 100644 --- a/source/build/src/voxmodel.cpp +++ b/source/build/src/voxmodel.cpp @@ -637,7 +637,7 @@ static int32_t loadvox(const char *filnam) fil.Read(tbuf, voxsiz.z); for (bssize_t z=voxsiz.z-1; z>=0; z--) - if (tbuf[z] != 255) + if (tbuf[z] != TRANSPARENT_INDEX) { const int32_t i = j+z; vbit[i>>5] |= (1< uniqueRemaps; TArray> 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; diff --git a/source/common/utility/palette.cpp b/source/common/utility/palette.cpp index 0e4b67d4a..5be3f6a45 100644 --- a/source/common/utility/palette.cpp +++ b/source/common/utility/palette.cpp @@ -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 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(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(768, lump.GetSize())); + return 256; + } } diff --git a/source/common/utility/palutil.h b/source/common/utility/palutil.h index 84e0f7d78..7e6e966cf 100644 --- a/source/common/utility/palutil.h +++ b/source/common/utility/palutil.h @@ -55,7 +55,9 @@ struct FSpecialColormap }; extern TArray 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); diff --git a/source/core/textures/formats/arttexture.cpp b/source/core/textures/formats/arttexture.cpp index 41fbfd53d..a7c98abce 100644 --- a/source/core/textures/formats/arttexture.cpp +++ b/source/core/textures/formats/arttexture.cpp @@ -148,7 +148,7 @@ int FArtTexture::CopyPixels(FBitmap *bmp, int conversion) for (int y = 0; y < numpixels; ++y) { int index = source[y]; - if (index == 255) + if (index == TRANSPARENT_INDEX) { hasalpha = true; continue; diff --git a/source/core/textures/texture.cpp b/source/core/textures/texture.cpp index 3b79ddd9d..5f6070fe6 100644 --- a/source/core/textures/texture.cpp +++ b/source/core/textures/texture.cpp @@ -41,6 +41,7 @@ #include "image.h" #include "palette.h" #include "../glbackend/gl_hwtexture.h" +#include "build.h" FTexture *CreateBrightmapTexture(FImageSource*); @@ -207,7 +208,7 @@ void FTexture::CheckTrans(unsigned char * buffer, int size, int trans) #define SOME_MASK 0x00ffffff #endif -#define CHKPIX(ofs) (l1[(ofs)*4+MSB]==255 ? (( ((uint32_t*)l1)[0] = ((uint32_t*)l1)[ofs]&SOME_MASK), trans=true ) : false) +#define CHKPIX(ofs) (l1[(ofs)*4+MSB]==TRANSPARENT_INDEX ? (( ((uint32_t*)l1)[0] = ((uint32_t*)l1)[ofs]&SOME_MASK), trans=true ) : false) int FTexture::SmoothEdges(unsigned char * buffer, int w, int h) { diff --git a/source/rr/src/rrdh.cpp b/source/rr/src/rrdh.cpp index 834919592..d501e2a2a 100644 --- a/source/rr/src/rrdh.cpp +++ b/source/rr/src/rrdh.cpp @@ -1871,7 +1871,7 @@ void sub_54FA4(int a1, int a2) { for (j = 0; j < dword_AA3AC; j++) { - if (*ptr1 != 255) + if (*ptr1 != TRANSPARENT_INDEX) { *ptr1 = ptr2[i*bytesperline+dword_AA3B0+j]; }