Skip to content

Commit

Permalink
feat: add itk::WasmTransformIOFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Apr 19, 2024
1 parent 930c87c commit fa735c7
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
70 changes: 70 additions & 0 deletions include/itkWasmTransformIOFactory.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions itk-module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ itk_module(WebAssemblyInterface
ITKCommon
ITKIOImageBase
ITKIOMeshBase
ITKIOTransformBase
COMPILE_DEPENDS
MeshToPolyData
ITKImageFunction
Expand All @@ -19,6 +20,7 @@ itk_module(WebAssemblyInterface
FACTORY_NAMES
ImageIO::Wasm
MeshIO::Wasm
TransformIO::Wasm
EXCLUDE_FROM_DEFAULT
DESCRIPTION
"${DOCUMENTATION}"
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(WebAssemblyInterface_SRCS
itkWasmMeshIOBase.cxx
itkWasmMeshIOFactory.cxx
itkWasmMeshIO.cxx
itkWasmTransformIOFactory.cxx
itkWasmStringStream.cxx
itkInputTextStream.cxx
itkOutputTextStream.cxx
Expand Down
71 changes: 71 additions & 0 deletions src/itkWasmTransformIOFactory.cxx
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/Input/LinearTransform.h5.cid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bafkreicdjbd4wnnbfzh44hdt3gp46brt7v3mbyqm6kspxb4btldmkweojy
79 changes: 79 additions & 0 deletions test/itkWasmTransformIOTest.cxx
Original file line number Diff line number Diff line change
@@ -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<PixelType, Dimension>;
using TransformPointer = TransformType::Pointer;

TransformPointer inputTransform = nullptr;
ITK_TRY_EXPECT_NO_EXCEPTION(inputTransform = itk::ReadTransform<TransformType>(inputTransformFile));

auto imageIO = itk::WasmTransformIO::New();

using WriterType = itk::TransformFileWriter<TransformType>;
auto wasmWriter = WriterType::New();
//wasmWriter->SetTransformIO( imageIO );
wasmWriter->SetFileName( imageDirectory );
wasmWriter->SetInput( inputTransform );

ITK_TRY_EXPECT_NO_EXCEPTION(wasmWriter->Update());

using ReaderType = itk::TransformFileReader<TransformType>;
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;
}
2 changes: 2 additions & 0 deletions wrapping/itkWasmTransformIO.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
itk_wrap_simple_class("itk::WasmTransformIO" POINTER)
itk_wrap_simple_class("itk::WasmTransformIOFactory" POINTER)

0 comments on commit fa735c7

Please sign in to comment.