1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
21 #include "vtkVOXImageWriter.h"
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"
32 #include <vtkstd/string>
36 //----------------------------------------------------------------------------
37 vtkCxxRevisionMacro(vtkVOXImageWriter, "DummyRevision");
38 vtkStandardNewMacro(vtkVOXImageWriter);
40 //----------------------------------------------------------------------------
41 vtkVOXImageWriter::vtkVOXImageWriter()
44 this->FileLowerLeft = 1;
47 //----------------------------------------------------------------------------
48 vtkVOXImageWriter::~vtkVOXImageWriter()
54 //----------------------------------------------------------------------------
55 void vtkVOXImageWriter::Write( )
57 this->SetErrorCode(vtkErrorCode::NoError);
59 this->GetInput()->UpdateInformation();
62 if (this->GetInput() == NULL )
64 vtkErrorMacro(<<"Write:Please specify an input!");
68 if ( this->FileName == 0)
70 vtkErrorMacro("Output file name not specified");
75 int * ext = this->GetInput()->GetWholeExtent();
76 if ( ext[4] == ext[5] )
79 if ( ext[2] == ext[3] )
85 double * origin = this->GetInput()->GetOrigin();
86 double * spacingDouble = this->GetInput()->GetSpacing();
89 spacing[0] = spacingDouble[0];
90 spacing[1] = spacingDouble[1];
91 spacing[2] = spacingDouble[2];
94 dimSize[0] = ext[1]-ext[0]+1;
95 dimSize[1] = ext[3]-ext[2]+1;
96 dimSize[2] = ext[5]-ext[4]+1;
98 std::string elementType;
100 int scalarType = this->GetInput()->GetScalarType();
101 switch ( scalarType )
104 elementType = "schar";
106 case VTK_UNSIGNED_CHAR:
107 elementType = "uchar";
110 elementType = "sshort";
112 case VTK_UNSIGNED_SHORT:
113 elementType = "ushort";
118 case VTK_UNSIGNED_INT:
119 elementType = "uint";
122 elementType = "slong";
124 case VTK_UNSIGNED_LONG:
125 elementType = "ulong";
128 elementType = "float";
131 elementType = "double";
134 vtkErrorMacro("Unknown scalar type." );
138 origin[0] += ext[0] * spacing[0];
139 origin[1] += ext[2] * spacing[1];
140 origin[2] += ext[4] * spacing[2];
142 this->GetInput()->SetUpdateExtent(ext[0], ext[1],
145 this->GetInput()->UpdateData();
148 this->SetFileDimensionality(nDims);
150 this->InvokeEvent(vtkCommand::StartEvent);
151 this->UpdateProgress(0.0);
153 std::cout << "Writing to file " << this->GetFileName() << " ..." << std::endl;
155 fstream out(this->GetFileName(),ios::out|ios::binary);
156 out << "VOX v2\n# Size\n" << dimSize[0] << " " << dimSize[1] << " "
157 << dimSize[2] << std::endl << "# Spacing" << std::endl
158 << spacing[0] << " " << spacing[1] << " " << spacing[2] << std::endl
159 << "# Image dim" << std::endl << nDims << std::endl
160 << "# Image type" << std::endl << elementType << std::endl;
161 out.write((char*)this->GetInput()->GetScalarPointer(),
162 dimSize[0]*dimSize[1]*dimSize[2]*this->GetInput()->GetScalarSize());
165 this->UpdateProgress(1.0);
166 this->InvokeEvent(vtkCommand::EndEvent);
169 //----------------------------------------------------------------------------
170 void vtkVOXImageWriter::PrintSelf(ostream& os, vtkIndent indent)
172 this->Superclass::PrintSelf(os,indent);
173 os << indent << "FileName: " << (this->FileName==0?this->FileName:"(none)") << endl;