SMIL  1.0.4
DVtkIO.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _D_VTK_IO_HPP
31 #define _D_VTK_IO_HPP
32 
33 #include "Core/include/DCore.h"
34 #include "Core/include/private/DSharedImage.hpp"
35 
36 #include <vtkDICOMImageReader.h>
37 #include <vtkImageFlip.h>
38 #include <vtkSmartPointer.h>
39 #include <vtkImageReslice.h>
40 
41 
42 namespace smil
43 {
56  template <class T>
57  RES_T readDICOM(const char *dirName, Image<T> &outIm, bool autoReslice=true)
58  {
59  vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
60  reader->SetDirectoryName(dirName);
61  reader->Update();
62 
63  if (reader->GetOutput()->GetScalarType()!=getVtkType<T>())
64  {
65  ERR_MSG("Wrong image type");
66  cout << "vtkImageData type is " << reader->GetOutput()->GetScalarTypeAsString() << endl;
67  return RES_ERR;
68  }
69 
70  double spc = reader->GetDataSpacing()[0];
71 
72  vtkSmartPointer<vtkImageReslice> reslice = vtkSmartPointer<vtkImageReslice>::New();
73  reslice->SetInputConnection(reader->GetOutputPort());
74  reslice->SetOutputSpacing(spc, spc, spc);
75 
76  vtkImageData *imData;
77 
78  if (autoReslice)
79  {
80  reslice->Update();
81  imData = reslice->GetOutput();
82  }
83  else
84  imData = reader->GetOutput();
85 
86  VtkInt<T> sIm(imData);
87 
88  if (sIm.isAllocated())
89  {
90  outIm.setSize(sIm);
91  copy(sIm, outIm);
92  return RES_OK;
93  }
94  return RES_ERR;
95 
96  }
97 
98  template <>
99  RES_T readDICOM<RGB>(const char */*dirName*/, Image<RGB> &/*outIm*/, bool /*autoReslice*/)
100  {
101  return RES_ERR_NOT_IMPLEMENTED;
102  }
103 
106 } // namespace smil
107 
108 #endif // _D_VTK_IO_HPP
bool isAllocated() const
Check if the image is allocated.
Definition: DBaseImage.h:176
Main Image class.
Definition: DImage.hpp:57
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
VTK Image Interface.
Definition: DVtkInterface.hpp:68
RES_T readDICOM(const char *dirName, Image< T > &outIm, bool autoReslice=true)
Read DICOM.
Definition: DVtkIO.hpp:57
RES_T copy(const Image< T1 > &imIn, size_t startX, size_t startY, size_t startZ, size_t sizeX, size_t sizeY, size_t sizeZ, Image< T2 > &imOut, size_t outStartX=0, size_t outStartY=0, size_t outStartZ=0)
copy() - Copy image (or a zone) into an output image
Definition: DImageTransform.hpp:84