1 /*! ========================================================================
2 ** Extended Template Library
3 ** Pen Template Class 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 =========================================================== */
30 /* === H E A D E R S ======================================================= */
32 #include "_curve_func.h"
37 /* === M A C R O S ========================================================= */
39 /* === T Y P E D E F S ===================================================== */
41 /* === C L A S S E S & S T R U C T S ======================================= */
46 class generic_pen_row_iterator
49 struct iterator_category : public std::random_access_iterator_tag {};
51 typedef int difference_type;
52 typedef value_type* pointer;
53 typedef value_type& reference;
55 typedef generic_pen_row_iterator<value_type> self_type;
60 reference operator[](int i)const { assert(data_); return *(pointer)( (char*)data_+pitch_*i ); }
61 reference operator*()const { assert(data_); return *data_; }
62 pointer operator->() const { assert(data_); return &(operator*()); }
64 void inc() { assert(data_); data_ = (pointer)((char*)data_ + pitch_); }
65 void inc(int n) { assert(data_); data_ = (pointer)((char*)data_ + n*pitch_); }
67 void dec() { assert(data_); data_ = (pointer)((char*)data_ - pitch_); }
68 void dec(int n) { assert(data_); data_ = (pointer)((char*)data_ - n*pitch_); }
70 const self_type &operator++() { assert(data_); inc(); return *this; }
71 const self_type &operator--() { assert(data_); dec(); return *this; }
73 self_type operator++(int)
74 { assert(data_); self_type ret(*this); inc(); return ret; }
75 self_type operator--(int)
76 { assert(data_); self_type ret(*this); dec(); return ret; }
78 bool operator==(const self_type &rhs)const
79 { return data_==rhs.data_; }
81 bool operator!=(const self_type &rhs)const
82 { return data_!=rhs.data_; }
84 difference_type operator-(const self_type &rhs)const
85 { assert(data_); return ((char*)data_-(char*)rhs.data_-1)/pitch_+1; }
87 self_type operator+(const difference_type &rhs)const
95 self_type operator-(const difference_type &rhs)const
103 operator const generic_pen_row_iterator<const value_type>()const
105 return generic_pen_row_iterator<const value_type>(data_,pitch_);
108 operator bool()const { return (bool)data_; }
109 bool operator!()const { return !data_; }
111 generic_pen_row_iterator(pointer data, int pitch):data_(data),pitch_(pitch) { }
112 generic_pen_row_iterator():data_(NULL) { }
115 template<typename T, typename AT=T>
119 typedef T value_type;
120 typedef AT accumulator_type;
121 typedef value_type* pointer;
122 typedef const value_type* const_pointer;
123 typedef value_type& reference;
124 typedef const value_type& const_reference;
126 typedef pointer iterator_x;
127 typedef const_pointer const_iterator_x;
129 typedef generic_pen_row_iterator<value_type> iterator_y;
130 typedef generic_pen_row_iterator<const value_type> const_iterator_y;
132 struct difference_type
134 typedef int value_type;
136 difference_type(value_type x, value_type y):x(x),y(y) { }
137 value_type &operator[](int i)const { return i?y:x; }
148 typedef generic_pen<T,AT> self_type;
150 void addptr(int nbytes)
152 data_ = (pointer)((char*)data_ + nbytes);
155 void subptr(int nbytes)
157 data_ = (pointer)((char*)data_ - nbytes);
162 generic_pen(value_type *data, int w, int h, int pitch):
172 generic_pen(value_type *data, int w, int h):
177 pitch_(sizeof(value_type)*w),
182 generic_pen():data_(NULL) { }
184 self_type& move(int a, int b)
188 addptr(b*pitch_ + a*sizeof(value_type));
191 self_type& move_to(int x, int y) { assert(data_); return move(x - x_,y - y_);}
192 void set_value(const value_type &v) { value_=v; }
194 void inc_x() { assert(data_); x_++; data_++; }
195 void dec_x() { assert(data_); x_--; data_--; }
196 void inc_y() { assert(data_); y_++; addptr(pitch_); }
197 void dec_y() { assert(data_); y_--; subptr(pitch_); }
199 void inc_x(int n) { assert(data_); x_+=n; data_+=n; }
200 void dec_x(int n) { assert(data_); x_-=n; data_-=n; }
201 void inc_y(int n) { assert(data_); y_+=n; data_ = (pointer)((char*)data_ + pitch_*n); }
202 void dec_y(int n) { assert(data_); y_-=n; data_ = (pointer)((char*)data_ - pitch_*n); }
204 void put_value(const value_type &v)const { assert(data_); *data_=v; }
205 void put_value()const { assert(data_); put_value(value_); }
207 void put_value_clip(const value_type &v)const
208 { if(!clipped()) put_value(v); }
209 void put_value_clip()const { put_value_clip(value_); }
211 const_reference get_value()const { assert(data_); return *data_; }
213 const_reference get_value_at(int x, int y)const { assert(data_); return ((pointer)(((char*)data_)+y*pitch_))[x]; }
215 const_reference get_value_clip_at(int x, int y)const { assert(data_); if(clipped(x,y))return value_type(); return ((pointer)(((char*)data_)+y*pitch_))[x]; }
217 value_type get_value_clip()const { assert(data_); if(clipped())return value_type(); return *data_; }
219 value_type get_pen_value()const { return value_; }
221 void put_hline(int l,const value_type &v)
222 {for(;l>0;l--,inc_x())put_value(v);}
224 void put_hline(int l) {put_hline(l,value_);}
226 void put_hline_clip(int l, const value_type &v)
227 {l=std::min(l,w_-x_);for(;l>0;l--,inc_x())put_value_clip(v);}
229 void put_hline_clip(int l) {put_hline_clip(l,value_);}
231 //the put_block functions do not modify the pen
232 void put_block(int h, int w, const value_type &v)
234 self_type row(*this);
235 for(;h>0;h--,row.inc_y())
242 void put_block(int h, int w) { put_block(h,w,value_); }
244 void put_block_clip(int h, int w, const value_type &v)
246 self_type row(*this);
248 //clip start position
249 if(row.x_ < 0) { w+=row.x_; row.inc_x(-row.x_); }
250 if(row.y_ < 0) { h+=row.y_; row.inc_y(-row.y_); }
252 //clip width and height of copy rect
253 h = std::min(h,h_-y_);
254 w = std::min(w,w_-x_);
257 for(;h>0;h--,row.inc_y())
260 col.put_hline(w,v); //already clipped
264 void put_block_clip(int h, int w) { put_block(h,w,value_); }
267 iterator_x operator[](int i)const { assert(data_); return (pointer)(((char*)data_)+i*pitch_); }
269 iterator_x x() { assert(data_); return data_; }
270 iterator_x begin_x() { assert(data_); return data_-x_; }
271 iterator_x end_x() { assert(data_); return data_-x_+w_; }
273 iterator_y y() { assert(data_); return iterator_y(data_,pitch_); }
274 iterator_y begin_y() { assert(data_); return iterator_y((pointer)((char*)data_ - y_*pitch_),pitch_); }
275 iterator_y end_y() { assert(data_); return iterator_y((pointer)((char*)data_ + (h_-y_)*pitch_),pitch_); }
277 operator bool()const { return (bool)data_; }
278 bool operator!()const { return !data_; }
279 bool operator==(const self_type &rhs)const { return data_==rhs.data_; }
280 bool operator!=(const self_type &rhs)const { return data_!=rhs.data_; }
281 bool clipped(int x, int y)const { return !(x_+x>=0 && y_+y>=0 && x_+x<w_ && y_+y<h_); }
282 bool clipped()const { return !(x_>=0 && y_>=0 && x_<w_ && y_<h_); }
284 difference_type operator-(const self_type &rhs)const
287 assert(pitch_==rhs.pitch_);
288 int ptr_diff=(char*)data_-(char*)rhs.data_-1;
289 return difference_type(ptr_diff%pitch_/sizeof(value_type)+1,ptr_diff/pitch_);
292 self_type operator+(const difference_type &rhs)const
295 self_type ret(*this);
296 ret.move(rhs.x,rhs.y);
300 difference_type diff_begin()const {return difference_type(-x_,-y_);}
301 difference_type diff_end()const {return difference_type(w_-x_,h_-y_);}
303 self_type get_start()const {return *this + diff_begin(); }
304 self_type get_end()const {return *this + diff_end(); }
306 int get_width()const {return w_;}
307 int get_height()const {return h_;}
309 int get_w()const {return w_;}
310 int get_h()const {return h_;}
311 int get_pitch()const {return pitch_;}
317 class AFFINE_=affine_combo<typename PEN_::value_type,A_>
319 class alpha_pen : public PEN_
322 typedef A_ alpha_type;
323 typedef AFFINE_ affine_func_type;
325 typedef typename PEN_::value_type value_type;
326 typedef alpha_pen self_type;
332 affine_func_type affine_func_;
335 using PEN_::get_value;
336 using PEN_::get_pen_value;
347 alpha_pen(const alpha_type &a = 1, const affine_func_type &func = affine_func_type()):alpha_(a),affine_func_(func) { }
348 alpha_pen(const PEN_ &x, const alpha_type &a=1, const affine_func_type &func=affine_func_type())
349 :PEN_(x),alpha_(a),affine_func_(func) { }
351 const alpha_type& get_alpha()const { return alpha_; }
352 void get_alpha(alpha_type &a) const { a=alpha_; }
353 void set_alpha(alpha_type a) { alpha_=a; }
355 void put_value(const value_type &v, alpha_type a=1)const
356 { PEN_::put_value(affine_func_(get_value(),v,alpha_*a)); }
357 void put_value()const { put_value(get_pen_value()); }
358 void put_value_alpha(alpha_type a)const { put_value(get_pen_value(),a); }
359 void put_hline(int l, const alpha_type &a = 1){for(;l>0;l--,inc_x())put_value_alpha(a);}
361 void put_value_clip(const value_type &v, alpha_type a=1)const
362 { if(!clipped())PEN_::put_value(affine_func_(get_value(),v,alpha_*a)); }
363 void put_value_clip()const { put_value_clip(get_pen_value()); }
364 void put_value_clip_alpha(alpha_type a)const { put_value_clip(get_pen_value(),a); }
365 void put_hline_clip(int l, const alpha_type &a = 1){l=std::min(l,w_-x_);for(;l>0;l--,inc_x())put_value_clip_alpha(a);}
367 //the put_block functions do not modify the pen
368 void put_block(int h, int w, const alpha_type &a = 1)
370 self_type row(*this);
371 for(;h>0;h--,row.inc_y())
378 void put_block_clip(int h, int w, const alpha_type &a = 1)
380 self_type row(*this);
382 //clip start position
383 if(row.x_ < 0) { w+=row.x_; row.inc_x(-row.x_); }
384 if(row.y_ < 0) { h+=row.y_; row.inc_y(-row.y_); }
386 //clip width and height of copy rect
387 h = std::min(h,h_-y_);
388 w = std::min(w,w_-x_);
391 for(;h>0;h--,row.inc_y())
394 col.put_hline(w,a); //already clipped
401 /* === E X T E R N S ======================================================= */
403 /* === E N D =============================================================== */