Skip to content

Commit

Permalink
Merge pull request #1166 from jadh4v/fix-WebAssemblyInterface-vector-…
Browse files Browse the repository at this point in the history
…images

fix(WebAssemblyInterface): support vector/multi-component images
  • Loading branch information
thewtex authored Jun 25, 2024
2 parents a411569 + 86b1687 commit 9ab6074
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 5 deletions.
7 changes: 4 additions & 3 deletions include/itkImageToWasmImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ ImageToWasmImageFilter<TImage>
imageJSON->SetImage(image);

using PointType = typename TImage::PointType;
using PixelType = typename TImage::IOPixelType;
using ConvertPixelTraits = DefaultConvertPixelTraits<PixelType>;
using PixelType = typename TImage::PixelType;
using IOPixelType = typename TImage::IOPixelType;
using ConvertPixelTraits = DefaultConvertPixelTraits<IOPixelType>;
using ComponentType = typename ConvertPixelTraits::ComponentType;

rapidjson::Document document;
Expand All @@ -160,7 +161,7 @@ ImageToWasmImageFilter<TImage>
pixelType.SetString( wasm::MapPixelType<PixelType>::PixelString.data(), allocator );
imageType.AddMember("pixelType", pixelType.Move(), allocator );

imageType.AddMember("components", rapidjson::Value( ConvertPixelTraits::GetNumberOfComponents() ).Move(), allocator );
imageType.AddMember("components", rapidjson::Value( image->GetNumberOfComponentsPerPixel() ).Move(), allocator );

document.AddMember( "imageType", imageType.Move(), allocator );

Expand Down
2 changes: 1 addition & 1 deletion include/itkOutputImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ 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()->GetPixelContainer()->Size() * sizeof(typename ConvertPixelTraits::ComponentType);
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.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const defaultImageTag = '20240605-0b83f08b'
const defaultImageTag = '20240624-b89bcb6c'
export default defaultImageTag
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ExternalData_Expand_Arguments(

set(WebAssemblyInterfaceTests
itkWasmImageInterfaceTest.cxx
itkWasmVectorImageInterfaceTest.cxx
itkWasmImageInterfaceWithNegativeIndexTest.cxx
itkWasmMeshInterfaceTest.cxx
itkWasmPolyDataInterfaceTest.cxx
Expand Down Expand Up @@ -87,6 +88,15 @@ itk_add_test(NAME itkWasmImageInterfaceTest
${ITK_TEST_OUTPUT_DIR}/itkWasmImageInterfaceTest.mha
)

itk_add_test(NAME itkWasmVectorImageInterfaceTest
COMMAND WebAssemblyInterfaceTestDriver
--compare DATA{Input/apple.jpg}
${ITK_TEST_OUTPUT_DIR}/itkWasmVectorImageInterfaceTest.mha
itkWasmVectorImageInterfaceTest
DATA{Input/apple.jpg}
${ITK_TEST_OUTPUT_DIR}/itkWasmVectorImageInterfaceTest.mha
)

itk_add_test(NAME itkWasmImageInterfaceWithNegativeIndexTest
COMMAND WebAssemblyInterfaceTestDriver
--compare DATA{Baseline/negative_idx_padded.mha}
Expand Down
64 changes: 64 additions & 0 deletions test/itkWasmVectorImageInterfaceTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkImageToWasmImageFilter.h"
#include "itkWasmImageToImageFilter.h"

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkTestingMacros.h"

int
itkWasmVectorImageInterfaceTest(int argc, char * argv[])
{
if (argc < 3)
{
std::cerr << "Missing parameters" << std::endl;
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " InputImage OutputImage" << std::endl;
return EXIT_FAILURE;
}
const char * inputImageFile = argv[1];
const char * outputImageFile = argv[2];

constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
using ImageType = itk::VectorImage<PixelType, Dimension>;
using ImagePointer = ImageType::Pointer;

ImagePointer inputImage = nullptr;
ITK_TRY_EXPECT_NO_EXCEPTION(inputImage = itk::ReadImage<ImageType>(inputImageFile));
std::cout << "inputImage: " << inputImage << std::endl;

using ImageToWasmImageFilterType = itk::ImageToWasmImageFilter<ImageType>;
auto imageToJSON = ImageToWasmImageFilterType::New();
imageToJSON->SetInput(inputImage);
imageToJSON->Update();
auto imageJSON = imageToJSON->GetOutput();
std::cout << "Image JSON: " << imageJSON->GetJSON() << std::endl;

using WasmImageToImageFilterType = itk::WasmImageToImageFilter<ImageType>;
auto jsonToImage = WasmImageToImageFilterType::New();
jsonToImage->SetInput(imageToJSON->GetOutput());
jsonToImage->Update();
ImageType::Pointer convertedImage = jsonToImage->GetOutput();

std::cout << "convertedImage: " << convertedImage << std::endl;

ITK_TRY_EXPECT_NO_EXCEPTION(itk::WriteImage(convertedImage, outputImageFile));

return EXIT_SUCCESS;
}

0 comments on commit 9ab6074

Please sign in to comment.