]> Creatis software - clitk.git/blob - vv/vtkVOXImageWriter.cxx
changes in license header
[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
32 #include <vtkstd/string>
33
34 #include <sys/stat.h>
35
36 //----------------------------------------------------------------------------
37 vtkCxxRevisionMacro(vtkVOXImageWriter, "DummyRevision");
38 vtkStandardNewMacro(vtkVOXImageWriter);
39
40 //----------------------------------------------------------------------------
41 vtkVOXImageWriter::vtkVOXImageWriter()
42 {
43   this->FileName = 0;
44   this->FileLowerLeft = 1;
45 }
46
47 //----------------------------------------------------------------------------
48 vtkVOXImageWriter::~vtkVOXImageWriter()
49 {
50   this->SetFileName(0);
51 }
52
53
54 //----------------------------------------------------------------------------
55 void vtkVOXImageWriter::Write( )
56 {
57   this->SetErrorCode(vtkErrorCode::NoError);
58
59   this->GetInput()->UpdateInformation();
60
61   // Error checking
62   if (this->GetInput() == NULL ) {
63     vtkErrorMacro(<<"Write:Please specify an input!");
64     return;
65   }
66
67   if ( this->FileName == 0) {
68     vtkErrorMacro("Output file name not specified");
69     return;
70   }
71
72   int nDims = 3;
73   int * ext = this->GetInput()->GetWholeExtent();
74   if ( ext[4] == ext[5] ) {
75     nDims = 2;
76     if ( ext[2] == ext[3] ) {
77       nDims = 1;
78     }
79   }
80
81   double * origin = this->GetInput()->GetOrigin();
82   double * spacingDouble = this->GetInput()->GetSpacing();
83
84   float spacing[3];
85   spacing[0] = spacingDouble[0];
86   spacing[1] = spacingDouble[1];
87   spacing[2] = spacingDouble[2];
88
89   int dimSize[3];
90   dimSize[0] = ext[1]-ext[0]+1;
91   dimSize[1] = ext[3]-ext[2]+1;
92   dimSize[2] = ext[5]-ext[4]+1;
93
94   std::string elementType;
95
96   int scalarType = this->GetInput()->GetScalarType();
97   switch ( scalarType ) {
98   case VTK_CHAR:
99     elementType = "schar";
100     break;
101   case VTK_UNSIGNED_CHAR:
102     elementType = "uchar";
103     break;
104   case VTK_SHORT:
105     elementType = "sshort";
106     break;
107   case VTK_UNSIGNED_SHORT:
108     elementType = "ushort";
109     break;
110   case VTK_INT:
111     elementType = "int";
112     break;
113   case VTK_UNSIGNED_INT:
114     elementType = "uint";
115     break;
116   case VTK_LONG:
117     elementType = "slong";
118     break;
119   case VTK_UNSIGNED_LONG:
120     elementType = "ulong";
121     break;
122   case VTK_FLOAT:
123     elementType = "float";
124     break;
125   case VTK_DOUBLE:
126     elementType = "double";
127     break;
128   default:
129     vtkErrorMacro("Unknown scalar type." );
130     return ;
131   }
132
133   origin[0] += ext[0] * spacing[0];
134   origin[1] += ext[2] * spacing[1];
135   origin[2] += ext[4] * spacing[2];
136
137   this->GetInput()->SetUpdateExtent(ext[0], ext[1],
138                                     ext[2], ext[3],
139                                     ext[4], ext[5]);
140   this->GetInput()->UpdateData();
141
142
143   this->SetFileDimensionality(nDims);
144
145   this->InvokeEvent(vtkCommand::StartEvent);
146   this->UpdateProgress(0.0);
147   //write here
148   std::cout << "Writing to file " << this->GetFileName() << " ..." << std::endl;
149   std::cout.flush();
150   fstream out(this->GetFileName(),ios::out|ios::binary);
151   out << "VOX v2\n# Size\n" << dimSize[0] << " " << dimSize[1] << " "
152       << dimSize[2] << std::endl << "# Spacing" << std::endl
153       << spacing[0] << " " << spacing[1] << " " << spacing[2] << std::endl
154       << "# Image dim" << std::endl << nDims << std::endl
155       << "# Image type" << std::endl << elementType << std::endl;
156   out.write((char*)this->GetInput()->GetScalarPointer(),
157             dimSize[0]*dimSize[1]*dimSize[2]*this->GetInput()->GetScalarSize());
158   out.close();
159
160   this->UpdateProgress(1.0);
161   this->InvokeEvent(vtkCommand::EndEvent);
162 }
163
164 //----------------------------------------------------------------------------
165 void vtkVOXImageWriter::PrintSelf(ostream& os, vtkIndent indent)
166 {
167   this->Superclass::PrintSelf(os,indent);
168   os << indent << "FileName: " << (this->FileName==0?this->FileName:"(none)") << endl;
169 }