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