00001
00002
00003 #ifndef _ITERATOR_HPP_
00004 #define _ITERATOR_HPP_
00005
00006 #include "image/Image.hpp"
00007
00008 namespace jafar {
00009 namespace image {
00010
00017 template <typename type>
00018 class image_iterator {
00019
00020 int i, i0,j;
00021 type* data;
00022 type* pix;
00023 int step;
00024 int nl, nc;
00025 int nch;
00026
00027 public:
00028
00029
00030 image_iterator(jafar::image::Image & image) {
00031
00032 begin(image);
00033
00034 nch= image.channels();
00035 pix= new type[nch];}
00036
00037
00038 image_iterator(image_iterator const & it) {
00039 i = it.i;
00040 j = it.j;
00041 i0 = it.i0;
00042 data = it.data;
00043 pix = it.pix;
00044 step = it.step;
00045 nl = it.nl;
00046 nc = it.nc;
00047 nch = it.nch;
00048 }
00049
00050
00051 void begin(jafar::image::Image & image) {
00052 i = 0;
00053 j = 0;
00054 i0 = 0;
00055 data= reinterpret_cast<type*>(image.data());
00056 step= image.step() / sizeof(type);
00057
00058 nl= image.height();
00059 nc= image.width() ;
00060 nc*= image.channels();
00061 nch = image.channels();
00062 }
00063
00064 image_iterator& operator=(image_iterator const & it) {
00065 i = it.i;
00066 j = it.j;
00067 i0 = it.i0;
00068 data = it.data;
00069 pix = it.pix;
00070 step = it.step;
00071 nl = it.nl;
00072 nc = it.nc;
00073 nch = it.nch;
00074 return *this;
00075 }
00076
00077
00078 bool operator!() const { return j < nl; }
00079
00080
00081 image_iterator& operator++() {i++;
00082 if (i >= nc) { i=i0; j++; data+= step; }
00083 return *this;}
00084 image_iterator operator++(int) {i++;
00085 if (i >= nc) { i=i0; j++; data+= step; }
00086 return *this;}
00087 image_iterator& operator+=(int s) {i+=s;
00088 while (i >= nc) { i=i0+(i-nc); j++; data+= step; }
00089 return *this;}
00090 image_iterator operator+ (int s) {
00091 image_iterator<type> it(*this);
00092 it += s;
00093 return it;
00094 }
00095
00096 image_iterator& operator--() {i--;
00097 if (i < 0) { i=nc-i; j--; data-= step; }
00098 return *this;}
00099 image_iterator& operator--(int) {i--;
00100 if (i < 0) { i=nc-i; j--; data-= step; }
00101 return *this;}
00102 image_iterator& operator-=(int s) {i-=s;
00103 while (i < 0) { i=nc-i; j--; data-= step; }
00104 return *this;}
00105 image_iterator operator- (int s) {
00106 image_iterator<type> it(*this);
00107 it -= s;
00108 return it;
00109 }
00110
00111 type& operator*() { return data[i]; }
00112 type* operator&() const { return data+i; }
00113
00114
00115
00116 int column() const { return i/nch; }
00117 int line() const { return j; }
00118 };
00122 template <typename type>
00123 class const_image_iterator {
00124
00125 int i, i0,j;
00126 type* data;
00127 type* pix;
00128 int step;
00129 int nl, nc;
00130 int nch;
00131
00132 public:
00133
00134
00135 const_image_iterator(jafar::image::Image const & image) {
00136
00137 begin(image);
00138
00139 nch= image.channels();
00140 pix= new type[nch];}
00141
00142
00143 const_image_iterator(const_image_iterator const & it) {
00144 i = it.i;
00145 j = it.j;
00146 i0 = it.i0;
00147 data = it.data;
00148 pix = it.pix;
00149 step = it.step;
00150 nl = it.nl;
00151 nc = it.nc;
00152 nch = it.nch;
00153 }
00154
00155
00156 void begin(jafar::image::Image const & image) {
00157 i = 0;
00158 j = 0;
00159 i0 = 0;
00160 data= reinterpret_cast<type*>(image.data());
00161 step= image.step() / sizeof(type);
00162
00163 nl= image.height();
00164 nc= image.width() ;
00165 nc*= image.channels();
00166 nch = image.channels();
00167 }
00168
00169 const_image_iterator& operator=(const_image_iterator const & it) {
00170 i = it.i;
00171 j = it.j;
00172 i0 = it.i0;
00173 data = it.data;
00174 pix = it.pix;
00175 step = it.step;
00176 nl = it.nl;
00177 nc = it.nc;
00178 nch = it.nch;
00179 return *this;
00180 }
00181
00182
00183 bool operator!() const { return j < nl; }
00184
00185
00186 const_image_iterator& operator++() {i++;
00187 if (i >= nc) { i=i0; j++; data+= step; }
00188 return *this;}
00189 const_image_iterator operator++(int) {i++;
00190 if (i >= nc) { i=i0; j++; data+= step; }
00191 return *this;}
00192 const_image_iterator& operator+=(int s) {i+=s;
00193 while (i >= nc) { i=i0+(i-nc); j++; data+= step; }
00194 return *this;}
00195 const_image_iterator operator+ (int s) {
00196 const_image_iterator<type> it(*this);
00197 it += s;
00198 return it;
00199 }
00200
00201 const_image_iterator& operator--() {i--;
00202 if (i < 0) { i=nc-i; j--; data-= step; }
00203 return *this;}
00204 const_image_iterator& operator--(int) {i--;
00205 if (i < 0) { i=nc-i; j--; data-= step; }
00206 return *this;}
00207 const_image_iterator& operator-=(int s) {i-=s;
00208 while (i < 0) { i=nc-i; j--; data-= step; }
00209 return *this;}
00210 const_image_iterator operator- (int s) {
00211 const_image_iterator<type> it(*this);
00212 it -= s;
00213 return it;
00214 }
00215
00216 const type operator*() const { return data[i]; }
00217 type const * operator&() const { return data+i; }
00218
00219
00220
00221 int column() const { return i/nch; }
00222 int line() const { return j; }
00223 };
00224 }
00225 }
00226
00227
00228 #endif