ITK  4.13.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_TEMPLATE_EXPORT ImageReverseConstIterator
88 {
89 public:
92 
97  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
98  TImage::ImageDimension);
99 
101  itkTypeMacroNoParent(ImageReverseConstIterator);
102 
104  typedef typename TImage::IndexType IndexType;
105 
107  typedef typename TImage::SizeType SizeType;
108 
110  typedef typename TImage::OffsetType OffsetType;
111 
113  typedef typename TImage::RegionType RegionType;
114 
116  typedef TImage ImageType;
117 
121  typedef typename TImage::PixelContainer PixelContainer;
122  typedef typename PixelContainer::Pointer PixelContainerPointer;
123 
125  typedef typename TImage::InternalPixelType InternalPixelType;
126 
128  typedef typename TImage::PixelType PixelType;
129 
132  typedef typename TImage::AccessorType AccessorType;
133 
135  typedef typename TImage::AccessorFunctorType AccessorFunctorType;
136 
139  ImageReverseConstIterator():m_PixelAccessor(), m_PixelAccessorFunctor()
140  {
141  m_Buffer = 0;
142  m_Offset = 0;
143  m_BeginOffset = 0;
144  m_EndOffset = 0;
145  m_PixelAccessorFunctor.SetBegin(m_Buffer);
146  }
148 
151 
155  {
156  m_Image = it.m_Image; // copy the smart pointer
157 
158  m_Region = it.m_Region;
159 
160  m_Buffer = it.m_Buffer;
161  m_Offset = it.m_Offset;
162  m_BeginOffset = it.m_BeginOffset;
163  m_EndOffset = it.m_EndOffset;
164  m_PixelAccessor = it.m_PixelAccessor;
165  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
166  m_PixelAccessorFunctor.SetBegin(m_Buffer);
167  }
168 
171  ImageReverseConstIterator(const ImageType *ptr, const RegionType & region)
172  {
173  SizeValueType offset;
174 
175  m_Image = ptr;
176  m_Buffer = m_Image->GetBufferPointer();
177  m_Region = region;
178 
179  // Compute the end offset, one pixel before the first pixel
180  offset = m_Image->ComputeOffset( m_Region.GetIndex() );
181  m_EndOffset = offset - 1;
182 
183  // Compute the begin offset, the last pixel in the region
184  IndexType ind( m_Region.GetIndex() );
185  SizeType size( m_Region.GetSize() );
186  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
187  {
188  ind[i] += ( size[i] - 1 );
189  }
190  m_BeginOffset = m_Image->ComputeOffset(ind);
191  m_Offset = m_BeginOffset;
192 
193  m_PixelAccessor = ptr->GetPixelAccessor();
194  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
195  m_PixelAccessorFunctor.SetBegin(m_Buffer);
196  }
197 
206  {
207  m_Image = it.GetImage();
208  m_Region = it.GetRegion();
209  m_Buffer = m_Image->GetBufferPointer();
211 
212  IndexType ind = it.GetIndex();
213 
214  m_Offset = m_Image->ComputeOffset(ind);
215 
216  // Compute the end offset, one pixel before the first pixel
217  m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
218 
219  // Compute the begin offset, the last pixel in the region
220  IndexType regInd( m_Region.GetIndex() );
221  SizeType regSize( m_Region.GetSize() );
222  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
223  {
224  regInd[i] += ( regSize[i] - 1 );
225  }
226  m_BeginOffset = m_Image->ComputeOffset(regInd);
227 
228  m_PixelAccessor = m_Image->GetPixelAccessor();
229  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
230  m_PixelAccessorFunctor.SetBegin(m_Buffer);
231  }
232 
235  Self & operator=(const Self & it)
236  {
237  if(this != &it)
238  {
239  m_Image = it.m_Image; // copy the smart pointer
240  m_Region = it.m_Region;
242 
243  m_Buffer = it.m_Buffer;
244  m_Offset = it.m_Offset;
245  m_BeginOffset = it.m_BeginOffset;
246  m_EndOffset = it.m_EndOffset;
247  m_PixelAccessor = it.m_PixelAccessor;
248  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
249  m_PixelAccessorFunctor.SetBegin(m_Buffer);
250  }
251  return *this;
252  }
253 
257  {
258  m_Image = it.GetImage();
259  m_Region = it.GetRegion();
260  m_Buffer = m_Image->GetBufferPointer();
262 
263  IndexType ind = it.GetIndex();
264 
265  m_Offset = m_Image->ComputeOffset(ind);
266 
267  // Compute the end offset, one pixel before the first pixel
268  m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
269 
270  // Compute the begin offset, the last pixel in the region
271  IndexType regInd( m_Region.GetIndex() );
272  SizeType regSize( m_Region.GetSize() );
273  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
274  {
275  regInd[i] += ( regSize[i] - 1 );
276  }
277  m_BeginOffset = m_Image->ComputeOffset(regInd);
278 
279  m_PixelAccessor = m_Image->GetPixelAccessor();
280  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
281  m_PixelAccessorFunctor.SetBegin(m_Buffer);
282  return *this;
283  }
284 
286  static unsigned int GetImageIteratorDimension()
287  { return TImage::ImageDimension; }
288 
291  bool
292  operator!=(const Self & it) const
293  {
294  // two iterators are the same if they "point to" the same memory location
295  return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
296  }
297 
300  bool
301  operator==(const Self & it) const
302  {
303  // two iterators are the same if they "point to" the same memory location
304  return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
305  }
306 
312  { return m_Image->ComputeIndex(m_Offset); }
313 
316  virtual void SetIndex(const IndexType & ind)
317  { m_Offset = m_Image->ComputeOffset(ind); }
318 
321  const RegionType & GetRegion() const
322  { return m_Region; }
323 
325  const PixelType Get(void) const
326  { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
327 
329  void Set(const PixelType & value) const
330  {
331  this->m_PixelAccessorFunctor.Set(*( const_cast< InternalPixelType * >(
332  this->m_Buffer + this->m_Offset ) ), value);
333  }
334 
338  const PixelType & Value(void) const
339  { return *( m_Buffer + m_Offset ); }
340 
344  const PixelType & Value(void)
345  { return *( m_Buffer + m_Offset ); }
346 
350  itkLegacyMacro(Self Begin() const);
351 
354  void GoToBegin()
355  {
356  m_Offset = m_BeginOffset;
357  }
358 
362  itkLegacyMacro(Self End() const);
363 
366  void GoToEnd()
367  {
368  m_Offset = m_EndOffset;
369  }
370 
373  bool IsAtBegin()
374  {
375  return ( m_Offset == m_BeginOffset );
376  }
377 
380  bool IsAtEnd()
381  {
382  return ( m_Offset == m_EndOffset );
383  }
384 
385 protected: //made protected so other iterators can access
386  typename ImageType::ConstWeakPointer m_Image;
387 
388  RegionType m_Region; // region to iterate over
389 
391  SizeValueType m_BeginOffset; // offset to last pixel in region
392  SizeValueType m_EndOffset; // offset to one pixel before first pixel
393 
395 
398 };
399 } // end namespace itk
400 
401 #ifndef ITK_MANUAL_INSTANTIATION
402 #include "itkImageReverseConstIterator.hxx"
403 #endif
404 
405 #endif
const RegionType & GetRegion() const
Self & operator=(const ImageConstIterator< TImage > &it)
const IndexType GetIndex() const
unsigned long SizeValueType
Definition: itkIntTypes.h:143
ImageReverseConstIterator(const ImageType *ptr, const RegionType &region)
A multi-dimensional image iterator templated over image type.
ImageReverseConstIterator(const ImageConstIterator< TImage > &it)
Multi-dimensional image iterator.
virtual void SetIndex(const IndexType &ind)
void Set(const PixelType &value) const
const PixelType & Value(void) const
TImage::AccessorFunctorType AccessorFunctorType
const ImageType * GetImage() const