xref: /haiku/headers/libs/agg/agg_gamma_lut.h (revision e39da397f5ff79f2db9f9a3ddf1852b6710578af)
139241fe2SDarkWyrm //----------------------------------------------------------------------------
2*e39da397SStephan Aßmus // Anti-Grain Geometry - Version 2.4
3*e39da397SStephan Aßmus // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
439241fe2SDarkWyrm //
539241fe2SDarkWyrm // Permission to copy, use, modify, sell and distribute this software
639241fe2SDarkWyrm // is granted provided this copyright notice appears in all copies.
739241fe2SDarkWyrm // This software is provided "as is" without express or implied
839241fe2SDarkWyrm // warranty, and with no claim as to its suitability for any purpose.
939241fe2SDarkWyrm //
1039241fe2SDarkWyrm //----------------------------------------------------------------------------
1139241fe2SDarkWyrm // Contact: mcseem@antigrain.com
1239241fe2SDarkWyrm //          mcseemagg@yahoo.com
1339241fe2SDarkWyrm //          http://www.antigrain.com
1439241fe2SDarkWyrm //----------------------------------------------------------------------------
1539241fe2SDarkWyrm 
1639241fe2SDarkWyrm #ifndef AGG_GAMMA_LUT_INCLUDED
1739241fe2SDarkWyrm #define AGG_GAMMA_LUT_INCLUDED
1839241fe2SDarkWyrm 
1939241fe2SDarkWyrm #include <math.h>
2039241fe2SDarkWyrm #include "agg_basics.h"
2139241fe2SDarkWyrm 
2239241fe2SDarkWyrm namespace agg
2339241fe2SDarkWyrm {
2439241fe2SDarkWyrm     template<class LoResT=int8u,
2539241fe2SDarkWyrm              class HiResT=int8u,
2639241fe2SDarkWyrm              unsigned GammaShift=8,
2739241fe2SDarkWyrm              unsigned HiResShift=8> class gamma_lut
2839241fe2SDarkWyrm     {
2939241fe2SDarkWyrm     public:
30*e39da397SStephan Aßmus         typedef gamma_lut<LoResT, HiResT, GammaShift, HiResShift> self_type;
31*e39da397SStephan Aßmus 
32*e39da397SStephan Aßmus         enum gamma_scale_e
3339241fe2SDarkWyrm         {
3439241fe2SDarkWyrm             gamma_shift = GammaShift,
3539241fe2SDarkWyrm             gamma_size  = 1 << gamma_shift,
3639241fe2SDarkWyrm             gamma_mask  = gamma_size - 1
3739241fe2SDarkWyrm         };
3839241fe2SDarkWyrm 
39*e39da397SStephan Aßmus         enum hi_res_scale_e
4039241fe2SDarkWyrm         {
4139241fe2SDarkWyrm             hi_res_shift = HiResShift,
4239241fe2SDarkWyrm             hi_res_size  = 1 << hi_res_shift,
4339241fe2SDarkWyrm             hi_res_mask  = hi_res_size - 1
4439241fe2SDarkWyrm         };
4539241fe2SDarkWyrm 
~gamma_lut()4639241fe2SDarkWyrm         ~gamma_lut()
4739241fe2SDarkWyrm         {
48*e39da397SStephan Aßmus             pod_allocator<LoResT>::deallocate(m_inv_gamma, hi_res_size);
49*e39da397SStephan Aßmus             pod_allocator<HiResT>::deallocate(m_dir_gamma, gamma_size);
5039241fe2SDarkWyrm         }
5139241fe2SDarkWyrm 
gamma_lut()5239241fe2SDarkWyrm         gamma_lut() :
5339241fe2SDarkWyrm             m_gamma(1.0),
54*e39da397SStephan Aßmus             m_dir_gamma(pod_allocator<HiResT>::allocate(gamma_size)),
55*e39da397SStephan Aßmus             m_inv_gamma(pod_allocator<LoResT>::allocate(hi_res_size))
5639241fe2SDarkWyrm         {
5739241fe2SDarkWyrm             unsigned i;
5839241fe2SDarkWyrm             for(i = 0; i < gamma_size; i++)
5939241fe2SDarkWyrm             {
6039241fe2SDarkWyrm                 m_dir_gamma[i] = HiResT(i << (hi_res_shift - gamma_shift));
6139241fe2SDarkWyrm             }
6239241fe2SDarkWyrm 
6339241fe2SDarkWyrm             for(i = 0; i < hi_res_size; i++)
6439241fe2SDarkWyrm             {
6539241fe2SDarkWyrm                 m_inv_gamma[i] = LoResT(i >> (hi_res_shift - gamma_shift));
6639241fe2SDarkWyrm             }
6739241fe2SDarkWyrm         }
6839241fe2SDarkWyrm 
gamma_lut(double g)6939241fe2SDarkWyrm         gamma_lut(double g) :
7039241fe2SDarkWyrm             m_gamma(1.0),
71*e39da397SStephan Aßmus             m_dir_gamma(pod_allocator<HiResT>::allocate(gamma_size)),
72*e39da397SStephan Aßmus             m_inv_gamma(pod_allocator<LoResT>::allocate(hi_res_size))
7339241fe2SDarkWyrm         {
7439241fe2SDarkWyrm             gamma(g);
7539241fe2SDarkWyrm         }
7639241fe2SDarkWyrm 
gamma(double g)7739241fe2SDarkWyrm         void gamma(double g)
7839241fe2SDarkWyrm         {
7939241fe2SDarkWyrm             m_gamma = g;
8039241fe2SDarkWyrm 
8139241fe2SDarkWyrm             unsigned i;
8239241fe2SDarkWyrm             for(i = 0; i < gamma_size; i++)
8339241fe2SDarkWyrm             {
84*e39da397SStephan Aßmus                 m_dir_gamma[i] = (HiResT)
85*e39da397SStephan Aßmus                     uround(pow(i / double(gamma_mask), m_gamma) * double(hi_res_mask));
8639241fe2SDarkWyrm             }
8739241fe2SDarkWyrm 
8839241fe2SDarkWyrm             double inv_g = 1.0 / g;
8939241fe2SDarkWyrm             for(i = 0; i < hi_res_size; i++)
9039241fe2SDarkWyrm             {
91*e39da397SStephan Aßmus                 m_inv_gamma[i] = (LoResT)
92*e39da397SStephan Aßmus                     uround(pow(i / double(hi_res_mask), inv_g) * double(gamma_mask));
9339241fe2SDarkWyrm             }
9439241fe2SDarkWyrm         }
9539241fe2SDarkWyrm 
gamma()9639241fe2SDarkWyrm         double gamma() const
9739241fe2SDarkWyrm         {
9839241fe2SDarkWyrm             return m_gamma;
9939241fe2SDarkWyrm         }
10039241fe2SDarkWyrm 
dir(LoResT v)10139241fe2SDarkWyrm         HiResT dir(LoResT v) const
10239241fe2SDarkWyrm         {
10339241fe2SDarkWyrm             return m_dir_gamma[unsigned(v)];
10439241fe2SDarkWyrm         }
10539241fe2SDarkWyrm 
inv(HiResT v)10639241fe2SDarkWyrm         LoResT inv(HiResT v) const
10739241fe2SDarkWyrm         {
10839241fe2SDarkWyrm             return m_inv_gamma[unsigned(v)];
10939241fe2SDarkWyrm         }
11039241fe2SDarkWyrm 
11139241fe2SDarkWyrm     private:
112*e39da397SStephan Aßmus         gamma_lut(const self_type&);
113*e39da397SStephan Aßmus         const self_type& operator = (const self_type&);
114*e39da397SStephan Aßmus 
11539241fe2SDarkWyrm         double m_gamma;
11639241fe2SDarkWyrm         HiResT* m_dir_gamma;
11739241fe2SDarkWyrm         LoResT* m_inv_gamma;
11839241fe2SDarkWyrm     };
11939241fe2SDarkWyrm }
12039241fe2SDarkWyrm 
12139241fe2SDarkWyrm #endif
122