ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkImageReverseConstIterator.h
Go to the documentation of this file.
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 __itkImageReverseConstIterator_h
19 #define __itkImageReverseConstIterator_h
20 
21 #include "itkSize.h"
22 #include "itkImageConstIterator.h"
23 #include <memory>
24 
25 namespace itk
26 {
86 template< typename TImage >
87 class ITK_EXPORT ImageReverseConstIterator
88 {
89 public:
92 
97  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
98  TImage::ImageDimension);
99 
101  typedef typename TImage::IndexType IndexType;
102 
104  typedef typename TImage::SizeType SizeType;
105 
107  typedef typename TImage::OffsetType OffsetType;
108 
110  typedef typename TImage::RegionType RegionType;
111 
113  typedef TImage ImageType;
114 
118  typedef typename TImage::PixelContainer PixelContainer;
119  typedef typename PixelContainer::Pointer PixelContainerPointer;
120 
122  typedef typename TImage::InternalPixelType InternalPixelType;
123 
125  typedef typename TImage::PixelType PixelType;
126 
129  typedef typename TImage::AccessorType AccessorType;
130 
132  typedef typename TImage::AccessorFunctorType AccessorFunctorType;
133 
136  ImageReverseConstIterator():m_PixelAccessor(), m_PixelAccessorFunctor()
137  {
138  m_Buffer = 0;
139  m_Offset = 0;
140  m_BeginOffset = 0;
141  m_EndOffset = 0;
142  m_PixelAccessorFunctor.SetBegin(m_Buffer);
143  }
145 
148 
152  {
153  m_Image = it.m_Image; // copy the smart pointer
154 
155  m_Region = it.m_Region;
156 
157  m_Buffer = it.m_Buffer;
158  m_Offset = it.m_Offset;
159  m_BeginOffset = it.m_BeginOffset;
160  m_EndOffset = it.m_EndOffset;
161  m_PixelAccessor = it.m_PixelAccessor;
162  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
163  m_PixelAccessorFunctor.SetBegin(m_Buffer);
164  }
165 
168  ImageReverseConstIterator(const ImageType *ptr, const RegionType & region)
169  {
170  SizeValueType offset;
171 
172  m_Image = ptr;
173  m_Buffer = m_Image->GetBufferPointer();
174  m_Region = region;
175 
176  // Compute the end offset, one pixel before the first pixel
177  offset = m_Image->ComputeOffset( m_Region.GetIndex() );
178  m_EndOffset = offset - 1;
179 
180  // Compute the begin offset, the last pixel in the region
181  IndexType ind( m_Region.GetIndex() );
182  SizeType size( m_Region.GetSize() );
183  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
184  {
185  ind[i] += ( size[i] - 1 );
186  }
187  m_BeginOffset = m_Image->ComputeOffset(ind);
188  m_Offset = m_BeginOffset;
189 
190  m_PixelAccessor = ptr->GetPixelAccessor();
191  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
192  m_PixelAccessorFunctor.SetBegin(m_Buffer);
193  }
194 
203  {
204  m_Image = it.GetImage();
205  m_Region = it.GetRegion();
206  m_Buffer = m_Image->GetBufferPointer();
208 
209  IndexType ind = it.GetIndex();
210 
211  m_Offset = m_Image->ComputeOffset(ind);
212 
213  // Compute the end offset, one pixel before the first pixel
214  m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
215 
216  // Compute the begin offset, the last pixel in the region
217  IndexType regInd( m_Region.GetIndex() );
218  SizeType regSize( m_Region.GetSize() );
219  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
220  {
221  regInd[i] += ( regSize[i] - 1 );
222  }
223  m_BeginOffset = m_Image->ComputeOffset(regInd);
224 
225  m_PixelAccessor = m_Image->GetPixelAccessor();
226  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
227  m_PixelAccessorFunctor.SetBegin(m_Buffer);
228  }
229 
232  Self & operator=(const Self & it)
233  {
234  if(this != &it)
235  {
236  m_Image = it.m_Image; // copy the smart pointer
237  m_Region = it.m_Region;
239 
240  m_Buffer = it.m_Buffer;
241  m_Offset = it.m_Offset;
242  m_BeginOffset = it.m_BeginOffset;
243  m_EndOffset = it.m_EndOffset;
244  m_PixelAccessor = it.m_PixelAccessor;
245  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
246  m_PixelAccessorFunctor.SetBegin(m_Buffer);
247  }
248  return *this;
249  }
250 
253  Self & operator=(const ImageConstIterator< TImage > & it)
254  {
255  m_Image = it.GetImage();
256  m_Region = it.GetRegion();
257  m_Buffer = m_Image->GetBufferPointer();
259 
260  IndexType ind = it.GetIndex();
261 
262  m_Offset = m_Image->ComputeOffset(ind);
263 
264  // Compute the end offset, one pixel before the first pixel
265  m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
266 
267  // Compute the begin offset, the last pixel in the region
268  IndexType regInd( m_Region.GetIndex() );
269  SizeType regSize( m_Region.GetSize() );
270  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
271  {
272  regInd[i] += ( regSize[i] - 1 );
273  }
274  m_BeginOffset = m_Image->ComputeOffset(regInd);
275 
276  m_PixelAccessor = m_Image->GetPixelAccessor();
277  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
278  m_PixelAccessorFunctor.SetBegin(m_Buffer);
279  return *this;
280  }
281 
283  static unsigned int GetImageIteratorDimension()
284  { return TImage::ImageDimension; }
285 
288  bool
289  operator!=(const Self & it) const
290  {
291  // two iterators are the same if they "point to" the same memory location
292  return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
293  }
294 
297  bool
298  operator==(const Self & it) const
299  {
300  // two iterators are the same if they "point to" the same memory location
301  return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
302  }
303 
308  const IndexType GetIndex()
309  { return m_Image->ComputeIndex(m_Offset); }
310 
313  virtual void SetIndex(const IndexType & ind)
314  { m_Offset = m_Image->ComputeOffset(ind); }
315 
318  const RegionType & GetRegion() const
319  { return m_Region; }
320 
322  const PixelType Get(void) const
323  { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
324 
326  void Set(const PixelType & value) const
327  {
328  this->m_PixelAccessorFunctor.Set(*( const_cast< InternalPixelType * >(
329  this->m_Buffer + this->m_Offset ) ), value);
330  }
331 
335  const PixelType & Value(void) const
336  { return *( m_Buffer + m_Offset ); }
337 
341  const PixelType & Value(void)
342  { return *( m_Buffer + m_Offset ); }
343 
347  itkLegacyMacro(Self Begin() const);
348 
351  void GoToBegin()
352  {
353  m_Offset = m_BeginOffset;
354  }
355 
359  itkLegacyMacro(Self End() const);
360 
363  void GoToEnd()
364  {
365  m_Offset = m_EndOffset;
366  }
367 
370  bool IsAtBegin()
371  {
372  return ( m_Offset == m_BeginOffset );
373  }
374 
377  bool IsAtEnd()
378  {
379  return ( m_Offset == m_EndOffset );
380  }
381 
382 protected: //made protected so other iterators can access
383  typename ImageType::ConstWeakPointer m_Image;
384 
385  RegionType m_Region; // region to iterate over
386 
388  SizeValueType m_BeginOffset; // offset to last pixel in region
389  SizeValueType m_EndOffset; // offset to one pixel before first pixel
390 
392 
395 };
396 } // end namespace itk
397 
398 #ifndef ITK_MANUAL_INSTANTIATION
399 #include "itkImageReverseConstIterator.hxx"
400 #endif
401 
402 #endif
403