diff --git a/Modules/Filtering/ImageFilterBase/include/itkCastImageFilter.hxx b/Modules/Filtering/ImageFilterBase/include/itkCastImageFilter.hxx index 30106a29a57..2cb1edbca6c 100644 --- a/Modules/Filtering/ImageFilterBase/include/itkCastImageFilter.hxx +++ b/Modules/Filtering/ImageFilterBase/include/itkCastImageFilter.hxx @@ -21,6 +21,7 @@ #include "itkProgressReporter.h" #include "itkImageAlgorithm.h" #include "itkImageRegionRange.h" +#include "itkVariableLengthVector.h" namespace itk { @@ -156,12 +157,19 @@ CastImageFilter::DynamicThreadedGenerateDataDispatche while (inputIt != inputEnd) { const InputPixelType & inputPixel = *inputIt; - OutputPixelType outputPixel{ *outputIt }; + + using OutputPixelValueType = typename OutputPixelType::ValueType; + + constexpr bool isVariableLengthVector = std::is_same_v>; + + // If the output pixel type is a VariableLengthVector, it behaves as a "reference" to the internal data. Otherwise + // declare outputPixel as a reference, `OutputPixelType &`, to allow it to access the internal buffer directly. + std::conditional_t outputPixel{ *outputIt }; + for (unsigned int k = 0; k < componentsPerPixel; ++k) { - outputPixel[k] = static_cast(inputPixel[k]); + outputPixel[k] = static_cast(inputPixel[k]); } - *outputIt = outputPixel; ++inputIt; ++outputIt; }