Skip to content

Commit

Permalink
BUG: Do not refuse to read images of vectors with many components
Browse files Browse the repository at this point in the history
This was encountered when reading 31-component image
which is float on disk, but is being read as double.
  • Loading branch information
dzenanz committed Jan 14, 2022
1 parent df6e287 commit 5492689
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Modules/IO/ImageBase/include/itkConvertPixelBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 39 additions & 3 deletions Modules/IO/ImageBase/include/itkConvertPixelBuffer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,19 @@ ConvertPixelBuffer<InputPixelType, OutputPixelType, OutputConvertTraits>::Conver
break;
}
default:
itkGenericExceptionMacro("No conversion available from "
<< inputNumberOfComponents
<< " components to: " << OutputConvertTraits::GetNumberOfComponents() << " components");
{
if (inputNumberOfComponents == static_cast<int>(OutputConvertTraits::GetNumberOfComponents()))
{
ConvertVectorToVector(inputData, inputNumberOfComponents, outputData, size);
}
else
{
itkGenericExceptionMacro("No conversion available from " << inputNumberOfComponents << " components to: "
<< OutputConvertTraits::GetNumberOfComponents()
<< " components");
}
break;
}
}
}

Expand Down Expand Up @@ -481,6 +490,33 @@ ConvertPixelBuffer<InputPixelType, OutputPixelType, OutputConvertTraits>::Conver
}
}

template <typename InputPixelType, typename OutputPixelType, typename OutputConvertTraits>
void
ConvertPixelBuffer<InputPixelType, OutputPixelType, OutputConvertTraits>::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<OutputComponentType>(*(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 <typename InputPixelType, typename OutputPixelType, typename OutputConvertTraits>
void
ConvertPixelBuffer<InputPixelType, OutputPixelType, OutputConvertTraits>::ConvertTensor6ToTensor6(
Expand Down

0 comments on commit 5492689

Please sign in to comment.