|
20 | 20 |
|
21 | 21 | #include "itkProgressReporter.h" |
22 | 22 | #include "itkImageAlgorithm.h" |
| 23 | +#include "itkImageRegionRange.h" |
23 | 24 |
|
24 | 25 | namespace itk |
25 | 26 | { |
@@ -139,29 +140,30 @@ CastImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateDataDispatche |
139 | 140 |
|
140 | 141 | this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread); |
141 | 142 |
|
142 | | - const unsigned int componentsPerPixel = outputPtr->GetNumberOfComponentsPerPixel(); |
| 143 | + ImageRegionRange<const TInputImage> inputRange(*inputPtr, inputRegionForThread); |
| 144 | + ImageRegionRange<TOutputImage> outputRange(*outputPtr, outputRegionForThread); |
143 | 145 |
|
144 | | - // Define the iterators |
145 | | - ImageScanlineConstIterator inputIt(inputPtr, inputRegionForThread); |
146 | | - ImageScanlineIterator outputIt(outputPtr, outputRegionForThread); |
| 146 | + auto inputIt = inputRange.begin(); |
| 147 | + auto outputIt = outputRange.begin(); |
| 148 | + const auto inputEnd = inputRange.end(); |
147 | 149 |
|
148 | | - while (!inputIt.IsAtEnd()) |
| 150 | + // Note: This loop has been timed for performance with conversions between image of vectors and VectorImages and other |
| 151 | + // combinations. The following was evaluated to be the best performance usage of iterators. Important considerations: |
| 152 | + // - Usage of NumericTraits::GetLength() is sometimes consant vs virutal method GetNumberOfComponentsPerPixel() |
| 153 | + // - The construction of inputPixel and outputPixel for VectorImages both reference the internal buffer and don't |
| 154 | + // require memory allocations. |
| 155 | + const unsigned int componentsPerPixel = itk::NumericTraits<OutputPixelType>::GetLength(*outputIt); |
| 156 | + while (inputIt != inputEnd) |
149 | 157 | { |
150 | | - while (!inputIt.IsAtEndOfLine()) |
| 158 | + const InputPixelType & inputPixel = *inputIt; |
| 159 | + OutputPixelType outputPixel{ *outputIt }; |
| 160 | + for (unsigned int k = 0; k < componentsPerPixel; ++k) |
151 | 161 | { |
152 | | - const InputPixelType & inputPixel = inputIt.Get(); |
153 | | - OutputPixelType value{ outputIt.Get() }; |
154 | | - for (unsigned int k = 0; k < componentsPerPixel; ++k) |
155 | | - { |
156 | | - value[k] = static_cast<typename OutputPixelType::ValueType>(inputPixel[k]); |
157 | | - } |
158 | | - outputIt.Set(value); |
159 | | - |
160 | | - ++inputIt; |
161 | | - ++outputIt; |
| 162 | + outputPixel[k] = static_cast<typename OutputPixelType::ValueType>(inputPixel[k]); |
162 | 163 | } |
163 | | - inputIt.NextLine(); |
164 | | - outputIt.NextLine(); |
| 164 | + *outputIt = outputPixel; |
| 165 | + ++inputIt; |
| 166 | + ++outputIt; |
165 | 167 | } |
166 | 168 | } |
167 | 169 |
|
|
0 commit comments