SMIL  0.9.1
DVtkInterface.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_IMAGE_HPP
31 #define _D_VTK_IMAGE_HPP
32 
33 #include "Core/include/DCore.h"
34 #include "Core/include/private/DSharedImage.hpp"
35 
36 #include <vtkImageData.h>
37 #include <vtkImageFlip.h>
38 
39 #if defined Py_PYCONFIG_H || defined SWIGPYTHON
40 #include <vtkPythonUtil.h>
41 #endif
42 
43 namespace smil
44 {
51  template <class T>
52  int getVtkType()
53  {
54  return -1;
55  }
56  template<> int getVtkType<UINT8>() { return VTK_UNSIGNED_CHAR; }
57  template<> int getVtkType<UINT16>() { return VTK_UNSIGNED_SHORT; }
58  template<> int getVtkType<INT8>() { return VTK_SIGNED_CHAR; }
59  template<> int getVtkType<INT16>() { return VTK_SHORT; }
60 
66  template <class T>
67  class VtkInt : public SharedImage<T>
68  {
69  public:
71 
72  VtkInt(vtkImageData *imData, bool flipImage=true)
73  {
74  BaseObject::className = "VtkInt";
75  parentClass::init();
76 
77  flip = vtkImageFlip::New();
78  attach(imData, flipImage);
79  }
80  #if defined Py_PYCONFIG_H || defined SWIGPYTHON
81  VtkInt(PyObject *obj, bool flipImage=true)
82  {
83  BaseObject::className = "VtkInt";
84  parentClass::init();
85 
86  flip = vtkImageFlip::New();
87 
88  vtkImageData * imData = (vtkImageData*)vtkPythonUtil::GetPointerFromObject(obj, "vtkImageData" );
89 
90  if ( imData == 0 ) // if the PyObject is not a vtk.vtkImageData object
91  {
92  PyErr_SetString( PyExc_TypeError, "Not a vtkImageData" );
93  }
94  else
95  {
96  attach(imData, flipImage);
97  }
98  }
99  #endif // Py_PYCONFIG_H
100 
101 
102  RES_T attach(vtkImageData *imData, bool flipImage)
103  {
104  if( getVtkType<T>()!=imData->GetScalarType() )
105  {
106  ERR_MSG("Wrong image type");
107  cout << "vtkImageData type is " << imData->GetScalarTypeAsString() << endl;
108  return RES_ERR;
109  }
110 
111  int *dims = imData->GetDimensions();
112 
113  if (flipImage)
114  {
115  flip->SetInput(imData);
116  flip->SetFilteredAxis(1);
117  flip->Update();
118 
119  typename Image<T>::lineType pix = static_cast<typename Image<T>::lineType>(flip->GetOutput()->GetScalarPointer());
120  SharedImage<T>::attach(pix, dims[0], dims[1], dims[2]);
121  }
122  else
123  {
124  typename Image<T>::lineType pix = static_cast<typename Image<T>::lineType>(imData->GetScalarPointer());
125  SharedImage<T>::attach(pix, dims[0], dims[1], dims[2]);
126  }
127 
128  return RES_OK;
129  }
130  protected:
131  vtkImageFlip *flip;
132  bool _flipImage;
133  };
134 
135  template <>
136  class VtkInt<RGB>
137  {
138  public:
139  VtkInt(vtkImageData *)
140  {
141  }
142  };
143 
146 } // namespace smil
147 
148 #endif // _D_VTK_IMAGE_HPP
Definition: DColorConvert.h:38
Image that uses an existing (1D) data pointer.
Definition: DImage.hpp:53
VTK Image Interface.
Definition: DVtkInterface.hpp:67
Definition: DColor.h:84