From fa735c7355757e465d09630b38acfbe6ef8227af Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Mon, 15 Apr 2024 16:35:58 -0400 Subject: [PATCH] feat: add itk::WasmTransformIOFactory --- include/itkWasmTransformIOFactory.h | 70 +++++++++++++++++++++++++ itk-module.cmake | 2 + src/CMakeLists.txt | 1 + src/itkWasmTransformIOFactory.cxx | 71 ++++++++++++++++++++++++++ test/Input/LinearTransform.h5.cid | 1 + test/itkWasmTransformIOTest.cxx | 79 +++++++++++++++++++++++++++++ wrapping/itkWasmTransformIO.wrap | 2 + 7 files changed, 226 insertions(+) create mode 100644 include/itkWasmTransformIOFactory.h create mode 100644 src/itkWasmTransformIOFactory.cxx create mode 100644 test/Input/LinearTransform.h5.cid create mode 100644 test/itkWasmTransformIOTest.cxx create mode 100644 wrapping/itkWasmTransformIO.wrap diff --git a/include/itkWasmTransformIOFactory.h b/include/itkWasmTransformIOFactory.h new file mode 100644 index 000000000..bef0ecf3e --- /dev/null +++ b/include/itkWasmTransformIOFactory.h @@ -0,0 +1,70 @@ +/*========================================================================= + * + * 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. + * + *=========================================================================*/ +#ifndef itkWasmTransformIOFactory_h +#define itkWasmTransformIOFactory_h +#include "WebAssemblyInterfaceExport.h" + +#include "itkObjectFactoryBase.h" +#include "itkTransformIOBase.h" + +namespace itk +{ +/** \class WasmTransformIOFactory + * + * \brief Create instances of WasmTransformIO objects using an object factory. + * + * \ingroup WebAssemblyInterface + */ +class WebAssemblyInterface_EXPORT WasmTransformIOFactory: public ObjectFactoryBase +{ +public: + /** Standard class typedefs. */ + typedef WasmTransformIOFactory Self; + typedef ObjectFactoryBase Superclass; + typedef SmartPointer< Self > Pointer; + typedef SmartPointer< const Self > ConstPointer; + + /** Class methods used to interface with the registered factories. */ + const char * GetITKSourceVersion() const override; + + const char * GetDescription() const override; + + /** Method for class instantiation. */ + itkFactorylessNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(WasmTransformIOFactory, ObjectFactoryBase); + + /** Register one factory of this type */ + static void RegisterOneFactory() + { + WasmTransformIOFactory::Pointer wasmFactory = WasmTransformIOFactory::New(); + + ObjectFactoryBase::RegisterFactoryInternal(wasmFactory); + } + +protected: + WasmTransformIOFactory(); + ~WasmTransformIOFactory() override; + +private: + ITK_DISALLOW_COPY_AND_ASSIGN(WasmTransformIOFactory); +}; +} // end namespace itk + +#endif diff --git a/itk-module.cmake b/itk-module.cmake index 5d1597191..9d636d4c7 100644 --- a/itk-module.cmake +++ b/itk-module.cmake @@ -9,6 +9,7 @@ itk_module(WebAssemblyInterface ITKCommon ITKIOImageBase ITKIOMeshBase + ITKIOTransformBase COMPILE_DEPENDS MeshToPolyData ITKImageFunction @@ -19,6 +20,7 @@ itk_module(WebAssemblyInterface FACTORY_NAMES ImageIO::Wasm MeshIO::Wasm + TransformIO::Wasm EXCLUDE_FROM_DEFAULT DESCRIPTION "${DOCUMENTATION}" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3f478b163..44ee9bfc5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,6 +11,7 @@ set(WebAssemblyInterface_SRCS itkWasmMeshIOBase.cxx itkWasmMeshIOFactory.cxx itkWasmMeshIO.cxx + itkWasmTransformIOFactory.cxx itkWasmStringStream.cxx itkInputTextStream.cxx itkOutputTextStream.cxx diff --git a/src/itkWasmTransformIOFactory.cxx b/src/itkWasmTransformIOFactory.cxx new file mode 100644 index 000000000..ff95f8c40 --- /dev/null +++ b/src/itkWasmTransformIOFactory.cxx @@ -0,0 +1,71 @@ +/*========================================================================= + * + * 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 "itkWasmTransformIOFactory.h" +#include "itkWasmTransformIO.h" +#include "itkVersion.h" + +namespace itk +{ + +WasmTransformIOFactory +::WasmTransformIOFactory() +{ + this->RegisterOverride( "itkTransformIOBase", + "itkWasmTransformIO", + "Wasm Transform IO", + 1, + CreateObjectFunction< WasmTransformIO >::New() ); +} + + +WasmTransformIOFactory +::~WasmTransformIOFactory() +{} + + +const char * +WasmTransformIOFactory +::GetITKSourceVersion() const +{ + return ITK_SOURCE_VERSION; +} + + +const char * +WasmTransformIOFactory +::GetDescription() const +{ + return "Wasm TransformIO Factory, allows the loading of Wasm images into Insight"; +} + + +// Undocumented API used to register during static initialization. +// DO NOT CALL DIRECTLY. + +static bool WasmTransformIOFactoryHasBeenRegistered; + +void WebAssemblyInterface_EXPORT WasmTransformIOFactoryRegister__Private(void) +{ + if( ! WasmTransformIOFactoryHasBeenRegistered ) + { + WasmTransformIOFactoryHasBeenRegistered = true; + WasmTransformIOFactory::RegisterOneFactory(); + } +} + +} // end namespace itk diff --git a/test/Input/LinearTransform.h5.cid b/test/Input/LinearTransform.h5.cid new file mode 100644 index 000000000..ae2f334c5 --- /dev/null +++ b/test/Input/LinearTransform.h5.cid @@ -0,0 +1 @@ +bafkreicdjbd4wnnbfzh44hdt3gp46brt7v3mbyqm6kspxb4btldmkweojy diff --git a/test/itkWasmTransformIOTest.cxx b/test/itkWasmTransformIOTest.cxx new file mode 100644 index 000000000..cc2cb5c15 --- /dev/null +++ b/test/itkWasmTransformIOTest.cxx @@ -0,0 +1,79 @@ +/*========================================================================= + * + * 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 "itkWasmTransformIOFactory.h" +#include "itkWasmTransformIO.h" +#include "itkTransformFileReader.h" +#include "itkTransformFileWriter.h" +#include "itkTestingMacros.h" + +int +itkWasmTransformIOTest(int argc, char * argv[]) +{ + if (argc < 6) + { + std::cerr << "Missing parameters" << std::endl; + std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " InputTransform TransformDirectory ConvertedDirectory TransformCBOR ConvertedCBOR" << std::endl; + return EXIT_FAILURE; + } + const char * inputTransformFile = argv[1]; + const char * imageDirectory = argv[2]; + const char * convertedDirectoryFile = argv[3]; + const char * imageCBOR = argv[4]; + const char * convertedCBORFile = argv[5]; + + itk::WasmTransformIOFactory::RegisterOneFactory(); + + constexpr unsigned int Dimension = 3; + using PixelType = unsigned char; + using TransformType = itk::Transform; + using TransformPointer = TransformType::Pointer; + + TransformPointer inputTransform = nullptr; + ITK_TRY_EXPECT_NO_EXCEPTION(inputTransform = itk::ReadTransform(inputTransformFile)); + + auto imageIO = itk::WasmTransformIO::New(); + + using WriterType = itk::TransformFileWriter; + auto wasmWriter = WriterType::New(); + //wasmWriter->SetTransformIO( imageIO ); + wasmWriter->SetFileName( imageDirectory ); + wasmWriter->SetInput( inputTransform ); + + ITK_TRY_EXPECT_NO_EXCEPTION(wasmWriter->Update()); + + using ReaderType = itk::TransformFileReader; + auto wasmReader = ReaderType::New(); + //wasmReader->SetTransformIO( imageIO ); + wasmReader->SetFileName( imageDirectory ); + + ITK_TRY_EXPECT_NO_EXCEPTION(wasmReader->Update()); + + TransformPointer writtenReadTransform = wasmReader->GetOutput(); + + ITK_TRY_EXPECT_NO_EXCEPTION(itk::WriteTransform(writtenReadTransform, convertedDirectoryFile)); + + wasmWriter->SetFileName( imageCBOR ); + ITK_TRY_EXPECT_NO_EXCEPTION(wasmWriter->Update()); + + wasmReader->SetFileName( imageCBOR ); + ITK_TRY_EXPECT_NO_EXCEPTION(wasmReader->Update()); + + ITK_TRY_EXPECT_NO_EXCEPTION(itk::WriteTransform(wasmReader->GetOutput(), convertedCBORFile)); + + return EXIT_SUCCESS; +} diff --git a/wrapping/itkWasmTransformIO.wrap b/wrapping/itkWasmTransformIO.wrap new file mode 100644 index 000000000..ff6516b15 --- /dev/null +++ b/wrapping/itkWasmTransformIO.wrap @@ -0,0 +1,2 @@ +itk_wrap_simple_class("itk::WasmTransformIO" POINTER) +itk_wrap_simple_class("itk::WasmTransformIOFactory" POINTER)