|
| 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 | +#ifndef itkOutputTransformIO_h |
| 19 | +#define itkOutputTransformIO_h |
| 20 | + |
| 21 | +#include "itkTransformBase.h" |
| 22 | + |
| 23 | +#include "itkPipeline.h" |
| 24 | + |
| 25 | +#include "itkTransformIOBase.h" |
| 26 | +#include "itkWasmTransformIOBase.h" |
| 27 | +#include "itkWasmTransformIO.h" |
| 28 | +#include "itkTransformJSON.h" |
| 29 | +#ifndef ITK_WASM_NO_MEMORY_IO |
| 30 | +#include "itkWasmExports.h" |
| 31 | +#endif |
| 32 | +#ifndef ITK_WASM_NO_FILESYSTEM_IO |
| 33 | +#endif |
| 34 | + |
| 35 | +namespace |
| 36 | +{ |
| 37 | + |
| 38 | +template <typename TParametersValueType> |
| 39 | +void |
| 40 | +addTransformBuffersToMemoryStore(const itk::TransformListJSON & transformListJSON, size_t index) |
| 41 | +{ |
| 42 | + using ParametersValueType = TParametersValueType; |
| 43 | + |
| 44 | + using TransformBaseType = itk::TransformBaseTemplate<ParametersValueType>; |
| 45 | + |
| 46 | + using FixedParametersValueType = typename TransformBaseType::FixedParametersValueType; |
| 47 | + |
| 48 | + unsigned int dataCount = 0; |
| 49 | + // iterate through the transform list and store each transfrom's fixed parameters and parameters |
| 50 | + for (const auto & transformJSON: transformListJSON) |
| 51 | + { |
| 52 | + if (transformJSON.transformType.transformParameterization == itk::JSONTransformParameterizationEnum::Composite) |
| 53 | + { |
| 54 | + continue; |
| 55 | + } |
| 56 | + const auto fixedParamsAddress = static_cast< size_t >( std::strtoull(transformJSON.fixedParameters.substr(35).c_str(), nullptr, 10) ); |
| 57 | + const auto fixedParamsSize = transformJSON.numberOfFixedParameters * sizeof(FixedParametersValueType); |
| 58 | + itk::wasm::setMemoryStoreOutputArray(0, index, dataCount, fixedParamsAddress, fixedParamsSize); |
| 59 | + ++dataCount; |
| 60 | + |
| 61 | + const auto paramsAddress = static_cast< size_t >( std::strtoull(transformJSON.parameters.substr(35).c_str(), nullptr, 10) ); |
| 62 | + const auto paramsSize = transformJSON.numberOfParameters * sizeof(ParametersValueType); |
| 63 | + itk::wasm::setMemoryStoreOutputArray(0, index, dataCount, paramsAddress, paramsSize); |
| 64 | + ++dataCount; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +} // end anonymous namespace |
| 69 | + |
| 70 | +namespace itk |
| 71 | +{ |
| 72 | +namespace wasm |
| 73 | +{ |
| 74 | +/** |
| 75 | + *\class OutputTransformIO |
| 76 | + * \brief Output transform for an itk::wasm::Pipeline from an itk::TransformIOBaseTemplate |
| 77 | + * |
| 78 | + * This transform is written to the filesystem or memory when it goes out of scope. |
| 79 | + * |
| 80 | + * This class is for the ReadTransform ITK-Wasm pipeline. Most pipelines will use itk::wasm::OutputTransform. |
| 81 | + * |
| 82 | + * \ingroup WebAssemblyInterface |
| 83 | + */ |
| 84 | +template <typename TParametersValueType> |
| 85 | +class OutputTransformIO |
| 86 | +{ |
| 87 | +public: |
| 88 | + using ParametersValueType = TParametersValueType; |
| 89 | + using TransformIOBaseType = itk::TransformIOBaseTemplate<ParametersValueType>; |
| 90 | + |
| 91 | + void Set(TransformIOBaseType * transformIO) { |
| 92 | + this->m_TransformIO = transformIO; |
| 93 | + } |
| 94 | + |
| 95 | + TransformIOBaseType * Get() const { |
| 96 | + return this->m_TransformIO.GetPointer(); |
| 97 | + } |
| 98 | + |
| 99 | + /** FileName or output index. */ |
| 100 | + void SetIdentifier(const std::string & identifier) |
| 101 | + { |
| 102 | + this->m_Identifier = identifier; |
| 103 | + } |
| 104 | + const std::string & GetIdentifier() const |
| 105 | + { |
| 106 | + return this->m_Identifier; |
| 107 | + } |
| 108 | + |
| 109 | + OutputTransformIO() = default; |
| 110 | + ~OutputTransformIO() { |
| 111 | + if(wasm::Pipeline::get_use_memory_io()) |
| 112 | + { |
| 113 | +#ifndef ITK_WASM_NO_MEMORY_IO |
| 114 | + if (!this->m_TransformIO.IsNull() && !this->m_Identifier.empty()) |
| 115 | + { |
| 116 | + const auto index = std::stoi(this->m_Identifier); |
| 117 | + auto wasmTransformIOBase = WasmTransformIOBase<ParametersValueType>::New(); |
| 118 | + wasmTransformIOBase->SetTransformIO(this->m_TransformIO); |
| 119 | + setMemoryStoreOutputDataObject(0, index, wasmTransformIOBase); |
| 120 | + |
| 121 | + const auto transformListJSON = wasmTransformIOBase->GetTransformListJSON(); |
| 122 | + |
| 123 | + if (transformListJSON.size() > 0) |
| 124 | + { |
| 125 | + addTransformBuffersToMemoryStore<ParametersValueType>(transformListJSON, index); |
| 126 | + } |
| 127 | + } |
| 128 | +#else |
| 129 | + std::cerr << "Memory IO not supported" << std::endl; |
| 130 | + abort(); |
| 131 | +#endif |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | +#ifndef ITK_WASM_NO_FILESYSTEM_IO |
| 136 | + if (!this->m_TransformIO.IsNull() && !this->m_Identifier.empty()) |
| 137 | + { |
| 138 | + this->m_TransformIO->Read(); |
| 139 | + |
| 140 | + using WasmTransformIOType = itk::WasmTransformIOTemplate<ParametersValueType>; |
| 141 | + auto wasmTransformIO = WasmTransformIOType::New(); |
| 142 | + |
| 143 | + wasmTransformIO->SetTransformList(*(reinterpret_cast<typename WasmTransformIOType::ConstTransformListType *>(&(this->m_TransformIO->GetTransformList())))); |
| 144 | + wasmTransformIO->SetFileName(this->m_Identifier); |
| 145 | + wasmTransformIO->Write(); |
| 146 | + } |
| 147 | +#else |
| 148 | + std::cerr << "Filesystem IO not supported" << std::endl; |
| 149 | + abort(); |
| 150 | +#endif |
| 151 | + } |
| 152 | + } |
| 153 | +protected: |
| 154 | + typename TransformIOBaseType::Pointer m_TransformIO; |
| 155 | + |
| 156 | + std::string m_Identifier; |
| 157 | +}; |
| 158 | + |
| 159 | +template<typename TParametersValueType> |
| 160 | +bool lexical_cast(const std::string &input, OutputTransformIO<TParametersValueType> &outputTransformIO) |
| 161 | +{ |
| 162 | + outputTransformIO.SetIdentifier(input); |
| 163 | + return true; |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace wasm |
| 167 | +} // namespace itk |
| 168 | + |
| 169 | +#endif |
0 commit comments