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