aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/matplotlib/py3/extern/agg24-svn/include/agg_gamma_lut.h
blob: ef1e38d8092678fb6f8a436e0a26877c028b73cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software 
// is granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//----------------------------------------------------------------------------

#ifndef AGG_GAMMA_LUT_INCLUDED
#define AGG_GAMMA_LUT_INCLUDED

#include <math.h>
#include "agg_basics.h"
#include "agg_gamma_functions.h"

namespace agg
{
    template<class LoResT=int8u, 
             class HiResT=int8u, 
             unsigned GammaShift=8, 
             unsigned HiResShift=8> class gamma_lut
    {
    public:
        typedef gamma_lut<LoResT, HiResT, GammaShift, HiResShift> self_type;

        enum gamma_scale_e
        {
            gamma_shift = GammaShift,
            gamma_size  = 1 << gamma_shift,
            gamma_mask  = gamma_size - 1
        };

        enum hi_res_scale_e
        {
            hi_res_shift = HiResShift,
            hi_res_size  = 1 << hi_res_shift,
            hi_res_mask  = hi_res_size - 1
        };

        ~gamma_lut()
        {
            pod_allocator<LoResT>::deallocate(m_inv_gamma, hi_res_size);
            pod_allocator<HiResT>::deallocate(m_dir_gamma, gamma_size);
        }

        gamma_lut() : 
            m_gamma(1.0), 
            m_dir_gamma(pod_allocator<HiResT>::allocate(gamma_size)),
            m_inv_gamma(pod_allocator<LoResT>::allocate(hi_res_size))
        {
            unsigned i;
            for(i = 0; i < gamma_size; i++)
            {
                m_dir_gamma[i] = HiResT(i << (hi_res_shift - gamma_shift));
            }

            for(i = 0; i < hi_res_size; i++)
            {
                m_inv_gamma[i] = LoResT(i >> (hi_res_shift - gamma_shift));
            }
        }

        gamma_lut(double g) :
            m_gamma(1.0), 
            m_dir_gamma(pod_allocator<HiResT>::allocate(gamma_size)),
            m_inv_gamma(pod_allocator<LoResT>::allocate(hi_res_size))
        {
            gamma(g);
        }

        void gamma(double g) 
        {
            m_gamma = g;

            unsigned i;
            for(i = 0; i < gamma_size; i++)
            {
                m_dir_gamma[i] = (HiResT)
                    uround(pow(i / double(gamma_mask), m_gamma) * double(hi_res_mask));
            }

            double inv_g = 1.0 / g;
            for(i = 0; i < hi_res_size; i++)
            {
                m_inv_gamma[i] = (LoResT)
                    uround(pow(i / double(hi_res_mask), inv_g) * double(gamma_mask));
            }
        }

        double gamma() const
        {
            return m_gamma;
        }

        HiResT dir(LoResT v) const 
        { 
            return m_dir_gamma[unsigned(v)]; 
        }

        LoResT inv(HiResT v) const 
        { 
            return m_inv_gamma[unsigned(v)];
        }

    private:
        gamma_lut(const self_type&);
        const self_type& operator = (const self_type&);

        double m_gamma;
        HiResT* m_dir_gamma;
        LoResT* m_inv_gamma;
    };

    //
    // sRGB support classes
    //

    // Optimized sRGB lookup table. The direct conversion (sRGB to linear) 
    // is a straightforward lookup. The inverse conversion (linear to sRGB) 
    // is implemented using binary search.
    template<class LinearType>
    class sRGB_lut_base
    {
    public:
        LinearType dir(int8u v) const
        {
            return m_dir_table[v];
        }

        int8u inv(LinearType v) const
        {
            // Unrolled binary search.
            int8u x = 0;
            if (v > m_inv_table[128]) x = 128;
            if (v > m_inv_table[x + 64]) x += 64;
            if (v > m_inv_table[x + 32]) x += 32;
            if (v > m_inv_table[x + 16]) x += 16;
            if (v > m_inv_table[x + 8]) x += 8;
            if (v > m_inv_table[x + 4]) x += 4;
            if (v > m_inv_table[x + 2]) x += 2;
            if (v > m_inv_table[x + 1]) x += 1;
            return x;
        }

    protected:
        LinearType m_dir_table[256];
        LinearType m_inv_table[256];

        // Only derived classes may instantiate.
        sRGB_lut_base() 
        {
        }
    };

    // sRGB_lut - implements sRGB conversion for the various types.
    // Base template is undefined, specializations are provided below.
    template<class LinearType>
    class sRGB_lut;

    template<>
    class sRGB_lut<float> : public sRGB_lut_base<float>
    {
    public:
        sRGB_lut()
        {
            // Generate lookup tables.
            m_dir_table[0] = 0;
            m_inv_table[0] = 0;
            for (unsigned i = 1; i <= 255; ++i)
            {
                // Floating-point RGB is in range [0,1].
                m_dir_table[i] = float(sRGB_to_linear(i / 255.0));
                m_inv_table[i] = float(sRGB_to_linear((i - 0.5) / 255.0));
            }
        }
    };

    template<>
    class sRGB_lut<int16u> : public sRGB_lut_base<int16u>
    {
    public:
        sRGB_lut()
        {
            // Generate lookup tables.
            m_dir_table[0] = 0;
            m_inv_table[0] = 0;
            for (unsigned i = 1; i <= 255; ++i)
            {
                // 16-bit RGB is in range [0,65535].
                m_dir_table[i] = uround(65535.0 * sRGB_to_linear(i / 255.0));
                m_inv_table[i] = uround(65535.0 * sRGB_to_linear((i - 0.5) / 255.0));
            }
        }
    };

    template<>
    class sRGB_lut<int8u> : public sRGB_lut_base<int8u>
    {
    public:
        sRGB_lut()
        {
            // Generate lookup tables. 
            m_dir_table[0] = 0;
            m_inv_table[0] = 0;
            for (unsigned i = 1; i <= 255; ++i)
            {
                // 8-bit RGB is handled with simple bidirectional lookup tables.
                m_dir_table[i] = uround(255.0 * sRGB_to_linear(i / 255.0));
                m_inv_table[i] = uround(255.0 * linear_to_sRGB(i / 255.0));
            }
        }

        int8u inv(int8u v) const
        {
            // In this case, the inverse transform is a simple lookup.
            return m_inv_table[v];
        }
    };

    // Common base class for sRGB_conv objects. Defines an internal 
    // sRGB_lut object so that users don't have to.
    template<class T>
    class sRGB_conv_base
    {
    public:
        static T rgb_from_sRGB(int8u x)
        {
            return lut.dir(x);
        }

        static int8u rgb_to_sRGB(T x)
        {
            return lut.inv(x);
        }

    private:
        static sRGB_lut<T> lut;
    };

    // Definition of sRGB_conv_base::lut. Due to the fact that this a template, 
    // we don't need to place the definition in a cpp file. Hurrah.
    template<class T>
    sRGB_lut<T> sRGB_conv_base<T>::lut;

    // Wrapper for sRGB-linear conversion. 
    // Base template is undefined, specializations are provided below.
    template<class T>
    class sRGB_conv;

    template<>
    class sRGB_conv<float> : public sRGB_conv_base<float>
    {
    public:
        static float alpha_from_sRGB(int8u x)
        {
            return float(x / 255.0);
        }

        static int8u alpha_to_sRGB(float x)
        {
            if (x <= 0) return 0;
            else if (x >= 1) return 255;
            else return int8u(0.5 + x * 255);
        }
    };

    template<>
    class sRGB_conv<int16u> : public sRGB_conv_base<int16u>
    {
    public:
        static int16u alpha_from_sRGB(int8u x)
        {
            return (x << 8) | x;
        }

        static int8u alpha_to_sRGB(int16u x)
        {
            return x >> 8;
        }
    };

    template<>
    class sRGB_conv<int8u> : public sRGB_conv_base<int8u>
    {
    public:
        static int8u alpha_from_sRGB(int8u x)
        {
            return x;
        }

        static int8u alpha_to_sRGB(int8u x)
        {
            return x;
        }
    };
}

#endif