ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkOutputImage.h
Go to the documentation of this file.
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 itkOutputImage_h
19#define itkOutputImage_h
20
21#include "itkPipeline.h"
22
23#ifndef ITK_WASM_NO_MEMORY_IO
24# include "itkWasmExports.h"
25# include "itkWasmImage.h"
27#endif
28#ifndef ITK_WASM_NO_FILESYSTEM_IO
29# include "itkImageFileWriter.h"
30#endif
31
32namespace itk
33{
34namespace wasm
35{
46template <typename TImage>
47class ITK_TEMPLATE_EXPORT OutputImage
48{
49public:
50 using ImageType = TImage;
51
52 void
53 Set(const ImageType * image)
54 {
55 this->m_Image = image;
56 }
57
58 const ImageType *
59 Get() const
60 {
61 return this->m_Image.GetPointer();
62 }
63
65 void
66 SetIdentifier(const std::string & identifier)
67 {
68 this->m_Identifier = identifier;
69 }
70 const std::string &
72 {
73 return this->m_Identifier;
74 }
75
76 OutputImage() = default;
78 {
80 {
81#ifndef ITK_WASM_NO_MEMORY_IO
82 if (!this->m_Image.IsNull() && !this->m_Identifier.empty())
83 {
84 using ImageToWasmImageFilterType = ImageToWasmImageFilter<ImageType>;
85 auto imageToWasmImageFilter = ImageToWasmImageFilterType::New();
86 imageToWasmImageFilter->SetInput(this->m_Image);
87 imageToWasmImageFilter->Update();
88 auto wasmImage = imageToWasmImageFilter->GetOutput();
89 const auto index = std::stoi(this->m_Identifier);
90 setMemoryStoreOutputDataObject(0, index, wasmImage);
91
92 const auto dataAddress = reinterpret_cast<size_t>(wasmImage->GetImage()->GetBufferPointer());
94 const auto dataSize = wasmImage->GetImage()->GetBufferedRegion().GetNumberOfPixels() *
95 sizeof(typename ConvertPixelTraits::ComponentType) *
96 wasmImage->GetImage()->GetNumberOfComponentsPerPixel();
97 setMemoryStoreOutputArray(0, index, 0, dataAddress, dataSize);
98
99 const auto directionAddress =
100 reinterpret_cast<size_t>(wasmImage->GetImage()->GetDirection().GetVnlMatrix().begin());
101 const auto directionSize = wasmImage->GetImage()->GetDirection().GetVnlMatrix().size() * sizeof(double);
102 setMemoryStoreOutputArray(0, index, 1, directionAddress, directionSize);
103 }
104#else
105 std::cerr << "Memory IO not supported" << std::endl;
106 abort();
107#endif
108 }
109 else
110 {
111#ifndef ITK_WASM_NO_FILESYSTEM_IO
112 if (!this->m_Image.IsNull() && !this->m_Identifier.empty())
113 {
115 }
116#else
117 std::cerr << "Filesystem IO not supported" << std::endl;
118 abort();
119#endif
120 }
121 }
122
123protected:
124 typename TImage::ConstPointer m_Image;
125
126 std::string m_Identifier;
127};
128
129template <typename TImage>
130bool
131lexical_cast(const std::string & input, OutputImage<TImage> & outputImage)
132{
133 outputImage.SetIdentifier(input);
134 return true;
135}
136
137} // namespace wasm
138} // namespace itk
139
140#endif
Traits class used to by ConvertPixels to convert blocks of pixels.
Convert an Image to an WasmImage object.
Output image for an itk::wasm::Pipeline.
const std::string & GetIdentifier() const
void Set(const ImageType *image)
const ImageType * Get() const
void SetIdentifier(const std::string &identifier)
TImage::ConstPointer m_Image
static auto get_use_memory_io()
WebAssemblyInterface_EXPORT void setMemoryStoreOutputDataObject(uint32_t memoryIndex, uint32_t index, const WasmDataObject *dataObject)
WebAssemblyInterface_EXPORT void setMemoryStoreOutputArray(uint32_t memoryIndex, uint32_t index, uint32_t subIndex, vcl_size_t address, vcl_size_t size)
bool lexical_cast(const std::string &input, InputImage< TImage > &inputImage)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)