Initializes line iterator. Returns number of points on the line or negative number if error. */
| 155 | Returns number of points on the line or negative number if error. |
| 156 | */ |
| 157 | LineIterator::LineIterator(const Mat& img, Point pt1, Point pt2, |
| 158 | int connectivity, bool left_to_right) |
| 159 | { |
| 160 | count = -1; |
| 161 | |
| 162 | CV_Assert( connectivity == 8 || connectivity == 4 ); |
| 163 | |
| 164 | if( (unsigned)pt1.x >= (unsigned)(img.cols) || |
| 165 | (unsigned)pt2.x >= (unsigned)(img.cols) || |
| 166 | (unsigned)pt1.y >= (unsigned)(img.rows) || |
| 167 | (unsigned)pt2.y >= (unsigned)(img.rows) ) |
| 168 | { |
| 169 | if( !clipLine( img.size(), pt1, pt2 ) ) |
| 170 | { |
| 171 | ptr = img.data; |
| 172 | err = plusDelta = minusDelta = plusStep = minusStep = count = 0; |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | int bt_pix0 = (int)img.elemSize(), bt_pix = bt_pix0; |
| 178 | size_t istep = img.step; |
| 179 | |
| 180 | int dx = pt2.x - pt1.x; |
| 181 | int dy = pt2.y - pt1.y; |
| 182 | int s = dx < 0 ? -1 : 0; |
| 183 | |
| 184 | if( left_to_right ) |
| 185 | { |
| 186 | dx = (dx ^ s) - s; |
| 187 | dy = (dy ^ s) - s; |
| 188 | pt1.x ^= (pt1.x ^ pt2.x) & s; |
| 189 | pt1.y ^= (pt1.y ^ pt2.y) & s; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | dx = (dx ^ s) - s; |
| 194 | bt_pix = (bt_pix ^ s) - s; |
| 195 | } |
| 196 | |
| 197 | ptr = (uchar*)(img.data + pt1.y * istep + pt1.x * bt_pix0); |
| 198 | |
| 199 | s = dy < 0 ? -1 : 0; |
| 200 | dy = (dy ^ s) - s; |
| 201 | istep = (istep ^ s) - s; |
| 202 | |
| 203 | s = dy > dx ? -1 : 0; |
| 204 | |
| 205 | /* conditional swaps */ |
| 206 | dx ^= dy & s; |
| 207 | dy ^= dx & s; |
| 208 | dx ^= dy & s; |
| 209 | |
| 210 | bt_pix ^= istep & s; |
| 211 | istep ^= bt_pix & s; |
| 212 | bt_pix ^= istep & s; |
| 213 | |
| 214 | if( connectivity == 8 ) |