5 #include "vtkVOXImageWriter.h"
7 #include "vtkCommand.h"
8 #include "vtkErrorCode.h"
9 #include "vtkImageData.h"
10 #include "vtkInformation.h"
11 #include "vtkInformationVector.h"
12 #include "vtkObjectFactory.h"
13 #include "vtkStreamingDemandDrivenPipeline.h"
14 #include "vtkDataSetAttributes.h"
16 #include <vtkstd/string>
20 //----------------------------------------------------------------------------
21 vtkCxxRevisionMacro(vtkVOXImageWriter, "DummyRevision");
22 vtkStandardNewMacro(vtkVOXImageWriter);
24 //----------------------------------------------------------------------------
25 vtkVOXImageWriter::vtkVOXImageWriter()
28 this->FileLowerLeft = 1;
31 //----------------------------------------------------------------------------
32 vtkVOXImageWriter::~vtkVOXImageWriter()
38 //----------------------------------------------------------------------------
39 void vtkVOXImageWriter::Write( )
41 this->SetErrorCode(vtkErrorCode::NoError);
43 this->GetInput()->UpdateInformation();
46 if (this->GetInput() == NULL )
48 vtkErrorMacro(<<"Write:Please specify an input!");
52 if ( this->FileName == 0)
54 vtkErrorMacro("Output file name not specified");
59 int * ext = this->GetInput()->GetWholeExtent();
60 if ( ext[4] == ext[5] )
63 if ( ext[2] == ext[3] )
69 double * origin = this->GetInput()->GetOrigin();
70 double * spacingDouble = this->GetInput()->GetSpacing();
73 spacing[0] = spacingDouble[0];
74 spacing[1] = spacingDouble[1];
75 spacing[2] = spacingDouble[2];
78 dimSize[0] = ext[1]-ext[0]+1;
79 dimSize[1] = ext[3]-ext[2]+1;
80 dimSize[2] = ext[5]-ext[4]+1;
82 std::string elementType;
84 int scalarType = this->GetInput()->GetScalarType();
88 elementType = "schar";
90 case VTK_UNSIGNED_CHAR:
91 elementType = "uchar";
94 elementType = "sshort";
96 case VTK_UNSIGNED_SHORT:
97 elementType = "ushort";
102 case VTK_UNSIGNED_INT:
103 elementType = "uint";
106 elementType = "slong";
108 case VTK_UNSIGNED_LONG:
109 elementType = "ulong";
112 elementType = "float";
115 elementType = "double";
118 vtkErrorMacro("Unknown scalar type." );
122 origin[0] += ext[0] * spacing[0];
123 origin[1] += ext[2] * spacing[1];
124 origin[2] += ext[4] * spacing[2];
126 this->GetInput()->SetUpdateExtent(ext[0], ext[1],
129 this->GetInput()->UpdateData();
132 this->SetFileDimensionality(nDims);
134 this->InvokeEvent(vtkCommand::StartEvent);
135 this->UpdateProgress(0.0);
137 std::cout << "Writing to file " << this->GetFileName() << " ..." << std::endl;
139 fstream out(this->GetFileName(),ios::out|ios::binary);
140 out << "VOX v2\n# Size\n" << dimSize[0] << " " << dimSize[1] << " "
141 << dimSize[2] << std::endl << "# Spacing" << std::endl
142 << spacing[0] << " " << spacing[1] << " " << spacing[2] << std::endl
143 << "# Image dim" << std::endl << nDims << std::endl
144 << "# Image type" << std::endl << elementType << std::endl;
145 out.write((char*)this->GetInput()->GetScalarPointer(),
146 dimSize[0]*dimSize[1]*dimSize[2]*this->GetInput()->GetScalarSize());
149 this->UpdateProgress(1.0);
150 this->InvokeEvent(vtkCommand::EndEvent);
153 //----------------------------------------------------------------------------
154 void vtkVOXImageWriter::PrintSelf(ostream& os, vtkIndent indent)
156 this->Superclass::PrintSelf(os,indent);
157 os << indent << "FileName: " << (this->FileName==0?this->FileName:"(none)") << endl;