diff --git a/src/gl/renderer/gl_postprocess.cpp b/src/gl/renderer/gl_postprocess.cpp index 43f7458eb..a19e90ef1 100644 --- a/src/gl/renderer/gl_postprocess.cpp +++ b/src/gl/renderer/gl_postprocess.cpp @@ -601,7 +601,8 @@ void FGLRenderer::CreateTonemapPalette() { for (int b = 0; b < 64; b++) { - PalEntry color = GPalette.BaseColors[(uint8_t)PTM_BestColor((uint32_t *)GPalette.BaseColors, (r << 2) | (r >> 4), (g << 2) | (g >> 4), (b << 2) | (b >> 4), 0, 256)]; + PalEntry color = GPalette.BaseColors[(uint8_t)PTM_BestColor((uint32_t *)GPalette.BaseColors, (r << 2) | (r >> 4), (g << 2) | (g >> 4), (b << 2) | (b >> 4), + gl_paltonemap_reverselookup, gl_paltonemap_powtable, 0, 256)]; int index = ((r * 64 + g) * 64 + b) * 4; lut[index] = color.b; lut[index + 1] = color.g; @@ -914,41 +915,3 @@ void FGLRenderer::ClearBorders() glDisable(GL_SCISSOR_TEST); } - -// [SP] Re-implemented BestColor for more precision rather than speed. This function is only ever called once until the game palette is changed. - -int FGLRenderer::PTM_BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num) -{ - const PalEntry *pal = (const PalEntry *)pal_in; - static double powtable[256]; - static bool firstTime = true; - static float trackpowtable = 0.; - - double fbestdist = DBL_MAX, fdist; - int bestcolor = 0; - - if (firstTime || trackpowtable != gl_paltonemap_powtable) - { - trackpowtable = gl_paltonemap_powtable; - firstTime = false; - for (int x = 0; x < 256; x++) powtable[x] = pow((double)x/255, (double)gl_paltonemap_powtable); - } - - for (int color = first; color < num; color++) - { - double x = powtable[abs(r-pal[color].r)]; - double y = powtable[abs(g-pal[color].g)]; - double z = powtable[abs(b-pal[color].b)]; - fdist = x + y + z; - if (color == first || ((gl_paltonemap_reverselookup)?(fdist <= fbestdist):(fdist < fbestdist))) - { - if (fdist == 0 && !gl_paltonemap_reverselookup) - return color; - - fbestdist = fdist; - bestcolor = color; - } - } - return bestcolor; -} - diff --git a/src/gl/renderer/gl_renderer.h b/src/gl/renderer/gl_renderer.h index ae908523c..19f58836d 100644 --- a/src/gl/renderer/gl_renderer.h +++ b/src/gl/renderer/gl_renderer.h @@ -202,8 +202,6 @@ public: double originx, double originy, double scalex, double scaley, DAngle rotation, const FColormap &colormap, PalEntry flatcolor, int lightlevel, int bottomclip); - int PTM_BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num); - static float GetZNear() { return 5.f; } static float GetZFar() { return 65536.f; } }; diff --git a/src/v_palette.cpp b/src/v_palette.cpp index 6d1114f91..a836da1ed 100644 --- a/src/v_palette.cpp +++ b/src/v_palette.cpp @@ -121,6 +121,48 @@ int BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num) return bestcolor; } + +// [SP] Re-implemented BestColor for more precision rather than speed. This function is only ever called once until the game palette is changed. + +int PTM_BestColor (const uint32_t *pal_in, int r, int g, int b, bool reverselookup, float powtable_val, int first, int num) +{ + const PalEntry *pal = (const PalEntry *)pal_in; + static double powtable[256]; + static bool firstTime = true; + static float trackpowtable = 0.; + + double fbestdist = DBL_MAX, fdist; + int bestcolor = 0; + + if (firstTime || trackpowtable != powtable_val) + { + auto pt = powtable_val; + trackpowtable = pt; + firstTime = false; + for (int x = 0; x < 256; x++) powtable[x] = pow((double)x/255, (double)pt); + } + + for (int color = first; color < num; color++) + { + double x = powtable[abs(r-pal[color].r)]; + double y = powtable[abs(g-pal[color].g)]; + double z = powtable[abs(b-pal[color].b)]; + fdist = x + y + z; + if (color == first || (reverselookup?(fdist <= fbestdist):(fdist < fbestdist))) + { + if (fdist == 0 && !reverselookup) + return color; + + fbestdist = fdist; + bestcolor = color; + } + } + return bestcolor; +} + + + + FPalette::FPalette () { } diff --git a/src/v_palette.h b/src/v_palette.h index 3f8f00039..a9a7e476b 100644 --- a/src/v_palette.h +++ b/src/v_palette.h @@ -71,6 +71,7 @@ extern FPalette GPalette; #define DIM_OVERLAY MAKEARGB(170,0,0,0) int BestColor (const uint32_t *pal, int r, int g, int b, 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 ReadPalette(int lumpnum, uint8_t *buffer);