forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkMetaImageWriter.cxx
188 lines (156 loc) · 5.89 KB
/
vtkMetaImageWriter.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMetaImageWriter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifdef _MSC_VER
#pragma warning(disable:4018)
#endif
#include "vtkMetaImageWriter.h"
#include "vtkCommand.h"
#include "vtkErrorCode.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkDataSetAttributes.h"
#include <vtkstd/string>
#include "vtkmetaio/metaTypes.h"
#include "vtkmetaio/metaUtils.h"
#include "vtkmetaio/metaEvent.h"
#include "vtkmetaio/metaObject.h"
#include "vtkmetaio/metaImageTypes.h"
#include "vtkmetaio/metaImageUtils.h"
#include "vtkmetaio/metaImage.h"
#include <sys/stat.h>
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkMetaImageWriter);
//----------------------------------------------------------------------------
vtkMetaImageWriter::vtkMetaImageWriter()
{
this->MHDFileName = 0;
this->FileLowerLeft = 1;
this->MetaImagePtr = new vtkmetaio::MetaImage;
this->Compress = true;
}
//----------------------------------------------------------------------------
vtkMetaImageWriter::~vtkMetaImageWriter()
{
this->SetFileName(0);
delete this->MetaImagePtr;
}
//----------------------------------------------------------------------------
void vtkMetaImageWriter::SetFileName(const char* fname)
{
this->SetMHDFileName(fname);
this->Superclass::SetFileName( 0 );
}
//----------------------------------------------------------------------------
void vtkMetaImageWriter::SetRAWFileName(const char* fname)
{
this->Superclass::SetFileName(fname);
}
//----------------------------------------------------------------------------
char* vtkMetaImageWriter::GetRAWFileName()
{
return this->Superclass::GetFileName();
}
//----------------------------------------------------------------------------
void vtkMetaImageWriter::Write( )
{
this->SetErrorCode(vtkErrorCode::NoError);
this->GetInput()->UpdateInformation();
// Error checking
if (this->GetInput() == NULL )
{
vtkErrorMacro(<<"Write:Please specify an input!");
return;
}
if ( !this->MHDFileName )
{
vtkErrorMacro("Output file name not specified");
return;
}
int nDims = 3;
int * ext = this->GetInput()->GetWholeExtent();
if ( ext[4] == ext[5] )
{
nDims = 2;
if ( ext[2] == ext[3] )
{
nDims = 1;
}
}
double * origin = this->GetInput()->GetOrigin();
double * spacingDouble = this->GetInput()->GetSpacing();
float spacing[3];
spacing[0] = spacingDouble[0];
spacing[1] = spacingDouble[1];
spacing[2] = spacingDouble[2];
int dimSize[3];
dimSize[0] = ext[1]-ext[0]+1;
dimSize[1] = ext[3]-ext[2]+1;
dimSize[2] = ext[5]-ext[4]+1;
vtkmetaio::MET_ValueEnumType elementType;
int scalarType = this->GetInput()->GetScalarType();
switch ( scalarType )
{
case VTK_CHAR: elementType = vtkmetaio::MET_CHAR; break;
case VTK_SIGNED_CHAR: elementType = vtkmetaio::MET_CHAR; break;
case VTK_UNSIGNED_CHAR: elementType = vtkmetaio::MET_UCHAR; break;
case VTK_SHORT: elementType = vtkmetaio::MET_SHORT; break;
case VTK_UNSIGNED_SHORT: elementType = vtkmetaio::MET_USHORT; break;
case VTK_INT: elementType = vtkmetaio::MET_INT; break;
case VTK_UNSIGNED_INT: elementType = vtkmetaio::MET_UINT; break;
case VTK_LONG: elementType = vtkmetaio::MET_LONG; break;
case VTK_UNSIGNED_LONG: elementType = vtkmetaio::MET_ULONG; break;
case VTK_FLOAT: elementType = vtkmetaio::MET_FLOAT; break;
case VTK_DOUBLE: elementType = vtkmetaio::MET_DOUBLE; break;
default:
vtkErrorMacro("Unknown scalar type." );
return ;
}
origin[0] += ext[0] * spacing[0];
origin[1] += ext[2] * spacing[1];
origin[2] += ext[4] * spacing[2];
int numberOfElements = this->GetInput()->GetNumberOfScalarComponents();
this->GetInput()->SetUpdateExtent(ext[0], ext[1],
ext[2], ext[3],
ext[4], ext[5]);
this->GetInput()->UpdateData();
this->MetaImagePtr->InitializeEssential( nDims,
dimSize,
spacing,
elementType,
numberOfElements,
this->GetInput()
->GetScalarPointer(ext[0],
ext[2],
ext[4]),
false );
this->MetaImagePtr->Position( origin );
if ( this->GetRAWFileName() )
{
this->MetaImagePtr->ElementDataFileName( this->GetRAWFileName() );
}
this->SetFileDimensionality(nDims);
this->MetaImagePtr->CompressedData(Compress);
this->InvokeEvent(vtkCommand::StartEvent);
this->UpdateProgress(0.0);
this->MetaImagePtr->Write(this->MHDFileName);
this->UpdateProgress(1.0);
this->InvokeEvent(vtkCommand::EndEvent);
}
//----------------------------------------------------------------------------
void vtkMetaImageWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "MHDFileName: "
<< (this->MHDFileName?this->MHDFileName:"(none)") << endl;
}