update dev300-m58
[ooovba.git] / agg / inc / agg_span_pattern_resample_rgb.h
blobc08ab26257ec7eff52114195c3ec7a638afdd170
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_RGB_INCLUDED
17 #define AGG_SPAN_PATTERN_RESAMPLE_RGB_INCLUDED
19 #include "agg_color_rgba.h"
20 #include "agg_span_image_resample.h"
22 namespace agg
25 //========================================span_pattern_resample_rgb_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_rgb_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_rgb_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_rgb_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[3];
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] = 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 * 3;
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 total_weight += weight;
126 x_hr += base_type::m_rx_inv;
127 x_lr = ++m_wrap_mode_x;
129 while(x_hr < filter_size);
131 y_hr += base_type::m_ry_inv;
132 y_lr = ++m_wrap_mode_y;
133 } while(y_hr < filter_size);
135 fg[0] /= total_weight;
136 fg[1] /= total_weight;
137 fg[2] /= total_weight;
139 if(fg[0] < 0) fg[0] = 0;
140 if(fg[1] < 0) fg[1] = 0;
141 if(fg[2] < 0) fg[2] = 0;
143 if(fg[0] > base_mask) fg[0] = base_mask;
144 if(fg[1] > base_mask) fg[1] = base_mask;
145 if(fg[2] > base_mask) fg[2] = base_mask;
147 span->r = (value_type)fg[order_type::R];
148 span->g = (value_type)fg[order_type::G];
149 span->b = (value_type)fg[order_type::B];
150 span->a = (value_type)base_mask;
152 ++span;
153 ++intr;
154 } while(--len);
155 return base_type::allocator().span();
158 private:
159 WrapModeX m_wrap_mode_x;
160 WrapModeY m_wrap_mode_y;
169 //=============================================span_pattern_resample_rgb
170 template<class ColorT,
171 class Order,
172 class Interpolator,
173 class WrapModeX,
174 class WrapModeY,
175 class Allocator = span_allocator<ColorT> >
176 class span_pattern_resample_rgb :
177 public span_image_resample<ColorT, Interpolator, Allocator>
179 public:
180 typedef ColorT color_type;
181 typedef Order order_type;
182 typedef Interpolator interpolator_type;
183 typedef Allocator alloc_type;
184 typedef span_image_resample<color_type, interpolator_type, alloc_type> base_type;
185 typedef typename color_type::value_type value_type;
186 typedef typename color_type::long_type long_type;
187 enum
189 base_shift = color_type::base_shift,
190 base_mask = color_type::base_mask,
191 downscale_shift = image_filter_shift
194 //--------------------------------------------------------------------
195 span_pattern_resample_rgb(alloc_type& alloc) :
196 base_type(alloc),
197 m_wrap_mode_x(1),
198 m_wrap_mode_y(1)
201 //--------------------------------------------------------------------
202 span_pattern_resample_rgb(alloc_type& alloc,
203 const rendering_buffer& src,
204 interpolator_type& inter,
205 const image_filter_lut& filter) :
206 base_type(alloc, src, color_type(0,0,0,0), inter, filter),
207 m_wrap_mode_x(src.width()),
208 m_wrap_mode_y(src.height())
211 //--------------------------------------------------------------------
212 void source_image(const rendering_buffer& src)
214 base_type::source_image(src);
215 m_wrap_mode_x = WrapModeX(src.width());
216 m_wrap_mode_y = WrapModeX(src.height());
219 //--------------------------------------------------------------------
220 color_type* generate(int x, int y, unsigned len)
222 color_type* span = base_type::allocator().span();
223 interpolator_type& intr = base_type::interpolator();
224 intr.begin(x + base_type::filter_dx_dbl(),
225 y + base_type::filter_dy_dbl(), len);
226 long_type fg[3];
228 int diameter = base_type::filter().diameter();
229 int filter_size = diameter << image_subpixel_shift;
230 const int16* weight_array = base_type::filter().weight_array();
234 int rx;
235 int ry;
236 int rx_inv = image_subpixel_size;
237 int ry_inv = image_subpixel_size;
238 intr.coordinates(&x, &y);
239 intr.local_scale(&rx, &ry);
241 rx = (rx * base_type::m_blur_x) >> image_subpixel_shift;
242 ry = (ry * base_type::m_blur_y) >> image_subpixel_shift;
244 if(rx < image_subpixel_size)
246 rx = image_subpixel_size;
248 else
250 if(rx > image_subpixel_size * base_type::m_scale_limit)
252 rx = image_subpixel_size * base_type::m_scale_limit;
254 rx_inv = image_subpixel_size * image_subpixel_size / rx;
257 if(ry < image_subpixel_size)
259 ry = image_subpixel_size;
261 else
263 if(ry > image_subpixel_size * base_type::m_scale_limit)
265 ry = image_subpixel_size * base_type::m_scale_limit;
267 ry_inv = image_subpixel_size * image_subpixel_size / ry;
270 int radius_x = (diameter * rx) >> 1;
271 int radius_y = (diameter * ry) >> 1;
272 int maxx = base_type::source_image().width() - 1;
273 int maxy = base_type::source_image().height() - 1;
275 x += base_type::filter_dx_int() - radius_x;
276 y += base_type::filter_dy_int() - radius_y;
278 fg[0] = fg[1] = fg[2] = image_filter_size / 2;
280 int y_lr = m_wrap_mode_y(y >> image_subpixel_shift);
281 int y_hr = ((image_subpixel_mask - (y & image_subpixel_mask)) *
282 ry_inv) >>
283 image_subpixel_shift;
284 int total_weight = 0;
285 int x_lr_ini = x >> image_subpixel_shift;
286 int x_hr_ini = ((image_subpixel_mask - (x & image_subpixel_mask)) *
287 rx_inv) >>
288 image_subpixel_shift;
292 int weight_y = weight_array[y_hr];
293 int x_lr = m_wrap_mode_x(x_lr_ini);
294 int x_hr = x_hr_ini;
295 const value_type* row_ptr = (const value_type*)base_type::source_image().row(y_lr);
298 const value_type* fg_ptr = row_ptr + x_lr * 3;
299 int weight = (weight_y * weight_array[x_hr] +
300 image_filter_size / 2) >>
301 downscale_shift;
302 fg[0] += fg_ptr[0] * weight;
303 fg[1] += fg_ptr[1] * weight;
304 fg[2] += fg_ptr[2] * weight;
305 total_weight += weight;
306 x_hr += rx_inv;
307 x_lr = ++m_wrap_mode_x;
309 while(x_hr < filter_size);
310 y_hr += ry_inv;
311 y_lr = ++m_wrap_mode_y;
313 while(y_hr < filter_size);
315 fg[0] /= total_weight;
316 fg[1] /= total_weight;
317 fg[2] /= total_weight;
319 if(fg[0] < 0) fg[0] = 0;
320 if(fg[1] < 0) fg[1] = 0;
321 if(fg[2] < 0) fg[2] = 0;
323 if(fg[0] > base_mask) fg[0] = base_mask;
324 if(fg[1] > base_mask) fg[1] = base_mask;
325 if(fg[2] > base_mask) fg[2] = base_mask;
327 span->r = (value_type)fg[order_type::R];
328 span->g = (value_type)fg[order_type::G];
329 span->b = (value_type)fg[order_type::B];
330 span->a = (value_type)base_mask;
332 ++span;
333 ++intr;
334 } while(--len);
335 return base_type::allocator().span();
338 private:
339 WrapModeX m_wrap_mode_x;
340 WrapModeY m_wrap_mode_y;
346 #endif