xref: /haiku/headers/libs/agg/agg_gamma_lut.h (revision 39241fe22890fb958b6ba32d6ab9526da98be187)
1*39241fe2SDarkWyrm //----------------------------------------------------------------------------
2*39241fe2SDarkWyrm // Anti-Grain Geometry - Version 2.2
3*39241fe2SDarkWyrm // Copyright (C) 2002-2004 Maxim Shemanarev (http://www.antigrain.com)
4*39241fe2SDarkWyrm //
5*39241fe2SDarkWyrm // Permission to copy, use, modify, sell and distribute this software
6*39241fe2SDarkWyrm // is granted provided this copyright notice appears in all copies.
7*39241fe2SDarkWyrm // This software is provided "as is" without express or implied
8*39241fe2SDarkWyrm // warranty, and with no claim as to its suitability for any purpose.
9*39241fe2SDarkWyrm //
10*39241fe2SDarkWyrm //----------------------------------------------------------------------------
11*39241fe2SDarkWyrm // Contact: mcseem@antigrain.com
12*39241fe2SDarkWyrm //          mcseemagg@yahoo.com
13*39241fe2SDarkWyrm //          http://www.antigrain.com
14*39241fe2SDarkWyrm //----------------------------------------------------------------------------
15*39241fe2SDarkWyrm 
16*39241fe2SDarkWyrm #ifndef AGG_GAMMA_LUT_INCLUDED
17*39241fe2SDarkWyrm #define AGG_GAMMA_LUT_INCLUDED
18*39241fe2SDarkWyrm 
19*39241fe2SDarkWyrm #include <math.h>
20*39241fe2SDarkWyrm #include "agg_basics.h"
21*39241fe2SDarkWyrm 
22*39241fe2SDarkWyrm namespace agg
23*39241fe2SDarkWyrm {
24*39241fe2SDarkWyrm     template<class LoResT=int8u,
25*39241fe2SDarkWyrm              class HiResT=int8u,
26*39241fe2SDarkWyrm              unsigned GammaShift=8,
27*39241fe2SDarkWyrm              unsigned HiResShift=8> class gamma_lut
28*39241fe2SDarkWyrm     {
29*39241fe2SDarkWyrm     public:
30*39241fe2SDarkWyrm         enum
31*39241fe2SDarkWyrm         {
32*39241fe2SDarkWyrm             gamma_shift = GammaShift,
33*39241fe2SDarkWyrm             gamma_size  = 1 << gamma_shift,
34*39241fe2SDarkWyrm             gamma_mask  = gamma_size - 1
35*39241fe2SDarkWyrm         };
36*39241fe2SDarkWyrm 
37*39241fe2SDarkWyrm         enum
38*39241fe2SDarkWyrm         {
39*39241fe2SDarkWyrm             hi_res_shift = HiResShift,
40*39241fe2SDarkWyrm             hi_res_size  = 1 << hi_res_shift,
41*39241fe2SDarkWyrm             hi_res_mask  = hi_res_size - 1
42*39241fe2SDarkWyrm         };
43*39241fe2SDarkWyrm 
44*39241fe2SDarkWyrm         ~gamma_lut()
45*39241fe2SDarkWyrm         {
46*39241fe2SDarkWyrm             delete [] m_inv_gamma;
47*39241fe2SDarkWyrm             delete [] m_dir_gamma;
48*39241fe2SDarkWyrm         }
49*39241fe2SDarkWyrm 
50*39241fe2SDarkWyrm         gamma_lut() :
51*39241fe2SDarkWyrm             m_gamma(1.0),
52*39241fe2SDarkWyrm             m_dir_gamma(new HiResT[gamma_size]),
53*39241fe2SDarkWyrm             m_inv_gamma(new LoResT[hi_res_size])
54*39241fe2SDarkWyrm         {
55*39241fe2SDarkWyrm             unsigned i;
56*39241fe2SDarkWyrm             for(i = 0; i < gamma_size; i++)
57*39241fe2SDarkWyrm             {
58*39241fe2SDarkWyrm                 m_dir_gamma[i] = HiResT(i << (hi_res_shift - gamma_shift));
59*39241fe2SDarkWyrm             }
60*39241fe2SDarkWyrm 
61*39241fe2SDarkWyrm             for(i = 0; i < hi_res_size; i++)
62*39241fe2SDarkWyrm             {
63*39241fe2SDarkWyrm                 m_inv_gamma[i] = LoResT(i >> (hi_res_shift - gamma_shift));
64*39241fe2SDarkWyrm             }
65*39241fe2SDarkWyrm         }
66*39241fe2SDarkWyrm 
67*39241fe2SDarkWyrm         gamma_lut(double g) :
68*39241fe2SDarkWyrm             m_gamma(1.0),
69*39241fe2SDarkWyrm             m_dir_gamma(new HiResT[gamma_size]),
70*39241fe2SDarkWyrm             m_inv_gamma(new LoResT[hi_res_size])
71*39241fe2SDarkWyrm         {
72*39241fe2SDarkWyrm             gamma(g);
73*39241fe2SDarkWyrm         }
74*39241fe2SDarkWyrm 
75*39241fe2SDarkWyrm         void gamma(double g)
76*39241fe2SDarkWyrm         {
77*39241fe2SDarkWyrm             m_gamma = g;
78*39241fe2SDarkWyrm 
79*39241fe2SDarkWyrm             unsigned i;
80*39241fe2SDarkWyrm             for(i = 0; i < gamma_size; i++)
81*39241fe2SDarkWyrm             {
82*39241fe2SDarkWyrm                 m_dir_gamma[i] = (HiResT)(pow(double(i) / double(gamma_mask), m_gamma) * double(hi_res_mask) + 0.5);
83*39241fe2SDarkWyrm             }
84*39241fe2SDarkWyrm 
85*39241fe2SDarkWyrm             double inv_g = 1.0 / g;
86*39241fe2SDarkWyrm             for(i = 0; i < hi_res_size; i++)
87*39241fe2SDarkWyrm             {
88*39241fe2SDarkWyrm                 m_inv_gamma[i] = (LoResT)(pow(double(i) / double(hi_res_mask), inv_g) * double(gamma_mask) + 0.5);
89*39241fe2SDarkWyrm             }
90*39241fe2SDarkWyrm         }
91*39241fe2SDarkWyrm 
92*39241fe2SDarkWyrm         double gamma() const
93*39241fe2SDarkWyrm         {
94*39241fe2SDarkWyrm             return m_gamma;
95*39241fe2SDarkWyrm         }
96*39241fe2SDarkWyrm 
97*39241fe2SDarkWyrm         HiResT dir(LoResT v) const
98*39241fe2SDarkWyrm         {
99*39241fe2SDarkWyrm             return m_dir_gamma[unsigned(v)];
100*39241fe2SDarkWyrm         }
101*39241fe2SDarkWyrm 
102*39241fe2SDarkWyrm         LoResT inv(HiResT v) const
103*39241fe2SDarkWyrm         {
104*39241fe2SDarkWyrm             return m_inv_gamma[unsigned(v)];
105*39241fe2SDarkWyrm         }
106*39241fe2SDarkWyrm 
107*39241fe2SDarkWyrm     private:
108*39241fe2SDarkWyrm         double m_gamma;
109*39241fe2SDarkWyrm         HiResT* m_dir_gamma;
110*39241fe2SDarkWyrm         LoResT* m_inv_gamma;
111*39241fe2SDarkWyrm     };
112*39241fe2SDarkWyrm }
113*39241fe2SDarkWyrm 
114*39241fe2SDarkWyrm #endif
115