diff --git a/src/bitmap.cpp b/src/bitmap.cpp index ce59cc35a99..8c01a602470 100644 --- a/src/bitmap.cpp +++ b/src/bitmap.cpp @@ -18,6 +18,7 @@ // Headers #define _USE_MATH_DEFINES #include +#include #include #include #include @@ -803,9 +804,13 @@ void Bitmap::ClearRect(Rect const& dst_rect) { } // Hard light lookup table mapping source color to destination color -static uint8_t hard_light_lookup[256][256]; +// FIXME: Replace this with std::array,256> when we have C++17 +struct HardLightTable { + uint8_t table[256][256] = {}; +}; -static void make_hard_light_lookup() { +static constexpr HardLightTable make_hard_light_lookup() { + HardLightTable hl; for (int i = 0; i < 256; ++i) { for (int j = 0; j < 256; ++j) { int res = 0; @@ -813,11 +818,14 @@ static void make_hard_light_lookup() { res = (2 * i * j) / 255; else res = 255 - 2 * (255 - i) * (255 - j) / 255; - hard_light_lookup[i][j] = res > 255 ? 255 : res < 0 ? 0 : res; + hl.table[i][j] = res > 255 ? 255 : res < 0 ? 0 : res; } } + return hl; } +constexpr auto hard_light = make_hard_light_lookup(); + // Saturation Tone Inline: Changes a pixel saturation static inline void saturation_tone(uint32_t &src_pixel, int saturation, int rs, int gs, int bs, int as) { // Algorithm from OpenPDN (MIT license) @@ -842,10 +850,10 @@ static inline void saturation_tone(uint32_t &src_pixel, int saturation, int rs, } // Color Tone Inline: Changes color of a pixel by hard light table -static inline void color_tone(uint32_t &src_pixel, Tone tone, uint8_t hard_light_lookup[256][256], int rs, int gs, int bs, int as) { - src_pixel = ((uint32_t)hard_light_lookup[tone.red][(src_pixel >> rs) & 0xFF] << rs) - | ((uint32_t)hard_light_lookup[tone.green][(src_pixel >> gs) & 0xFF] << gs) - | ((uint32_t)hard_light_lookup[tone.blue][(src_pixel >> bs) & 0xFF] << bs) +static inline void color_tone(uint32_t &src_pixel, Tone tone, int rs, int gs, int bs, int as) { + src_pixel = ((uint32_t)hard_light.table[tone.red][(src_pixel >> rs) & 0xFF] << rs) + | ((uint32_t)hard_light.table[tone.green][(src_pixel >> gs) & 0xFF] << gs) + | ((uint32_t)hard_light.table[tone.blue][(src_pixel >> bs) & 0xFF] << bs) | ((uint32_t)((src_pixel >> as) & 0xFF) << as); } @@ -870,13 +878,6 @@ void Bitmap::ToneBlit(int x, int y, Bitmap const& src, Rect const& src_rect, con x, y, src_rect.width, src_rect.height); - // To implement Saturation and Color: - static bool index_made = false; - if (!index_made) { - make_hard_light_lookup(); - index_made = true; - } - int as = pixel_format.a.shift; int rs = pixel_format.r.shift; int gs = pixel_format.g.shift; @@ -900,7 +901,7 @@ void Bitmap::ToneBlit(int x, int y, Bitmap const& src, Rect const& src_rect, con continue; saturation_tone(pixels[j], sat, rs, gs, bs, as); - color_tone(pixels[j], tone, hard_light_lookup, rs, gs, bs, as); + color_tone(pixels[j], tone, rs, gs, bs, as); } } } @@ -909,7 +910,7 @@ void Bitmap::ToneBlit(int x, int y, Bitmap const& src, Rect const& src_rect, con pixels += next_row; for (uint16_t j = 0; j < limit_width; ++j) { saturation_tone(pixels[j], sat, rs, gs, bs, as); - color_tone(pixels[j], tone, hard_light_lookup, rs, gs, bs, as); + color_tone(pixels[j], tone, rs, gs, bs, as); } } } @@ -949,7 +950,7 @@ void Bitmap::ToneBlit(int x, int y, Bitmap const& src, Rect const& src_rect, con if ((uint8_t)((pixels[j] >> as) & 0xFF) == 0) continue; - color_tone(pixels[j], tone, hard_light_lookup, rs, gs, bs, as); + color_tone(pixels[j], tone, rs, gs, bs, as); } } } @@ -957,7 +958,7 @@ void Bitmap::ToneBlit(int x, int y, Bitmap const& src, Rect const& src_rect, con for (uint16_t i = 0; i < limit_height; ++i) { pixels += next_row; for (uint16_t j = 0; j < limit_width; ++j) { - color_tone(pixels[j], tone, hard_light_lookup, rs, gs, bs, as); + color_tone(pixels[j], tone, rs, gs, bs, as); } } }