Loading...
Searching...
No Matches
BandIterator.h
Go to the documentation of this file.
1/* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2
3 This file is part of the TerraLib - a Framework for building GIS enabled applications.
4
5 TerraLib is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 TerraLib is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with TerraLib. See COPYING. If not, write to
17 TerraLib Team at <terralib-team@terralib.org>.
18 */
19
20/*!
21 \file terralib/raster/BandIterator.h
22
23 \brief It implements an iterator to "navigate" over a single band (const or not const).
24*/
25
26#ifndef __TERRALIB_RASTER_INTERNAL_BANDITERATOR_H
27#define __TERRALIB_RASTER_INTERNAL_BANDITERATOR_H
28
29// Terralib
30#include "Band.h"
31#include "BandProperty.h"
32#include "BlockUtils.h"
33
34// Boost
35#include <boost/dynamic_bitset.hpp>
36
37#include <cstring>
38
39namespace te
40{
41 namespace rst
42 {
43// Forward declaration.
44 class Band;
45
46 /*!
47 \class AbstractBandIterator
48
49 \brief This class implements an abstract iterator to "navigate" over a single band.
50
51 We provide an efficient method to obtain all values in a raster, without regard
52 to their locations. The implementation navigates through the blocks of the image.
53
54 \ingroup rst
55
56 \sa te::rst::Band
57 */
58 template<class T> class AbstractBandIterator
59 {
60 public:
61
62 /*! \brief Constructor. */
64
65 /*!
66 \brief Constructor.
67
68 \param b The band to iterate.
69 */
71
72 /*!
73 \brief Copy constructor.
74
75 \param rhs The right-hand-side copy used to copy from.
76 */
78
79 /*! \brief Destructor. */
80 virtual ~AbstractBandIterator();
81
82 /*! \brief Returns the value in current position (column, row) from iterator. */
83 virtual const T operator*() const = 0;
84
85 /*! \brief Returns the current row in iterator. */
86 unsigned int getRow() const;
87
88 /*! \brief Returns the current column in iterator. */
89 unsigned int getColumn() const;
90
91 /*! \brief Advances to the next position. */
92 virtual void operator++();
93
94 /*! \brief Returns to the previous position. */
95 void operator--();
96
97 /*!
98 \brief Assignment operator.
99
100 \param rhs The right-hand-side copy used to copy from.
101
102 \return A reference to this object.
103 */
105
106 /*!
107 \brief Difference operator.
108
109 \param rhs The right-hand side to compare.
110
111 \return Returns true if the iterators are at different positions, or false otherwise.
112 */
113 bool operator!=(const AbstractBandIterator& rhs) const;
114
115 /*! \brief Replaces the current bufferized block values. */
116 virtual void replaceBlock() = 0;
117
118 protected:
119
120 int m_blkw; //!< The internal block width.
121 int m_blkh; //!< The internal block height.
122 int m_npxlsblk; //!< The maximum number of pixels inside the block.
123 int m_nblocksx; //!< The number of blocks in X direction.
124 int m_nblocksy; //!< The number of blocks in Y direction.
125 int m_i; //!< The actual position inside the block.
126 int m_blkx; //!< The position in X of the current block.
127 int m_blky; //!< The position in Y of the current block.
128 int m_lastblksize; //!< The number of pixels inside the last block.
129 int m_blksize; //!< The block size of the band.
130 T* m_blk; //!< Internal block.
131 te::rst::GetBufferValueFPtr m_getBuff; //!< A pointer to a function that helps to extract a double or complex value from a specific buffer data type (char, int16, int32, float, ...).
132 te::rst::GetBufferValueFPtr m_getBuffI; //!< A pointer to a function that helps to extract the imaginary part value from a specific buffer data type (cint16, cint32, cfloat, cdouble).
133 te::rst::SetBufferValueFPtr m_setBuff; //!< A pointer to a function that helps to insert a double or complex value into a specific buffer data type (char, int16, int32, float, ...).
134 te::rst::SetBufferValueFPtr m_setBuffI; //!< A pointer to a function that helps to insert the imaginary part value into a specific buffer data type (cint16, cint32, cfloat, cdouble).
135
136 /*! \brief Reset this instance to a initial state. */
137 void reset();
138
139 };
140
141 /*!
142 \class BandIterator
143
144 \brief This class implements an iterator to "navigate" over a single band.
145
146 We provide an efficient method to obtain all values in a raster, without regard
147 to their locations. The implementation navigates through the blocks of the image.
148
149 \ingroup rst
150
151 \sa te::rst::Band
152 */
153 template<class T> class BandIterator : public te::rst::AbstractBandIterator<T>
154 {
155 public:
156
157 /*! \brief Constructor. */
158 BandIterator();
159
160 /*!
161 \brief Constructor.
162
163 \param b The band to iterate.
164 */
165 BandIterator(Band* b);
166
167 /*!
168 \brief Copy constructor.
169
170 \param rhs The right-hand-side copy used to copy from.
171 */
172 BandIterator(const BandIterator& rhs);
173
174 /*! \brief Returns the value in current position (column, row) from iterator. */
175 T operator*();
176
177 /*! \brief Returns the value in current position (column, row) from iterator. */
178 const T operator*() const;
179
180 /*!
181 \brief Assignment operator.
182
183 \param rhs The right-hand-side copy used to copy from.
184
185 \return A reference to this object.
186 */
188
189 /*! \brief Returns an iterator referring to the first value of the band.*/
190 static BandIterator begin(Band* b);
191
192 /*! \brief Returns an iterator referring to after the end of the iterator. */
193 static BandIterator end(Band* b);
194
195 /*! \brief Replaces the current bufferized block values. */
196 void replaceBlock();
197
198 protected:
199
200 Band* m_band; //!< The band from where to get the values.
201
202 /*! \brief Reset this instance to a initial state. */
203 void reset();
204
205 };
206
207 /*!
208 \class ConstBandIterator
209
210 \brief This class implements an iterator to "navigate" over a single band (const).
211
212 We provide an efficient method to obtain all values in a raster, without regard
213 to their locations. The implementation navigates through the blocks of the image.
214
215 \ingroup rst
216
217 \sa te::rst::Band
218 */
219 template<class T> class ConstBandIterator : public te::rst::AbstractBandIterator<T>
220 {
221 public:
222
223 /*! \brief Constructor. */
225
226 /*!
227 \brief Constructor.
228
229 \param b The band to iterate.
230 */
231 ConstBandIterator(const Band* b);
232
233 /*!
234 \brief Copy constructor.
235
236 \param rhs The right-hand-side copy used to copy from.
237 */
239
240 /*! \brief Returns the value in current position (column, row) from iterator. */
241 const T operator*() const;
242
243 /*!
244 \brief Assignment operator.
245
246 \param rhs The right-hand-side copy used to copy from.
247
248 \return A reference to this object.
249 */
251
252 /*! \brief Returns an iterator referring to the first value of the band.*/
253 static ConstBandIterator begin(const Band* b);
254
255 /*! \brief Returns an iterator referring to after the end of the iterator. */
256 static ConstBandIterator end(const Band* b);
257
258 /*! \brief Replaces the current bufferized block values. */
259 void replaceBlock();
260
261 protected:
262
263 const Band* m_band; //!< The band from where to get the values.
264 };
265
266 /*!
267 \class BandIteratorWithMask
268
269 \brief This class implements an iterator to "navigate" over a single band, with a spatial restriction given by a mask.
270
271 We provide an efficient method to obtain all values in a raster, without regard
272 to their locations. The implementation navigates through the blocks of the image.
273 The mask is a 1bit raster where the pixels with value 0 are not returned by the
274 iterator, and pixels with value 1 are returned.
275
276 \ingroup rst
277
278 \sa te::rst::Band
279 */
280 template<class T> class BandIteratorWithMask : public te::rst::BandIterator<T>
281 {
282 public:
283
284 /*! \brief Constructor. */
286
287 /*!
288 \brief Constructor.
289
290 \param b The band to iterate.
291 \param m The raster with the mask.
292 */
294
295 /*!
296 \brief Copy constructor.
297
298 \param rhs The right-hand-side copy used to copy from.
299 */
301
302 void operator++();
303
304 void operator--();
305
306 /*!
307 \brief Assignment operator.
308
309 \param rhs The right-hand-side copy used to copy from.
310
311 \return A reference to this object.
312 */
314
315 /*! \brief Returns an iterator with the mask referring to the first value of the band.*/
316 static BandIteratorWithMask begin(Band* b, Raster* m);
317
318 /*! \brief Returns an iterator with the mask referring to after the end of the iterator. */
319 static BandIteratorWithMask end(Band* b, Raster* m);
320
321 protected:
322 boost::dynamic_bitset<> m_mask; //!< The internal mask of bits, one bit per pixel.
323 unsigned int m_masksize; //!< The size of the mask (rows * columns of the mask raster).
324 unsigned int m_currentpixelindex; //!< The index of the current pixel location.
325
326 };
327
328// Abstract Band Iterator implementation
330 {
331 reset();
332 }
333
335 {
336 reset();
337
338 m_blkw = b->getProperty()->m_blkw;
339 m_blkh = b->getProperty()->m_blkh;
340 m_npxlsblk = (m_blkw * m_blkh);
341 m_nblocksx = b->getProperty()->m_nblocksx;
342 m_nblocksy = b->getProperty()->m_nblocksy;
343 m_i = 0;
344 m_blkx = 0;
345 m_blky = 0;
346 m_blksize = b->getBlockSize();
347 m_getBuff = 0;
348 m_getBuffI = 0;
349 m_setBuff = 0;
350 m_setBuffI = 0;
351
352 m_lastblksize = m_npxlsblk - ((m_npxlsblk * m_nblocksx * m_nblocksy) -
354
355 if( m_blksize > 0 )
356 {
357 m_blk = new T[m_blksize];
358 }
359
360 te::rst::SetBlockFunctions(&m_getBuff, &m_getBuffI, &m_setBuff, &m_setBuffI, b->getProperty()->getType());
361 }
362
364 {
365 reset();
366
367 operator=( rhs );
368 }
369
371 {
372 if (m_blk)
373 {
374 delete [] m_blk;
375 }
376 }
377
378 template<class T> unsigned int te::rst::AbstractBandIterator<T>::getRow() const
379 {
380 return m_blky * m_blkh + (m_i - (m_i % m_blkw)) / m_blkw;
381 }
382
383 template<class T> unsigned int te::rst::AbstractBandIterator<T>::getColumn() const
384 {
385 return m_blkx * m_blkw + m_i % m_blkw;
386 }
387
389 {
390 m_i++;
391
392 if(m_i < m_npxlsblk)
393 return;
394
395 m_i = 0;
396
397 m_blkx++;
398
399 if(m_blkx < m_nblocksx)
400 {
401 replaceBlock();
402
403 return;
404 }
405
406 m_blkx = 0;
407
408 m_blky++;
409
410 if(m_blky < m_nblocksy)
411 replaceBlock();
412
413 if((m_blkx == m_nblocksx - 1) && (m_blky == m_nblocksy - 1))
414 m_npxlsblk = m_lastblksize;
415 }
416
418 {
419 m_i--;
420
421 if(m_i >= 0)
422 return;
423
424 m_i = m_npxlsblk - 1;
425
426 m_blkx--;
427
428 if(m_blkx >= 0)
429 {
430 replaceBlock();
431
432 return;
433 }
434
435 m_blkx = m_nblocksx - 1;
436
437 m_blky--;
438
439 if(m_blky >= 0)
440 replaceBlock();
441 else
442 m_blky = m_nblocksy;
443 }
444
446 {
447 if (this != &rhs)
448 {
449 if( m_blk )
450 {
451 delete [] m_blk;
452 m_blk = 0;
453 }
454
455 reset();
456
457 m_blkw = rhs.m_blkw;
458 m_blkh = rhs.m_blkh;
459 m_npxlsblk = rhs.m_npxlsblk;
460 m_nblocksx = rhs.m_nblocksx;
461 m_nblocksy = rhs.m_nblocksy;
462 m_i = rhs.m_i;
463 m_blkx = rhs.m_blkx;
464 m_blky = rhs.m_blky;
465 m_lastblksize = rhs.m_lastblksize;
466 m_blksize = rhs.m_blksize;
467
468 if( rhs.m_blk )
469 {
470 m_blk = new T[rhs.m_blksize];
471 std::memcpy( m_blk, rhs.m_blk, rhs.m_blksize );
472 }
473
474 m_getBuff = rhs.m_getBuff;
475 m_getBuffI = rhs.m_getBuffI;
476 m_setBuff = rhs.m_setBuff;
477 m_setBuffI = rhs.m_setBuffI;
478 }
479
480 return *this;
481 }
482
484 {
485 return (m_blky != rhs.m_blky);
486 }
487
488 template<class T>
490 {
491 m_blkw = -1;
492 m_blkh = -1;
493 m_npxlsblk = -1;
494 m_nblocksx = -1;
495 m_nblocksy = -1;
496 m_i = -1;
497 m_blkx = -1;
498 m_blky = -1;
499 m_lastblksize = -1;
500 m_blksize = -1;
501 m_blk = 0;
502 m_getBuff = 0;
503 m_getBuffI = 0;
504 m_setBuff = 0;
505 m_setBuffI = 0;
506 }
507
508// Band Iterator implementation
510 {
511 reset();
512 }
513
515 : te::rst::AbstractBandIterator<T>(b)
516 {
517 reset();
518
519 m_band = b;
520
522 {
523 replaceBlock();
524 }
525 }
526
528 : te::rst::AbstractBandIterator<T>(rhs)
529 {
530 reset();
531 operator=( rhs );
532 }
533
535 {
536 double value;
537
538 this->m_getBuff(this->m_i, this->m_blk, &value);
539
540 return static_cast<T>(value);
541 }
542
543 template<class T> const T te::rst::BandIterator<T>::operator*() const
544 {
545 double value;
546
547 this->m_getBuff(this->m_i, this->m_blk, &value);
548
549 return static_cast<T>(value);
550 }
551
553 {
554 reset();
555
557
558 m_band = rhs.m_band;
559
560 return *this;
561 }
562
564 {
565 return te::rst::BandIterator<T>(b);
566 }
567
569 {
571
572 it.m_blky = b->getProperty()->m_nblocksy;
573
574 return it;
575 }
576
578 {
579 m_band->read(this->m_blkx, this->m_blky, this->m_blk);
580 }
581
582 template<class T>
584 {
585 m_band = 0;
586 }
587
588// Const Band Iterator implementation
590 : te::rst::AbstractBandIterator<T>(),
591 m_band(0)
592 {
593 }
594
596 : te::rst::AbstractBandIterator<T>(b), // (te::rst::Band*)(b) ?
597 m_band(b)
598 {
599 if (this->m_blksize > 0)
600 this->replaceBlock();
601 }
602
604 : te::rst::AbstractBandIterator<T>(rhs),
605 m_band(rhs.m_band)
606 {
607 if (this->m_blksize > 0)
608 this->replaceBlock();
609 }
610
611 template<class T> const T te::rst::ConstBandIterator<T>::operator*() const
612 {
613 double value;
614
615 this->m_getBuff(this->m_i, this->m_blk, &value);
616
617 return static_cast<T>(value);
618 }
619
621 {
623
624 m_band = rhs.m_band;
625
626 return *this;
627 }
628
630 {
632 }
633
635 {
637
638 it.m_blky = b->getProperty()->m_nblocksy;
639
640 return it;
641 }
642
644 {
645 m_band->read(this->m_blkx, this->m_blky, this->m_blk);
646 }
647
648// Band Iterator With Mask implementation
650 : te::rst::BandIterator<T>(),
651 m_mask(0),
652 m_masksize(0),
653 m_currentpixelindex(0)
654 {
655 }
656
658 : te::rst::BandIterator<T>(b),
659 m_mask(0),
660 m_currentpixelindex(0)
661 {
662// fill bitset maks with raster values
665
667 m_mask.resize(m_masksize);
668
669 unsigned int i = 0;
670 while(it != itend)
671 {
672 m_mask[i++] = *it;
673
674 ++it;
675 }
676
677// to avoid the first position be outside the mask
678 if (!m_mask[0])
679 operator++();
680 }
681
683 : te::rst::BandIterator<T>(rhs),
684 m_mask(rhs.m_mask)
685 {
686 }
687
689 {
690 do
691 {
693
694 m_currentpixelindex++;
695
696 if (m_currentpixelindex >= m_masksize)
697 break;
698 }
699 while (m_mask[m_currentpixelindex] == 0);
700 }
701
703 {
704 do
705 {
707
708 m_currentpixelindex--;
709
710 if (m_currentpixelindex < 0)
711 break;
712 }
713 while (m_mask[m_currentpixelindex] == 0);
714 }
715
717 {
719
720 m_currentpixelindex = rhs.m_currentpixelindex;
721 m_masksize = rhs.m_masksize;
722 m_mask = rhs.m_mask;
723
724 return *this;
725 }
726
728 {
730 }
731
733 {
735
736 it.m_blky = b->getProperty()->m_nblocksy;
737
738 return it;
739 }
740
741 } // end namespace rst
742} // end namespace te
743
744#endif // __TERRALIB_RASTER_INTERNAL_BANDITERATOR_H
It describes one band (or dimension) of a raster.
Utility functions for dealing with raster data blocks.
This class implements an abstract iterator to "navigate" over a single band.
Definition: BandIterator.h:59
unsigned int getRow() const
Returns the current row in iterator.
Definition: BandIterator.h:378
int m_nblocksx
The number of blocks in X direction.
Definition: BandIterator.h:123
int m_blkh
The internal block height.
Definition: BandIterator.h:121
T * m_blk
Internal block.
Definition: BandIterator.h:130
te::rst::SetBufferValueFPtr m_setBuffI
A pointer to a function that helps to insert the imaginary part value into a specific buffer data typ...
Definition: BandIterator.h:134
AbstractBandIterator & operator=(const AbstractBandIterator &rhs)
Assignment operator.
Definition: BandIterator.h:445
int m_blksize
The block size of the band.
Definition: BandIterator.h:129
int m_nblocksy
The number of blocks in Y direction.
Definition: BandIterator.h:124
int m_blkw
The internal block width.
Definition: BandIterator.h:120
void operator--()
Returns to the previous position.
Definition: BandIterator.h:417
int m_lastblksize
The number of pixels inside the last block.
Definition: BandIterator.h:128
bool operator!=(const AbstractBandIterator &rhs) const
Difference operator.
Definition: BandIterator.h:483
AbstractBandIterator()
Constructor.
Definition: BandIterator.h:329
int m_blkx
The position in X of the current block.
Definition: BandIterator.h:126
int m_i
The actual position inside the block.
Definition: BandIterator.h:125
void reset()
Reset this instance to a initial state.
Definition: BandIterator.h:489
te::rst::SetBufferValueFPtr m_setBuff
A pointer to a function that helps to insert a double or complex value into a specific buffer data ty...
Definition: BandIterator.h:133
unsigned int getColumn() const
Returns the current column in iterator.
Definition: BandIterator.h:383
te::rst::GetBufferValueFPtr m_getBuff
A pointer to a function that helps to extract a double or complex value from a specific buffer data t...
Definition: BandIterator.h:131
virtual ~AbstractBandIterator()
Destructor.
Definition: BandIterator.h:370
te::rst::GetBufferValueFPtr m_getBuffI
A pointer to a function that helps to extract the imaginary part value from a specific buffer data ty...
Definition: BandIterator.h:132
int m_npxlsblk
The maximum number of pixels inside the block.
Definition: BandIterator.h:122
virtual void replaceBlock()=0
Replaces the current bufferized block values.
virtual const T operator*() const =0
Returns the value in current position (column, row) from iterator.
int m_blky
The position in Y of the current block.
Definition: BandIterator.h:127
virtual void operator++()
Advances to the next position.
Definition: BandIterator.h:388
This class implements an iterator to "navigate" over a single band, with a spatial restriction given ...
Definition: BandIterator.h:281
unsigned int m_currentpixelindex
The index of the current pixel location.
Definition: BandIterator.h:324
static BandIteratorWithMask begin(Band *b, Raster *m)
Returns an iterator with the mask referring to the first value of the band.
Definition: BandIterator.h:727
void operator++()
Advances to the next position.
Definition: BandIterator.h:688
BandIteratorWithMask & operator=(const BandIteratorWithMask &rhs)
Assignment operator.
Definition: BandIterator.h:716
unsigned int m_masksize
The size of the mask (rows * columns of the mask raster).
Definition: BandIterator.h:323
BandIteratorWithMask()
Constructor.
Definition: BandIterator.h:649
static BandIteratorWithMask end(Band *b, Raster *m)
Returns an iterator with the mask referring to after the end of the iterator.
Definition: BandIterator.h:732
boost::dynamic_bitset m_mask
The internal mask of bits, one bit per pixel.
Definition: BandIterator.h:322
This class implements an iterator to "navigate" over a single band.
Definition: BandIterator.h:154
Band * m_band
The band from where to get the values.
Definition: BandIterator.h:200
BandIterator & operator=(const BandIterator &rhs)
Assignment operator.
Definition: BandIterator.h:552
static BandIterator begin(Band *b)
Returns an iterator referring to the first value of the band.
Definition: BandIterator.h:563
void reset()
Reset this instance to a initial state.
Definition: BandIterator.h:583
T operator*()
Returns the value in current position (column, row) from iterator.
Definition: BandIterator.h:534
static BandIterator end(Band *b)
Returns an iterator referring to after the end of the iterator.
Definition: BandIterator.h:568
void replaceBlock()
Replaces the current bufferized block values.
Definition: BandIterator.h:577
BandIterator()
Constructor.
Definition: BandIterator.h:509
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
A raster band description.
Definition: Band.h:64
BandProperty * getProperty()
Returns the band property.
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
virtual Raster * getRaster() const =0
Returns the associated raster.
This class implements an iterator to "navigate" over a single band (const).
Definition: BandIterator.h:220
static ConstBandIterator end(const Band *b)
Returns an iterator referring to after the end of the iterator.
Definition: BandIterator.h:634
void replaceBlock()
Replaces the current bufferized block values.
Definition: BandIterator.h:643
ConstBandIterator & operator=(const ConstBandIterator &rhs)
Assignment operator.
Definition: BandIterator.h:620
ConstBandIterator()
Constructor.
Definition: BandIterator.h:589
static ConstBandIterator begin(const Band *b)
Returns an iterator referring to the first value of the band.
Definition: BandIterator.h:629
const T operator*() const
Returns the value in current position (column, row) from iterator.
Definition: BandIterator.h:611
const Band * m_band
The band from where to get the values.
Definition: BandIterator.h:263
An abstract class for raster data strucutures.
Definition: Raster.h:72
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
void(* SetBufferValueFPtr)(int index, void *buffer, const double *value)
The type of function used to extract data from a buffer.
Definition: BlockUtils.h:40
void(* GetBufferValueFPtr)(int index, void *buffer, double *value)
The type of function used to extract data from a buffer.
Definition: BlockUtils.h:37
TERASTEREXPORT void SetBlockFunctions(GetBufferValueFPtr *gb, GetBufferValueFPtr *gbi, SetBufferValueFPtr *sb, SetBufferValueFPtr *sbi, int type)
Sets the pointers to functions that helps to extract a double or complex value from a specific buffer...
TerraLib.
Band implementation for TerraLib 4.x.