File: vtkMetaImageWriter.cxx

package info (click to toggle)
vtk 5.0.4-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 51,084 kB
  • ctags: 70,426
  • sloc: cpp: 524,166; ansic: 220,276; tcl: 43,377; python: 14,037; perl: 3,102; java: 1,436; yacc: 1,033; sh: 339; lex: 248; makefile: 197; asm: 154
file content (214 lines) | stat: -rw-r--r-- 6,697 bytes parent folder | download | duplicates (2)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkMetaImageWriter.cxx,v $

  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 "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkDataSetAttributes.h"

#include <vtkstd/string>

#include <sys/stat.h>

//----------------------------------------------------------------------------
vtkCxxRevisionMacro(vtkMetaImageWriter, "$Revision: 1.13 $");
vtkStandardNewMacro(vtkMetaImageWriter);

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

//----------------------------------------------------------------------------
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();
}

//----------------------------------------------------------------------------
int vtkMetaImageWriter::RequestData(
  vtkInformation* request,
  vtkInformationVector** inputVector,
  vtkInformationVector* outputVector)
{
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkImageData *input = 
    vtkImageData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
  
  // Error checking
  if (input == NULL )
    {
    vtkErrorMacro(<<"Write:Please specify an input!");
    return 0;
    }

  if ( !this->MHDFileName )
    {
    vtkErrorMacro("Output file name not specified");
    return 0;
    }

  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;
    }

  ofstream ofs_with_warning_C4701(this->MHDFileName, ios::out);
  if ( !ofs_with_warning_C4701 )
    {
    vtkErrorMacro("Cannot open file: " << this->MHDFileName << " for writing");
    return 0;
    }

  int ndims = 3;
  int ext[6];
  inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),ext);
  if ( ext[4] == ext[5] )
    {
    ndims = 2;
    if ( ext[2] == ext[3] )
      {
      ndims = 1;
      }
    }
  double origin[3];
  double spacing[3];
  inInfo->Get(vtkDataObject::ORIGIN(),origin);
  inInfo->Get(vtkDataObject::SPACING(),spacing);
  
  const char* scalar_type;
  vtkInformation *scalarInfo = vtkDataObject::GetActiveFieldInformation(inInfo, 
    vtkDataObject::FIELD_ASSOCIATION_POINTS, vtkDataSetAttributes::SCALARS);
  if (!scalarInfo)
    {
    vtkErrorMacro( "No active scalar information!" );
    return 0;
    }
  switch ( scalarInfo->Get(vtkDataObject::FIELD_ARRAY_TYPE()) )
    {
    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: " 
                    << scalarInfo->Get(vtkDataObject::FIELD_ARRAY_TYPE()));
      return 1;
    }
  
  origin[0] += ext[0] * spacing[0];
  origin[1] += ext[2] * spacing[1];
  origin[2] += ext[4] * spacing[2];

  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;
      }
    }

  ofs_with_warning_C4701 
    << "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 = " 
    << scalarInfo->Get(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS()) << endl
    << "ElementType = " << scalar_type 
    << (scalarInfo->Get(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS()) > 1?"_ARRAY":"") << endl
    << "ElementDataFile = " << data_file << endl;
  this->SetFileDimensionality(ndims);
  return this->Superclass::RequestData(request,inputVector,outputVector);
}

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