forked from InsightSoftwareConsortium/itk-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkOutputImageIO.h
148 lines (132 loc) · 4.7 KB
/
itkOutputImageIO.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*=========================================================================
*
* 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 itkOutputImageIO_h
#define itkOutputImageIO_h
#include "itkPipeline.h"
#include "itkImageIOBase.h"
#include "itkWASMImageIOBase.h"
#include "itkWASMImageIO.h"
#ifndef ITK_WASM_NO_MEMORY_IO
#include "itkWASMExports.h"
#endif
#ifndef ITK_WASM_NO_FILESYSTEM_IO
#endif
namespace itk
{
namespace wasm
{
/**
*\class OutputImageIO
* \brief Output image for an itk::wasm::Pipeline from an itk::ImageIOBase
*
* This image is written to the filesystem or memory when it goes out of scope.
*
* This class is for the ReadImage itk-wasm pipeline. Most pipelines will use itk::wasm::OutputImage.
*
* \ingroup WebAssemblyInterface
*/
class OutputImageIO
{
public:
void Set(ImageIOBase * imageIO) {
this->m_ImageIO = imageIO;
}
ImageIOBase * Get() const {
return this->m_ImageIO.GetPointer();
}
/** FileName or output index. */
void SetIdentifier(const std::string & identifier)
{
this->m_Identifier = identifier;
}
const std::string & GetIdentifier() const
{
return this->m_Identifier;
}
OutputImageIO() = default;
~OutputImageIO() {
if(wasm::Pipeline::get_use_memory_io())
{
#ifndef ITK_WASM_NO_MEMORY_IO
if (!this->m_ImageIO.IsNull() && !this->m_Identifier.empty())
{
const auto index = std::stoi(this->m_Identifier);
auto wasmImageIOBase = itk::WASMImageIOBase::New();
wasmImageIOBase->SetImageIO(this->m_ImageIO);
setMemoryStoreOutputDataObject(0, index, wasmImageIOBase);
const auto dataAddress = reinterpret_cast< size_t >( &(wasmImageIOBase->GetPixelDataContainer()->at(0)) );
const auto dataSize = wasmImageIOBase->GetPixelDataContainer()->size();
setMemoryStoreOutputArray(0, index, 0, dataAddress, dataSize);
const auto directionAddress = reinterpret_cast< size_t >( &(wasmImageIOBase->GetDirectionContainer()->at(0)) );
const auto directionSize = wasmImageIOBase->GetDirectionContainer()->size() * sizeof(double);
setMemoryStoreOutputArray(0, index, 1, directionAddress, directionSize);
}
#else
throw std::logic_error("Memory IO not supported");
#endif
}
else
{
#ifndef ITK_WASM_NO_FILESYSTEM_IO
if (!this->m_ImageIO.IsNull() && !this->m_Identifier.empty())
{
this->m_ImageIO->ReadImageInformation();
auto wasmImageIO = itk::WASMImageIO::New();
const unsigned int dimension = this->m_ImageIO->GetNumberOfDimensions();
wasmImageIO->SetNumberOfDimensions(dimension);
wasmImageIO->SetComponentType(this->m_ImageIO->GetComponentType());
wasmImageIO->SetNumberOfComponents(this->m_ImageIO->GetNumberOfComponents());
wasmImageIO->SetPixelType(this->m_ImageIO->GetPixelType());
for (unsigned int dim = 0; dim < dimension; ++dim)
{
wasmImageIO->SetDirection(dim, this->m_ImageIO->GetDirection(dim));
wasmImageIO->SetOrigin(dim, this->m_ImageIO->GetOrigin(dim));
wasmImageIO->SetSpacing(dim, this->m_ImageIO->GetSpacing(dim));
wasmImageIO->SetDimensions(dim, this->m_ImageIO->GetDimensions(dim));
}
std::vector<char> pixelData(this->m_ImageIO->GetImageSizeInBytes());
itk::ImageIORegion ioRegion( dimension );
for(unsigned int dim = 0; dim < dimension; ++dim)
{
ioRegion.SetSize(dim, this->m_ImageIO->GetDimensions( dim ));
}
this->m_ImageIO->SetIORegion( ioRegion );
this->m_ImageIO->SetUseStreamedReading(false);
this->m_ImageIO->Read(reinterpret_cast< void * >( &(pixelData.at(0)) ));
wasmImageIO->SetFileName(this->m_Identifier);
wasmImageIO->WriteImageInformation();
wasmImageIO->SetIORegion( ioRegion );
wasmImageIO->Write(reinterpret_cast< void * >( &(pixelData.at(0)) ));
}
#else
throw std::logic_error("Filesystem IO not supported");
#endif
}
}
protected:
typename ImageIOBase::Pointer m_ImageIO;
std::string m_Identifier;
};
bool lexical_cast(const std::string &input, OutputImageIO &outputImageIO)
{
outputImageIO.SetIdentifier(input);
return true;
}
} // namespace wasm
} // namespace itk
#endif