Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc1 / src / synfig / gamma.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file gamma.h
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
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
40 /*!     \class Gamma
41 **      \brief This class performs color correction on Color classes.
42 **      \stub
43 */
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