vtkMetaImageWriter.cxx 5.45 KB
Newer Older
Andy Cedilnik's avatar
Andy Cedilnik committed
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
/*=========================================================================

  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.

=========================================================================*/
#include "vtkMetaImageWriter.h"

#include "vtkObjectFactory.h"
#include "vtkImageData.h"
#include "vtkXMLImageDataWriter.h"

#include <vtkstd/string>

#include <sys/stat.h>

//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
26
vtkCxxRevisionMacro(vtkMetaImageWriter, "1.3");
Andy Cedilnik's avatar
Andy Cedilnik committed
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
vtkStandardNewMacro(vtkMetaImageWriter);

//----------------------------------------------------------------------------
vtkMetaImageWriter::vtkMetaImageWriter()
{
  this->MHDFileName = 0;
}

//----------------------------------------------------------------------------
vtkMetaImageWriter::~vtkMetaImageWriter()
{
  this->SetFileName(0);
}

//----------------------------------------------------------------------------
void vtkMetaImageWriter::SetFileName(const char* fname)
{
  this->SetMHDFileName(fname);
}

//----------------------------------------------------------------------------
void vtkMetaImageWriter::SetRAWFileName(const char* fname)
{
  this->Superclass::SetFileName(fname);
}

//----------------------------------------------------------------------------
char* vtkMetaImageWriter::GetRAWFileName()
{
  return this->Superclass::GetFileName();
}

//----------------------------------------------------------------------------
void vtkMetaImageWriter::Write()
{
  vtkImageData* id = this->GetInput();
  if ( !id )
    {
    vtkErrorMacro("Input not specified");
    return;
    }
  if ( !this->MHDFileName )
    {
    vtkErrorMacro("Output file name not specified");
    return;
    }
  if ( !this->GetRAWFileName() )
    {
    vtkDebugMacro("Raw file name not specified. Specifying one...");
    // Allocate new file name and leave space for extension
    char* rfname = new char [ strlen(this->MHDFileName) + 10 ]; 
    strcpy(rfname, this->MHDFileName);
    size_t cc;
    for ( cc = strlen(rfname)-1; cc > 0; cc -- )
      {
      if ( rfname[cc] == '.' )
        {
        rfname[cc] = 0;
        break;
        }
      if ( rfname[cc] == '/' || rfname[cc] == '\\' )
        {
        break;
        }
      }
    strcat(rfname, ".raw");
    if ( strcmp(rfname, this->MHDFileName) == 0 )
      {
      strcat(rfname, ".raw");
      }
    this->SetRAWFileName(rfname);
    delete [] rfname;
    }

Ken Martin's avatar
Ken Martin committed
101
  ofstream ofs(this->MHDFileName, ios::out);
Andy Cedilnik's avatar
Andy Cedilnik committed
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
  if ( !ofs )
    {
    vtkErrorMacro("Cannot open file: " << this->MHDFileName << " for writing");
    return;
    }

  int ndims = 3;
  int ext[6];
  id->GetWholeExtent(ext);
  if ( ext[4] == ext[5] )
    {
    ndims = 2;
    if ( ext[2] == ext[3] )
      {
      ndims = 1;
      }
    }
  double origin[3];
  double spacing[3];
  id->GetOrigin(origin);
  id->GetSpacing(spacing);

  const char* scalar_type = 0;
  switch ( id->GetScalarType() )
    {
  case VTK_CHAR:           scalar_type = "MET_CHAR"; break;
  case VTK_UNSIGNED_CHAR:  scalar_type = "MET_UCHAR"; break;
  case VTK_SHORT:          scalar_type = "MET_SHORT"; break;
  case VTK_UNSIGNED_SHORT: scalar_type = "MET_USHORT"; break;
  case VTK_INT:            scalar_type = "MET_INT"; break;
  case VTK_UNSIGNED_INT:   scalar_type = "MET_UINT"; break;
  case VTK_LONG:           scalar_type = "MET_LONG"; break;
  case VTK_UNSIGNED_LONG:  scalar_type = "MET_ULONG"; break;
  case VTK_FLOAT:          scalar_type = "MET_FLOAT"; break;
  case VTK_DOUBLE:         scalar_type = "MET_DOUBLE"; break;
  default:
    vtkErrorMacro("Unknown scalar type: " << id->GetScalarTypeAsString());
    return;
    }

  origin[0] += ext[0];
  origin[1] += ext[2];
  origin[2] += ext[4];

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  const char* data_file = this->GetRAWFileName();
  int pos = 0;
  int cc;
  for ( cc = 0; data_file[cc]; cc ++ )
    {
    if ( data_file[cc] == '/' || data_file[cc] == '\\' )
      {
      pos = cc;
      }
    }
  if ( pos > 0 )
    {
    if ( strncmp(data_file, this->GetFileName(), pos) == 0 )
      {
      data_file = this->GetRAWFileName() + pos + 1;
      }
    }

Andy Cedilnik's avatar
Andy Cedilnik committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  ofs 
    << "ObjectType = Image" << endl
    << "NDims = " << ndims << endl
    << "BinaryData = True" << endl
#ifdef VTK_WORDS_BIGENDIAN
    << "BinaryDataByteOrderMSB = True" << endl
#else
    << "BinaryDataByteOrderMSB = False" << endl
#endif
    << "ElementSpacing = " << spacing[0] << " " << spacing[1] << " " << spacing[2] << endl
    << "DimSize = " << (ext[1]-ext[0]+1) << " " << (ext[3]-ext[2]+1) << " " << (ext[5]-ext[4]+1) << endl
    << "Position = " << origin[0] << " " << origin[1] << " " << origin[2] << endl
    << "ElementNumberOfChannels = " << id->GetNumberOfScalarComponents() << endl
    << "ElementType = " << scalar_type << (id->GetNumberOfScalarComponents() > 1?"_ARRAY":"") << endl
178
    << "ElementDataFile = " << data_file << endl;
Andy Cedilnik's avatar
Andy Cedilnik committed
179
180
181
182
183
184
185
186
187
188
  this->SetFileDimensionality(ndims);
  this->Superclass::Write();
}

//----------------------------------------------------------------------------
void vtkMetaImageWriter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
  os << indent << "MHDFileName: " << (this->MHDFileName?this->MHDFileName:"(none)") << endl;
}