update dev300-m58
[ooovba.git] / agg / inc / agg_span_pattern_resample_rgba.h
blobb295a91f73c7fcec11b86c60e8da882f18f7ba6f
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
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 //----------------------------------------------------------------------------
16 #ifndef AGG_SPAN_PATTERN_RESAMPLE_RGBA_INCLUDED
17 #define AGG_SPAN_PATTERN_RESAMPLE_RGBA_INCLUDED
19 #include "agg_color_rgba.h"
20 #include "agg_span_image_resample.h"
22 namespace agg
25 //=======================================span_pattern_resample_rgba_affine
26 template<class ColorT,
27 class Order,
28 class WrapModeX,
29 class WrapModeY,
30 class Allocator = span_allocator<ColorT> >
31 class span_pattern_resample_rgba_affine :
32 public span_image_resample_affine<ColorT, Allocator>
34 public:
35 typedef ColorT color_type;
36 typedef Order order_type;
37 typedef Allocator alloc_type;
38 typedef span_image_resample_affine<color_type, alloc_type> base_type;
39 typedef typename base_type::interpolator_type interpolator_type;
40 typedef typename color_type::value_type value_type;
41 typedef typename color_type::long_type long_type;
42 enum
44 base_shift = color_type::base_shift,
45 base_mask = color_type::base_mask,
46 downscale_shift = image_filter_shift
49 //--------------------------------------------------------------------
50 span_pattern_resample_rgba_affine(alloc_type& alloc) :
51 base_type(alloc),
52 m_wrap_mode_x(1),
53 m_wrap_mode_y(1)
56 //--------------------------------------------------------------------
57 span_pattern_resample_rgba_affine(alloc_type& alloc,
58 const rendering_buffer& src,
59 interpolator_type& inter,
60 const image_filter_lut& filter_) :
61 base_type(alloc, src, color_type(0,0,0,0), inter, filter_),
62 m_wrap_mode_x(src.width()),
63 m_wrap_mode_y(src.height())
66 //--------------------------------------------------------------------
67 void source_image(const rendering_buffer& src)
69 base_type::source_image(src);
70 m_wrap_mode_x = WrapModeX(src.width());
71 m_wrap_mode_y = WrapModeX(src.height());
74 //--------------------------------------------------------------------
75 color_type* generate(int x, int y, unsigned len)
77 color_type* span = base_type::allocator().span();
78 interpolator_type& intr = base_type::interpolator();
79 intr.begin(x + base_type::filter_dx_dbl(),
80 y + base_type::filter_dy_dbl(), len);
81 long_type fg[4];
83 int diameter = base_type::filter().diameter();
84 int filter_size = diameter << image_subpixel_shift;
85 int radius_x = (diameter * base_type::m_rx) >> 1;
86 int radius_y = (diameter * base_type::m_ry) >> 1;
87 int maxx = base_type::source_image().width() - 1;
88 int maxy = base_type::source_image().height() - 1;
89 const int16* weight_array = base_type::filter().weight_array();
93 intr.coordinates(&x, &y);
95 x += base_type::filter_dx_int() - radius_x;
96 y += base_type::filter_dy_int() - radius_y;
98 fg[0] = fg[1] = fg[2] = fg[3] = image_filter_size / 2;
100 int y_lr = m_wrap_mode_y(y >> image_subpixel_shift);
101 int y_hr = ((image_subpixel_mask - (y & image_subpixel_mask)) *
102 base_type::m_ry_inv) >>
103 image_subpixel_shift;
104 int total_weight = 0;
105 int x_lr_ini = x >> image_subpixel_shift;
106 int x_hr_ini = ((image_subpixel_mask - (x & image_subpixel_mask)) *
107 base_type::m_rx_inv) >>
108 image_subpixel_shift;
111 int weight_y = weight_array[y_hr];
112 int x_lr = m_wrap_mode_x(x_lr_ini);
113 int x_hr = x_hr_ini;
114 const value_type* row_ptr = (const value_type*)base_type::source_image().row(y_lr);
117 const value_type* fg_ptr = row_ptr + (x_lr << 2);
118 int weight = (weight_y * weight_array[x_hr] +
119 image_filter_size / 2) >>
120 downscale_shift;
122 fg[0] += fg_ptr[0] * weight;
123 fg[1] += fg_ptr[1] * weight;
124 fg[2] += fg_ptr[2] * weight;
125 fg[3] += fg_ptr[3] * weight;
126 total_weight += weight;
127 x_hr += base_type::m_rx_inv;
128 x_lr = ++m_wrap_mode_x;
130 while(x_hr < filter_size);
132 y_hr += base_type::m_ry_inv;
133 y_lr = ++m_wrap_mode_y;
134 } while(y_hr < filter_size);
136 fg[0] /= total_weight;
137 fg[1] /= total_weight;
138 fg[2] /= total_weight;
139 fg[3] /= total_weight;
141 if(fg[0] < 0) fg[0] = 0;
142 if(fg[1] < 0) fg[1] = 0;
143 if(fg[2] < 0) fg[2] = 0;
144 if(fg[3] < 0) fg[3] = 0;
146 if(fg[order_type::A] > base_mask) fg[order_type::A] = base_mask;
147 if(fg[order_type::R] > fg[order_type::A]) fg[order_type::R] = fg[order_type::A];
148 if(fg[order_type::G] > fg[order_type::A]) fg[order_type::G] = fg[order_type::A];
149 if(fg[order_type::B] > fg[order_type::A]) fg[order_type::B] = fg[order_type::A];
151 span->r = (value_type)fg[order_type::R];
152 span->g = (value_type)fg[order_type::G];
153 span->b = (value_type)fg[order_type::B];
154 span->a = (value_type)fg[order_type::A];
156 ++span;
157 ++intr;
158 } while(--len);
159 return base_type::allocator().span();
162 private:
163 WrapModeX m_wrap_mode_x;
164 WrapModeY m_wrap_mode_y;
173 //============================================span_pattern_resample_rgba
174 template<class ColorT,
175 class Order,
176 class Interpolator,
177 class WrapModeX,
178 class WrapModeY,
179 class Allocator = span_allocator<ColorT> >
180 class span_pattern_resample_rgba :
181 public span_image_resample<ColorT, Interpolator, Allocator>
183 public:
184 typedef ColorT color_type;
185 typedef Order order_type;
186 typedef Interpolator interpolator_type;
187 typedef Allocator alloc_type;
188 typedef span_image_resample<color_type, interpolator_type, alloc_type> base_type;
189 typedef typename color_type::value_type value_type;
190 typedef typename color_type::long_type long_type;
191 enum
193 base_shift = color_type::base_shift,
194 base_mask = color_type::base_mask,
195 downscale_shift = image_filter_shift
198 //--------------------------------------------------------------------
199 span_pattern_resample_rgba(alloc_type& alloc) :
200 base_type(alloc),
201 m_wrap_mode_x(1),
202 m_wrap_mode_y(1)
205 //--------------------------------------------------------------------
206 span_pattern_resample_rgba(alloc_type& alloc,
207 const rendering_buffer& src,
208 interpolator_type& inter,
209 const image_filter_lut& filter) :
210 base_type(alloc, src, color_type(0,0,0,0), inter, filter),
211 m_wrap_mode_x(src.width()),
212 m_wrap_mode_y(src.height())
215 //--------------------------------------------------------------------
216 void source_image(const rendering_buffer& src)
218 base_type::source_image(src);
219 m_wrap_mode_x = WrapModeX(src.width());
220 m_wrap_mode_y = WrapModeX(src.height());
223 //--------------------------------------------------------------------
224 color_type* generate(int x, int y, unsigned len)
226 color_type* span = base_type::allocator().span();
227 interpolator_type& intr = base_type::interpolator();
228 intr.begin(x + base_type::filter_dx_dbl(),
229 y + base_type::filter_dy_dbl(), len);
230 long_type fg[4];
232 int diameter = base_type::filter().diameter();
233 int filter_size = diameter << image_subpixel_shift;
234 const int16* weight_array = base_type::filter().weight_array();
238 int rx;
239 int ry;
240 int rx_inv = image_subpixel_size;
241 int ry_inv = image_subpixel_size;
242 intr.coordinates(&x, &y);
243 intr.local_scale(&rx, &ry);
245 rx = (rx * base_type::m_blur_x) >> image_subpixel_shift;
246 ry = (ry * base_type::m_blur_y) >> image_subpixel_shift;
248 if(rx < image_subpixel_size)
250 rx = image_subpixel_size;
252 else
254 if(rx > image_subpixel_size * base_type::m_scale_limit)
256 rx = image_subpixel_size * base_type::m_scale_limit;
258 rx_inv = image_subpixel_size * image_subpixel_size / rx;
261 if(ry < image_subpixel_size)
263 ry = image_subpixel_size;
265 else
267 if(ry > image_subpixel_size * base_type::m_scale_limit)
269 ry = image_subpixel_size * base_type::m_scale_limit;
271 ry_inv = image_subpixel_size * image_subpixel_size / ry;
274 int radius_x = (diameter * rx) >> 1;
275 int radius_y = (diameter * ry) >> 1;
276 int maxx = base_type::source_image().width() - 1;
277 int maxy = base_type::source_image().height() - 1;
279 x += base_type::filter_dx_int() - radius_x;
280 y += base_type::filter_dy_int() - radius_y;
282 fg[0] = fg[1] = fg[2] = fg[3] = image_filter_size / 2;
284 int y_lr = m_wrap_mode_y(y >> image_subpixel_shift);
285 int y_hr = ((image_subpixel_mask - (y & image_subpixel_mask)) *
286 ry_inv) >>
287 image_subpixel_shift;
288 int total_weight = 0;
289 int x_lr_ini = x >> image_subpixel_shift;
290 int x_hr_ini = ((image_subpixel_mask - (x & image_subpixel_mask)) *
291 rx_inv) >>
292 image_subpixel_shift;
296 int weight_y = weight_array[y_hr];
297 int x_lr = m_wrap_mode_x(x_lr_ini);
298 int x_hr = x_hr_ini;
299 const value_type* row_ptr = (const value_type*)base_type::source_image().row(y_lr);
302 const value_type* fg_ptr = row_ptr + (x_lr << 2);
303 int weight = (weight_y * weight_array[x_hr] +
304 image_filter_size / 2) >>
305 downscale_shift;
306 fg[0] += fg_ptr[0] * weight;
307 fg[1] += fg_ptr[1] * weight;
308 fg[2] += fg_ptr[2] * weight;
309 fg[3] += fg_ptr[3] * weight;
310 total_weight += weight;
311 x_hr += rx_inv;
312 x_lr = ++m_wrap_mode_x;
314 while(x_hr < filter_size);
315 y_hr += ry_inv;
316 y_lr = ++m_wrap_mode_y;
318 while(y_hr < filter_size);
320 fg[0] /= total_weight;
321 fg[1] /= total_weight;
322 fg[2] /= total_weight;
323 fg[3] /= total_weight;
325 if(fg[0] < 0) fg[0] = 0;
326 if(fg[1] < 0) fg[1] = 0;
327 if(fg[2] < 0) fg[2] = 0;
328 if(fg[3] < 0) fg[3] = 0;
330 if(fg[order_type::A] > base_mask) fg[order_type::A] = base_mask;
331 if(fg[order_type::R] > fg[order_type::A]) fg[order_type::R] = fg[order_type::A];
332 if(fg[order_type::G] > fg[order_type::A]) fg[order_type::G] = fg[order_type::A];
333 if(fg[order_type::B] > fg[order_type::A]) fg[order_type::B] = fg[order_type::A];
335 span->r = (value_type)fg[order_type::R];
336 span->g = (value_type)fg[order_type::G];
337 span->b = (value_type)fg[order_type::B];
338 span->a = (value_type)fg[order_type::A];
340 ++span;
341 ++intr;
342 } while(--len);
343 return base_type::allocator().span();
346 private:
347 WrapModeX m_wrap_mode_x;
348 WrapModeY m_wrap_mode_y;
354 #endif