moreupdates
[synfig.git] / synfig-core / trunk / src / synfig / gamma.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file gamma.h
3 **      \brief Template Header
4 **
5 **      $Id: gamma.h,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __SYNFIG_GAMMA_H
25 #define __SYNFIG_GAMMA_H
26
27 /* === H E A D E R S ======================================================= */
28
29 #include <cmath>
30
31 /* === M A C R O S ========================================================= */
32
33 /* === T Y P E D E F S ===================================================== */
34
35 /* === C L A S S E S & S T R U C T S ======================================= */
36
37 namespace synfig {
38
39 /*!     \class Gamma
40 **      \brief This class performs color correction on Color classes.
41 **      \stub
42 */
43 class Gamma
44 {
45         float gamma_r;
46         float gamma_g;
47         float gamma_b;
48         float black_level;
49         float red_blue_level;
50         
51         unsigned char table_r_U16_to_U8[65536];
52         unsigned char table_g_U16_to_U8[65536];
53         unsigned char table_b_U16_to_U8[65536];
54
55         float table_r_U8_to_F32[256];
56         float table_g_U8_to_F32[256];
57         float table_b_U8_to_F32[256];
58         
59 public:
60         Gamma(float x=1):black_level(0) { set_gamma(x); }
61
62         void set_gamma(float x);
63         void set_gamma_r(float x);
64         void set_gamma_g(float x);
65         void set_gamma_b(float x);
66         void set_black_level(float x);
67
68         void set_red_blue_level(float x);
69         void set_all(float r, float g, float b, float black, float red_blue=1.0f);
70
71         float get_gamma()const { return (gamma_r+gamma_g+gamma_b)*0.33333333; }
72         float get_gamma_r()const { return gamma_r; }
73         float get_gamma_g()const { return gamma_g; }
74         float get_gamma_b()const { return gamma_b; }
75         float get_black_level()const { return black_level; }
76         float get_red_blue_level()const { return red_blue_level; }
77         
78         void refresh_gamma_r();
79         void refresh_gamma_g();
80         void refresh_gamma_b();
81         
82         const unsigned char &r_U16_to_U8(int i)const { return table_r_U16_to_U8[i]; }
83         const unsigned char &g_U16_to_U8(int i)const { return table_g_U16_to_U8[i]; }
84         const unsigned char &b_U16_to_U8(int i)const { return table_b_U16_to_U8[i]; }
85
86         const unsigned char &r_F32_to_U8(float x)const { return table_r_U16_to_U8[(int)(x*65535.0f)]; }
87         const unsigned char &g_F32_to_U8(float x)const { return table_g_U16_to_U8[(int)(x*65535.0f)]; }
88         const unsigned char &b_F32_to_U8(float x)const { return table_b_U16_to_U8[(int)(x*65535.0f)]; }
89
90         unsigned short r_F32_to_U16(float x)const { return (unsigned short)table_r_U16_to_U8[(int)(x*65535.0f)]<<8; }
91         unsigned short g_F32_to_U16(float x)const { return (unsigned short)table_g_U16_to_U8[(int)(x*65535.0f)]<<8; }
92         unsigned short b_F32_to_U16(float x)const { return (unsigned short)table_b_U16_to_U8[(int)(x*65535.0f)]<<8; }
93
94         const float& r_U8_to_F32(int i)const { return table_r_U8_to_F32[i]; }
95         const float& g_U8_to_F32(int i)const { return table_g_U8_to_F32[i]; }
96         const float& b_U8_to_F32(int i)const { return table_b_U8_to_F32[i]; }
97
98         float r_F32_to_F32(float x)const { return static_cast<float>(pow(x,gamma_r)*(1.0f-black_level)+black_level); }
99         float g_F32_to_F32(float x)const { return static_cast<float>(pow(x,gamma_g)*(1.0f-black_level)+black_level); }
100         float b_F32_to_F32(float x)const { return static_cast<float>(pow(x,gamma_b)*(1.0f-black_level)+black_level); }
101 }; // END of class Gamma
102         
103 }; // END of namespace synfig
104
105 /* === E N D =============================================================== */
106
107 #endif