File: vtkimage.cc

package info (click to toggle)
mia 2.4.7-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,420 kB
  • sloc: cpp: 162,689; python: 1,322; sh: 321; xml: 127; makefile: 42; csh: 24; ansic: 9
file content (546 lines) | stat: -rw-r--r-- 17,066 bytes parent folder | download | duplicates (3)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* -*- mia-c++  -*-
 *
 * This file is part of MIA - a toolbox for medical image analysis
 * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
 *
 * MIA is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <vtk/vtkimage.hh>

#include <vtkDataArray.h>
#include <vtkPointData.h>
#include <vtkBitArray.h>
#include <vtkSignedCharArray.h>
#include <vtkUnsignedCharArray.h>
#include <vtkShortArray.h>
#include <vtkUnsignedShortArray.h>
#include <vtkIntArray.h>
#include <vtkUnsignedIntArray.h>

#ifdef LONG_64BIT
#include <vtkLongArray.h>
#include <vtkUnsignedLongArray.h>
#endif
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>

#include <vtkXMLImageDataReader.h>
#include <vtkXMLImageDataWriter.h>

#include <vtkDataSetWriter.h>
#include <vtkDataSetReader.h>

#include <vtkMetaImageReader.h>
#include <vtkMetaImageWriter.h>

#include <vtkImageData.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>

#include <type_traits>

using namespace mia;
using namespace std;
using namespace vtkimage;

// LONG_64BIT seems to be buggy in vtkDataReader
#ifdef LONG_64BIT
#undef LONG_64BIT
#endif

template <typename T>
struct __vtk_data_array {
       typedef void type;
       typedef false_type supported;
       \
};


#define VTK_ARRAY_TRANSLATE(TYPE, VTK_TYPE, ID)	\
	template <>			    \
	struct __vtk_data_array<TYPE> {	    \
		typedef VTK_TYPE type;	    \
		typedef true_type supported; \
		static const int value = ID; \
	};


#if  VTK_MAJOR_VERSION < 7
VTK_ARRAY_TRANSLATE(bool, vtkBitArray, VTK_BIT);
#endif

VTK_ARRAY_TRANSLATE(int8_t, vtkSignedCharArray, VTK_SIGNED_CHAR);
VTK_ARRAY_TRANSLATE(uint8_t, vtkUnsignedCharArray, VTK_UNSIGNED_CHAR);
VTK_ARRAY_TRANSLATE(int16_t, vtkShortArray, VTK_SHORT);
VTK_ARRAY_TRANSLATE(uint16_t, vtkUnsignedShortArray, VTK_UNSIGNED_SHORT);
VTK_ARRAY_TRANSLATE(int32_t, vtkIntArray, VTK_INT);
VTK_ARRAY_TRANSLATE(uint32_t, vtkUnsignedIntArray, VTK_UNSIGNED_INT);

#ifdef LONG_64BIT
VTK_ARRAY_TRANSLATE(int64_t, vtkLongArray, VTK_LONG);
VTK_ARRAY_TRANSLATE(uint64_t, vtkUnsignedLongArray, VTK_UNSIGNED_LONG);
#endif

VTK_ARRAY_TRANSLATE(float, vtkFloatArray, VTK_FLOAT);
VTK_ARRAY_TRANSLATE(double, vtkDoubleArray, VTK_DOUBLE);


template <typename T>
struct read_image {
       static C3DImage *apply(const C3DBounds& size, void *scalars)
       {
              cvdebug() << "VTK/MetaIO read image of type " <<  __type_descr<T>::value << "\n";
              const T *my_scalars = reinterpret_cast<const T *>(scalars);

              if (!my_scalars)
                     throw create_exception<logic_error>("CVtk3DImageIOPlugin::load: input image scalar type bogus");

              T3DImage<T> *result = new  T3DImage<T>(size);
              copy(my_scalars, my_scalars + result->size(), result->begin());
              return result;
       }
};

#if  VTK_MAJOR_VERSION < 7
template <>
struct read_image<bool> {
       static C3DImage *apply(const C3DBounds& size, void *scalars)
       {
              cvdebug() << "VTK/MetaIO read image of type " <<  __type_descr<bool>::value << "\n";
              const unsigned char *my_scalars = reinterpret_cast<const unsigned char *>(scalars);

              if (!my_scalars)
                     throw create_exception<logic_error>("CVtk3DImageIOPlugin::load: input image scalar type bogus");

              T3DImage<bool> *result = new  T3DImage<bool>(size);
              auto i = result->begin();
              auto e = result->end();

              while (i != e) {
                     unsigned char obyte = *my_scalars++;
                     unsigned char mask = 0x80;

                     for (int pos = 0; pos < 8 && i != e; ++i, ++pos, mask >>= 1) {
                            if (obyte & mask)
                                   *i = true;
                     }
              }

              return result;
       }
};
#endif

static C3DImage *image_vtk_to_mia(vtkImageData *vtk_image, const string& fname)
{
       int dim = vtk_image->GetDataDimension();

       if (dim != 3)
              throw create_exception<invalid_argument>("3D Vtk/MetaImageIO load (", fname,
                            "): Expect 3 dimensions but got ", dim);

       int components = vtk_image->GetNumberOfScalarComponents();

       if (components != 1)
              throw create_exception<invalid_argument>("3D Vtk/MetaImageIO load (", fname,
                            "): only scalar pixel values are allowed, "
                            "but got ", components, " components");

       auto dims = vtk_image->GetDimensions();
       C3DBounds size(dims[0], dims[1], dims[2]);
       auto array = vtk_image->GetScalarPointer();
       C3DImage *result_image = nullptr;

       switch 	 (vtk_image->GetScalarType()) {
#if  VTK_MAJOR_VERSION < 7

       case VTK_BIT:
              result_image = read_image<bool>::apply(size, array);
              break;
#endif

       case VTK_SIGNED_CHAR:
              result_image = read_image<int8_t>::apply(size, array);
              break;

       case VTK_UNSIGNED_CHAR:
              result_image = read_image<uint8_t>::apply(size, array);
              break;

       case VTK_SHORT:
              result_image = read_image<int16_t>::apply(size, array);
              break;

       case VTK_UNSIGNED_SHORT:
              result_image = read_image<uint16_t>::apply(size, array);
              break;

       case VTK_INT:
              result_image = read_image<int32_t>::apply(size, array);
              break;

       case VTK_UNSIGNED_INT:
              result_image = read_image<uint32_t>::apply(size, array);
              break;
#ifdef LONG_64BIT

       case VTK_LONG:
              result_image = read_image<int64_t>::apply(size, array);
              break;

       case VTK_UNSIGNED_LONG:
              result_image = read_image<uint64_t>::apply(size, array);
              break;
#endif

       case VTK_FLOAT:
              result_image = read_image<float>::apply(size, array);
              break;

       case VTK_DOUBLE:
              result_image = read_image<double>::apply(size, array);
              break;

       default:
              throw create_exception<invalid_argument>("3D Vtk/MetaImageIO load (", fname, "): "
                            "data type ", vtk_image->GetScalarTypeAsString(),
                            "(", vtk_image->GetScalarType(), ") not supported");
       }

       double sp[3];
       vtk_image->GetSpacing (sp);
       result_image->set_voxel_size(C3DFVector(sp[0], sp[1], sp[2]));
       vtk_image->GetOrigin (sp);
       result_image->set_origin(C3DFVector(sp[0], sp[1], sp[2]));
       return result_image;
}


template <typename T, typename supported>
struct __dispatch_convert {
       static void  apply(vtkImageData */*output*/, const T3DImage<T>& /*input*/)
       {
              throw create_exception<invalid_argument>("CVtk3DImageIOPlugin: no support saving '",
                            __type_descr<T>::value, "' pixel values");
       }
};

template <typename T>
struct __dispatch_convert<T, true_type> {
       static void  apply (vtkImageData *output, const T3DImage<T>& input)
       {
              cvdebug() << "Input is an image of pixel type " << __type_descr<T>::value << "\n";
#if  VTK_MAJOR_VERSION < 6
              output->SetScalarType(__vtk_data_array<T>::value);
              output->SetNumberOfScalarComponents(1);
              output->AllocateScalars();
#else
              output->AllocateScalars(__vtk_data_array<T>::value, 1);
#endif
              T *out_ptr =  reinterpret_cast<T *>(output->GetScalarPointer());
              copy(input.begin(), input.end(), out_ptr);
       }
};

#if  VTK_MAJOR_VERSION < 7
template <>
struct __dispatch_convert<bool, true_type> {
       static void  apply (vtkImageData *output, const T3DImage<bool>& input)
       {
              cvdebug() << "Input is an image of pixel type bool\n";
#if  VTK_MAJOR_VERSION < 6
              output->SetScalarType(__vtk_data_array<bool>::value);
              output->SetNumberOfScalarComponents(1);
              output->AllocateScalars();
#else
              output->AllocateScalars(__vtk_data_array<bool>::value, 1);
#endif
              unsigned char *out_ptr =  reinterpret_cast<unsigned char *>(output->GetScalarPointer());
              auto i = input.begin();
              auto e = input.end();
              int pos = 0;
              unsigned char mask = 0x80;
              unsigned char obyte = 0;

              while (i != e) {
                     if (*i)
                            obyte |= mask;

                     ++pos;
                     mask >>= 1;

                     if (pos == 8) {
                            *out_ptr++ = obyte;
                            obyte = 0;
                            mask = 0x80;
                            pos = 0;
                     }

                     ++i;
              }

              if ( pos > 0) {
                     *out_ptr = obyte;
              }
       }
};
#endif

class FSetArray: public TFilter<void>
{
public:
       FSetArray(vtkImageData *output): m_output(output) {}

       template <typename T>
       void  operator () (const T3DImage<T>& input) const
       {
              typedef typename __vtk_data_array<T>::supported supported;
              __dispatch_convert<T, supported>::apply(m_output, input);
       }
private:
       vtkImageData *m_output;
};


static vtkSmartPointer<vtkImageData> image_mia_to_vtk(const C3DImage& mia_image)
{
       auto outimage = vtkSmartPointer<vtkImageData>::New();
       auto origin = mia_image.get_origin();
       outimage->SetOrigin(origin.x, origin.y, origin.z);
       auto dx = mia_image.get_voxel_size();
       outimage->SetSpacing(dx.x, dx.y, dx.z);
       outimage->SetDimensions(mia_image.get_size().x, mia_image.get_size().y, mia_image.get_size().z);
       FSetArray set_array(outimage);
       mia::filter(set_array, mia_image);
       return outimage;
}

CVtk3DImageIOPlugin::CVtk3DImageIOPlugin():
       C3DImageIOPlugin("vtk")
{
       // indicate support for all pixel types available (only scalar types are possible)
#if  VTK_MAJOR_VERSION < 7
       add_supported_type(it_bit);
#endif
       add_supported_type(it_sbyte);
       add_supported_type(it_ubyte);
       add_supported_type(it_sshort);
       add_supported_type(it_ushort);
       add_supported_type(it_sint);
       add_supported_type(it_uint);
#ifdef LONG_64BIT
       add_supported_type(it_slong);
       add_supported_type(it_ulong);
#endif
       add_supported_type(it_float);
       add_supported_type(it_double);
       add_suffix(".vtk");
       add_suffix(".VTK");
       add_suffix(".vtkimage");
       add_suffix(".VTKIMAGE");
}

CVtk3DImageIOPlugin::PData CVtk3DImageIOPlugin::do_load(const string&  fname) const
{
       auto reader = vtkSmartPointer<vtkDataSetReader>::New();
       reader->SetFileName(fname.c_str());

       if (!reader->OpenVTKFile())
              return CVtk3DImageIOPlugin::PData();

       reader->Update();
       auto vtk_image = dynamic_cast<vtkImageData *>(reader->GetOutput());

       if (!vtk_image)
              return PData();

       CVtk3DImageIOPlugin::PData result(new Data);
       result->push_back(P3DImage(image_vtk_to_mia(vtk_image, fname)));
       return result;
}





bool CVtk3DImageIOPlugin::do_save(const string& fname, const Data& data) const
{
       if (data.size() != 1)
              throw create_exception<invalid_argument>("CVtk3DImageIOPlugin::save only supports writing one image, "
                            "but you passed in ", data.size(), " images");

       const auto& image = *data[0];
       auto outimage = image_mia_to_vtk(image);
       // add writing of the image
       auto writer = vtkSmartPointer<vtkDataSetWriter>::New();
#if  VTK_MAJOR_VERSION < 6
       writer->SetInput(outimage);
#else
       writer->SetInputData(outimage);
#endif
       writer->SetFileTypeToBinary();
       writer->SetFileName(fname.c_str());
       writer->Write();
       return true;
}

const string CVtk3DImageIOPlugin::do_get_descr() const
{
       return "3D VTK image legacy in- and output (experimental).";
}


CVtkXML3DImageIOPlugin::CVtkXML3DImageIOPlugin():
       C3DImageIOPlugin("vti")
{
       // indicate support for all pixel types available (only scalar types are possible)
       add_supported_type(it_sbyte);
       add_supported_type(it_ubyte);
       add_supported_type(it_sshort);
       add_supported_type(it_ushort);
       add_supported_type(it_sint);
       add_supported_type(it_uint);
#ifdef LONG_64BIT
       add_supported_type(it_slong);
       add_supported_type(it_ulong);
#endif
       add_supported_type(it_float);
       add_supported_type(it_double);
       add_suffix(".vti");
       add_suffix(".VTI");
}

CVtkXML3DImageIOPlugin::PData CVtkXML3DImageIOPlugin::do_load(const string&  fname) const
{
       auto reader = vtkSmartPointer<vtkXMLImageDataReader>::New();
       reader->SetFileName(fname.c_str());
       reader->Update();
       auto vtk_image = reader->GetOutput();

       if (!vtk_image)
              return PData();

       CVtkXML3DImageIOPlugin::PData result(new Data);
       result->push_back(P3DImage(image_vtk_to_mia(vtk_image, fname)));
       return result;
}





bool CVtkXML3DImageIOPlugin::do_save(const string& fname, const Data& data) const
{
       if (data.size() != 1)
              throw create_exception<invalid_argument>("CVtkXML3DImageIOPlugin::save only supports writing one image, "
                            "but you passed in ", data.size(), " images");

       const auto& image = *data[0];
       auto outimage = image_mia_to_vtk(image);
       // add writing of the image
       auto writer = vtkSmartPointer<vtkXMLImageDataWriter>::New();
#if  VTK_MAJOR_VERSION == 5
       writer->SetInput(outimage);
#else
       writer->SetInputData(outimage);
#endif
       writer->SetFileName(fname.c_str());
       writer->Write();
       return true;
}

const string CVtkXML3DImageIOPlugin::do_get_descr() const
{
       return "3D image VTK-XML in- and output (experimental).";
}



CMhd3DImageIOPlugin::CMhd3DImageIOPlugin():
       C3DImageIOPlugin("mhd")
{
       // indicate support for all pixel types available (only scalar types are possible)
       add_supported_type(it_sbyte);
       add_supported_type(it_ubyte);
       add_supported_type(it_sshort);
       add_supported_type(it_ushort);
       add_supported_type(it_sint);
       add_supported_type(it_uint);
#ifdef LONG_64BIT
       add_supported_type(it_slong);
       add_supported_type(it_ulong);
#endif
       add_supported_type(it_float);
       add_supported_type(it_double);
       add_suffix(".mhd");
       add_suffix(".MHD");
       add_suffix(".mha");
       add_suffix(".MHA");
}

const std::string CMhd3DImageIOPlugin::do_get_descr() const
{
       return "MetaIO 3D image IO using the VTK implementation (experimental).";
}

CMhd3DImageIOPlugin::PData CMhd3DImageIOPlugin::do_load(const std::string&  filename) const
{
       auto reader = vtkSmartPointer<vtkMetaImageReader>::New();
       reader->SetFileName(filename.c_str());
       reader->Update();
       auto vtk_image = reader->GetOutput();

       if (!vtk_image)
              return PData();

       CVtkXML3DImageIOPlugin::PData result(new Data);
       result->push_back(P3DImage(image_vtk_to_mia(vtk_image, filename)));
       cvdebug() << "Pixel type set to" << (*result)[0]->get_pixel_type() << "\n";
       return result;
}


bool CMhd3DImageIOPlugin::do_save(const std::string& fname, const Data& data) const
{
       if (data.size() != 1)
              throw create_exception<invalid_argument>("CMhd3DImageIOPlugin::save only supports writing one image, "
                            "but you passed in ", data.size(), " images");

       const auto& image = *data[0];
       auto outimage = image_mia_to_vtk(image);
       // add writing of the image
       auto writer = vtkSmartPointer<vtkMetaImageWriter>::New();
#if  VTK_MAJOR_VERSION < 6
       writer->SetInput(outimage);
#else
       writer->SetInputData(outimage);
#endif
       writer->SetFileName(fname.c_str());
       // seems that compression is broken for (un)signed char.
       writer->SetCompression(false);
       writer->Write();
       return true;
}



extern "C" EXPORT CPluginBase *get_plugin_interface()
{
       auto plugin =  new CVtk3DImageIOPlugin();
       plugin->append_interface(new CVtkXML3DImageIOPlugin);
       plugin->append_interface(new CMhd3DImageIOPlugin);
       return plugin;
}