TinyGPU
Loading...
Searching...
No Matches
GammaTable.h
Go to the documentation of this file.
1#pragma once
2#include <math.h>
3#include <stdint.h>
4
5namespace tinygpu {
6
7/**
8 * @brief Precomputed 8-bit gamma correction lookup table.
9 *
10 * Applies out = pow(in / 255, gamma) * 255 to a channel value. This
11 * compensates for a display's non-linear brightness response - on some
12 * cheap panels, near-black tones show a visible color tint because tiny
13 * per-channel differences (e.g. R=33 vs B=32) get amplified into a
14 * visible hue by the panel's own response curve, even though the exact
15 * same values render correctly at higher, well-saturated brightness.
16 *
17 * There's no single correct gamma value for an unknown/undocumented
18 * panel - try values on both sides of 1.0 and compare on real hardware:
19 * gamma < 1.0 lifts (brightens) dark input values before they reach the
20 * panel; gamma > 1.0 compresses them further. 1.0 (the default) applies
21 * no correction.
22 */
24 public:
26 explicit GammaTable(float gamma) { setGamma(gamma); }
27
28 void setGamma(float gamma) {
29 gamma_ = gamma;
30 for (int i = 0; i < 256; ++i) {
31 if (gamma == 1.0f) {
32 table_[i] = static_cast<uint8_t>(i);
33 continue;
34 }
35 const float normalized = static_cast<float>(i) / 255.0f;
36 const float corrected = powf(normalized, gamma);
37 int value = static_cast<int>(corrected * 255.0f + 0.5f);
38 if (value < 0) value = 0;
39 if (value > 255) value = 255;
40 table_[i] = static_cast<uint8_t>(value);
41 }
42 }
43
44 float gamma() const { return gamma_; }
45
46 uint8_t apply(uint8_t value) const { return table_[value]; }
47
48 protected:
49 float gamma_ = 1.0f;
50 uint8_t table_[256];
51};
52
53} // namespace tinygpu
Precomputed 8-bit gamma correction lookup table.
Definition: GammaTable.h:23
float gamma_
Definition: GammaTable.h:49
GammaTable(float gamma)
Definition: GammaTable.h:26
uint8_t table_[256]
Definition: GammaTable.h:50
void setGamma(float gamma)
Definition: GammaTable.h:28
float gamma() const
Definition: GammaTable.h:44
GammaTable()
Definition: GammaTable.h:25
uint8_t apply(uint8_t value) const
Definition: GammaTable.h:46
Definition: DSIBusESP32.h:19