synfig-core  1.0.3
gamma.h
Go to the documentation of this file.
1 /* === S Y N F I G ========================================================= */
21 /* ========================================================================= */
22 
23 /* === S T A R T =========================================================== */
24 
25 #ifndef __SYNFIG_GAMMA_H
26 #define __SYNFIG_GAMMA_H
27 
28 /* === H E A D E R S ======================================================= */
29 
30 #include <cmath>
31 
32 /* === M A C R O S ========================================================= */
33 
34 /* === T Y P E D E F S ===================================================== */
35 
36 /* === C L A S S E S & S T R U C T S ======================================= */
37 
38 namespace synfig {
39 
44 class Gamma
45 {
46  float gamma_r;
47  float gamma_g;
48  float gamma_b;
49  float black_level;
50  float red_blue_level;
51 
52  unsigned char table_r_U16_to_U8[65536];
53  unsigned char table_g_U16_to_U8[65536];
54  unsigned char table_b_U16_to_U8[65536];
55 
56  float table_r_U8_to_F32[256];
57  float table_g_U8_to_F32[256];
58  float table_b_U8_to_F32[256];
59 
60 public:
61  Gamma(float x=1):black_level(0) { set_gamma(x); }
62 
63  void set_gamma(float x);
64  void set_gamma_r(float x);
65  void set_gamma_g(float x);
66  void set_gamma_b(float x);
67  void set_black_level(float x);
68 
69  void set_red_blue_level(float x);
70  void set_all(float r, float g, float b, float black, float red_blue=1.0f);
71 
72  float get_gamma()const { return (gamma_r+gamma_g+gamma_b)*0.33333333; }
73  float get_gamma_r()const { return gamma_r; }
74  float get_gamma_g()const { return gamma_g; }
75  float get_gamma_b()const { return gamma_b; }
76  float get_black_level()const { return black_level; }
77  float get_red_blue_level()const { return red_blue_level; }
78 
79  void refresh_gamma_r();
80  void refresh_gamma_g();
81  void refresh_gamma_b();
82 
83  const unsigned char &r_U16_to_U8(int i)const { return table_r_U16_to_U8[i]; }
84  const unsigned char &g_U16_to_U8(int i)const { return table_g_U16_to_U8[i]; }
85  const unsigned char &b_U16_to_U8(int i)const { return table_b_U16_to_U8[i]; }
86 
87  const unsigned char &r_F32_to_U8(float x)const { return table_r_U16_to_U8[(int)(x*65535.0f)]; }
88  const unsigned char &g_F32_to_U8(float x)const { return table_g_U16_to_U8[(int)(x*65535.0f)]; }
89  const unsigned char &b_F32_to_U8(float x)const { return table_b_U16_to_U8[(int)(x*65535.0f)]; }
90 
91  unsigned short r_F32_to_U16(float x)const { return (unsigned short)table_r_U16_to_U8[(int)(x*65535.0f)]<<8; }
92  unsigned short g_F32_to_U16(float x)const { return (unsigned short)table_g_U16_to_U8[(int)(x*65535.0f)]<<8; }
93  unsigned short b_F32_to_U16(float x)const { return (unsigned short)table_b_U16_to_U8[(int)(x*65535.0f)]<<8; }
94 
95  const float& r_U8_to_F32(int i)const { return table_r_U8_to_F32[i]; }
96  const float& g_U8_to_F32(int i)const { return table_g_U8_to_F32[i]; }
97  const float& b_U8_to_F32(int i)const { return table_b_U8_to_F32[i]; }
98 
99  float r_F32_to_F32(float x)const { return static_cast<float>(pow(x,gamma_r)*(1.0f-black_level)+black_level); }
100  float g_F32_to_F32(float x)const { return static_cast<float>(pow(x,gamma_g)*(1.0f-black_level)+black_level); }
101  float b_F32_to_F32(float x)const { return static_cast<float>(pow(x,gamma_b)*(1.0f-black_level)+black_level); }
102 }; // END of class Gamma
103 
104 }; // END of namespace synfig
105 
106 /* === E N D =============================================================== */
107 
108 #endif