ITK  6.0.0
Insight Toolkit
itkImageReverseConstIterator.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
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 * https://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"
23#include <memory>
24
25namespace itk
26{
86template <typename TImage>
87class ITK_TEMPLATE_EXPORT ImageReverseConstIterator
88{
89public:
92
97 static constexpr unsigned int ImageIteratorDimension = TImage::ImageDimension;
98
100 itkVirtualGetNameOfClassMacro(ImageReverseConstIterator);
101
103 using IndexType = typename TImage::IndexType;
104
106 using SizeType = typename TImage::SizeType;
107
109 using OffsetType = typename TImage::OffsetType;
110
113
115 using ImageType = TImage;
116
120 using PixelContainer = typename TImage::PixelContainer;
122
124 using InternalPixelType = typename TImage::InternalPixelType;
125
127 using PixelType = typename TImage::PixelType;
128
131 using AccessorType = typename TImage::AccessorType;
132
134 using AccessorFunctorType = typename TImage::AccessorFunctorType;
135
139 : m_PixelAccessor()
140 , m_PixelAccessorFunctor()
141 {
142 m_Buffer = 0;
143 m_Offset = 0;
144 m_BeginOffset = 0;
145 m_EndOffset = 0;
146 m_PixelAccessorFunctor.SetBegin(m_Buffer);
147 }
151 virtual ~ImageReverseConstIterator() = default;
152
156 {
157 m_Image = it.m_Image; // copy the smart pointer
158
159 m_Region = it.m_Region;
160
161 m_Buffer = it.m_Buffer;
162 m_Offset = it.m_Offset;
163 m_BeginOffset = it.m_BeginOffset;
164 m_EndOffset = it.m_EndOffset;
165 m_PixelAccessor = it.m_PixelAccessor;
166 m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
167 m_PixelAccessorFunctor.SetBegin(m_Buffer);
168 }
169
172 ImageReverseConstIterator(const ImageType * ptr, const RegionType & region)
173 {
174 SizeValueType offset;
175
176 m_Image = ptr;
177 m_Buffer = m_Image->GetBufferPointer();
178 m_Region = region;
179
180 // Compute the end offset, one pixel before the first pixel
181 offset = m_Image->ComputeOffset(m_Region.GetIndex());
182 m_EndOffset = offset - 1;
183
184 // Compute the begin offset, the last pixel in the region
185 IndexType ind(m_Region.GetIndex());
186 SizeType size(m_Region.GetSize());
187 for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
188 {
189 ind[i] += (size[i] - 1);
190 }
191 m_BeginOffset = m_Image->ComputeOffset(ind);
192 m_Offset = m_BeginOffset;
193
194 m_PixelAccessor = ptr->GetPixelAccessor();
195 m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
196 m_PixelAccessorFunctor.SetBegin(m_Buffer);
197 }
198
207 {
208 m_Image = it.GetImage();
209 m_Region = it.GetRegion();
210 m_Buffer = m_Image->GetBufferPointer();
213 IndexType ind = it.GetIndex();
214
215 m_Offset = m_Image->ComputeOffset(ind);
216
217 // Compute the end offset, one pixel before the first pixel
218 m_EndOffset = m_Image->ComputeOffset(m_Region.GetIndex()) - 1;
219
220 // Compute the begin offset, the last pixel in the region
221 IndexType regInd(m_Region.GetIndex());
222 SizeType regSize(m_Region.GetSize());
223 for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
224 {
225 regInd[i] += (regSize[i] - 1);
226 }
227 m_BeginOffset = m_Image->ComputeOffset(regInd);
228
229 m_PixelAccessor = m_Image->GetPixelAccessor();
230 m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
231 m_PixelAccessorFunctor.SetBegin(m_Buffer);
232 }
233
236 Self &
237 operator=(const Self & it)
238 {
239 if (this != &it)
240 {
241 m_Image = it.m_Image; // copy the smart pointer
242 m_Region = it.m_Region;
245 m_Buffer = it.m_Buffer;
246 m_Offset = it.m_Offset;
247 m_BeginOffset = it.m_BeginOffset;
248 m_EndOffset = it.m_EndOffset;
249 m_PixelAccessor = it.m_PixelAccessor;
250 m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
251 m_PixelAccessorFunctor.SetBegin(m_Buffer);
252 }
253 return *this;
254 }
255
258 Self &
260 {
261 m_Image = it.GetImage();
262 m_Region = it.GetRegion();
263 m_Buffer = m_Image->GetBufferPointer();
266 IndexType ind = it.GetIndex();
267
268 m_Offset = m_Image->ComputeOffset(ind);
269
270 // Compute the end offset, one pixel before the first pixel
271 m_EndOffset = m_Image->ComputeOffset(m_Region.GetIndex()) - 1;
272
273 // Compute the begin offset, the last pixel in the region
274 IndexType regInd(m_Region.GetIndex());
275 SizeType regSize(m_Region.GetSize());
276 for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
277 {
278 regInd[i] += (regSize[i] - 1);
279 }
280 m_BeginOffset = m_Image->ComputeOffset(regInd);
281
282 m_PixelAccessor = m_Image->GetPixelAccessor();
283 m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
284 m_PixelAccessorFunctor.SetBegin(m_Buffer);
285 return *this;
286 }
287
289 static unsigned int
291 {
292 return TImage::ImageDimension;
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
305
306
311 const IndexType
313 {
314 return m_Image->ComputeIndex(m_Offset);
315 }
316
319 virtual void
320 SetIndex(const IndexType & ind)
321 {
322 m_Offset = m_Image->ComputeOffset(ind);
323 }
324
327 const RegionType &
328 GetRegion() const
329 {
330 return m_Region;
331 }
332
334 const PixelType
335 Get() const
336 {
337 return m_PixelAccessorFunctor.Get(*(m_Buffer + m_Offset));
338 }
339
341 void
342 Set(const PixelType & value) const
343 {
344 this->m_PixelAccessorFunctor.Set(*(const_cast<InternalPixelType *>(this->m_Buffer + this->m_Offset)), value);
345 }
346
350 const PixelType &
351 Value() const
352 {
353 return *(m_Buffer + m_Offset);
354 }
355
359 const PixelType &
361 {
362 return *(m_Buffer + m_Offset);
363 }
364
367 void
369 {
370 m_Offset = m_BeginOffset;
371 }
372
375 void
377 {
378 m_Offset = m_EndOffset;
379 }
380
383 bool
384 IsAtBegin() const
385 {
386 return (m_Offset == m_BeginOffset);
387 }
388
391 bool
392 IsAtEnd() const
393 {
394 return (m_Offset == m_EndOffset);
395 }
396
397protected: // made protected so other iterators can access
398 typename ImageType::ConstWeakPointer m_Image{};
399
400 RegionType m_Region{}; // region to iterate over
401
402 SizeValueType m_Offset{};
403 SizeValueType m_BeginOffset{}; // offset to last pixel in region
404 SizeValueType m_EndOffset{}; // offset to one pixel before first pixel
405
406 const InternalPixelType * m_Buffer{};
407
408 AccessorType m_PixelAccessor{};
409 AccessorFunctorType m_PixelAccessorFunctor{};
410};
411} // end namespace itk
412
413#endif
Pixel-wise addition of two images.
A multi-dimensional image iterator templated over image type.
const RegionType & GetRegion() const
const IndexType GetIndex() const
const ImageType * GetImage() const
Multi-dimensional image iterator.
virtual void SetIndex(const IndexType &ind)
ImageReverseConstIterator(const ImageConstIterator< TImage > &it)
typename TImage::AccessorFunctorType AccessorFunctorType
Self & operator=(const ImageConstIterator< TImage > &it)
virtual ~ImageReverseConstIterator()=default
typename TImage::AccessorType AccessorType
typename PixelContainer::Pointer PixelContainerPointer
typename TImage::PixelContainer PixelContainer
typename TImage::InternalPixelType InternalPixelType
ImageReverseConstIterator(const ImageType *ptr, const RegionType &region)
void Set(const PixelType &value) const
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition: itkIntTypes.h:86