1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 //          mcseemagg@yahoo.com
13 //          http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 
16 #ifndef AGG_GAMMA_FUNCTIONS_INCLUDED
17 #define AGG_GAMMA_FUNCTIONS_INCLUDED
18 
19 #include <math.h>
20 #include "agg_basics.h"
21 
22 namespace agg
23 {
24     //===============================================================gamma_none
25     struct gamma_none
26     {
operatorgamma_none27         double operator()(double x) const { return x; }
28     };
29 
30 
31     //==============================================================gamma_power
32     class gamma_power
33     {
34     public:
gamma_power()35         gamma_power() : m_gamma(1.0) {}
gamma_power(double g)36         gamma_power(double g) : m_gamma(g) {}
37 
gamma(double g)38         void gamma(double g) { m_gamma = g; }
gamma()39         double gamma() const { return m_gamma; }
40 
operator()41         double operator() (double x) const
42         {
43             return pow(x, m_gamma);
44         }
45 
46     private:
47         double m_gamma;
48     };
49 
50 
51     //==========================================================gamma_threshold
52     class gamma_threshold
53     {
54     public:
gamma_threshold()55         gamma_threshold() : m_threshold(0.5) {}
gamma_threshold(double t)56         gamma_threshold(double t) : m_threshold(t) {}
57 
threshold(double t)58         void threshold(double t) { m_threshold = t; }
threshold()59         double threshold() const { return m_threshold; }
60 
operator()61         double operator() (double x) const
62         {
63             return (x < m_threshold) ? 0.0 : 1.0;
64         }
65 
66     private:
67         double m_threshold;
68     };
69 
70 
71     //============================================================gamma_linear
72     class gamma_linear
73     {
74     public:
gamma_linear()75         gamma_linear() : m_start(0.0), m_end(1.0) {}
gamma_linear(double s,double e)76         gamma_linear(double s, double e) : m_start(s), m_end(e) {}
77 
set(double s,double e)78         void set(double s, double e) { m_start = s; m_end = e; }
start(double s)79         void start(double s) { m_start = s; }
end(double e)80         void end(double e) { m_end = e; }
start()81         double start() const { return m_start; }
end()82         double end() const { return m_end; }
83 
operator()84         double operator() (double x) const
85         {
86             if(x < m_start) return 0.0;
87             if(x > m_end) return 1.0;
88             return (x - m_start) / (m_end - m_start);
89         }
90 
91     private:
92         double m_start;
93         double m_end;
94     };
95 
96 
97     //==========================================================gamma_multiply
98     class gamma_multiply
99     {
100     public:
gamma_multiply()101         gamma_multiply() : m_mul(1.0) {}
gamma_multiply(double v)102         gamma_multiply(double v) : m_mul(v) {}
103 
value(double v)104         void value(double v) { m_mul = v; }
value()105         double value() const { return m_mul; }
106 
operator()107         double operator() (double x) const
108         {
109             double y = x * m_mul;
110             if(y > 1.0) y = 1.0;
111             return y;
112         }
113 
114     private:
115         double m_mul;
116     };
117 
sRGB_to_linear(double x)118     inline double sRGB_to_linear(double x)
119     {
120         return (x <= 0.04045) ? (x / 12.92) : pow((x + 0.055) / (1.055), 2.4);
121     }
122 
linear_to_sRGB(double x)123     inline double linear_to_sRGB(double x)
124     {
125         return (x <= 0.0031308) ? (x * 12.92) : (1.055 * pow(x, 1 / 2.4) - 0.055);
126     }
127 }
128 
129 #endif
130 
131 
132 
133