1 /*! ========================================================================
2 ** Extended Template Library
3 ** Box Blur Template Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
27 #ifndef __ETL__BOXBLUR_H
28 #define __ETL__BOXBLUR_H
30 /* === H E A D E R S ======================================================= */
32 /* === M A C R O S ========================================================= */
34 /* === T Y P E D E F S ===================================================== */
36 /* === C L A S S E S & S T R U C T S ======================================= */
40 template<typename T1,typename T2> void
41 hbox_blur(T1 pen,int w, int h, int length, T2 outpen)
44 typename T1::iterator_x iter, end;
46 length=std::min(w,length);
47 const float divisor(1.0f/(length*2+1));
49 for(y=0;y<h;y++,pen.inc_y(),outpen.inc_y())
54 typename T1::accumulator_type tot(*iter*(length+1));
56 for (x=0;x<length && iter!=end;x++,++iter) tot+=*iter;
59 for (x=0;x<w && iter!=end;x++,++iter,outpen.inc_x())
61 tot -= (x>length) ? iter[-length-1] : *pen.x();
62 tot += ((x+length)<w) ? iter[length] : end[-1];
64 outpen.put_value(tot*divisor);
71 template<typename T1,typename T2> void
72 vbox_blur(T1 pen,const int w, const int h, int length, T2 outpen)
75 typename T1::iterator_y iter, end;
77 length=std::min(h,length);
78 const float divisor(1.0f/(length*2+1));
80 for(x=0;x<w;x++,pen.inc_x(),outpen.inc_x())
85 typename T1::accumulator_type tot(*iter*(length+1));
87 for (y=0;y<length && iter!=end;y++,++iter) tot+=*iter;
90 for (y=0;y<h && iter!=end;y++,++iter,outpen.inc_y())
92 tot -= (y>length) ? iter[-length-1] : *pen.y();
93 tot += ((y+length)<h) ? iter[length] : end[-1];
95 outpen.put_value(tot*divisor);
103 template<typename T1,typename T2> void
104 vbox_blur(T1 pen,int w, int h, int length,T2 outpen)
107 typename T1::iterator_y iter, end, biter,eiter;
109 //print out the info I need to figure out if this is somehow a buffer overrun...
110 /*char *beginptr=0,*endptr=0;
120 printf("V Blur (%d,%d,s-%d) in(%p,%p,st %d) out(%p,%p)\n",
121 w,h,length,(char*)pen.x(),(char*)endpen.x(),(char*)ypen.x()-(char*)pen.x(),
122 (char*)open.x(),(char*)oepen.x());
124 length=min(h-1,length);
126 const float divisor(1.0f/(length*2+1));
127 //const int div = (length*2+1);
129 //since the filter range is 2*length+1 we need h-1
130 for(x=0;x<w;x++,pen.inc_x(),outpen.inc_x())
135 const typename T1::value_type bval = *iter;
136 const typename T1::value_type eval = end[-1];
138 typename T1::accumulator_type tot(bval*(length+1));
139 //beginptr = (char*)&*iter; endptr = (char*)&*end;
141 //printf("\nx line %d (%p,%p)\n",x,beginptr,endptr);
143 //printf("Init %.3f - ",tot);
144 for (y=0;y<length && iter!=end;y++)
147 //printf("(%d,%p,+%.3f->%.3f),",y,&iter[y],iter[y],tot);
151 //printf(" tot=%.3f\n",tot);
153 biter = iter+(-length-1); //get the first one...
156 //y will always be > length
158 for (y=0;y<h && iter!=end;y++,++iter,++biter,++eiter,outpen.inc_y())
160 //printf("y line %d - (%f) ",y,tot);
164 typename T1::value_type &v = *biter;
165 /*if( (char*)&v < beginptr ||
167 printf("crap! %d (%p off %p)\n",y,(char*)&v,(char*)&*iter);*/
169 //printf("[%.3f,",v);
174 //printf("[%.3f,",bval);
179 typename T1::value_type &v = *eiter;
180 /*if( (char*)&v < beginptr ||
182 printf("crap! %d (%p off %p)\n",y,(char*)&v,(char*)&*iter);*/
189 //printf("%.3f]",eval);
192 //test handled in the previous case...
193 //tot -= (y>length) ? *biter : bval;
194 //tot += (y+length<h) ? *eiter : eval;
196 //printf(" - %.3f\n",tot);
197 outpen.put_value(tot*divisor);
204 template<typename T1,typename T2> void
205 box_blur(T1 pen,int w, int h, int blur_w, int blur_h, T2 outpen)
206 { hbox_blur(pen,w,h,blur_w,outpen); vbox_blur(pen,w,h,blur_h,outpen); }
208 template<typename T1,typename T2> void
209 box_blur(T1 pen,int w, int h, int size, T2 outpen)
210 { hbox_blur(pen,w,h,size,outpen); vbox_blur(pen,w,h,size,outpen); }
212 template<typename T1,typename T2> void
213 hbox_blur(T1 begin,T1 end, int len,T2 outpen)
215 typename T1::difference_type size(end-begin);
216 hbox_blur(begin,size.x,size.y,len,outpen);
219 template<typename T1,typename T2> void
220 vbox_blur(T1 begin,T1 end, int len,T2 outpen)
222 typename T1::difference_type size(end-begin);
223 vbox_blur(begin,size.x,size.y,len,outpen);
226 template<typename T1,typename T2> void
227 box_blur(T1 begin,T1 end, int blur_w, int blur_h,T2 outpen)
229 typename T1::difference_type size(end-begin);
230 hbox_blur(begin,size.x,size.y,blur_w,outpen); vbox_blur(begin,size.x,size.y,blur_h,outpen);
233 template<typename T1,typename T2> void
234 box_blur(T1 begin,T1 end, int blursize,T2 outpen)
236 typename T1::difference_type size(end-begin);
237 hbox_blur(begin,size.x,size.y,blursize,outpen); vbox_blur(begin,size.x,size.y,blursize,outpen);
242 /* === E X T E R N S ======================================================= */
244 /* === E N D =============================================================== */