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 itkImageRegionIteratorWithIndex_h
19 #define itkImageRegionIteratorWithIndex_h
20 
21 #include "itkImageRegionConstIteratorWithIndex.h"
22 #include "itkImageIteratorWithIndex.h"
23 
24 namespace itk
25 {
26 /** \class ImageRegionIteratorWithIndex
27  * \brief A multi-dimensional iterator templated over image type that walks
28  * pixels within a region and is specialized to keep track of its image index
29  * location.
30  *
31  * This class is a specialization of ImageRegionConstIteratorWithIndex that
32  * adds write-access (the Set() method).  Please see
33  * ImageRegionConstIteratorWithIndex for more information.
34  *
35  * \par MORE INFORMATION
36  * For a complete description of the ITK Image Iterators and their API, please
37  * see the Iterators chapter in the ITK Software Guide.  The ITK Software Guide
38  * is available in print and as a free .pdf download from https://www.itk.org.
39  *
40  * \ingroup ImageIterators
41  *
42  * \sa ImageConstIterator \sa ConditionalConstIterator
43  * \sa ConstNeighborhoodIterator \sa ConstShapedNeighborhoodIterator
44  * \sa ConstSliceIterator  \sa CorrespondenceDataStructureIterator
45  * \sa FloodFilledFunctionConditionalConstIterator
46  * \sa FloodFilledImageFunctionConditionalConstIterator
47  * \sa FloodFilledImageFunctionConditionalIterator
48  * \sa FloodFilledSpatialFunctionConditionalConstIterator
49  * \sa FloodFilledSpatialFunctionConditionalIterator
50  * \sa ImageConstIterator \sa ImageConstIteratorWithIndex
51  * \sa ImageIterator \sa ImageIteratorWithIndex
52  * \sa ImageLinearConstIteratorWithIndex  \sa ImageLinearIteratorWithIndex
53  * \sa ImageRandomConstIteratorWithIndex  \sa ImageRandomIteratorWithIndex
54  * \sa ImageRegionConstIterator \sa ImageRegionConstIteratorWithIndex
55  * \sa ImageRegionExclusionConstIteratorWithIndex
56  * \sa ImageRegionExclusionIteratorWithIndex
57  * \sa ImageRegionIterator  \sa ImageRegionIteratorWithIndex
58  * \sa ImageRegionReverseConstIterator  \sa ImageRegionReverseIterator
59  * \sa ImageReverseConstIterator  \sa ImageReverseIterator
60  * \sa ImageSliceConstIteratorWithIndex  \sa ImageSliceIteratorWithIndex
61  * \sa NeighborhoodIterator \sa PathConstIterator  \sa PathIterator
62  * \sa ShapedNeighborhoodIterator  \sa SliceIterator
63  * \sa ImageConstIteratorWithIndex
64  * \ingroup ITKCommon
65  *
66  * \wiki
67  * \wikiexample{Iterators/ImageRegionIteratorWithIndex,Iterate over a region of an image with efficient access to the current index (with write access)}
68  * \endwiki
69  */
70 template< typename TImage >
71 class ITK_TEMPLATE_EXPORT ImageRegionIteratorWithIndex:public ImageRegionConstIteratorWithIndex< TImage >
72 {
73 public:
74   /** Standard class type aliases. */
75   using Self = ImageRegionIteratorWithIndex;
76   using Superclass = ImageRegionConstIteratorWithIndex< TImage >;
77 
78   /** Types inherited from the Superclass */
79   using IndexType = typename Superclass::IndexType;
80   using SizeType = typename Superclass::SizeType;
81   using OffsetType = typename Superclass::OffsetType;
82   using RegionType = typename Superclass::RegionType;
83   using ImageType = typename Superclass::ImageType;
84   using PixelContainer = typename Superclass::PixelContainer;
85   using PixelContainerPointer = typename Superclass::PixelContainerPointer;
86   using InternalPixelType = typename Superclass::InternalPixelType;
87   using PixelType = typename Superclass::PixelType;
88   using AccessorType = typename Superclass::AccessorType;
89 
90   /** Default constructor. Needed since we provide a cast constructor. */
91   ImageRegionIteratorWithIndex() = default;
92 
93   /** Constructor establishes an iterator to walk a particular image and a
94    * particular region of that image. */
95   ImageRegionIteratorWithIndex(TImage *ptr, const RegionType & region);
96 
97   /** Constructor that can be used to cast from an ImageIterator to an
98    * ImageRegionIteratorWithIndex. Many routines return an ImageIterator, but for a
99    * particular task, you may want an ImageRegionIteratorWithIndex.  Rather than
100    * provide overloaded APIs that return different types of Iterators, itk
101    * returns ImageIterators and uses constructors to cast from an
102    * ImageIterator to a ImageRegionIteratorWithIndex. */
103   ImageRegionIteratorWithIndex(const ImageIteratorWithIndex< TImage > & it);
104 
105   /** Set the pixel value */
Set(const PixelType & value)106   void Set(const PixelType & value) const
107   { this->m_PixelAccessorFunctor.Set(*( const_cast< InternalPixelType * >( this->m_Position ) ), value); }
108 
109   /** Return a reference to the pixel.
110    * This method will provide the fastest access to pixel
111    * data, but it will NOT support ImageAdaptors. */
Value()112   PixelType & Value()
113   { return *( const_cast< InternalPixelType * >( this->m_Position ) ); }
114 
115 protected:
116   /** The construction from a const iterator is declared protected
117       in order to enforce const correctness. */
118   ImageRegionIteratorWithIndex(const ImageRegionConstIteratorWithIndex< TImage > & it);
119   Self & operator=(const ImageRegionConstIteratorWithIndex< TImage > & it);
120 };
121 } // end namespace itk
122 
123 #ifndef ITK_MANUAL_INSTANTIATION
124 #include "itkImageRegionIteratorWithIndex.hxx"
125 #endif
126 
127 #endif
128