summaryrefslogtreecommitdiff
path: root/agg/inc/agg_span_pattern_resample_rgb.h
blob: baef860eb4728aabbd16bd832287c8411d13e9b3 (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
//----------------------------------------------------------------------------
// 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
//----------------------------------------------------------------------------

#ifndef AGG_SPAN_PATTERN_RESAMPLE_RGB_INCLUDED
#define AGG_SPAN_PATTERN_RESAMPLE_RGB_INCLUDED

#include "agg_color_rgba.h"
#include "agg_span_image_resample.h"

namespace agg
{

    //========================================span_pattern_resample_rgb_affine
    template<class ColorT,
             class Order,
             class WrapModeX,
             class WrapModeY,
             class Allocator = span_allocator<ColorT> >
    class span_pattern_resample_rgb_affine :
    public span_image_resample_affine<ColorT, Allocator>
    {
    public:
        typedef ColorT color_type;
        typedef Order order_type;
        typedef Allocator alloc_type;
        typedef span_image_resample_affine<color_type, alloc_type> base_type;
        typedef typename base_type::interpolator_type interpolator_type;
        typedef typename color_type::value_type value_type;
        typedef typename color_type::long_type long_type;
        enum
        {
            base_shift      = color_type::base_shift,
            base_mask       = color_type::base_mask,
            downscale_shift = image_filter_shift
        };

        //--------------------------------------------------------------------
        span_pattern_resample_rgb_affine(alloc_type& alloc) :
            base_type(alloc),
            m_wrap_mode_x(1),
            m_wrap_mode_y(1)
        {}

        //--------------------------------------------------------------------
        span_pattern_resample_rgb_affine(alloc_type& alloc,
                                         const rendering_buffer& src,
                                         interpolator_type& inter,
                                         const image_filter_lut& filter_) :
            base_type(alloc, src, color_type(0,0,0,0), inter, filter_),
            m_wrap_mode_x(src.width()),
            m_wrap_mode_y(src.height())
        {}

        //--------------------------------------------------------------------
        void source_image(const rendering_buffer& src)
        {
            base_type::source_image(src);
            m_wrap_mode_x = WrapModeX(src.width());
            m_wrap_mode_y = WrapModeX(src.height());
        }

        //--------------------------------------------------------------------
        color_type* generate(int x, int y, unsigned len)
        {
            color_type* span = base_type::allocator().span();
            interpolator_type& intr = base_type::interpolator();
            intr.begin(x + base_type::filter_dx_dbl(),
                       y + base_type::filter_dy_dbl(), len);
            long_type fg[3];

            int diameter = base_type::filter().diameter();
            int filter_size = diameter << image_subpixel_shift;
            int radius_x = (diameter * base_type::m_rx) >> 1;
            int radius_y = (diameter * base_type::m_ry) >> 1;
            int maxx = base_type::source_image().width() - 1;
            int maxy = base_type::source_image().height() - 1;
            const int16* weight_array = base_type::filter().weight_array();

            do
            {
                intr.coordinates(&x, &y);

                x += base_type::filter_dx_int() - radius_x;
                y += base_type::filter_dy_int() - radius_y;

                fg[0] = fg[1] = fg[2] = image_filter_size / 2;

                int y_lr  = m_wrap_mode_y(y >> image_subpixel_shift);
                int y_hr = ((image_subpixel_mask - (y & image_subpixel_mask)) *
                                base_type::m_ry_inv) >>
                                    image_subpixel_shift;
                int total_weight = 0;
                int x_lr_ini = x >> image_subpixel_shift;
                int x_hr_ini = ((image_subpixel_mask - (x & image_subpixel_mask)) *
                                   base_type::m_rx_inv) >>
                                       image_subpixel_shift;
                do
                {
                    int weight_y = weight_array[y_hr];
                    int x_lr = m_wrap_mode_x(x_lr_ini);
                    int x_hr = x_hr_ini;
                    const value_type* row_ptr = (const value_type*)base_type::source_image().row(y_lr);
                    do
                    {
                        const value_type* fg_ptr = row_ptr + x_lr * 3;
                        int weight = (weight_y * weight_array[x_hr] +
                                     image_filter_size / 2) >>
                                     downscale_shift;

                        fg[0] += fg_ptr[0] * weight;
                        fg[1] += fg_ptr[1] * weight;
                        fg[2] += fg_ptr[2] * weight;
                        total_weight += weight;
                        x_hr   += base_type::m_rx_inv;
                        x_lr = ++m_wrap_mode_x;
                    }
                    while(x_hr < filter_size);

                    y_hr += base_type::m_ry_inv;
                    y_lr = ++m_wrap_mode_y;
                } while(y_hr < filter_size);

                fg[0] /= total_weight;
                fg[1] /= total_weight;
                fg[2] /= total_weight;

                if(fg[0] < 0) fg[0] = 0;
                if(fg[1] < 0) fg[1] = 0;
                if(fg[2] < 0) fg[2] = 0;

                if(fg[0] > base_mask) fg[0] = base_mask;
                if(fg[1] > base_mask) fg[1] = base_mask;
                if(fg[2] > base_mask) fg[2] = base_mask;

                span->r = (value_type)fg[order_type::R];
                span->g = (value_type)fg[order_type::G];
                span->b = (value_type)fg[order_type::B];
                span->a = (value_type)base_mask;

                ++span;
                ++intr;
            } while(--len);
            return base_type::allocator().span();
        }

    private:
        WrapModeX m_wrap_mode_x;
        WrapModeY m_wrap_mode_y;
    };







    //=============================================span_pattern_resample_rgb
    template<class ColorT,
             class Order,
             class Interpolator,
             class WrapModeX,
             class WrapModeY,
             class Allocator = span_allocator<ColorT> >
    class span_pattern_resample_rgb :
    public span_image_resample<ColorT, Interpolator, Allocator>
    {
    public:
        typedef ColorT color_type;
        typedef Order order_type;
        typedef Interpolator interpolator_type;
        typedef Allocator alloc_type;
        typedef span_image_resample<color_type, interpolator_type, alloc_type> base_type;
        typedef typename color_type::value_type value_type;
        typedef typename color_type::long_type long_type;
        enum
        {
            base_shift = color_type::base_shift,
            base_mask  = color_type::base_mask,
            downscale_shift = image_filter_shift
        };

        //--------------------------------------------------------------------
        span_pattern_resample_rgb(alloc_type& alloc) :
            base_type(alloc),
            m_wrap_mode_x(1),
            m_wrap_mode_y(1)
        {}

        //--------------------------------------------------------------------
        span_pattern_resample_rgb(alloc_type& alloc,
                                  const rendering_buffer& src,
                                  interpolator_type& inter,
                                  const image_filter_lut& filter) :
            base_type(alloc, src, color_type(0,0,0,0), inter, filter),
            m_wrap_mode_x(src.width()),
            m_wrap_mode_y(src.height())
        {}

        //--------------------------------------------------------------------
        void source_image(const rendering_buffer& src)
        {
            base_type::source_image(src);
            m_wrap_mode_x = WrapModeX(src.width());
            m_wrap_mode_y = WrapModeX(src.height());
        }

        //--------------------------------------------------------------------
        color_type* generate(int x, int y, unsigned len)
        {
            color_type* span = base_type::allocator().span();
            interpolator_type& intr = base_type::interpolator();
            intr.begin(x + base_type::filter_dx_dbl(),
                       y + base_type::filter_dy_dbl(), len);
            long_type fg[3];

            int diameter = base_type::filter().diameter();
            int filter_size = diameter << image_subpixel_shift;
            const int16* weight_array = base_type::filter().weight_array();

            do
            {
                int rx;
                int ry;
                int rx_inv = image_subpixel_size;
                int ry_inv = image_subpixel_size;
                intr.coordinates(&x,  &y);
                intr.local_scale(&rx, &ry);

                rx = (rx * base_type::m_blur_x) >> image_subpixel_shift;
                ry = (ry * base_type::m_blur_y) >> image_subpixel_shift;

                if(rx < image_subpixel_size)
                {
                    rx = image_subpixel_size;
                }
                else
                {
                    if(rx > image_subpixel_size * base_type::m_scale_limit)
                    {
                        rx = image_subpixel_size * base_type::m_scale_limit;
                    }
                    rx_inv = image_subpixel_size * image_subpixel_size / rx;
                }

                if(ry < image_subpixel_size)
                {
                    ry = image_subpixel_size;
                }
                else
                {
                    if(ry > image_subpixel_size * base_type::m_scale_limit)
                    {
                        ry = image_subpixel_size * base_type::m_scale_limit;
                    }
                    ry_inv = image_subpixel_size * image_subpixel_size / ry;
                }

                int radius_x = (diameter * rx) >> 1;
                int radius_y = (diameter * ry) >> 1;
                int maxx = base_type::source_image().width() - 1;
                int maxy = base_type::source_image().height() - 1;

                x += base_type::filter_dx_int() - radius_x;
                y += base_type::filter_dy_int() - radius_y;

                fg[0] = fg[1] = fg[2] = image_filter_size / 2;

                int y_lr  = m_wrap_mode_y(y >> image_subpixel_shift);
                int y_hr = ((image_subpixel_mask - (y & image_subpixel_mask)) *
                               ry_inv) >>
                                   image_subpixel_shift;
                int total_weight = 0;
                int x_lr_ini = x >> image_subpixel_shift;
                int x_hr_ini = ((image_subpixel_mask - (x & image_subpixel_mask)) *
                                   rx_inv) >>
                                       image_subpixel_shift;

                do
                {
                    int weight_y = weight_array[y_hr];
                    int x_lr = m_wrap_mode_x(x_lr_ini);
                    int x_hr = x_hr_ini;
                    const value_type* row_ptr = (const value_type*)base_type::source_image().row(y_lr);
                    do
                    {
                        const value_type* fg_ptr = row_ptr + x_lr * 3;
                        int weight = (weight_y * weight_array[x_hr] +
                                     image_filter_size / 2) >>
                                     downscale_shift;
                        fg[0] += fg_ptr[0] * weight;
                        fg[1] += fg_ptr[1] * weight;
                        fg[2] += fg_ptr[2] * weight;
                        total_weight += weight;
                        x_hr   += rx_inv;
                        x_lr = ++m_wrap_mode_x;
                    }
                    while(x_hr < filter_size);
                    y_hr += ry_inv;
                    y_lr = ++m_wrap_mode_y;
                }
                while(y_hr < filter_size);

                fg[0] /= total_weight;
                fg[1] /= total_weight;
                fg[2] /= total_weight;

                if(fg[0] < 0) fg[0] = 0;
                if(fg[1] < 0) fg[1] = 0;
                if(fg[2] < 0) fg[2] = 0;

                if(fg[0] > base_mask) fg[0] = base_mask;
                if(fg[1] > base_mask) fg[1] = base_mask;
                if(fg[2] > base_mask) fg[2] = base_mask;

                span->r = (value_type)fg[order_type::R];
                span->g = (value_type)fg[order_type::G];
                span->b = (value_type)fg[order_type::B];
                span->a = (value_type)base_mask;

                ++span;
                ++intr;
            } while(--len);
            return base_type::allocator().span();
        }

    private:
        WrapModeX m_wrap_mode_x;
        WrapModeY m_wrap_mode_y;
    };

}


#endif