-
Notifications
You must be signed in to change notification settings - Fork 2
/
slice.cxx
326 lines (272 loc) · 12.1 KB
/
slice.cxx
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
////program to extract a slice
//01: based on template.cxx
//02: based on template_vec.cxx
#include <complex>
#include "itkFilterWatcher.h"
#include <itkImageFileReader.h>
#include <itkExtractImageFilter.h>
#include <itkImageFileWriter.h>
template<typename InputComponentType, typename InputPixelType, size_t CompPerPixel, size_t Dimension>
int DoIt(int argc, char *argv[]){
typedef InputPixelType OutputPixelType;
typedef itk::Image<InputPixelType, Dimension> InputImageType;
typedef itk::Image<OutputPixelType, Dimension-1> OutputImageType;
int CompChunk= atoi(argv[3]);
bool noSDI= CompChunk <= 1; // SDI only if CompChunk > 1
typedef itk::ImageFileReader<InputImageType> ReaderType;
typename ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->ReleaseDataFlagOn();
reader->UpdateOutputInformation();
const typename InputImageType::Pointer& input= reader->GetOutput();
unsigned int axis;
switch(argv[4][0]){ // strcasestr(argv[3],"x"):
case 'x':
case 'X':
axis= 0;
break;
case 'y':
case 'Y':
axis= 1;
break;
case 'z':
case 'Z':
axis= 2;
break;
default:
std::cerr << "Warning: Axis specification not known. Interpreting as numeric specification!" << std::endl;
axis= atoi(argv[4]);
break;
}//switch
typedef itk::ExtractImageFilter<InputImageType,OutputImageType> FilterType;
typename FilterType::Pointer filter= FilterType::New();
filter->SetInput(input);
filter->SetDirectionCollapseToSubmatrix(); // SetDirectionCollapseToIdentity();
typename FilterType::InputImageRegionType slice(input->GetLargestPossibleRegion());
slice.SetSize(axis, 0);
slice.SetIndex(axis, atoi(argv[5]));
std::cerr << "slice region index: " << slice.GetIndex()
<< " size: " << slice.GetSize()
<< std::endl;
filter->SetExtractionRegion(slice);
filter->ReleaseDataFlagOn();
filter->InPlaceOn();
const typename OutputImageType::Pointer& output= filter->GetOutput();
typedef itk::ImageFileWriter<OutputImageType> WriterType;
typename WriterType::Pointer writer = WriterType::New();
FilterWatcher watcherO(writer);
writer->SetFileName(argv[2]);
writer->SetInput(output);
if(noSDI){
writer->SetUseCompression(CompChunk);
}
else{
writer->UseCompressionOff(); // writing compressed is not supported when streaming!
writer->SetNumberOfStreamDivisions(CompChunk);
}
try{
writer->Update();
}
catch(itk::ExceptionObject &ex){
std::cerr << ex << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
template<typename InputComponentType, size_t CompPerPixel, size_t Dimension>
int dispatch_pT(itk::ImageIOBase::IOPixelType pixelType, int argc, char *argv[]){
int res= EXIT_FAILURE;
//http://www.itk.org/Doxygen45/html/classitk_1_1ImageIOBase.html#abd189f096c2a1b3ea559bc3e4849f658
//http://www.itk.org/Doxygen45/html/itkImageIOBase_8h_source.html#l00099
//IOPixelType:: UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR, POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR, DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX
switch (pixelType){
case itk::ImageIOBase::SCALAR:{ // 1 component per pixel
typedef InputComponentType InputPixelType;
res= DoIt<InputComponentType, InputPixelType, CompPerPixel, Dimension>(argc, argv);
} break;
case itk::ImageIOBase::COMPLEX:{ // 2 components per pixel
typedef std::complex<InputComponentType> InputPixelType;
res= DoIt<InputComponentType, InputPixelType, CompPerPixel, Dimension>(argc, argv);
} break;
case itk::ImageIOBase::RGB:{ // 3 components per pixel, limited [0,1]
typedef itk::RGBPixel<InputComponentType> InputPixelType;
res= DoIt<InputComponentType, InputPixelType, CompPerPixel, Dimension>(argc, argv);
} break;
case itk::ImageIOBase::RGBA:{ // 4 components per pixel, limited [0,1]
typedef itk::RGBAPixel<InputComponentType> InputPixelType;
res= DoIt<InputComponentType, InputPixelType, CompPerPixel, Dimension>(argc, argv);
} break;
case itk::ImageIOBase::VECTOR:{
typedef itk::Vector<InputComponentType, CompPerPixel> InputPixelType;
res= DoIt<InputComponentType, InputPixelType, CompPerPixel, Dimension>(argc, argv);
} break;
case itk::ImageIOBase::UNKNOWNPIXELTYPE:
default:
std::cerr << std::endl << "Error: Pixel type not handled!" << std::endl;
break;
}//switch
return res;
}
template<typename InputComponentType, size_t CompPerPixel>
int dispatch_D(itk::ImageIOBase::IOPixelType pixelType, size_t dimensionType, int argc, char *argv[]){
int res= EXIT_FAILURE;
switch (dimensionType){
case 2:
res= dispatch_pT<InputComponentType, CompPerPixel, 2>(pixelType, argc, argv);
break;
case 3:
res= dispatch_pT<InputComponentType, CompPerPixel, 3>(pixelType, argc, argv);
break;
default:
std::cerr << "Error: Images of dimension " << dimensionType << " are not handled!" << std::endl;
break;
}//switch
return res;
}
template<typename InputComponentType>
int dispatch_cPP(size_t compPerPixel, itk::ImageIOBase::IOPixelType pixelType, size_t dimensionType, int argc, char *argv[]){
int res= EXIT_FAILURE;
switch (compPerPixel){
case 1:
res= dispatch_D<InputComponentType, 1>(pixelType, dimensionType, argc, argv);
break;
case 2:
res= dispatch_D<InputComponentType, 2>(pixelType, dimensionType, argc, argv);
break;
case 3:
res= dispatch_D<InputComponentType, 3>(pixelType, dimensionType, argc, argv);
break;
// case 4:
// res= dispatch_D<InputComponentType, 4>(pixelType, dimensionType, argc, argv);
// break;
// case 5:
// res= dispatch_D<InputComponentType, 5>(pixelType, dimensionType, argc, argv);
// break;
default:
std::cerr << "Error: NumberOfComponentsPerPixel (" << compPerPixel << ") not handled!" << std::endl;
break;
}//switch
return res;
}
int dispatch_cT(itk::ImageIOBase::IOComponentType componentType, size_t compPerPixel, itk::ImageIOBase::IOPixelType pixelType, size_t dimensionType, int argc, char *argv[]){
int res= EXIT_FAILURE;
//http://www.itk.org/Doxygen45/html/classitk_1_1ImageIOBase.html#a8dc783055a0af6f0a5a26cb080feb178
//http://www.itk.org/Doxygen45/html/itkImageIOBase_8h_source.html#l00107
//IOComponentType: UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT, ULONG, LONG, FLOAT, DOUBLE
switch (componentType){
case itk::ImageIOBase::UCHAR:{ // uint8_t
typedef unsigned char InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::CHAR:{ // int8_t
typedef char InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::USHORT:{ // uint16_t
typedef unsigned short InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::SHORT:{ // int16_t
typedef short InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::UINT:{ // uint32_t
typedef unsigned int InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::INT:{ // int32_t
typedef int InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::ULONG:{ // uint64_t
typedef unsigned long InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::LONG:{ // int64_t
typedef long InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::FLOAT:{ // float32
typedef float InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::DOUBLE:{ // float64
typedef double InputComponentType;
res= dispatch_cPP<InputComponentType>(compPerPixel, pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
default:
std::cerr << "unknown component type" << std::endl;
break;
}//switch
return res;
}
////from http://itk-users.7.n7.nabble.com/Pad-image-with-0-but-keep-its-type-what-ever-it-is-td27442.html
//namespace itk{
// Description:
// Get the PixelType and ComponentType from fileName
void GetImageType (std::string fileName,
itk::ImageIOBase::IOPixelType &pixelType,
itk::ImageIOBase::IOComponentType &componentType,
size_t &compPerPixel,
size_t &dimensionType
){
typedef itk::VectorImage<char, 1> ImageType; //template initialization parameters need to be given but can be arbitrary here
itk::ImageFileReader<ImageType>::Pointer imageReader= itk::ImageFileReader<ImageType>::New();
imageReader->SetFileName(fileName.c_str());
imageReader->UpdateOutputInformation();
if(!imageReader->GetImageIO()->CanStreamRead())
std::cerr << "Cannot stream the reading of the input. Streaming will be inefficient!" << std::endl;
pixelType = imageReader->GetImageIO()->GetPixelType();
componentType = imageReader->GetImageIO()->GetComponentType();
dimensionType= imageReader->GetImageIO()->GetNumberOfDimensions();
compPerPixel = imageReader->GetOutput()->GetNumberOfComponentsPerPixel(); // needs VectorImage
std::cerr << std::endl << "dimensions: " << dimensionType << std::endl;
std::cerr << "component type: " << imageReader->GetImageIO()->GetComponentTypeAsString(componentType) << std::endl;
std::cerr << "component size: " << imageReader->GetImageIO()->GetComponentSize() << std::endl;
std::cerr << "pixel type (string): " << imageReader->GetImageIO()->GetPixelTypeAsString(imageReader->GetImageIO()->GetPixelType()) << std::endl;
std::cerr << "pixel type: " << pixelType << std::endl << std::endl;
std::cerr << "NumberOfComponentsPerPixel: " << compPerPixel << std::endl;
}
int main(int argc, char *argv[]){
if ( argc != 6 ){
std::cerr << "Missing Parameters: "
<< argv[0]
<< " Input_Image"
<< " Output_Image"
<< " compress|stream-chunks"
<< " axis slice#"
<< std::endl;
std::cerr << std::endl;
std::cerr << " no-compress: 0, compress: 1, stream > 1" << std::endl;
return EXIT_FAILURE;
}
int CompChunk= atoi(argv[3]);
std::cerr << std::endl;
if(CompChunk == 0){
std::cerr << "Employing no compression and no streaming." << std::endl;
}
else if (CompChunk == 1){
std::cerr << "Employing compression (streaming not possible then)." << std::endl;
}
else if (CompChunk > 1){
std::cerr << "Employing streaming (compression not possible then)." << std::endl;
}
else {
std::cerr << "compress|stream-chunks must be a positive integer" << std::endl;
return EXIT_FAILURE;
}
itk::ImageIOBase::IOPixelType pixelType;
typename itk::ImageIOBase::IOComponentType componentType;
size_t compPerPixel;
size_t dimensionType;
try {
GetImageType(argv[1], pixelType, componentType, compPerPixel, dimensionType);
}//try
catch( itk::ExceptionObject &excep){
std::cerr << argv[0] << ": exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
return dispatch_cT(componentType, compPerPixel, pixelType, dimensionType, argc, argv);
}