]> Creatis software - crea.git/blob - src/creaVtk.txx
#3106 crea Bug New Normal - branch vtk7itk4 compilation with vtk7
[crea.git] / src / creaVtk.txx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ 
26 */                                                                         
27
28
29
30 #include <vtkDataArrayTemplate.h>
31
32 #include <vtkCharArray.h>
33 #include <vtkSignedCharArray.h>
34 #include <vtkUnsignedCharArray.h>
35 #include <vtkShortArray.h>
36 #include <vtkUnsignedShortArray.h>
37 #include <vtkIntArray.h>
38 #include <vtkUnsignedIntArray.h>
39 #include <vtkLongArray.h>
40 #include <vtkUnsignedLongArray.h>
41 #include <vtkFloatArray.h>
42 #include <vtkDoubleArray.h>
43
44 #include <vtkPointData.h>
45 #include <vtkTypeTraits.h>
46 #include <creaMessageManager.h>
47 namespace crea
48 {  
49   template <class T>
50   /*CREA_EXPORT*/ vtkImageData* NewVtkImageDataFromRaw( T* data, 
51                                                     int nx, 
52                                                     int ny,
53                                                     int nz,
54                                                     bool do_not_desalloc)
55   {
56     //    std::cout << "NV "<<nx<<" " <<ny<<" " << nz<<std::endl;
57     //    std::cout <<  vtkTypeTraits<T>::SizedName() << std::endl;
58     vtkImageData *image = vtkImageData::New();
59
60     // Shouldn't we pass NumberOfScalarComponents to deal with RGB, RGBA images as well? // JPR
61
62
63     int vtktype = vtkTypeTraits<T>::VTKTypeID();
64     image->SetDimensions(nx, ny ,nz);
65     image->SetSpacing(1, 1, 1);
66
67 //EED 2017-01-01 Migration VTK7
68 #if (VTK_MAJOR_VERSION <= 5) 
69     image->SetNumberOfScalarComponents(1);
70     image->SetScalarType(vtktype);
71     image->AllocateScalars();
72 #endif
73 #if (VTK_MAJOR_VERSION >= 6) 
74     image->AllocateScalars(vtktype,1);
75 #endif
76
77
78     vtkDataArray* array = 0;
79     switch (vtktype)
80       {
81       case VTK_CHAR:
82         array = vtkCharArray::New(); break;
83       case VTK_SIGNED_CHAR:
84         array = vtkSignedCharArray::New(); break;
85       case VTK_UNSIGNED_CHAR:
86         array = vtkUnsignedCharArray::New(); break;
87       case VTK_SHORT:
88         array = vtkShortArray::New(); break;
89       case VTK_UNSIGNED_SHORT:
90         array = vtkUnsignedShortArray::New(); break;
91       case VTK_INT:
92         array = vtkIntArray::New(); break;      
93       case VTK_UNSIGNED_INT:
94         array = vtkUnsignedIntArray::New(); break;
95       case VTK_LONG:
96         array = vtkLongArray::New(); break;
97       case VTK_UNSIGNED_LONG:
98         array = vtkUnsignedLongArray::New(); break;
99       case VTK_FLOAT:
100         array = vtkFloatArray::New(); break;
101       case VTK_DOUBLE:
102         array = vtkDoubleArray::New(); break;
103       default:
104         creaGlobalError("NewVtkImageDataFromRaw : type "
105                   <<vtkTypeTraits<T>::SizedName()
106                   <<" non implemented");
107       }
108     vtkDataArrayTemplate<T>* tarray 
109                 = dynamic_cast<vtkDataArrayTemplate<T>*>(array);
110     array->SetNumberOfComponents( 1 );
111     size_t size = (long)nx*(long)ny*(long)nz;
112     // The last param of SetArray is set to 1 to keep the class from deleting the array 
113     // when it cleans up or reallocates memory.
114     int dndesa = 0;
115     if (do_not_desalloc)
116         dndesa = 1;
117     tarray->SetArray( data, size, dndesa );
118     image->GetPointData( )->SetScalars( tarray );
119     array->Delete( );
120     return image;
121   }
122   
123 }
124