4 #include "vtkVOXImageWriter.h"
6 #include "vtkCommand.h"
7 #include "vtkErrorCode.h"
8 #include "vtkImageData.h"
9 #include "vtkInformation.h"
10 #include "vtkInformationVector.h"
11 #include "vtkObjectFactory.h"
12 #include "vtkStreamingDemandDrivenPipeline.h"
13 #include "vtkDataSetAttributes.h"
15 #include <vtkstd/string>
19 //----------------------------------------------------------------------------
20 vtkCxxRevisionMacro(vtkVOXImageWriter, "DummyRevision");
21 vtkStandardNewMacro(vtkVOXImageWriter);
23 //----------------------------------------------------------------------------
24 vtkVOXImageWriter::vtkVOXImageWriter()
27 this->FileLowerLeft = 1;
30 //----------------------------------------------------------------------------
31 vtkVOXImageWriter::~vtkVOXImageWriter()
37 //----------------------------------------------------------------------------
38 void vtkVOXImageWriter::Write( )
40 this->SetErrorCode(vtkErrorCode::NoError);
42 this->GetInput()->UpdateInformation();
45 if (this->GetInput() == NULL )
47 vtkErrorMacro(<<"Write:Please specify an input!");
51 if ( this->FileName == 0)
53 vtkErrorMacro("Output file name not specified");
58 int * ext = this->GetInput()->GetWholeExtent();
59 if ( ext[4] == ext[5] )
62 if ( ext[2] == ext[3] )
68 double * origin = this->GetInput()->GetOrigin();
69 double * spacingDouble = this->GetInput()->GetSpacing();
72 spacing[0] = spacingDouble[0];
73 spacing[1] = spacingDouble[1];
74 spacing[2] = spacingDouble[2];
77 dimSize[0] = ext[1]-ext[0]+1;
78 dimSize[1] = ext[3]-ext[2]+1;
79 dimSize[2] = ext[5]-ext[4]+1;
81 std::string elementType;
83 int scalarType = this->GetInput()->GetScalarType();
87 elementType = "schar";
89 case VTK_UNSIGNED_CHAR:
90 elementType = "uchar";
93 elementType = "sshort";
95 case VTK_UNSIGNED_SHORT:
96 elementType = "ushort";
101 case VTK_UNSIGNED_INT:
102 elementType = "uint";
105 elementType = "slong";
107 case VTK_UNSIGNED_LONG:
108 elementType = "ulong";
111 elementType = "float";
114 elementType = "double";
117 vtkErrorMacro("Unknown scalar type." );
121 origin[0] += ext[0] * spacing[0];
122 origin[1] += ext[2] * spacing[1];
123 origin[2] += ext[4] * spacing[2];
125 this->GetInput()->SetUpdateExtent(ext[0], ext[1],
128 this->GetInput()->UpdateData();
131 this->SetFileDimensionality(nDims);
133 this->InvokeEvent(vtkCommand::StartEvent);
134 this->UpdateProgress(0.0);
136 std::cout << "Writing to file " << this->GetFileName() << " ..." << std::endl;
138 fstream out(this->GetFileName(),ios::out|ios::binary);
139 out << "VOX v2\n# Size\n" << dimSize[0] << " " << dimSize[1] << " "
140 << dimSize[2] << std::endl << "# Spacing" << std::endl
141 << spacing[0] << " " << spacing[1] << " " << spacing[2] << std::endl
142 << "# Image dim" << std::endl << nDims << std::endl
143 << "# Image type" << std::endl << elementType << std::endl;
144 out.write((char*)this->GetInput()->GetScalarPointer(),
145 dimSize[0]*dimSize[1]*dimSize[2]*this->GetInput()->GetScalarSize());
148 this->UpdateProgress(1.0);
149 this->InvokeEvent(vtkCommand::EndEvent);
152 //----------------------------------------------------------------------------
153 void vtkVOXImageWriter::PrintSelf(ostream& os, vtkIndent indent)
155 this->Superclass::PrintSelf(os,indent);
156 os << indent << "FileName: " << (this->FileName==0?this->FileName:"(none)") << endl;