Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and add tests for computation of buffer size when converting VectorImage between native-image and wasm-image. #1193

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/itkImportVectorImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ ImportVectorImageFilter<TOutputImage>::GenerateOutputInformation()
outputPtr->SetDirection(m_Direction);
outputPtr->SetLargestPossibleRegion(m_LargestPossibleRegion);

if (outputPtr->GetNameOfClass() == "VectorImage")
if (outputPtr->GetNameOfClass() == std::string("VectorImage"))
{
outputPtr->SetNumberOfComponentsPerPixel(m_VectorImageComponentsPerPixel);
}
Expand Down
10 changes: 9 additions & 1 deletion include/itkOutputImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ class ITK_TEMPLATE_EXPORT OutputImage

const auto dataAddress = reinterpret_cast< size_t >( wasmImage->GetImage()->GetBufferPointer() );
using ConvertPixelTraits = DefaultConvertPixelTraits<typename ImageType::PixelType>;
const auto dataSize = wasmImage->GetImage()->GetPixelContainer()->Size() * sizeof(typename ConvertPixelTraits::ComponentType) * ConvertPixelTraits::GetNumberOfComponents();
const auto dataSize =
wasmImage->GetImage()->GetBufferedRegion().GetNumberOfPixels()
* sizeof(typename ConvertPixelTraits::ComponentType)
* wasmImage->GetImage()->GetNumberOfComponentsPerPixel();
if (dataSize <= 0)
{
std::cerr << "dataSize cannot be zero or negative." << std::endl;
abort();
}
setMemoryStoreOutputArray(0, index, 0, dataAddress, dataSize);

const auto directionAddress = reinterpret_cast< size_t >( wasmImage->GetImage()->GetDirection().GetVnlMatrix().begin() );
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ itk_add_test(NAME itkPipelineMemoryIOTest
${ITK_TEST_OUTPUT_DIR}/itkPipelineMemoryIOTestOutputCompositeTransform.h5
DATA{Input/cow.vtk}
${ITK_TEST_OUTPUT_DIR}/itkPipelineMemoryIOTestOutputPointSet.vtk
DATA{Input/apple.jpg}
${ITK_TEST_OUTPUT_DIR}/itkPipelineMemoryIOTestOutputVectorImage.mha
)

itk_add_test(NAME itkPipelineInterfaceJSONTest
Expand Down
34 changes: 32 additions & 2 deletions test/itkPipelineMemoryIOTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,28 @@ itkPipelineMemoryIOTest(int argc, char * argv[])
void * readWasmPointSetPointer = reinterpret_cast< void * >( itk_wasm_input_json_alloc(0, 7, readPointSetJSON.size()));
std::memcpy(readWasmPointSetPointer, readPointSetJSON.data(), readPointSetJSON.size());

const char * mockArgv[] = {"itkPipelineMemoryIOTest", "--memory-io", "0", "0", "1", "1", "2", "2", "3", "3", "4", "4", "5", "5", "6", "6", "7", "7", NULL};
itk::wasm::Pipeline pipeline("pipeline-test", "A test ITK Wasm Pipeline", 18, const_cast< char ** >(mockArgv));
using VectorImageType = itk::VectorImage<PixelType, Dimension>;
// VectorImage test
const char * inputVectorImageFile = argv[18];
auto readVectorInputImage = itk::ReadImage<VectorImageType>(inputVectorImageFile);
using VectorImageToWasmImageFilterType = itk::ImageToWasmImageFilter<VectorImageType>;
auto vectorImageToWasmImageFilter = VectorImageToWasmImageFilterType::New();
vectorImageToWasmImageFilter->SetInput(readVectorInputImage);
vectorImageToWasmImageFilter->Update();
auto readWasmVectorImage = vectorImageToWasmImageFilter->GetOutput();

auto readWasmVectorImageData = reinterpret_cast< const void * >(readWasmVectorImage->GetImage()->GetBufferPointer());
const auto readWasmVectorImageDataSize = readWasmVectorImage->GetImage()->GetPixelContainer()->Size();
const size_t readWasmVectorImageDataPointerAddress = itk_wasm_input_array_alloc(0, 8, 0, readWasmVectorImageDataSize);
auto readWasmVectorImageDataPointer = reinterpret_cast< void * >(readWasmVectorImageDataPointerAddress);
std::memcpy(readWasmVectorImageDataPointer, readWasmVectorImageData, readWasmVectorImageDataSize);

auto readVectorImageJSON = readWasmVectorImage->GetJSON();
void * readWasmVectorImagePointer = reinterpret_cast< void * >( itk_wasm_input_json_alloc(0, 8, readVectorImageJSON.size()));
std::memcpy(readWasmVectorImagePointer, readVectorImageJSON.data(), readVectorImageJSON.size());

const char * mockArgv[] = {"itkPipelineMemoryIOTest", "--memory-io", "0", "0", "1", "1", "2", "2", "3", "3", "4", "4", "5", "5", "6", "6", "7", "7", "8", "8", NULL};
itk::wasm::Pipeline pipeline("pipeline-test", "A test ITK Wasm Pipeline", 20, const_cast< char ** >(mockArgv));

std::string example_string_option = "default";
pipeline.add_option("-s,--string", example_string_option, "A help string");
Expand Down Expand Up @@ -294,6 +314,14 @@ itkPipelineMemoryIOTest(int argc, char * argv[])
OutputPointSetType outputPointSet;
pipeline.add_option("output-point-set", outputPointSet, "The output point set")->required()->type_name("OUTPUT_POINTSET");

using InputVectorImageType = itk::wasm::InputImage<VectorImageType>;
InputVectorImageType inputVectorImage;
pipeline.add_option("input-vector-image", inputVectorImage, "The inputImage")->required()->type_name("INPUT_IMAGE");

using OutputVectorImageType = itk::wasm::OutputImage<VectorImageType>;
OutputVectorImageType outputVectorImage;
pipeline.add_option("output-vector-image", outputVectorImage, "The outputVectorImage")->required()->type_name("OUTPUT_IMAGE");

ITK_WASM_PARSE(pipeline);

outputImage.Set(inputImage.Get());
Expand All @@ -318,5 +346,7 @@ itkPipelineMemoryIOTest(int argc, char * argv[])
outputTransform.Set(inputTransform.Get());
outputCompositeTransform.Set(inputCompositeTransform.Get());

outputVectorImage.Set(inputVectorImage.Get());

return EXIT_SUCCESS;
}
4 changes: 4 additions & 0 deletions test/itkPipelineTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ itkPipelineTest(int argc, char * argv[])

const std::string inputBinaryStreamContent{ std::istreambuf_iterator<char>(inputBinaryStream.Get()),
std::istreambuf_iterator<char>() };
#if defined(_WIN64) || defined(_WIN32)
ITK_TEST_EXPECT_TRUE(inputBinaryStreamContent == "test 123\r\n");
#else
ITK_TEST_EXPECT_TRUE(inputBinaryStreamContent == "test 123\n");
#endif

outputBinaryStream.Get() << inputBinaryStreamContent;

Expand Down
Loading