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