|
| 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 itkInputTransform_h |
| 19 | +#define itkInputTransform_h |
| 20 | + |
| 21 | +#include "itkPipeline.h" |
| 22 | + |
| 23 | +#ifndef ITK_WASM_NO_MEMORY_IO |
| 24 | +#include "itkWasmExports.h" |
| 25 | +#include "itkWasmTransform.h" |
| 26 | +#include "itkWasmTransformToTransformFilter.h" |
| 27 | +#endif |
| 28 | +#ifndef ITK_WASM_NO_FILESYSTEM_IO |
| 29 | +#include "itkTransformFileReader.h" |
| 30 | +#endif |
| 31 | + |
| 32 | +namespace itk |
| 33 | +{ |
| 34 | +namespace wasm |
| 35 | +{ |
| 36 | + |
| 37 | +/** |
| 38 | + *\class InputTransform |
| 39 | + * \brief Input transform for an itk::wasm::Pipeline |
| 40 | + * |
| 41 | + * This transform is read from the filesystem or memory when ITK_WASM_PARSE_ARGS is called. |
| 42 | + * |
| 43 | + * Call `Get()` to get the TTransform * to use an input to a pipeline. |
| 44 | + * |
| 45 | + * \ingroup WebAssemblyInterface |
| 46 | + */ |
| 47 | +template <typename TTransform> |
| 48 | +class ITK_TEMPLATE_EXPORT InputTransform |
| 49 | +{ |
| 50 | +public: |
| 51 | + using TransformType = TTransform; |
| 52 | + |
| 53 | + void Set(const TransformType * transform) { |
| 54 | + this->m_Transform = transform; |
| 55 | + } |
| 56 | + |
| 57 | + const TransformType * Get() const { |
| 58 | + return this->m_Transform.GetPointer(); |
| 59 | + } |
| 60 | + |
| 61 | + InputTransform() = default; |
| 62 | + ~InputTransform() = default; |
| 63 | +protected: |
| 64 | + typename TTransform::ConstPointer m_Transform; |
| 65 | +}; |
| 66 | + |
| 67 | + |
| 68 | +template <typename TTransform> |
| 69 | +bool lexical_cast(const std::string &input, InputTransform<TTransform> &inputTransform) |
| 70 | +{ |
| 71 | + if (input.empty()) |
| 72 | + { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + if (wasm::Pipeline::get_use_memory_io()) |
| 77 | + { |
| 78 | +#ifndef ITK_WASM_NO_MEMORY_IO |
| 79 | + using WasmTransformToTransformFilterType = WasmTransformToTransformFilter<TTransform>; |
| 80 | + auto wasmTransformToTransformFilter = WasmTransformToTransformFilterType::New(); |
| 81 | + auto wasmTransform = WasmTransformToTransformFilterType::WasmTransformType::New(); |
| 82 | + const unsigned int index = std::stoi(input); |
| 83 | + auto json = getMemoryStoreInputJSON(0, index); |
| 84 | + wasmTransform->SetJSON(json); |
| 85 | + wasmTransformToTransformFilter->SetInput(wasmTransform); |
| 86 | + wasmTransformToTransformFilter->Update(); |
| 87 | + inputTransform.Set(wasmTransformToTransformFilter->GetOutput()); |
| 88 | +#else |
| 89 | + return false; |
| 90 | +#endif |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | +#ifndef ITK_WASM_NO_FILESYSTEM_IO |
| 95 | + using ParametersValueType = typename TTransform::ParametersValueType; |
| 96 | + using ReaderType = itk::TransformFileReaderTemplate<ParametersValueType>; |
| 97 | + auto reader = ReaderType::New(); |
| 98 | + reader->SetFileName(input); |
| 99 | + reader->Update(); |
| 100 | + auto transformList = reader->GetTransformList(); |
| 101 | + if (transformList->empty()) |
| 102 | + { |
| 103 | + return false; |
| 104 | + } |
| 105 | + auto transform = dynamic_cast<const TTransform *>(transformList->front().GetPointer()); |
| 106 | + if (!transform) |
| 107 | + { |
| 108 | + return false; |
| 109 | + } |
| 110 | + inputTransform.Set(transform); |
| 111 | +#else |
| 112 | + return false; |
| 113 | +#endif |
| 114 | + } |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +} // end namespace wasm |
| 119 | +} // end namespace itk |
| 120 | + |
| 121 | +#endif |
0 commit comments