Skip to content

Commit 51a93b5

Browse files
authored
Merge pull request #1072 from ramonemiliani93/main
fix: Compute origin using largest possible region
2 parents 6f960f6 + 49b7f23 commit 51a93b5

File tree

6 files changed

+99
-2
lines changed

6 files changed

+99
-2
lines changed

include/itkImageToWasmImageFilter.hxx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ ImageToWasmImageFilter<TImage>
137137

138138
imageJSON->SetImage(image);
139139

140+
using PointType = typename TImage::PointType;
140141
using PixelType = typename TImage::IOPixelType;
141142
using ConvertPixelTraits = DefaultConvertPixelTraits<PixelType>;
142143
using ComponentType = typename ConvertPixelTraits::ComponentType;
@@ -164,7 +165,10 @@ ImageToWasmImageFilter<TImage>
164165
document.AddMember( "imageType", imageType.Move(), allocator );
165166

166167
rapidjson::Value origin(rapidjson::kArrayType);
167-
const auto imageOrigin = image->GetOrigin();
168+
169+
const auto largestRegion = image->GetLargestPossibleRegion();
170+
PointType imageOrigin;
171+
image->TransformIndexToPhysicalPoint(largestRegion.GetIndex(), imageOrigin);
168172
for( unsigned int ii = 0; ii < dimension; ++ii )
169173
{
170174
origin.PushBack(rapidjson::Value().SetDouble(imageOrigin[ii]), allocator);

itk-module.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ itk_module(WebAssemblyInterface
1515
TEST_DEPENDS
1616
ITKTestKernel
1717
ITKMesh
18+
ITKImageGrid
1819
FACTORY_NAMES
1920
ImageIO::Wasm
2021
MeshIO::Wasm
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bafkreifzpwttltw372hen6ouewhpqwodxs4zo4kjk7ekyclzcoshfnxq3u

test/CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ExternalData_Expand_Arguments(
3030
DATA{Input/sphere.fsa}
3131
DATA{Input/sphere.fsb}
3232
DATA{Input/box.obj}
33+
DATA{Input/negative_idx_base.mha}
3334
DATA{Input/octa.off}
3435
DATA{Input/sphere.stl}
3536
DATA{Input/cthead1.iwi.cbor}
@@ -50,10 +51,12 @@ ExternalData_Expand_Arguments(
5051
DATA{Input/PNGSeries/,REGEX:mri3D_.[0-9]+.png}
5152
DATA{Input/11706c2.CNG.swc}
5253
DATA{Baseline/web_worker_pool.cy.png}
53-
)
54+
DATA{Baseline/negative_idx_padded.mha}
55+
)
5456

5557
set(WebAssemblyInterfaceTests
5658
itkWasmImageInterfaceTest.cxx
59+
itkWasmImageInterfaceWithNegativeIndexTest.cxx
5760
itkWasmMeshInterfaceTest.cxx
5861
itkWasmPolyDataInterfaceTest.cxx
5962
itkWasmImageIOTest.cxx
@@ -84,6 +87,15 @@ itk_add_test(NAME itkWasmImageInterfaceTest
8487
${ITK_TEST_OUTPUT_DIR}/itkWasmImageInterfaceTest.mha
8588
)
8689

90+
itk_add_test(NAME itkWasmImageInterfaceWithNegativeIndexTest
91+
COMMAND WebAssemblyInterfaceTestDriver
92+
--compare DATA{Baseline/negative_idx_padded.mha}
93+
${ITK_TEST_OUTPUT_DIR}/itkWasmImageInterfaceWithNegativeIndexTest.mha
94+
itkWasmImageInterfaceWithNegativeIndexTest
95+
DATA{Input/negative_idx_base.mha}
96+
${ITK_TEST_OUTPUT_DIR}/itkWasmImageInterfaceWithNegativeIndexTest.mha
97+
)
98+
8799
itk_add_test(NAME itkWasmMeshInterfaceTest
88100
COMMAND WebAssemblyInterfaceTestDriver
89101
itkWasmMeshInterfaceTest

test/Input/negative_idx_base.mha.cid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bafkreiec2npcdz52d6gjmod7f4cfckgqcmizch3dpujxhjpxxqggxlkemy
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
#include "itkImageToWasmImageFilter.h"
19+
#include "itkWasmImageToImageFilter.h"
20+
21+
#include "itkConstantPadImageFilter.h"
22+
#include "itkImageFileReader.h"
23+
#include "itkImageFileWriter.h"
24+
#include "itkTestingMacros.h"
25+
26+
int
27+
itkWasmImageInterfaceWithNegativeIndexTest(int argc, char * argv[])
28+
{
29+
if (argc < 3)
30+
{
31+
std::cerr << "Missing parameters" << std::endl;
32+
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " InputImage OutputImage" << std::endl;
33+
return EXIT_FAILURE;
34+
}
35+
const char * inputImageFile = argv[1];
36+
const char * outputImageFile = argv[2];
37+
38+
constexpr unsigned int Dimension = 3;
39+
using PixelType = unsigned char;
40+
using ImageType = itk::Image<PixelType, Dimension>;
41+
using ImagePointer = ImageType::Pointer;
42+
43+
ImagePointer inputImage = nullptr;
44+
ITK_TRY_EXPECT_NO_EXCEPTION(inputImage = itk::ReadImage<ImageType>(inputImageFile));
45+
std::cout << "inputImage: " << inputImage << std::endl;
46+
47+
ImageType::SizeType lowerExtendRegion;
48+
lowerExtendRegion.Fill(1);
49+
50+
ImageType::SizeType upperExtendRegion;
51+
upperExtendRegion.Fill(1);
52+
53+
using ConstantPadImageFilterType = itk::ConstantPadImageFilter<ImageType,ImageType>;
54+
auto constantPad = ConstantPadImageFilterType::New();
55+
constantPad->SetInput(inputImage);
56+
constantPad->SetPadLowerBound(lowerExtendRegion);
57+
constantPad->SetPadUpperBound(upperExtendRegion);
58+
constantPad->SetConstant(0);
59+
60+
using ImageToWasmImageFilterType = itk::ImageToWasmImageFilter<ImageType>;
61+
auto imageToJSON = ImageToWasmImageFilterType::New();
62+
imageToJSON->SetInput(constantPad->GetOutput());
63+
imageToJSON->Update();
64+
auto imageJSON = imageToJSON->GetOutput();
65+
std::cout << "Image JSON: " << imageJSON->GetJSON() << std::endl;
66+
67+
using WasmImageToImageFilterType = itk::WasmImageToImageFilter<ImageType>;
68+
auto jsonToImage = WasmImageToImageFilterType::New();
69+
jsonToImage->SetInput(imageToJSON->GetOutput());
70+
jsonToImage->Update();
71+
ImageType::Pointer convertedImage = jsonToImage->GetOutput();
72+
73+
std::cout << "convertedImage: " << convertedImage << std::endl;
74+
75+
ITK_TRY_EXPECT_NO_EXCEPTION(itk::WriteImage(convertedImage, outputImageFile));
76+
77+
return EXIT_SUCCESS;
78+
}

0 commit comments

Comments
 (0)