summaryrefslogtreecommitdiff
path: root/agg/inc/agg_image_filters.h
blob: a2037fe0eff1e09e3bfbc0b90fa05f25fb6582bd (plain)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.3
// 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
//----------------------------------------------------------------------------
//
// Image transformation filters,
// Filtering classes (image_filter_lut, image_filter),
// Basic filter shape classes
//----------------------------------------------------------------------------
#ifndef AGG_IMAGE_FILTERS_INCLUDED
#define AGG_IMAGE_FILTERS_INCLUDED

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

namespace agg
{

    // See Implementation agg_image_filters.cpp

    enum
    {
        image_filter_shift = 14,                      //----image_filter_shift
        image_filter_size  = 1 << image_filter_shift, //----image_filter_size
        image_filter_mask  = image_filter_size - 1,   //----image_filter_mask

        image_subpixel_shift = 8,                         //----image_subpixel_shift
        image_subpixel_size  = 1 << image_subpixel_shift, //----image_subpixel_size
        image_subpixel_mask  = image_subpixel_size - 1    //----image_subpixel_mask
    };


    //-----------------------------------------------------image_filter_lut
    class image_filter_lut
    {
    public:
        ~image_filter_lut();
        image_filter_lut();

        template<class FilterF> void calculate(const FilterF& filter,
                                               bool normalization=true)
        {
            double r = filter.radius();
            realloc(r);
            unsigned i;
            unsigned pivot = diameter() << (image_subpixel_shift - 1);
            for(i = 0; i < pivot; i++)
            {
                double x = double(i) / double(image_subpixel_size);
                double y = filter.calc_weight(x);
                m_weight_array[pivot + i] =
                m_weight_array[pivot - i] = int16(y * image_filter_size + 0.5);
            }
            unsigned end = (diameter() << image_subpixel_shift) - 1;
            m_weight_array[0] = m_weight_array[end];
            if(normalization)
            {
                normalize();
            }
        }

        template<class FilterF> image_filter_lut(const FilterF& filter,
                                                 bool normalization=true) :
            m_weight_array(0),
            m_max_size(0)
        {
            calculate(filter, normalization);
        }

        double       radius()       const { return m_radius;       }
        unsigned     diameter()     const { return m_diameter;     }
        int          start()        const { return m_start;        }
        const int16* weight_array() const { return m_weight_array; }
        void         normalize();

    private:
        void realloc(double radius);
        image_filter_lut(const image_filter_lut&);
        const image_filter_lut& operator = (const image_filter_lut&);

        double   m_radius;
        unsigned m_diameter;
        int      m_start;
        int16*   m_weight_array;
        unsigned m_max_size;
    };



    //--------------------------------------------------------image_filter
    template<class FilterF> class image_filter : public image_filter_lut
    {
    public:
        image_filter()
        {
            calculate(m_filter_function);
        }
    private:
        FilterF m_filter_function;
    };


    //-----------------------------------------------image_filter_bilinear
    struct image_filter_bilinear
    {
        static double radius() { return 1.0; }
        static double calc_weight(double x)
        {
            return 1.0 - x;
        }
    };


    //-----------------------------------------------image_filter_hanning
    struct image_filter_hanning
    {
        static double radius() { return 1.0; }
        static double calc_weight(double x)
        {
            return 0.5 + 0.5 * cos(pi * x);
        }
    };


    //-----------------------------------------------image_filter_hamming
    struct image_filter_hamming
    {
        static double radius() { return 1.0; }
        static double calc_weight(double x)
        {
            return 0.54 + 0.46 * cos(pi * x);
        }
    };

    //-----------------------------------------------image_filter_hermite
    struct image_filter_hermite
    {
        static double radius() { return 1.0; }
        static double calc_weight(double x)
        {
            return (2.0 * x - 3.0) * x * x + 1.0;
        }
    };

    //------------------------------------------------image_filter_quadric
    struct image_filter_quadric
    {
        static double radius() { return 1.5; }
        static double calc_weight(double x)
        {
            double t;
            if(x <  0.5) return 0.75 - x * x;
            if(x <  1.5) {t = x - 1.5; return 0.5 * t * t;}
            return 0.0;
        }
    };

    //------------------------------------------------image_filter_bicubic
    class image_filter_bicubic
    {
        static double pow3(double x)
        {
            return (x <= 0.0) ? 0.0 : x * x * x;
        }

    public:
        static double radius() { return 2.0; }
        static double calc_weight(double x)
        {
            return
                (1.0/6.0) *
                (pow3(x + 2) - 4 * pow3(x + 1) + 6 * pow3(x) - 4 * pow3(x - 1));
        }
    };

    //-------------------------------------------------image_filter_kaiser
    class image_filter_kaiser
    {
        double a;
        double i0a;
        double epsilon;

    public:
        image_filter_kaiser(double b = 6.33) :
            a(b), epsilon(1e-12)
        {
            i0a = 1.0 / bessel_i0(b);
        }

        static double radius() { return 1.0; }
        double calc_weight(double x) const
        {
            return bessel_i0(a * sqrt(1. - x * x)) * i0a;
        }

    private:
        double bessel_i0(double x) const
        {
            int i;
            double sum, y, t;

            sum = 1.;
            y = x * x / 4.;
            t = y;

            for(i = 2; t > epsilon; i++)
            {
                sum += t;
                t *= (double)y / (i * i);
            }
            return sum;
        }
    };

    //----------------------------------------------image_filter_catrom
    struct image_filter_catrom
    {
        static double radius() { return 2.0; }
        static double calc_weight(double x)
        {
            if(x <  1.0) return 0.5 * (2.0 + x * x * (-5.0 + x * 3.0));
            if(x <  2.0) return 0.5 * (4.0 + x * (-8.0 + x * (5.0 - x)));
            return 0.;
        }
    };

    //---------------------------------------------image_filter_mitchell
    class image_filter_mitchell
    {
        double p0, p2, p3;
        double q0, q1, q2, q3;

    public:
        image_filter_mitchell(double b = 1.0/3.0, double c = 1.0/3.0) :
            p0((6.0 - 2.0 * b) / 6.0),
            p2((-18.0 + 12.0 * b + 6.0 * c) / 6.0),
            p3((12.0 - 9.0 * b - 6.0 * c) / 6.0),
            q0((8.0 * b + 24.0 * c) / 6.0),
            q1((-12.0 * b - 48.0 * c) / 6.0),
            q2((6.0 * b + 30.0 * c) / 6.0),
            q3((-b - 6.0 * c) / 6.0)
        {}

        static double radius() { return 2.0; }
        double calc_weight(double x) const
        {
            if(x < 1.0) return p0 + x * x * (p2 + x * p3);
            if(x < 2.0) return q0 + x * (q1 + x * (q2 + x * q3));
            return 0.0;
        }
    };


    //----------------------------------------------image_filter_spline16
    struct image_filter_spline16
    {
        static double radius() { return 2.0; }
        static double calc_weight(double x)
        {
            if(x < 1.0)
            {
                return ((x - 9.0/5.0 ) * x - 1.0/5.0 ) * x + 1.0;
            }
            return ((-1.0/3.0 * (x-1) + 4.0/5.0) * (x-1) - 7.0/15.0 ) * (x-1);
        }
    };


    //---------------------------------------------image_filter_spline36
    struct image_filter_spline36
    {
        static double radius() { return 3.0; }
        static double calc_weight(double x)
        {
           if(x < 1.0)
           {
              return ((13.0/11.0 * x - 453.0/209.0) * x - 3.0/209.0) * x + 1.0;
           }
           if(x < 2.0)
           {
              return ((-6.0/11.0 * (x-1) + 270.0/209.0) * (x-1) - 156.0/ 209.0) * (x-1);
           }
           return ((1.0/11.0 * (x-2) - 45.0/209.0) * (x-2) +  26.0/209.0) * (x-2);
        }
    };


    //----------------------------------------------image_filter_gaussian
    struct image_filter_gaussian
    {
        static double radius() { return 2.0; }
        static double calc_weight(double x)
        {
            return exp(-2.0 * x * x) * sqrt(2.0 / pi);
        }
    };


    //------------------------------------------------image_filter_bessel
    struct image_filter_bessel
    {
        static double radius() { return 3.2383; }
        static double calc_weight(double x)
        {
            return (x == 0.0) ? pi / 4.0 : j1(pi * x) / (2.0 * x);
        }
    };


    //-------------------------------------------------image_filter_sinc
    class image_filter_sinc
    {
    public:
        image_filter_sinc(double r) : m_radius(r < 2.0 ? 2.0 : r) {}
        double radius() const { return m_radius; }
        double calc_weight(double x) const
        {
            if(x == 0.0) return 1.0;
            x *= pi;
            return sin(x) / x;
        }
    private:
        double m_radius;
    };


    //-----------------------------------------------image_filter_lanczos
    class image_filter_lanczos
    {
    public:
        image_filter_lanczos(double r) : m_radius(r < 2.0 ? 2.0 : r) {}
        double radius() const { return m_radius; }
        double calc_weight(double x) const
        {
           if(x == 0.0) return 1.0;
           if(x > m_radius) return 0.0;
           x *= pi;
           double xr = x / m_radius;
           return (sin(x) / x) * (sin(xr) / xr);
        }
    private:
        double m_radius;
    };


    //----------------------------------------------image_filter_blackman
    class image_filter_blackman
    {
    public:
        image_filter_blackman(double r) : m_radius(r < 2.0 ? 2.0 : r) {}
        double radius() const { return m_radius; }
        double calc_weight(double x) const
        {
           if(x == 0.0) return 1.0;
           if(x > m_radius) return 0.0;
           x *= pi;
           double xr = x / m_radius;
           return (sin(x) / x) * (0.42 + 0.5*cos(xr) + 0.08*cos(2*xr));
        }
    private:
        double m_radius;
    };

    //------------------------------------------------image_filter_sinc36
    class image_filter_sinc36 : public image_filter_sinc
    { public: image_filter_sinc36() : image_filter_sinc(3.0){} };

    //------------------------------------------------image_filter_sinc64
    class image_filter_sinc64 : public image_filter_sinc
    { public: image_filter_sinc64() : image_filter_sinc(4.0){} };

    //-----------------------------------------------image_filter_sinc100
    class image_filter_sinc100 : public image_filter_sinc
    { public: image_filter_sinc100() : image_filter_sinc(5.0){} };

    //-----------------------------------------------image_filter_sinc144
    class image_filter_sinc144 : public image_filter_sinc
    { public: image_filter_sinc144() : image_filter_sinc(6.0){} };

    //-----------------------------------------------image_filter_sinc196
    class image_filter_sinc196 : public image_filter_sinc
    { public: image_filter_sinc196() : image_filter_sinc(7.0){} };

    //-----------------------------------------------image_filter_sinc256
    class image_filter_sinc256 : public image_filter_sinc
    { public: image_filter_sinc256() : image_filter_sinc(8.0){} };

    //---------------------------------------------image_filter_lanczos36
    class image_filter_lanczos36 : public image_filter_lanczos
    { public: image_filter_lanczos36() : image_filter_lanczos(3.0){} };

    //---------------------------------------------image_filter_lanczos64
    class image_filter_lanczos64 : public image_filter_lanczos
    { public: image_filter_lanczos64() : image_filter_lanczos(4.0){} };

    //--------------------------------------------image_filter_lanczos100
    class image_filter_lanczos100 : public image_filter_lanczos
    { public: image_filter_lanczos100() : image_filter_lanczos(5.0){} };

    //--------------------------------------------image_filter_lanczos144
    class image_filter_lanczos144 : public image_filter_lanczos
    { public: image_filter_lanczos144() : image_filter_lanczos(6.0){} };

    //--------------------------------------------image_filter_lanczos196
    class image_filter_lanczos196 : public image_filter_lanczos
    { public: image_filter_lanczos196() : image_filter_lanczos(7.0){} };

    //--------------------------------------------image_filter_lanczos256
    class image_filter_lanczos256 : public image_filter_lanczos
    { public: image_filter_lanczos256() : image_filter_lanczos(8.0){} };

    //--------------------------------------------image_filter_blackman36
    class image_filter_blackman36 : public image_filter_blackman
    { public: image_filter_blackman36() : image_filter_blackman(3.0){} };

    //--------------------------------------------image_filter_blackman64
    class image_filter_blackman64 : public image_filter_blackman
    { public: image_filter_blackman64() : image_filter_blackman(4.0){} };

    //-------------------------------------------image_filter_blackman100
    class image_filter_blackman100 : public image_filter_blackman
    { public: image_filter_blackman100() : image_filter_blackman(5.0){} };

    //-------------------------------------------image_filter_blackman144
    class image_filter_blackman144 : public image_filter_blackman
    { public: image_filter_blackman144() : image_filter_blackman(6.0){} };

    //-------------------------------------------image_filter_blackman196
    class image_filter_blackman196 : public image_filter_blackman
    { public: image_filter_blackman196() : image_filter_blackman(7.0){} };

    //-------------------------------------------image_filter_blackman256
    class image_filter_blackman256 : public image_filter_blackman
    { public: image_filter_blackman256() : image_filter_blackman(8.0){} };


}

#endif