Skip to content

Commit

Permalink
Bitmap: Make the hard light lookup table constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
fmatthew5876 committed Dec 7, 2019
1 parent e67a1ac commit 6c19a5e
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/bitmap.cpp
Expand Up @@ -18,6 +18,7 @@
// Headers
#define _USE_MATH_DEFINES
#include <cmath>
#include <array>
#include <cstdlib>
#include <cstring>
#include <algorithm>
Expand Down Expand Up @@ -803,21 +804,28 @@ 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<std::array<uint8_t,256>,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;
if (i <= 128)
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)
Expand All @@ -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);
}

Expand All @@ -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;
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -949,15 +950,15 @@ 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);
}
}
}
else {
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);
}
}
}
Expand Down

0 comments on commit 6c19a5e

Please sign in to comment.