-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: feat: itk::WasmTransformIO initial addition
- Loading branch information
Showing
14 changed files
with
1,384 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*========================================================================= | ||
* | ||
* 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 itkWasmIOCommon_h | ||
#define itkWasmIOCommon_h | ||
// Common functions used across the WebAssembly IO classes | ||
|
||
#include "WebAssemblyInterfaceExport.h" | ||
|
||
#include "itkIntTypes.h" | ||
#include "itkMath.h" | ||
#include "itkCommonEnums.h" | ||
|
||
#include <fstream> | ||
|
||
#include "cbor.h" | ||
|
||
namespace itk | ||
{ | ||
|
||
/** \brief Opens a file for reading and random access | ||
* | ||
* \param[out] inputStream is an istream presumed to be opened for reading | ||
* \param[in] filename is the name of the file | ||
* \param[in] ascii optional (default is false); | ||
* if true than the file will be opened in ASCII mode, | ||
* which generally only applies to Windows | ||
* | ||
* The stream is closed if it's already opened. If an error is | ||
* encountered than an exception will be thrown. | ||
*/ | ||
WebAssemblyInterface_EXPORT | ||
void | ||
openFileForReading(std::ifstream & inputStream, const std::string & filename, | ||
bool ascii = false); | ||
|
||
/** \brief Opens a file for writing and random access | ||
* | ||
* \param[out] outputStream is an ostream presumed to be opened for writing | ||
* \param[in] filename is the name of the file | ||
* \param[in] truncate optional (default is true); | ||
* if true than the file's existing content is truncated, | ||
* if false than the file is opened for reading and | ||
* writing with existing content intact | ||
* \param[in] ascii optional (default is false); | ||
* if true than the file will be opened in ASCII mode, | ||
* which generally only applies to Windows | ||
* | ||
* The stream is closed if it's already opened. If an error is | ||
* encountered than an exception will be thrown. | ||
*/ | ||
WebAssemblyInterface_EXPORT | ||
void | ||
openFileForWriting(std::ofstream & outputStream, const std::string & filename, | ||
bool truncate = true, bool ascii = false); | ||
|
||
/** Convenient method to read a buffer as binary. Return true on success. */ | ||
WebAssemblyInterface_EXPORT | ||
bool | ||
readBufferAsBinary(std::istream & os, void *buffer, SizeValueType numberOfBytesToBeRead); | ||
|
||
WebAssemblyInterface_EXPORT | ||
bool fileNameIsCBOR(const char * fileName); | ||
|
||
WebAssemblyInterface_EXPORT | ||
void | ||
readCBORBuffer(cbor_item_t * index, const char * dataName, void * buffer, SizeValueType numberOfBytesToBeRead); | ||
|
||
WebAssemblyInterface_EXPORT | ||
void | ||
writeCBORBuffer(cbor_item_t * index, const char * dataName, void * buffer, SizeValueType numberOfBytesToWrite, IOComponentEnum ioComponent); | ||
|
||
WebAssemblyInterface_EXPORT | ||
size_t | ||
ITKComponentSize( const CommonEnums::IOComponent ); | ||
|
||
} // end namespace itk | ||
|
||
#endif // itkWasmIOCommon_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/*========================================================================= | ||
* | ||
* 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 itkWasmTransformIO_h | ||
#define itkWasmTransformIO_h | ||
#include "WebAssemblyInterfaceExport.h" | ||
|
||
#include "itkTransformIOBase.h" | ||
#include "itkMacro.h" | ||
#include <fstream> | ||
|
||
#include "rapidjson/document.h" | ||
#include "cbor.h" | ||
|
||
namespace itk | ||
{ | ||
/** \class WasmTransformIOTemplate | ||
* | ||
* \brief Read and write the an itk::Transform in a format for interfacing in WebAssembly (Wasm). | ||
* | ||
* This format is intended to facilitate data exchange in itk-wasm. | ||
* It reads and writes an itk-wasm Transform object where TypedArrays are | ||
* replaced by binary files on the filesystem or in a CBOR file. | ||
* | ||
* The file extensions used are .iwt and .iwt.cbor. | ||
* | ||
* \ingroup IOFilters | ||
* \ingroup WebAssemblyInterface | ||
*/ | ||
template <typename TParametersValueType> | ||
class ITK_TEMPLATE_EXPORT WasmTransformIOTemplate: public TransformIOBaseTemplate<TParametersValueType> | ||
{ | ||
public: | ||
using Self = WasmTransformIOTemplate; | ||
using Superclass = TransformIOBaseTemplate<TParametersValueType>; | ||
using Pointer = SmartPointer<Self>; | ||
using typename Superclass::TransformType; | ||
using typename Superclass::TransformPointer; | ||
using typename Superclass::TransformListType; | ||
using ParametersType = typename TransformType::ParametersType; | ||
using ParametersValueType = typename TransformType::ParametersValueType; | ||
using FixedParametersType = typename TransformType::FixedParametersType; | ||
using FixedParametersValueType = typename TransformType::FixedParametersValueType; | ||
|
||
using ConstTransformListType = typename TransformIOBaseTemplate<ParametersValueType>::ConstTransformListType; | ||
|
||
/** Method for creation through the object factory. */ | ||
itkNewMacro(Self); | ||
|
||
/** Run-time type information (and related methods). */ | ||
itkTypeMacro(WasmTransformIOTemplate, TransformIOBaseTemplate); | ||
|
||
/** Reads the data from disk into the memory buffer provided. */ | ||
void | ||
Read() override; | ||
|
||
bool CanReadFile(const char *) override; | ||
|
||
/** Set the JSON representation of the image information. */ | ||
void SetJSON(rapidjson::Document & json); | ||
|
||
/*-------- This part of the interfaces deals with writing data ----- */ | ||
|
||
bool CanWriteFile(const char *) override; | ||
|
||
void Write() override; | ||
|
||
#if !defined(ITK_WRAPPING_PARSER) | ||
/** Get the JSON representation of the mesh information. */ | ||
rapidjson::Document GetJSON(); | ||
#endif | ||
|
||
protected: | ||
WasmTransformIOTemplate(); | ||
~WasmTransformIOTemplate() override; | ||
void PrintSelf(std::ostream & os, Indent indent) const override; | ||
|
||
void ReadTransformInformation(); | ||
void ReadFixedParameters(void *buffer); | ||
void ReadParameters(void *buffer); | ||
|
||
void WriteTransformInformation(); | ||
void WriteFixedParameters(void *buffer); | ||
void WriteParameters(void *buffer); | ||
|
||
/** Reads in the mesh information and populates the related buffers. */ | ||
void ReadCBOR(void * buffer = nullptr, unsigned char * cborBuffer = nullptr, size_t cborBufferLength = 0); | ||
/** Writes the buffers into the CBOR item and the buffer out to disk. */ | ||
void WriteCBOR(); | ||
|
||
cbor_item_t * m_CBORRoot{ nullptr }; | ||
|
||
private: | ||
ITK_DISALLOW_COPY_AND_ASSIGN(WasmTransformIOTemplate); | ||
}; | ||
} // end namespace itk | ||
|
||
#endif // itkWasmTransformIO_h | ||
|
||
/** Explicit instantiations */ | ||
#ifndef ITK_TEMPLATE_EXPLICIT_WasmTransformIO | ||
// Explicit instantiation is required to ensure correct dynamic_cast | ||
// behavior across shared libraries. | ||
// | ||
// IMPORTANT: Since within the same compilation unit, | ||
// ITK_TEMPLATE_EXPLICIT_<classname> defined and undefined states | ||
// need to be considered. This code *MUST* be *OUTSIDE* the header | ||
// guards. | ||
// | ||
#if defined(WebAssemblyInterface_EXPORTS) | ||
// We are building this library | ||
# define WebAssemblyInterface_EXPORT_EXPLICIT ITK_FORWARD_EXPORT | ||
#else | ||
// We are using this library | ||
# define WebAssemblyInterface_EXPORT_EXPLICIT WebAssemblyInterface_EXPORT | ||
#endif | ||
namespace itk | ||
{ | ||
ITK_GCC_PRAGMA_DIAG_PUSH() | ||
ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes") | ||
|
||
extern template class WebAssemblyInterface_EXPORT_EXPLICIT WasmTransformIOTemplate<double>; | ||
extern template class WebAssemblyInterface_EXPORT_EXPLICIT WasmTransformIOTemplate<float>; | ||
|
||
ITK_GCC_PRAGMA_DIAG_POP() | ||
|
||
} // end namespace itk | ||
#undef WebAssemblyInterface_EXPORT_EXPLICIT | ||
#endif |
Oops, something went wrong.