Debian Codegraph @

extern/agg24-svn/include/agg_blur.h

raw
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
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: [email protected]
12 // [email protected]
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // The Stack Blur Algorithm was invented by Mario Klingemann,
17 // [email protected] and described here:
18 // http://incubator.quasimondo.com/processing/fast_blur_deluxe.php
19 // (search phrase "Stackblur: Fast But Goodlooking").
20 // The major improvement is that there's no more division table
21 // that was very expensive to create for large blur radii. Insted,
22 // for 8-bit per channel and radius not exceeding 254 the division is
23 // replaced by multiplication and shift.
24 //
25 //----------------------------------------------------------------------------
26
27 #ifndef AGG_BLUR_INCLUDED
28 #define AGG_BLUR_INCLUDED
29
30 #include "agg_array.h"
31 #include "agg_pixfmt_base.h"
32 #include "agg_pixfmt_transposer.h"
33
34 namespace agg
35 {
36
37 template<class T> struct stack_blur_tables
38 {
39 static int16u const g_stack_blur8_mul[255];
40 static int8u const g_stack_blur8_shr[255];
41 };
42
43 //------------------------------------------------------------------------
44 template<class T>
45 int16u const stack_blur_tables<T>::g_stack_blur8_mul[255] =
46 {
47 512,512,456,512,328,456,335,512,405,328,271,456,388,335,292,512,
48 454,405,364,328,298,271,496,456,420,388,360,335,312,292,273,512,
49 482,454,428,405,383,364,345,328,312,298,284,271,259,496,475,456,
50 437,420,404,388,374,360,347,335,323,312,302,292,282,273,265,512,
51 497,482,468,454,441,428,417,405,394,383,373,364,354,345,337,328,
52 320,312,305,298,291,284,278,271,265,259,507,496,485,475,465,456,
53 446,437,428,420,412,404,396,388,381,374,367,360,354,347,341,335,
54 329,323,318,312,307,302,297,292,287,282,278,273,269,265,261,512,
55 505,497,489,482,475,468,461,454,447,441,435,428,422,417,411,405,
56 399,394,389,383,378,373,368,364,359,354,350,345,341,337,332,328,
57 324,320,316,312,309,305,301,298,294,291,287,284,281,278,274,271,
58 268,265,262,259,257,507,501,496,491,485,480,475,470,465,460,456,
59 451,446,442,437,433,428,424,420,416,412,408,404,400,396,392,388,
60 385,381,377,374,370,367,363,360,357,354,350,347,344,341,338,335,
61 332,329,326,323,320,318,315,312,310,307,304,302,299,297,294,292,
62 289,287,285,282,280,278,275,273,271,269,267,265,263,261,259
63 };
64
65 //------------------------------------------------------------------------
66 template<class T>
67 int8u const stack_blur_tables<T>::g_stack_blur8_shr[255] =
68 {
69 9, 11, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17,
70 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19,
71 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20,
72 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21,
73 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
74 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22,
75 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
76 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23,
77 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
78 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
79 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
80 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
81 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
82 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
83 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
84 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24
85 };
86
87
88
89 //==============================================================stack_blur
90 template<class ColorT, class CalculatorT> class stack_blur
91 {
92 public:
93 typedef ColorT color_type;
94 typedef CalculatorT calculator_type;
95
96 //--------------------------------------------------------------------
97 template<class Img> void blur_x(Img& img, unsigned radius)
98 {
99 if(radius < 1) return;
100
101 unsigned x, y, xp, i;
102 unsigned stack_ptr;
103 unsigned stack_start;
104
105 color_type pix;
106 color_type* stack_pix;
107 calculator_type sum;
108 calculator_type sum_in;
109 calculator_type sum_out;
110
111 unsigned w = img.width();
112 unsigned h = img.height();
113 unsigned wm = w - 1;
114 unsigned div = radius * 2 + 1;
115
116 unsigned div_sum = (radius + 1) * (radius + 1);
117 unsigned mul_sum = 0;
118 unsigned shr_sum = 0;
119 unsigned max_val = color_type::base_mask;
120
121 if(max_val <= 255 && radius < 255)
122 {
123 mul_sum = stack_blur_tables<int>::g_stack_blur8_mul[radius];
124 shr_sum = stack_blur_tables<int>::g_stack_blur8_shr[radius];
125 }
126
127 m_buf.allocate(w, 128);
128 m_stack.allocate(div, 32);
129
130 for(y = 0; y < h; y++)
131 {
132 sum.clear();
133 sum_in.clear();
134 sum_out.clear();
135
136 pix = img.pixel(0, y);
137 for(i = 0; i <= radius; i++)
138 {
139 m_stack[i] = pix;
140 sum.add(pix, i + 1);
141 sum_out.add(pix);
142 }
143 for(i = 1; i <= radius; i++)
144 {
145 pix = img.pixel((i > wm) ? wm : i, y);
146 m_stack[i + radius] = pix;
147 sum.add(pix, radius + 1 - i);
148 sum_in.add(pix);
149 }
150
151 stack_ptr = radius;
152 for(x = 0; x < w; x++)
153 {
154 if(mul_sum) sum.calc_pix(m_buf[x], mul_sum, shr_sum);
155 else sum.calc_pix(m_buf[x], div_sum);
156
157 sum.sub(sum_out);
158
159 stack_start = stack_ptr + div - radius;
160 if(stack_start >= div) stack_start -= div;
161 stack_pix = &m_stack[stack_start];
162
163 sum_out.sub(*stack_pix);
164
165 xp = x + radius + 1;
166 if(xp > wm) xp = wm;
167 pix = img.pixel(xp, y);
168
169 *stack_pix = pix;
170
171 sum_in.add(pix);
172 sum.add(sum_in);
173
174 ++stack_ptr;
175 if(stack_ptr >= div) stack_ptr = 0;
176 stack_pix = &m_stack[stack_ptr];
177
178 sum_out.add(*stack_pix);
179 sum_in.sub(*stack_pix);
180 }
181 img.copy_color_hspan(0, y, w, &m_buf[0]);
182 }
183 }
184
185 //--------------------------------------------------------------------
186 template<class Img> void blur_y(Img& img, unsigned radius)
187 {
188 pixfmt_transposer<Img> img2(img);
189 blur_x(img2, radius);
190 }
191
192 //--------------------------------------------------------------------
193 template<class Img> void blur(Img& img, unsigned radius)
194 {
195 blur_x(img, radius);
196 pixfmt_transposer<Img> img2(img);
197 blur_x(img2, radius);
198 }
199
200 private:
201 pod_vector<color_type> m_buf;
202 pod_vector<color_type> m_stack;
203 };
204
205 //====================================================stack_blur_calc_rgba
206 template<class T=unsigned> struct stack_blur_calc_rgba
207 {
208 typedef T value_type;
209 value_type r,g,b,a;
210
211 AGG_INLINE void clear()
212 {
213 r = g = b = a = 0;
214 }
215
216 template<class ArgT> AGG_INLINE void add(const ArgT& v)
217 {
218 r += v.r;
219 g += v.g;
220 b += v.b;
221 a += v.a;
222 }
223
224 template<class ArgT> AGG_INLINE void add(const ArgT& v, unsigned k)
225 {
226 r += v.r * k;
227 g += v.g * k;
228 b += v.b * k;
229 a += v.a * k;
230 }
231
232 template<class ArgT> AGG_INLINE void sub(const ArgT& v)
233 {
234 r -= v.r;
235 g -= v.g;
236 b -= v.b;
237 a -= v.a;
238 }
239
240 template<class ArgT> AGG_INLINE void calc_pix(ArgT& v, unsigned div)
241 {
242 typedef typename ArgT::value_type value_type;
243 v.r = value_type(r / div);
244 v.g = value_type(g / div);
245 v.b = value_type(b / div);
246 v.a = value_type(a / div);
247 }
248
249 template<class ArgT>
250 AGG_INLINE void calc_pix(ArgT& v, unsigned mul, unsigned shr)
251 {
252 typedef typename ArgT::value_type value_type;
253 v.r = value_type((r * mul) >> shr);
254 v.g = value_type((g * mul) >> shr);
255 v.b = value_type((b * mul) >> shr);
256 v.a = value_type((a * mul) >> shr);
257 }
258 };
259
260
261 //=====================================================stack_blur_calc_rgb
262 template<class T=unsigned> struct stack_blur_calc_rgb
263 {
264 typedef T value_type;
265 value_type r,g,b;
266
267 AGG_INLINE void clear()
268 {
269 r = g = b = 0;
270 }
271
272 template<class ArgT> AGG_INLINE void add(const ArgT& v)
273 {
274 r += v.r;
275 g += v.g;
276 b += v.b;
277 }
278
279 template<class ArgT> AGG_INLINE void add(const ArgT& v, unsigned k)
280 {
281 r += v.r * k;
282 g += v.g * k;
283 b += v.b * k;
284 }
285
286 template<class ArgT> AGG_INLINE void sub(const ArgT& v)
287 {
288 r -= v.r;
289 g -= v.g;
290 b -= v.b;
291 }
292
293 template<class ArgT> AGG_INLINE void calc_pix(ArgT& v, unsigned div)
294 {
295 typedef typename ArgT::value_type value_type;
296 v.r = value_type(r / div);
297 v.g = value_type(g / div);
298 v.b = value_type(b / div);
299 }
300
301 template<class ArgT>
302 AGG_INLINE void calc_pix(ArgT& v, unsigned mul, unsigned shr)
303 {
304 typedef typename ArgT::value_type value_type;
305 v.r = value_type((r * mul) >> shr);
306 v.g = value_type((g * mul) >> shr);
307 v.b = value_type((b * mul) >> shr);
308 }
309 };
310
311
312 //====================================================stack_blur_calc_gray
313 template<class T=unsigned> struct stack_blur_calc_gray
314 {
315 typedef T value_type;
316 value_type v;
317
318 AGG_INLINE void clear()
319 {
320 v = 0;
321 }
322
323 template<class ArgT> AGG_INLINE void add(const ArgT& a)
324 {
325 v += a.v;
326 }
327
328 template<class ArgT> AGG_INLINE void add(const ArgT& a, unsigned k)
329 {
330 v += a.v * k;
331 }
332
333 template<class ArgT> AGG_INLINE void sub(const ArgT& a)
334 {
335 v -= a.v;
336 }
337
338 template<class ArgT> AGG_INLINE void calc_pix(ArgT& a, unsigned div)
339 {
340 typedef typename ArgT::value_type value_type;
341 a.v = value_type(v / div);
342 }
343
344 template<class ArgT>
345 AGG_INLINE void calc_pix(ArgT& a, unsigned mul, unsigned shr)
346 {
347 typedef typename ArgT::value_type value_type;
348 a.v = value_type((v * mul) >> shr);
349 }
350 };
351
352
353
354 //========================================================stack_blur_gray8
355 template<class Img>
356 void stack_blur_gray8(Img& img, unsigned rx, unsigned ry)
357 {
358 unsigned x, y, xp, yp, i;
359 unsigned stack_ptr;
360 unsigned stack_start;
361
362 const int8u* src_pix_ptr;
363 int8u* dst_pix_ptr;
364 unsigned pix;
365 unsigned stack_pix;
366 unsigned sum;
367 unsigned sum_in;
368 unsigned sum_out;
369
370 unsigned w = img.width();
371 unsigned h = img.height();
372 unsigned wm = w - 1;
373 unsigned hm = h - 1;
374
375 unsigned div;
376 unsigned mul_sum;
377 unsigned shr_sum;
378
379 pod_vector<int8u> stack;
380
381 if(rx > 0)
382 {
383 if(rx > 254) rx = 254;
384 div = rx * 2 + 1;
385 mul_sum = stack_blur_tables<int>::g_stack_blur8_mul[rx];
386 shr_sum = stack_blur_tables<int>::g_stack_blur8_shr[rx];
387 stack.allocate(div);
388
389 for(y = 0; y < h; y++)
390 {
391 sum = sum_in = sum_out = 0;
392
393 src_pix_ptr = img.pix_ptr(0, y);
394 pix = *src_pix_ptr;
395 for(i = 0; i <= rx; i++)
396 {
397 stack[i] = pix;
398 sum += pix * (i + 1);
399 sum_out += pix;
400 }
401 for(i = 1; i <= rx; i++)
402 {
403 if(i <= wm) src_pix_ptr += Img::pix_width;
404 pix = *src_pix_ptr;
405 stack[i + rx] = pix;
406 sum += pix * (rx + 1 - i);
407 sum_in += pix;
408 }
409
410 stack_ptr = rx;
411 xp = rx;
412 if(xp > wm) xp = wm;
413 src_pix_ptr = img.pix_ptr(xp, y);
414 dst_pix_ptr = img.pix_ptr(0, y);
415 for(x = 0; x < w; x++)
416 {
417 *dst_pix_ptr = (sum * mul_sum) >> shr_sum;
418 dst_pix_ptr += Img::pix_width;
419
420 sum -= sum_out;
421
422 stack_start = stack_ptr + div - rx;
423 if(stack_start >= div) stack_start -= div;
424 sum_out -= stack[stack_start];
425
426 if(xp < wm)
427 {
428 src_pix_ptr += Img::pix_width;
429 pix = *src_pix_ptr;
430 ++xp;
431 }
432
433 stack[stack_start] = pix;
434
435 sum_in += pix;
436 sum += sum_in;
437
438 ++stack_ptr;
439 if(stack_ptr >= div) stack_ptr = 0;
440 stack_pix = stack[stack_ptr];
441
442 sum_out += stack_pix;
443 sum_in -= stack_pix;
444 }
445 }
446 }
447
448 if(ry > 0)
449 {
450 if(ry > 254) ry = 254;
451 div = ry * 2 + 1;
452 mul_sum = stack_blur_tables<int>::g_stack_blur8_mul[ry];
453 shr_sum = stack_blur_tables<int>::g_stack_blur8_shr[ry];
454 stack.allocate(div);
455
456 int stride = img.stride();
457 for(x = 0; x < w; x++)
458 {
459 sum = sum_in = sum_out = 0;
460
461 src_pix_ptr = img.pix_ptr(x, 0);
462 pix = *src_pix_ptr;
463 for(i = 0; i <= ry; i++)
464 {
465 stack[i] = pix;
466 sum += pix * (i + 1);
467 sum_out += pix;
468 }
469 for(i = 1; i <= ry; i++)
470 {
471 if(i <= hm) src_pix_ptr += stride;
472 pix = *src_pix_ptr;
473 stack[i + ry] = pix;
474 sum += pix * (ry + 1 - i);
475 sum_in += pix;
476 }
477
478 stack_ptr = ry;
479 yp = ry;
480 if(yp > hm) yp = hm;
481 src_pix_ptr = img.pix_ptr(x, yp);
482 dst_pix_ptr = img.pix_ptr(x, 0);
483 for(y = 0; y < h; y++)
484 {
485 *dst_pix_ptr = (sum * mul_sum) >> shr_sum;
486 dst_pix_ptr += stride;
487
488 sum -= sum_out;
489
490 stack_start = stack_ptr + div - ry;
491 if(stack_start >= div) stack_start -= div;
492 sum_out -= stack[stack_start];
493
494 if(yp < hm)
495 {
496 src_pix_ptr += stride;
497 pix = *src_pix_ptr;
498 ++yp;
499 }
500
501 stack[stack_start] = pix;
502
503 sum_in += pix;
504 sum += sum_in;
505
506 ++stack_ptr;
507 if(stack_ptr >= div) stack_ptr = 0;
508 stack_pix = stack[stack_ptr];
509
510 sum_out += stack_pix;
511 sum_in -= stack_pix;
512 }
513 }
514 }
515 }
516
517
518
519 //========================================================stack_blur_rgb24
520 template<class Img>
521 void stack_blur_rgb24(Img& img, unsigned rx, unsigned ry)
522 {
523 typedef typename Img::color_type color_type;
524 typedef typename Img::order_type order_type;
525 enum order_e
526 {
527 R = order_type::R,
528 G = order_type::G,
529 B = order_type::B
530 };
531
532 unsigned x, y, xp, yp, i;
533 unsigned stack_ptr;
534 unsigned stack_start;
535
536 const int8u* src_pix_ptr;
537 int8u* dst_pix_ptr;
538 color_type* stack_pix_ptr;
539
540 unsigned sum_r;
541 unsigned sum_g;
542 unsigned sum_b;
543 unsigned sum_in_r;
544 unsigned sum_in_g;
545 unsigned sum_in_b;
546 unsigned sum_out_r;
547 unsigned sum_out_g;
548 unsigned sum_out_b;
549
550 unsigned w = img.width();
551 unsigned h = img.height();
552 unsigned wm = w - 1;
553 unsigned hm = h - 1;
554
555 unsigned div;
556 unsigned mul_sum;
557 unsigned shr_sum;
558
559 pod_vector<color_type> stack;
560
561 if(rx > 0)
562 {
563 if(rx > 254) rx = 254;
564 div = rx * 2 + 1;
565 mul_sum = stack_blur_tables<int>::g_stack_blur8_mul[rx];
566 shr_sum = stack_blur_tables<int>::g_stack_blur8_shr[rx];
567 stack.allocate(div);
568
569 for(y = 0; y < h; y++)
570 {
571 sum_r =
572 sum_g =
573 sum_b =
574 sum_in_r =
575 sum_in_g =
576 sum_in_b =
577 sum_out_r =
578 sum_out_g =
579 sum_out_b = 0;
580
581 src_pix_ptr = img.pix_ptr(0, y);
582 for(i = 0; i <= rx; i++)
583 {
584 stack_pix_ptr = &stack[i];
585 stack_pix_ptr->r = src_pix_ptr[R];
586 stack_pix_ptr->g = src_pix_ptr[G];
587 stack_pix_ptr->b = src_pix_ptr[B];
588 sum_r += src_pix_ptr[R] * (i + 1);
589 sum_g += src_pix_ptr[G] * (i + 1);
590 sum_b += src_pix_ptr[B] * (i + 1);
591 sum_out_r += src_pix_ptr[R];
592 sum_out_g += src_pix_ptr[G];
593 sum_out_b += src_pix_ptr[B];
594 }
595 for(i = 1; i <= rx; i++)
596 {
597 if(i <= wm) src_pix_ptr += Img::pix_width;
598 stack_pix_ptr = &stack[i + rx];
599 stack_pix_ptr->r = src_pix_ptr[R];
600 stack_pix_ptr->g = src_pix_ptr[G];
601 stack_pix_ptr->b = src_pix_ptr[B];
602 sum_r += src_pix_ptr[R] * (rx + 1 - i);
603 sum_g += src_pix_ptr[G] * (rx + 1 - i);
604 sum_b += src_pix_ptr[B] * (rx + 1 - i);
605 sum_in_r += src_pix_ptr[R];
606 sum_in_g += src_pix_ptr[G];
607 sum_in_b += src_pix_ptr[B];
608 }
609
610 stack_ptr = rx;
611 xp = rx;
612 if(xp > wm) xp = wm;
613 src_pix_ptr = img.pix_ptr(xp, y);
614 dst_pix_ptr = img.pix_ptr(0, y);
615 for(x = 0; x < w; x++)
616 {
617 dst_pix_ptr[R] = (sum_r * mul_sum) >> shr_sum;
618 dst_pix_ptr[G] = (sum_g * mul_sum) >> shr_sum;
619 dst_pix_ptr[B] = (sum_b * mul_sum) >> shr_sum;
620 dst_pix_ptr += Img::pix_width;
621
622 sum_r -= sum_out_r;
623 sum_g -= sum_out_g;
624 sum_b -= sum_out_b;
625
626 stack_start = stack_ptr + div - rx;
627 if(stack_start >= div) stack_start -= div;
628 stack_pix_ptr = &stack[stack_start];
629
630 sum_out_r -= stack_pix_ptr->r;
631 sum_out_g -= stack_pix_ptr->g;
632 sum_out_b -= stack_pix_ptr->b;
633
634 if(xp < wm)
635 {
636 src_pix_ptr += Img::pix_width;
637 ++xp;
638 }
639
640 stack_pix_ptr->r = src_pix_ptr[R];
641 stack_pix_ptr->g = src_pix_ptr[G];
642 stack_pix_ptr->b = src_pix_ptr[B];
643
644 sum_in_r += src_pix_ptr[R];
645 sum_in_g += src_pix_ptr[G];
646 sum_in_b += src_pix_ptr[B];
647 sum_r += sum_in_r;
648 sum_g += sum_in_g;
649 sum_b += sum_in_b;
650
651 ++stack_ptr;
652 if(stack_ptr >= div) stack_ptr = 0;
653 stack_pix_ptr = &stack[stack_ptr];
654
655 sum_out_r += stack_pix_ptr->r;
656 sum_out_g += stack_pix_ptr->g;
657 sum_out_b += stack_pix_ptr->b;
658 sum_in_r -= stack_pix_ptr->r;
659 sum_in_g -= stack_pix_ptr->g;
660 sum_in_b -= stack_pix_ptr->b;
661 }
662 }
663 }
664
665 if(ry > 0)
666 {
667 if(ry > 254) ry = 254;
668 div = ry * 2 + 1;
669 mul_sum = stack_blur_tables<int>::g_stack_blur8_mul[ry];
670 shr_sum = stack_blur_tables<int>::g_stack_blur8_shr[ry];
671 stack.allocate(div);
672
673 int stride = img.stride();
674 for(x = 0; x < w; x++)
675 {
676 sum_r =
677 sum_g =
678 sum_b =
679 sum_in_r =
680 sum_in_g =
681 sum_in_b =
682 sum_out_r =
683 sum_out_g =
684 sum_out_b = 0;
685
686 src_pix_ptr = img.pix_ptr(x, 0);
687 for(i = 0; i <= ry; i++)
688 {
689 stack_pix_ptr = &stack[i];
690 stack_pix_ptr->r = src_pix_ptr[R];
691 stack_pix_ptr->g = src_pix_ptr[G];
692 stack_pix_ptr->b = src_pix_ptr[B];
693 sum_r += src_pix_ptr[R] * (i + 1);
694 sum_g += src_pix_ptr[G] * (i + 1);
695 sum_b += src_pix_ptr[B] * (i + 1);
696 sum_out_r += src_pix_ptr[R];
697 sum_out_g += src_pix_ptr[G];
698 sum_out_b += src_pix_ptr[B];
699 }
700 for(i = 1; i <= ry; i++)
701 {
702 if(i <= hm) src_pix_ptr += stride;
703 stack_pix_ptr = &stack[i + ry];
704 stack_pix_ptr->r = src_pix_ptr[R];
705 stack_pix_ptr->g = src_pix_ptr[G];
706 stack_pix_ptr->b = src_pix_ptr[B];
707 sum_r += src_pix_ptr[R] * (ry + 1 - i);
708 sum_g += src_pix_ptr[G] * (ry + 1 - i);
709 sum_b += src_pix_ptr[B] * (ry + 1 - i);
710 sum_in_r += src_pix_ptr[R];
711 sum_in_g += src_pix_ptr[G];
712 sum_in_b += src_pix_ptr[B];
713 }
714
715 stack_ptr = ry;
716 yp = ry;
717 if(yp > hm) yp = hm;
718 src_pix_ptr = img.pix_ptr(x, yp);
719 dst_pix_ptr = img.pix_ptr(x, 0);
720 for(y = 0; y < h; y++)
721 {
722 dst_pix_ptr[R] = (sum_r * mul_sum) >> shr_sum;
723 dst_pix_ptr[G] = (sum_g * mul_sum) >> shr_sum;
724 dst_pix_ptr[B] = (sum_b * mul_sum) >> shr_sum;
725 dst_pix_ptr += stride;
726
727 sum_r -= sum_out_r;
728 sum_g -= sum_out_g;
729 sum_b -= sum_out_b;
730
731 stack_start = stack_ptr + div - ry;
732 if(stack_start >= div) stack_start -= div;
733
734 stack_pix_ptr = &stack[stack_start];
735 sum_out_r -= stack_pix_ptr->r;
736 sum_out_g -= stack_pix_ptr->g;
737 sum_out_b -= stack_pix_ptr->b;
738
739 if(yp < hm)
740 {
741 src_pix_ptr += stride;
742 ++yp;
743 }
744
745 stack_pix_ptr->r = src_pix_ptr[R];
746 stack_pix_ptr->g = src_pix_ptr[G];
747 stack_pix_ptr->b = src_pix_ptr[B];
748
749 sum_in_r += src_pix_ptr[R];
750 sum_in_g += src_pix_ptr[G];
751 sum_in_b += src_pix_ptr[B];
752 sum_r += sum_in_r;
753 sum_g += sum_in_g;
754 sum_b += sum_in_b;
755
756 ++stack_ptr;
757 if(stack_ptr >= div) stack_ptr = 0;
758 stack_pix_ptr = &stack[stack_ptr];
759
760 sum_out_r += stack_pix_ptr->r;
761 sum_out_g += stack_pix_ptr->g;
762 sum_out_b += stack_pix_ptr->b;
763 sum_in_r -= stack_pix_ptr->r;
764 sum_in_g -= stack_pix_ptr->g;
765 sum_in_b -= stack_pix_ptr->b;
766 }
767 }
768 }
769 }
770
771
772
773 //=======================================================stack_blur_rgba32
774 template<class Img>
775 void stack_blur_rgba32(Img& img, unsigned rx, unsigned ry)
776 {
777 typedef typename Img::color_type color_type;
778 typedef typename Img::order_type order_type;
779 enum order_e
780 {
781 R = order_type::R,
782 G = order_type::G,
783 B = order_type::B,
784 A = order_type::A
785 };
786
787 unsigned x, y, xp, yp, i;
788 unsigned stack_ptr;
789 unsigned stack_start;
790
791 const int8u* src_pix_ptr;
792 int8u* dst_pix_ptr;
793 color_type* stack_pix_ptr;
794
795 unsigned sum_r;
796 unsigned sum_g;
797 unsigned sum_b;
798 unsigned sum_a;
799 unsigned sum_in_r;
800 unsigned sum_in_g;
801 unsigned sum_in_b;
802 unsigned sum_in_a;
803 unsigned sum_out_r;
804 unsigned sum_out_g;
805 unsigned sum_out_b;
806 unsigned sum_out_a;
807
808 unsigned w = img.width();
809 unsigned h = img.height();
810 unsigned wm = w - 1;
811 unsigned hm = h - 1;
812
813 unsigned div;
814 unsigned mul_sum;
815 unsigned shr_sum;
816
817 pod_vector<color_type> stack;
818
819 if(rx > 0)
820 {
821 if(rx > 254) rx = 254;
822 div = rx * 2 + 1;
823 mul_sum = stack_blur_tables<int>::g_stack_blur8_mul[rx];
824 shr_sum = stack_blur_tables<int>::g_stack_blur8_shr[rx];
825 stack.allocate(div);
826
827 for(y = 0; y < h; y++)
828 {
829 sum_r =
830 sum_g =
831 sum_b =
832 sum_a =
833 sum_in_r =
834 sum_in_g =
835 sum_in_b =
836 sum_in_a =
837 sum_out_r =
838 sum_out_g =
839 sum_out_b =
840 sum_out_a = 0;
841
842 src_pix_ptr = img.pix_ptr(0, y);
843 for(i = 0; i <= rx; i++)
844 {
845 stack_pix_ptr = &stack[i];
846 stack_pix_ptr->r = src_pix_ptr[R];
847 stack_pix_ptr->g = src_pix_ptr[G];
848 stack_pix_ptr->b = src_pix_ptr[B];
849 stack_pix_ptr->a = src_pix_ptr[A];
850 sum_r += src_pix_ptr[R] * (i + 1);
851 sum_g += src_pix_ptr[G] * (i + 1);
852 sum_b += src_pix_ptr[B] * (i + 1);
853 sum_a += src_pix_ptr[A] * (i + 1);
854 sum_out_r += src_pix_ptr[R];
855 sum_out_g += src_pix_ptr[G];
856 sum_out_b += src_pix_ptr[B];
857 sum_out_a += src_pix_ptr[A];
858 }
859 for(i = 1; i <= rx; i++)
860 {
861 if(i <= wm) src_pix_ptr += Img::pix_width;
862 stack_pix_ptr = &stack[i + rx];
863 stack_pix_ptr->r = src_pix_ptr[R];
864 stack_pix_ptr->g = src_pix_ptr[G];
865 stack_pix_ptr->b = src_pix_ptr[B];
866 stack_pix_ptr->a = src_pix_ptr[A];
867 sum_r += src_pix_ptr[R] * (rx + 1 - i);
868 sum_g += src_pix_ptr[G] * (rx + 1 - i);
869 sum_b += src_pix_ptr[B] * (rx + 1 - i);
870 sum_a += src_pix_ptr[A] * (rx + 1 - i);
871 sum_in_r += src_pix_ptr[R];
872 sum_in_g += src_pix_ptr[G];
873 sum_in_b += src_pix_ptr[B];
874 sum_in_a += src_pix_ptr[A];
875 }
876
877 stack_ptr = rx;
878 xp = rx;
879 if(xp > wm) xp = wm;
880 src_pix_ptr = img.pix_ptr(xp, y);
881 dst_pix_ptr = img.pix_ptr(0, y);
882 for(x = 0; x < w; x++)
883 {
884 dst_pix_ptr[R] = (sum_r * mul_sum) >> shr_sum;
885 dst_pix_ptr[G] = (sum_g * mul_sum) >> shr_sum;
886 dst_pix_ptr[B] = (sum_b * mul_sum) >> shr_sum;
887 dst_pix_ptr[A] = (sum_a * mul_sum) >> shr_sum;
888 dst_pix_ptr += Img::pix_width;
889
890 sum_r -= sum_out_r;
891 sum_g -= sum_out_g;
892 sum_b -= sum_out_b;
893 sum_a -= sum_out_a;
894
895 stack_start = stack_ptr + div - rx;
896 if(stack_start >= div) stack_start -= div;
897 stack_pix_ptr = &stack[stack_start];
898
899 sum_out_r -= stack_pix_ptr->r;
900 sum_out_g -= stack_pix_ptr->g;
901 sum_out_b -= stack_pix_ptr->b;
902 sum_out_a -= stack_pix_ptr->a;
903
904 if(xp < wm)
905 {
906 src_pix_ptr += Img::pix_width;
907 ++xp;
908 }
909
910 stack_pix_ptr->r = src_pix_ptr[R];
911 stack_pix_ptr->g = src_pix_ptr[G];
912 stack_pix_ptr->b = src_pix_ptr[B];
913 stack_pix_ptr->a = src_pix_ptr[A];
914
915 sum_in_r += src_pix_ptr[R];
916 sum_in_g += src_pix_ptr[G];
917 sum_in_b += src_pix_ptr[B];
918 sum_in_a += src_pix_ptr[A];
919 sum_r += sum_in_r;
920 sum_g += sum_in_g;
921 sum_b += sum_in_b;
922 sum_a += sum_in_a;
923
924 ++stack_ptr;
925 if(stack_ptr >= div) stack_ptr = 0;
926 stack_pix_ptr = &stack[stack_ptr];
927
928 sum_out_r += stack_pix_ptr->r;
929 sum_out_g += stack_pix_ptr->g;
930 sum_out_b += stack_pix_ptr->b;
931 sum_out_a += stack_pix_ptr->a;
932 sum_in_r -= stack_pix_ptr->r;
933 sum_in_g -= stack_pix_ptr->g;
934 sum_in_b -= stack_pix_ptr->b;
935 sum_in_a -= stack_pix_ptr->a;
936 }
937 }
938 }
939
940 if(ry > 0)
941 {
942 if(ry > 254) ry = 254;
943 div = ry * 2 + 1;
944 mul_sum = stack_blur_tables<int>::g_stack_blur8_mul[ry];
945 shr_sum = stack_blur_tables<int>::g_stack_blur8_shr[ry];
946 stack.allocate(div);
947
948 int stride = img.stride();
949 for(x = 0; x < w; x++)
950 {
951 sum_r =
952 sum_g =
953 sum_b =
954 sum_a =
955 sum_in_r =
956 sum_in_g =
957 sum_in_b =
958 sum_in_a =
959 sum_out_r =
960 sum_out_g =
961 sum_out_b =
962 sum_out_a = 0;
963
964 src_pix_ptr = img.pix_ptr(x, 0);
965 for(i = 0; i <= ry; i++)
966 {
967 stack_pix_ptr = &stack[i];
968 stack_pix_ptr->r = src_pix_ptr[R];
969 stack_pix_ptr->g = src_pix_ptr[G];
970 stack_pix_ptr->b = src_pix_ptr[B];
971 stack_pix_ptr->a = src_pix_ptr[A];
972 sum_r += src_pix_ptr[R] * (i + 1);
973 sum_g += src_pix_ptr[G] * (i + 1);
974 sum_b += src_pix_ptr[B] * (i + 1);
975 sum_a += src_pix_ptr[A] * (i + 1);
976 sum_out_r += src_pix_ptr[R];
977 sum_out_g += src_pix_ptr[G];
978 sum_out_b += src_pix_ptr[B];
979 sum_out_a += src_pix_ptr[A];
980 }
981 for(i = 1; i <= ry; i++)
982 {
983 if(i <= hm) src_pix_ptr += stride;
984 stack_pix_ptr = &stack[i + ry];
985 stack_pix_ptr->r = src_pix_ptr[R];
986 stack_pix_ptr->g = src_pix_ptr[G];
987 stack_pix_ptr->b = src_pix_ptr[B];
988 stack_pix_ptr->a = src_pix_ptr[A];
989 sum_r += src_pix_ptr[R] * (ry + 1 - i);
990 sum_g += src_pix_ptr[G] * (ry + 1 - i);
991 sum_b += src_pix_ptr[B] * (ry + 1 - i);
992 sum_a += src_pix_ptr[A] * (ry + 1 - i);
993 sum_in_r += src_pix_ptr[R];
994 sum_in_g += src_pix_ptr[G];
995 sum_in_b += src_pix_ptr[B];
996 sum_in_a += src_pix_ptr[A];
997 }
998
999 stack_ptr = ry;
1000 yp = ry;
1001 if(yp > hm) yp = hm;
1002 src_pix_ptr = img.pix_ptr(x, yp);
1003 dst_pix_ptr = img.pix_ptr(x, 0);
1004 for(y = 0; y < h; y++)
1005 {
1006 dst_pix_ptr[R] = (sum_r * mul_sum) >> shr_sum;
1007 dst_pix_ptr[G] = (sum_g * mul_sum) >> shr_sum;
1008 dst_pix_ptr[B] = (sum_b * mul_sum) >> shr_sum;
1009 dst_pix_ptr[A] = (sum_a * mul_sum) >> shr_sum;
1010 dst_pix_ptr += stride;
1011
1012 sum_r -= sum_out_r;
1013 sum_g -= sum_out_g;
1014 sum_b -= sum_out_b;
1015 sum_a -= sum_out_a;
1016
1017 stack_start = stack_ptr + div - ry;
1018 if(stack_start >= div) stack_start -= div;
1019
1020 stack_pix_ptr = &stack[stack_start];
1021 sum_out_r -= stack_pix_ptr->r;
1022 sum_out_g -= stack_pix_ptr->g;
1023 sum_out_b -= stack_pix_ptr->b;
1024 sum_out_a -= stack_pix_ptr->a;
1025
1026 if(yp < hm)
1027 {
1028 src_pix_ptr += stride;
1029 ++yp;
1030 }
1031
1032 stack_pix_ptr->r = src_pix_ptr[R];
1033 stack_pix_ptr->g = src_pix_ptr[G];
1034 stack_pix_ptr->b = src_pix_ptr[B];
1035 stack_pix_ptr->a = src_pix_ptr[A];
1036
1037 sum_in_r += src_pix_ptr[R];
1038 sum_in_g += src_pix_ptr[G];
1039 sum_in_b += src_pix_ptr[B];
1040 sum_in_a += src_pix_ptr[A];
1041 sum_r += sum_in_r;
1042 sum_g += sum_in_g;
1043 sum_b += sum_in_b;
1044 sum_a += sum_in_a;
1045
1046 ++stack_ptr;
1047 if(stack_ptr >= div) stack_ptr = 0;
1048 stack_pix_ptr = &stack[stack_ptr];
1049
1050 sum_out_r += stack_pix_ptr->r;
1051 sum_out_g += stack_pix_ptr->g;
1052 sum_out_b += stack_pix_ptr->b;
1053 sum_out_a += stack_pix_ptr->a;
1054 sum_in_r -= stack_pix_ptr->r;
1055 sum_in_g -= stack_pix_ptr->g;
1056 sum_in_b -= stack_pix_ptr->b;
1057 sum_in_a -= stack_pix_ptr->a;
1058 }
1059 }
1060 }
1061 }
1062
1063
1064
1065 //===========================================================recursive_blur
1066 template<class ColorT, class CalculatorT> class recursive_blur
1067 {
1068 public:
1069 typedef ColorT color_type;
1070 typedef CalculatorT calculator_type;
1071 typedef typename color_type::value_type value_type;
1072 typedef typename calculator_type::value_type calc_type;
1073
1074 //--------------------------------------------------------------------
1075 template<class Img> void blur_x(Img& img, double radius)
1076 {
1077 if(radius < 0.62) return;
1078 if(img.width() < 3) return;
1079
1080 calc_type s = calc_type(radius * 0.5);
1081 calc_type q = calc_type((s < 2.5) ?
1082 3.97156 - 4.14554 * sqrt(1 - 0.26891 * s) :
1083 0.98711 * s - 0.96330);
1084
1085 calc_type q2 = calc_type(q * q);
1086 calc_type q3 = calc_type(q2 * q);
1087
1088 calc_type b0 = calc_type(1.0 / (1.578250 +
1089 2.444130 * q +
1090 1.428100 * q2 +
1091 0.422205 * q3));
1092
1093 calc_type b1 = calc_type( 2.44413 * q +
1094 2.85619 * q2 +
1095 1.26661 * q3);
1096
1097 calc_type b2 = calc_type(-1.42810 * q2 +
1098 -1.26661 * q3);
1099
1100 calc_type b3 = calc_type(0.422205 * q3);
1101
1102 calc_type b = calc_type(1 - (b1 + b2 + b3) * b0);
1103
1104 b1 *= b0;
1105 b2 *= b0;
1106 b3 *= b0;
1107
1108 int w = img.width();
1109 int h = img.height();
1110 int wm = w-1;
1111 int x, y;
1112
1113 m_sum1.allocate(w);
1114 m_sum2.allocate(w);
1115 m_buf.allocate(w);
1116
1117 for(y = 0; y < h; y++)
1118 {
1119 calculator_type c;
1120 c.from_pix(img.pixel(0, y));
1121 m_sum1[0].calc(b, b1, b2, b3, c, c, c, c);
1122 c.from_pix(img.pixel(1, y));
1123 m_sum1[1].calc(b, b1, b2, b3, c, m_sum1[0], m_sum1[0], m_sum1[0]);
1124 c.from_pix(img.pixel(2, y));
1125 m_sum1[2].calc(b, b1, b2, b3, c, m_sum1[1], m_sum1[0], m_sum1[0]);
1126
1127 for(x = 3; x < w; ++x)
1128 {
1129 c.from_pix(img.pixel(x, y));
1130 m_sum1[x].calc(b, b1, b2, b3, c, m_sum1[x-1], m_sum1[x-2], m_sum1[x-3]);
1131 }
1132
1133 m_sum2[wm ].calc(b, b1, b2, b3, m_sum1[wm ], m_sum1[wm ], m_sum1[wm], m_sum1[wm]);
1134 m_sum2[wm-1].calc(b, b1, b2, b3, m_sum1[wm-1], m_sum2[wm ], m_sum2[wm], m_sum2[wm]);
1135 m_sum2[wm-2].calc(b, b1, b2, b3, m_sum1[wm-2], m_sum2[wm-1], m_sum2[wm], m_sum2[wm]);
1136 m_sum2[wm ].to_pix(m_buf[wm ]);
1137 m_sum2[wm-1].to_pix(m_buf[wm-1]);
1138 m_sum2[wm-2].to_pix(m_buf[wm-2]);
1139
1140 for(x = wm-3; x >= 0; --x)
1141 {
1142 m_sum2[x].calc(b, b1, b2, b3, m_sum1[x], m_sum2[x+1], m_sum2[x+2], m_sum2[x+3]);
1143 m_sum2[x].to_pix(m_buf[x]);
1144 }
1145 img.copy_color_hspan(0, y, w, &m_buf[0]);
1146 }
1147 }
1148
1149 //--------------------------------------------------------------------
1150 template<class Img> void blur_y(Img& img, double radius)
1151 {
1152 pixfmt_transposer<Img> img2(img);
1153 blur_x(img2, radius);
1154 }
1155
1156 //--------------------------------------------------------------------
1157 template<class Img> void blur(Img& img, double radius)
1158 {
1159 blur_x(img, radius);
1160 pixfmt_transposer<Img> img2(img);
1161 blur_x(img2, radius);
1162 }
1163
1164 private:
1165 agg::pod_vector<calculator_type> m_sum1;
1166 agg::pod_vector<calculator_type> m_sum2;
1167 agg::pod_vector<color_type> m_buf;
1168 };
1169
1170
1171 //=================================================recursive_blur_calc_rgba
1172 template<class T=double> struct recursive_blur_calc_rgba
1173 {
1174 typedef T value_type;
1175 typedef recursive_blur_calc_rgba<T> self_type;
1176
1177 value_type r,g,b,a;
1178
1179 template<class ColorT>
1180 AGG_INLINE void from_pix(const ColorT& c)
1181 {
1182 r = c.r;
1183 g = c.g;
1184 b = c.b;
1185 a = c.a;
1186 }
1187
1188 AGG_INLINE void calc(value_type b1,
1189 value_type b2,
1190 value_type b3,
1191 value_type b4,
1192 const self_type& c1,
1193 const self_type& c2,
1194 const self_type& c3,
1195 const self_type& c4)
1196 {
1197 r = b1*c1.r + b2*c2.r + b3*c3.r + b4*c4.r;
1198 g = b1*c1.g + b2*c2.g + b3*c3.g + b4*c4.g;
1199 b = b1*c1.b + b2*c2.b + b3*c3.b + b4*c4.b;
1200 a = b1*c1.a + b2*c2.a + b3*c3.a + b4*c4.a;
1201 }
1202
1203 template<class ColorT>
1204 AGG_INLINE void to_pix(ColorT& c) const
1205 {
1206 typedef typename ColorT::value_type cv_type;
1207 c.r = cv_type(r);
1208 c.g = cv_type(g);
1209 c.b = cv_type(b);
1210 c.a = cv_type(a);
1211 }
1212 };
1213
1214
1215 //=================================================recursive_blur_calc_rgb
1216 template<class T=double> struct recursive_blur_calc_rgb
1217 {
1218 typedef T value_type;
1219 typedef recursive_blur_calc_rgb<T> self_type;
1220
1221 value_type r,g,b;
1222
1223 template<class ColorT>
1224 AGG_INLINE void from_pix(const ColorT& c)
1225 {
1226 r = c.r;
1227 g = c.g;
1228 b = c.b;
1229 }
1230
1231 AGG_INLINE void calc(value_type b1,
1232 value_type b2,
1233 value_type b3,
1234 value_type b4,
1235 const self_type& c1,
1236 const self_type& c2,
1237 const self_type& c3,
1238 const self_type& c4)
1239 {
1240 r = b1*c1.r + b2*c2.r + b3*c3.r + b4*c4.r;
1241 g = b1*c1.g + b2*c2.g + b3*c3.g + b4*c4.g;
1242 b = b1*c1.b + b2*c2.b + b3*c3.b + b4*c4.b;
1243 }
1244
1245 template<class ColorT>
1246 AGG_INLINE void to_pix(ColorT& c) const
1247 {
1248 typedef typename ColorT::value_type cv_type;
1249 c.r = cv_type(r);
1250 c.g = cv_type(g);
1251 c.b = cv_type(b);
1252 }
1253 };
1254
1255
1256 //================================================recursive_blur_calc_gray
1257 template<class T=double> struct recursive_blur_calc_gray
1258 {
1259 typedef T value_type;
1260 typedef recursive_blur_calc_gray<T> self_type;
1261
1262 value_type v;
1263
1264 template<class ColorT>
1265 AGG_INLINE void from_pix(const ColorT& c)
1266 {
1267 v = c.v;
1268 }
1269
1270 AGG_INLINE void calc(value_type b1,
1271 value_type b2,
1272 value_type b3,
1273 value_type b4,
1274 const self_type& c1,
1275 const self_type& c2,
1276 const self_type& c3,
1277 const self_type& c4)
1278 {
1279 v = b1*c1.v + b2*c2.v + b3*c3.v + b4*c4.v;
1280 }
1281
1282 template<class ColorT>
1283 AGG_INLINE void to_pix(ColorT& c) const
1284 {
1285 typedef typename ColorT::value_type cv_type;
1286 c.v = cv_type(v);
1287 }
1288 };
1289
1290 //================================================slight_blur
1291 // Special-purpose filter for applying a Gaussian blur with a radius small enough
1292 // that the blur only affects adjacent pixels. A Gaussian curve with a standard
1293 // deviation of r/2 is used, as per the HTML/CSS spec. At 3 standard deviations,
1294 // the contribution drops to less than 0.005, i.e. less than half a percent,
1295 // therefore the radius can be at least 1.33 before errors become significant.
1296 // This filter is useful for smoothing artifacts caused by detail rendered
1297 // at the pixel scale, e.g. single-pixel lines. Note that the filter should
1298 // only be used with premultiplied pixel formats (or those without alpha).
1299 // See the "line_thickness" example for a demonstration.
1300 template<class PixFmt>
1301 class slight_blur
1302 {
1303 public:
1304 typedef typename PixFmt::pixel_type pixel_type;
1305 typedef typename PixFmt::value_type value_type;
1306 typedef typename PixFmt::order_type order_type;
1307
1308 slight_blur(double r = 1.33)
1309 {
1310 radius(r);
1311 }
1312
1313 void radius(double r)
1314 {
1315 if (r > 0)
1316 {
1317 // Sample the gaussian curve at 0 and r/2 standard deviations.
1318 // At 3 standard deviations, the response is < 0.005.
1319 double pi = 3.14159;
1320 double n = 2 / r;
1321 m_g0 = 1 / sqrt(2 * pi);
1322 m_g1 = m_g0 * exp(-n * n);
1323
1324 // Normalize.
1325 double sum = m_g0 + 2 * m_g1;
1326 m_g0 /= sum;
1327 m_g1 /= sum;
1328 }
1329 else
1330 {
1331 m_g0 = 1;
1332 m_g1 = 0;
1333 }
1334 }
1335
1336 void blur(PixFmt& img, rect_i bounds)
1337 {
1338 // Make sure we stay within the image area.
1339 bounds.clip(rect_i(0, 0, img.width() - 1, img.height() - 1));
1340
1341 int w = bounds.x2 - bounds.x1 + 1;
1342 int h = bounds.y2 - bounds.y1 + 1;
1343
1344 if (w < 3 || h < 3) return;
1345
1346 // Allocate 3 rows of buffer space.
1347 m_buf.allocate(w * 3);
1348
1349 // Set up row pointers
1350 pixel_type * begin = &m_buf[0];
1351 pixel_type * r0 = begin;
1352 pixel_type * r1 = r0 + w;
1353 pixel_type * r2 = r1 + w;
1354 pixel_type * end = r2 + w;
1355
1356 // Horizontally blur the first two input rows.
1357 calc_row(img, bounds.x1, bounds.y1, w, r0);
1358 memcpy(r1, r0, w * sizeof(pixel_type));
1359
1360 for (int y = 0; ; )
1361 {
1362 // Get pointer to first pixel.
1363 pixel_type* p = img.pix_value_ptr(bounds.x1, bounds.y1 + y, bounds.x1 + w);
1364
1365 // Horizontally blur the row below.
1366 if (y + 1 < h)
1367 {
1368 calc_row(img, bounds.x1, bounds.y1 + y + 1, w, r2);
1369 }
1370 else
1371 {
1372 memcpy(r2, r1, w * sizeof(pixel_type)); // duplicate bottom row
1373 }
1374
1375 // Combine blurred rows into destination.
1376 for (int x = 0; x < w; ++x)
1377 {
1378 calc_pixel(*r0++, *r1++, *r2++, *p++);
1379 }
1380
1381 if (++y >= h) break;
1382
1383 // Wrap bottom row pointer around to top of buffer.
1384 if (r2 == end) r2 = begin;
1385 else if (r1 == end) r1 = begin;
1386 else if (r0 == end) r0 = begin;
1387 }
1388 }
1389
1390 private:
1391 void calc_row(PixFmt& img, int x, int y, int w, pixel_type* row)
1392 {
1393 const int wm = w - 1;
1394
1395 pixel_type* p = img.pix_value_ptr(x, y, w);
1396
1397 pixel_type c[3];
1398 pixel_type* p0 = c;
1399 pixel_type* p1 = c + 1;
1400 pixel_type* p2 = c + 2;
1401 pixel_type* end = c + 3;
1402 *p0 = *p1 = *p;
1403
1404 for (int x = 0; x < wm; ++x)
1405 {
1406 *p2 = *(p = p->next());
1407
1408 calc_pixel(*p0++, *p1++, *p2++, *row++);
1409
1410 if (p0 == end) p0 = c;
1411 else if (p1 == end) p1 = c;
1412 else if (p2 == end) p2 = c;
1413 }
1414
1415 calc_pixel(*p0, *p1, *p1, *row);
1416 }
1417
1418 void calc_pixel(
1419 pixel_type const & c1,
1420 pixel_type const & c2,
1421 pixel_type const & c3,
1422 pixel_type & x)
1423 {
1424 calc_pixel(c1, c2, c3, x, PixFmt::pixfmt_category());
1425 }
1426
1427 void calc_pixel(
1428 pixel_type const & c1,
1429 pixel_type const & c2,
1430 pixel_type const & c3,
1431 pixel_type & x,
1432 pixfmt_gray_tag)
1433 {
1434 x.c[0] = calc_value(c1.c[0], c2.c[0], c3.c[0]);
1435 }
1436
1437 void calc_pixel(
1438 pixel_type const & c1,
1439 pixel_type const & c2,
1440 pixel_type const & c3,
1441 pixel_type & x,
1442 pixfmt_rgb_tag)
1443 {
1444 enum { R = order_type::R, G = order_type::G, B = order_type::B };
1445 x.c[R] = calc_value(c1.c[R], c2.c[R], c3.c[R]);
1446 x.c[G] = calc_value(c1.c[G], c2.c[G], c3.c[G]);
1447 x.c[B] = calc_value(c1.c[B], c2.c[B], c3.c[B]);
1448 }
1449
1450 void calc_pixel(
1451 pixel_type const & c1,
1452 pixel_type const & c2,
1453 pixel_type const & c3,
1454 pixel_type & x,
1455 pixfmt_rgba_tag)
1456 {
1457 enum { R = order_type::R, G = order_type::G, B = order_type::B, A = order_type::A };
1458 x.c[R] = calc_value(c1.c[R], c2.c[R], c3.c[R]);
1459 x.c[G] = calc_value(c1.c[G], c2.c[G], c3.c[G]);
1460 x.c[B] = calc_value(c1.c[B], c2.c[B], c3.c[B]);
1461 x.c[A] = calc_value(c1.c[A], c2.c[A], c3.c[A]);
1462 }
1463
1464 value_type calc_value(value_type v1, value_type v2, value_type v3)
1465 {
1466 return value_type(m_g1 * v1 + m_g0 * v2 + m_g1 * v3);
1467 }
1468
1469 double m_g0, m_g1;
1470 pod_vector<pixel_type> m_buf;
1471 };
1472
1473 // Helper functions for applying blur to a surface without having to create an intermediate object.
1474
1475 template<class PixFmt>
1476 void apply_slight_blur(PixFmt& img, const rect_i& bounds, double r = 1)
1477 {
1478 if (r > 0) slight_blur<PixFmt>(r).blur(img, bounds);
1479 }
1480
1481 template<class PixFmt>
1482 void apply_slight_blur(PixFmt& img, double r = 1)
1483 {
1484 if (r > 0) slight_blur<PixFmt>(r).blur(img, rect_i(0, 0, img.width() - 1, img.height() - 1));
1485 }
1486
1487 template<class PixFmt>
1488 void apply_slight_blur(renderer_base<PixFmt>& img, const rect_i& bounds, double r = 1)
1489 {
1490 if (r > 0) slight_blur<PixFmt>(r).blur(img.ren(), bounds);
1491 }
1492
1493 template<class PixFmt>
1494 void apply_slight_blur(renderer_base<PixFmt>& img, double r = 1)
1495 {
1496 if (r > 0) slight_blur<PixFmt>(r).blur(img.ren(), img.clip_box());
1497 }
1498 }
1499
1500
1501
1502
1503 #endif