diff --git a/Modules/IO/ImageBase/include/itkConvertPixelBuffer.h b/Modules/IO/ImageBase/include/itkConvertPixelBuffer.h index dc613090dd0..e061dc16186 100644 --- a/Modules/IO/ImageBase/include/itkConvertPixelBuffer.h +++ b/Modules/IO/ImageBase/include/itkConvertPixelBuffer.h @@ -139,6 +139,13 @@ class ITK_TEMPLATE_EXPORT ConvertPixelBuffer OutputPixelType * outputData, size_t size); + /** This is meant for many component vectors, e.g. converting 31-component float to 31-component double. */ + static void + ConvertVectorToVector(InputPixelType * inputData, + int inputNumberOfComponents, + OutputPixelType * outputData, + size_t size); + /** Convert tensor output. */ /** Each input is made into a 6 component symmetric pixel */ static void diff --git a/Modules/IO/ImageBase/include/itkConvertPixelBuffer.hxx b/Modules/IO/ImageBase/include/itkConvertPixelBuffer.hxx index 546ec2fc931..d98bf4199e4 100644 --- a/Modules/IO/ImageBase/include/itkConvertPixelBuffer.hxx +++ b/Modules/IO/ImageBase/include/itkConvertPixelBuffer.hxx @@ -146,10 +146,19 @@ ConvertPixelBuffer::Conver break; } default: - itkGenericExceptionMacro("No conversion available from " - << inputNumberOfComponents - << " components to: " << OutputConvertTraits::GetNumberOfComponents() << " components"); + { + if (inputNumberOfComponents == static_cast(OutputConvertTraits::GetNumberOfComponents())) + { + ConvertVectorToVector(inputData, inputNumberOfComponents, outputData, size); + } + else + { + itkGenericExceptionMacro("No conversion available from " << inputNumberOfComponents << " components to: " + << OutputConvertTraits::GetNumberOfComponents() + << " components"); + } break; + } } } @@ -481,6 +490,33 @@ ConvertPixelBuffer::Conver } } +template +void +ConvertPixelBuffer::ConvertVectorToVector( + InputPixelType * inputData, + int inputNumberOfComponents, + OutputPixelType * outputData, + size_t size) +{ + int outputNumberOfComponents = OutputConvertTraits::GetNumberOfComponents(); + int componentCount = std::min(inputNumberOfComponents, outputNumberOfComponents); + + for (size_t i = 0; i < size; ++i) + { + for (int c = 0; c < componentCount; ++c) + { + OutputConvertTraits::SetNthComponent(c, *outputData, static_cast(*(inputData + c))); + } + for (int c = componentCount; c < outputNumberOfComponents; ++c) + { + OutputConvertTraits::SetNthComponent(c, *outputData, 0); // set the rest of components to zero + } + + ++outputData; + inputData += inputNumberOfComponents; + } +} + template void ConvertPixelBuffer::ConvertTensor6ToTensor6(