1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkImageRegionConstIterator_hxx
19 #define itkImageRegionConstIterator_hxx
20 
21 #include "itkImageRegionConstIterator.h"
22 
23 namespace itk
24 {
25 
26 //----------------------------------------------------------------------------
27 // Increment when the fastest moving direction has reached its bound.
28 // This method should *ONLY* be invoked from the operator++() method.
29 template< typename TImage >
30 void
31 ImageRegionConstIterator< TImage >
Increment()32 ::Increment()
33 {
34   // We have reached the end of the span (row), need to wrap around.
35 
36   // First back up one pixel, because we are going to use a different
37   // algorithm to compute the next pixel
38   --this->m_Offset;
39 
40   // Get the index of the last pixel on the span (row)
41   typename ImageIterator< TImage >::IndexType
42   ind = this->m_Image->ComputeIndex( static_cast< OffsetValueType >( this->m_Offset ) );
43 
44   const typename ImageIterator< TImage >::IndexType &
45   startIndex = this->m_Region.GetIndex();
46   const typename ImageIterator< TImage >::SizeType &
47   size = this->m_Region.GetSize();
48 
49   // Increment along a row, then wrap at the end of the region row.
50 
51   // Check to see if we are past the last pixel in the region
52   // Note that ++ind[0] moves to the next pixel along the row.
53   ++ind[0];
54   bool done = ( ind[0] == startIndex[0] + static_cast< IndexValueType >( size[0] ) );
55   for ( unsigned int i = 1; done && i < ImageIteratorDimension; i++ )
56     {
57     done = ( ind[i] == startIndex[i] + static_cast< IndexValueType >( size[i] ) - 1 );
58     }
59 
60   // if the iterator is outside the region (but not past region end) then
61   // we need to wrap around the region
62   unsigned int d = 0;
63   if ( !done )
64     {
65     while ( ( ( d + 1 ) < ImageIteratorDimension )
66             &&  static_cast< SizeValueType >( ind[d] - startIndex[d] ) >= size[d] )
67       {
68       ind[d] = startIndex[d];
69       ind[++d]++;
70       }
71     }
72   this->m_Offset = this->m_Image->ComputeOffset(ind);
73   m_SpanEndOffset = this->m_Offset + static_cast< OffsetValueType >( size[0] );
74   m_SpanBeginOffset = this->m_Offset;
75 }
76 
77 //----------------------------------------------------------------------------
78 // Decrement when the fastest moving direction has reached its bound.
79 // This method should *ONLY* be invoked from the operator--() method.
80 template< typename TImage >
81 void
82 ImageRegionConstIterator< TImage >
Decrement()83 ::Decrement()
84 {
85   // We have pasted the beginning of the span (row), need to wrap around.
86 
87   // First move forward one pixel, because we are going to use a different
88   // algorithm to compute the next pixel
89   this->m_Offset++;
90 
91   // Get the index of the first pixel on the span (row)
92   typename ImageIterator< TImage >::IndexType
93   ind = this->m_Image->ComputeIndex( static_cast< IndexValueType >( this->m_Offset ) );
94 
95   const typename ImageIterator< TImage >::IndexType &
96   startIndex = this->m_Region.GetIndex();
97   const typename ImageIterator< TImage >::SizeType &
98   size = this->m_Region.GetSize();
99 
100   // Deccrement along a row, then wrap at the beginning of the region row.
101 
102   // Check to see if we are past the first pixel in the region
103   // Note that --ind[0] moves to the previous pixel along the row.
104   bool done = ( --ind[0] == startIndex[0] - 1 );
105   for ( unsigned int i = 1; done && i < ImageIteratorDimension; i++ )
106     {
107     done = ( ind[i] == startIndex[i] );
108     }
109 
110   // if the iterator is outside the region (but not past region begin) then
111   // we need to wrap around the region
112   unsigned int dim = 0;
113   if ( !done )
114     {
115     while ( ( ( dim + 1 ) < ImageIteratorDimension )
116             && ( ind[dim] < startIndex[dim] ) )
117       {
118       ind[dim] = startIndex[dim] + static_cast< IndexValueType >( size[dim] ) - 1;
119       ind[++dim]--;
120       }
121     }
122   this->m_Offset = this->m_Image->ComputeOffset(ind);
123   m_SpanEndOffset = this->m_Offset + 1;
124   m_SpanBeginOffset = m_SpanEndOffset - static_cast< OffsetValueType >( size[0] );
125 }
126 
127 } // end namespace itk
128 
129 #endif
130