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