Skip to content

Commit

Permalink
COMP: Do not use file IO with Wasm
Browse files Browse the repository at this point in the history
Avoid Wasm binary size issues and file access issues.
  • Loading branch information
thewtex committed Oct 1, 2023
1 parent 7244b04 commit bc1829f
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 18 deletions.
2 changes: 0 additions & 2 deletions Common/ImageSamplers/itkImageToVectorContainerFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#ifndef itkImageToVectorContainerFilter_hxx
#define itkImageToVectorContainerFilter_hxx

#include "itkImageToVectorContainerFilter.h"

#include "itkMath.h"

namespace itk
Expand Down
16 changes: 16 additions & 0 deletions Common/Transforms/elxTransformIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@

#include <itkTransformBase.h>
#include <itkTransformFactoryBase.h>
#ifndef __wasm32__
#include <itkTransformFileReader.h>
#include <itkTransformFileWriter.h>
#endif

#include <string>

Expand Down Expand Up @@ -191,6 +193,11 @@ elastix::TransformIO::ConvertItkTransformBaseToSingleItkTransform(const itk::Tra
void
elastix::TransformIO::Write(const itk::Object & itkTransform, const std::string & fileName)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
throw std::runtime_error(message);
#else
try
{
const auto writer = itk::TransformFileWriter::New();
Expand All @@ -203,12 +210,20 @@ elastix::TransformIO::Write(const itk::Object & itkTransform, const std::string
{
log::error(std::ostringstream{} << "Error trying to write " << fileName << ":\n" << stdException.what());
}
#endif
}


itk::SmartPointer<itk::TransformBase>
elastix::TransformIO::Read(const std::string & fileName)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
throw std::runtime_error(message);
return nullptr;
#else

const auto reader = itk::TransformFileReader::New();

reader->SetFileName(fileName);
Expand All @@ -221,6 +236,7 @@ elastix::TransformIO::Read(const std::string & fileName)
assert(transformList->size() <= 1);

return transformList->empty() ? nullptr : transformList->front();
#endif
}


Expand Down
6 changes: 5 additions & 1 deletion Common/itkMeshFileReaderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

#include "itkMeshSource.h"
#include "itkMacro.h"
#include "itkMeshFileReader.h" // for MeshFileReaderException
#ifndef __wasm32__
#include "itkMeshFileReaderException.h"
#else
#define MeshFileReaderException ExceptionObject
#endif

namespace itk
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#include "elxIncludes.h"
#include "itkMissingStructurePenalty.h"

#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"

namespace elastix
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#include "elxMissingStructurePenalty.h"
#include "itkTimeProbe.h"

#ifndef __wasm32__
#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"
#endif

namespace elastix
{

Expand Down Expand Up @@ -145,6 +150,11 @@ MissingStructurePenalty<TElastix>::BeforeRegistration()
fmeshArgument << ch << metricNumber;
std::string fixedMeshName = this->GetConfiguration()->GetCommandLineArgument(fmeshArgument.str());
typename MeshType::Pointer fixedMesh; // default-constructed (null)
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
#else
if (itksys::SystemTools::GetFilenameLastExtension(fixedMeshName) == ".txt")
{
this->ReadTransformixPoints(fixedMeshName, fixedMesh);
Expand All @@ -153,6 +163,7 @@ MissingStructurePenalty<TElastix>::BeforeRegistration()
{
this->ReadMesh(fixedMeshName, fixedMesh);
}
#endif

meshPointerContainer->SetElement(meshNumber, fixedMesh.GetPointer());
}
Expand Down Expand Up @@ -281,6 +292,12 @@ template <class TElastix>
unsigned int
MissingStructurePenalty<TElastix>::ReadMesh(const std::string & meshFileName, typename FixedMeshType::Pointer & mesh)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
return 0;
#else
/** Read the input mesh. */
auto meshReader = itk::MeshFileReader<MeshType>::New();
meshReader->SetFileName(meshFileName);
Expand All @@ -300,6 +317,7 @@ MissingStructurePenalty<TElastix>::ReadMesh(const std::string & meshFileName, ty
log::info(std::ostringstream{} << " Number of specified input points: " << nrofpoints);

return nrofpoints;
#endif
} // end ReadMesh()


Expand All @@ -311,6 +329,11 @@ template <class TElastix>
void
MissingStructurePenalty<TElastix>::WriteResultMesh(const std::string & filename, MeshIdType meshId)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
#else
/** Setup the pipeline. */

/** Set the points of the latest transformation. */
Expand Down Expand Up @@ -376,7 +399,7 @@ MissingStructurePenalty<TElastix>::WriteResultMesh(const std::string & filename,
// restore celldata as undefined
mappedMesh->SetCellData(nullptr);
}

#endif
} // end WriteResultMesh()


Expand All @@ -389,6 +412,12 @@ unsigned int
MissingStructurePenalty<TElastix>::ReadTransformixPoints(const std::string & filename,
typename MeshType::Pointer & mesh) // const
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
return 0;
#else
/*
FB: Majority of the code is copied from elxTransformBase.hxx: TransformPointsSomePoints()
Function to read 2d structures by reading elastix point files (transformix format) and connecting
Expand Down Expand Up @@ -528,6 +557,7 @@ the sequence of points to form a 2d connected polydata contour.
}
}
return nrofpoints;
#endif
} // end ReadTransformixPoints()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

//#include "elxMetricBase.h"

#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"

namespace elastix
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

#include <typeinfo>

#ifndef __wasm32__
#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"
#endif

namespace elastix
{

Expand Down Expand Up @@ -270,6 +275,12 @@ template <class TElastix>
unsigned int
PolydataDummyPenalty<TElastix>::ReadMesh(const std::string & meshFileName, typename FixedMeshType::Pointer & mesh)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
return 0;
#else
/** Read the input mesh. */
auto meshReader = itk::MeshFileReader<MeshType>::New();
meshReader->SetFileName(meshFileName);
Expand All @@ -291,6 +302,7 @@ PolydataDummyPenalty<TElastix>::ReadMesh(const std::string & meshFileName, typen
log::info(std::ostringstream{} << " Number of specified input points: " << nrofpoints);

return nrofpoints;
#endif
} // end ReadMesh()


Expand All @@ -302,6 +314,11 @@ template <class TElastix>
void
PolydataDummyPenalty<TElastix>::WriteResultMesh(const std::string & filename, MeshIdType meshId)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
#else
/** Set the points of the latest transformation. */
const MappedMeshContainerPointer mappedMeshContainer = this->GetModifiableMappedMeshContainer();
FixedMeshPointer mappedMesh = mappedMeshContainer->ElementAt(meshId);
Expand Down Expand Up @@ -360,7 +377,7 @@ PolydataDummyPenalty<TElastix>::WriteResultMesh(const std::string & filename, Me
{
mappedMesh->SetCellData(nullptr);
}

#endif
} // end WriteResultMesh()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include "itkDefaultStaticMeshTraits.h"
#include "itkTransformMeshFilter.h"
#include <itkMesh.h>
#ifndef __wasm32__
#include <itkMeshFileReader.h>
#endif

#include <fstream>
#include <typeinfo>
Expand Down Expand Up @@ -352,6 +354,12 @@ StatisticalShapePenalty<TElastix>::ReadShape(const std::string &
typename PointSetType::Pointer & pointSet,
const typename ImageType::ConstPointer image)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
return 0;
#else
/** Typedef's. \todo test DummyIPPPixelType=bool. */
using DummyIPPPixelType = double;
using MeshTraitsType =
Expand Down Expand Up @@ -380,7 +388,7 @@ StatisticalShapePenalty<TElastix>::ReadShape(const std::string &
pointSet = PointSetType::New();
pointSet->SetPoints(mesh->GetPoints());
return nrofpoints;

#endif
} // end ReadShape()


Expand Down
11 changes: 10 additions & 1 deletion Core/ComponentBaseClasses/elxFixedImagePyramidBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,30 @@ FixedImagePyramidBase<TElastix>::WritePyramidImage(const std::string & filename,

/** Do the writing. */
log::to_stdout(" Writing fixed pyramid image ...");
#ifndef __wasm32__
try
{
itk::WriteCastedImage(*(this->GetAsITKBaseType()->GetOutput(level)), filename, resultImagePixelType, doCompression);
itk::WriteCastedImage(*(this->GetAsITKBaseType()->GetOutput(level)), filename, resultImagePixelType, doCompression);
}
catch (itk::ExceptionObject & excp)
{
#else
// Always throw -- do not include support code or access filesystem with wasm
itk::ExceptionObject excp;
#endif
/** Add information to the exception. */
excp.SetLocation("FixedImagePyramidBase - BeforeEachResolutionBase()");
std::string err_str = excp.GetDescription();
err_str += "\nError occurred while writing pyramid image.\n";
excp.SetDescription(err_str);

/** Pass the exception to an higher level. */
#ifndef __wasm32__
throw;
}
#else
throw excp;
#endif

} // end WritePyramidImage()

Expand Down
9 changes: 9 additions & 0 deletions Core/ComponentBaseClasses/elxMovingImagePyramidBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,30 @@ MovingImagePyramidBase<TElastix>::WritePyramidImage(const std::string & filename

/** Do the writing. */
log::to_stdout(" Writing moving pyramid image ...");
#ifndef __wasm32__
try
{
itk::WriteCastedImage(*(this->GetAsITKBaseType()->GetOutput(level)), filename, resultImagePixelType, doCompression);
}
catch (itk::ExceptionObject & excp)
{
#else
// Always throw -- do not include support code or access filesystem with wasm
itk::ExceptionObject excp;
#endif
/** Add information to the exception. */
excp.SetLocation("MovingImagePyramidBase - BeforeEachResolutionBase()");
std::string err_str = excp.GetDescription();
err_str += "\nError occurred while writing pyramid image.\n";
excp.SetDescription(err_str);

/** Pass the exception to an higher level. */
#ifndef __wasm32__
throw;
}
#else
throw excp;
#endif

} // end WritePyramidImage()

Expand Down
11 changes: 10 additions & 1 deletion Core/ComponentBaseClasses/elxResamplerBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -395,21 +395,30 @@ ResamplerBase<TElastix>::WriteResultImage(OutputImageType * image,
{
log::to_stdout("\n Writing image ...");
}
#ifndef __wasm32__
try
{
itk::WriteCastedImage(*(infoChanger->GetOutput()), filename, resultImagePixelType, doCompression);
itk::WriteCastedImage(*(infoChanger->GetOutput()), filename, resultImagePixelType, doCompression);
}
catch (itk::ExceptionObject & excp)
{
#else
// Always throw -- do not include support code or access filesystem with wasm
itk::ExceptionObject excp;
#endif
/** Add information to the exception. */
excp.SetLocation("ResamplerBase - AfterRegistrationBase()");
std::string err_str = excp.GetDescription();
err_str += "\nError occurred while writing resampled image.\n";
excp.SetDescription(err_str);

/** Pass the exception to an higher level. */
#ifndef __wasm32__
throw;
}
#else
throw excp;
#endif
} // end WriteResultImage()


Expand Down
Loading

0 comments on commit bc1829f

Please sign in to comment.