]> Creatis software - clitk.git/blob - vv/vtkVOXImageWriter.cxx
Debug RTStruct conversion with empty struc
[clitk.git] / vv / vtkVOXImageWriter.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #include <string>
19 #include <fstream>
20
21 #include "vtkVOXImageWriter.h"
22
23 #include "vtkCommand.h"
24 #include "vtkErrorCode.h"
25 #include "vtkImageData.h"
26 #include "vtkInformation.h"
27 #include "vtkInformationVector.h"
28 #include "vtkObjectFactory.h"
29 #include "vtkStreamingDemandDrivenPipeline.h"
30 #include "vtkDataSetAttributes.h"
31 #include <vtkVersion.h>
32 #include <vtkAlgorithm.h>
33
34 #include <sys/stat.h>
35
36 //----------------------------------------------------------------------------
37 vtkStandardNewMacro(vtkVOXImageWriter);
38
39 //----------------------------------------------------------------------------
40 vtkVOXImageWriter::vtkVOXImageWriter()
41 {
42   this->FileName = 0;
43   this->FileLowerLeft = 1;
44 }
45
46 //----------------------------------------------------------------------------
47 vtkVOXImageWriter::~vtkVOXImageWriter()
48 {
49   this->SetFileName(0);
50 }
51
52
53 //----------------------------------------------------------------------------
54 void vtkVOXImageWriter::Write( )
55 {
56   this->SetErrorCode(vtkErrorCode::NoError);
57 #if VTK_MAJOR_VERSION <= 5
58   this->GetInput()->UpdateInformation();
59 #else
60   this->UpdateInformation();
61 #endif
62   // Error checking
63   if (this->GetInput() == NULL ) {
64     vtkErrorMacro(<<"Write:Please specify an input!");
65     return;
66   }
67
68   if ( this->FileName == 0) {
69     vtkErrorMacro("Output file name not specified");
70     return;
71   }
72
73   int nDims = 3;
74 #if VTK_MAJOR_VERSION <= 5
75   int * ext = this->GetInput()->GetWholeExtent();
76 #else
77   int * ext = this->GetInformation()->Get(vtkDataObject::DATA_EXTENT());
78 #endif
79   if ( ext[4] == ext[5] ) {
80     nDims = 2;
81     if ( ext[2] == ext[3] ) {
82       nDims = 1;
83     }
84   }
85
86   double * origin = this->GetInput()->GetOrigin();
87   double * spacingDouble = this->GetInput()->GetSpacing();
88
89   float spacing[3];
90   spacing[0] = spacingDouble[0];
91   spacing[1] = spacingDouble[1];
92   spacing[2] = spacingDouble[2];
93
94   int dimSize[3];
95   dimSize[0] = ext[1]-ext[0]+1;
96   dimSize[1] = ext[3]-ext[2]+1;
97   dimSize[2] = ext[5]-ext[4]+1;
98
99   std::string elementType;
100
101   int scalarType = this->GetInput()->GetScalarType();
102   switch ( scalarType ) {
103   case VTK_CHAR:
104     elementType = "schar";
105     break;
106   case VTK_UNSIGNED_CHAR:
107     elementType = "uchar";
108     break;
109   case VTK_SHORT:
110     elementType = "sshort";
111     break;
112   case VTK_UNSIGNED_SHORT:
113     elementType = "ushort";
114     break;
115   case VTK_INT:
116     elementType = "int";
117     break;
118   case VTK_UNSIGNED_INT:
119     elementType = "uint";
120     break;
121   case VTK_LONG:
122     elementType = "slong";
123     break;
124   case VTK_UNSIGNED_LONG:
125     elementType = "ulong";
126     break;
127   case VTK_FLOAT:
128     elementType = "float";
129     break;
130   case VTK_DOUBLE:
131     elementType = "double";
132     break;
133   default:
134     vtkErrorMacro("Unknown scalar type." );
135     return ;
136   }
137
138   origin[0] += ext[0] * spacing[0];
139   origin[1] += ext[2] * spacing[1];
140   origin[2] += ext[4] * spacing[2];
141
142 #if VTK_MAJOR_VERSION <= 5
143   this->GetInput()->SetUpdateExtent(ext[0], ext[1],
144                                     ext[2], ext[3],
145                                     ext[4], ext[5]);
146   this->GetInput()->UpdateData();
147 #elif VTK_MAJOR_VERSION >= 8 || (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1)
148   this->UpdateExtent(ext);
149   this->Update();
150 #else
151   this->SetUpdateExtent(ext);
152   this->Update();
153 #endif
154   this->SetFileDimensionality(nDims);
155
156   this->InvokeEvent(vtkCommand::StartEvent);
157   this->UpdateProgress(0.0);
158   //write here
159   std::cout << "Writing to file " << this->GetFileName() << " ..." << std::endl;
160   std::cout.flush();
161   fstream out(this->GetFileName(),ios::out|ios::binary);
162   out << "VOX v2\n# Size\n" << dimSize[0] << " " << dimSize[1] << " "
163       << dimSize[2] << std::endl << "# Spacing" << std::endl
164       << spacing[0] << " " << spacing[1] << " " << spacing[2] << std::endl
165       << "# Image dim" << std::endl << nDims << std::endl
166       << "# Image type" << std::endl << elementType << std::endl;
167   out.write((char*)this->GetInput()->GetScalarPointer(),
168             dimSize[0]*dimSize[1]*dimSize[2]*this->GetInput()->GetScalarSize());
169   out.close();
170
171   this->UpdateProgress(1.0);
172   this->InvokeEvent(vtkCommand::EndEvent);
173 }
174
175 //----------------------------------------------------------------------------
176 void vtkVOXImageWriter::PrintSelf(ostream& os, vtkIndent indent)
177 {
178   this->Superclass::PrintSelf(os,indent);
179   os << indent << "FileName: " << (this->FileName==0?this->FileName:"(none)") << endl;
180 }