File: itkGEAdwImageIO.cxx

package info (click to toggle)
insighttoolkit 3.6.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 94,956 kB
  • ctags: 74,981
  • sloc: cpp: 355,621; ansic: 195,070; fortran: 28,713; python: 3,802; tcl: 1,996; sh: 1,175; java: 583; makefile: 415; csh: 184; perl: 175
file content (231 lines) | stat: -rw-r--r-- 6,584 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkGEAdwImageIO.cxx,v $
  Language:  C++
  Date:      $Date: 2008-01-23 15:30:00 $
  Version:   $Revision: 1.28 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/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 notices for more information.

  =========================================================================*/
#include "itkGEAdwImageIO.h"
#include "itkIOCommon.h"
#include <itksys/SystemTools.hxx>

#include "itkExceptionObject.h"
#include "itkByteSwapper.h"
#include "itkGEImageHeader.h"
#include "itkDirectory.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <string>

//From uiig library "The University of Iowa Imaging Group-UIIG"

namespace itk 
{
// Default constructor
GEAdwImageIO::GEAdwImageIO()
{
  //Purposefully left blank
}

GEAdwImageIO::~GEAdwImageIO()
{
  //Purposefully left blank
    
}

bool GEAdwImageIO::CanReadFile( const char* FileNameToRead )
{
  size_t imageSize;
  short matrixX;
  short matrixY;
  int varHdrSize;
  //this->SetFileName(FileNameToRead);
  //
  // Can you open it?
  std::ifstream f(FileNameToRead,std::ios::binary | std::ios::in);
  if(!f.is_open())
    {
    return false;
    }
  //
  // This test basically snoops out the image dimensions, and the
  // length of the variable-length part of the header, and computes
  // the size the file should be and compares it with the actual size.
  // if it's not reading a GEAdw file, chances are overwhelmingly good
  // that this operation will fail somewhere along the line.
  if(this->GetShortAt(f,GE_ADW_IM_IMATRIX_X,&matrixX,false) != 0)
    {
    f.close();
    return false;
    }

  if(this->GetShortAt(f,GE_ADW_IM_IMATRIX_Y,&matrixY,false) != 0)
    {
    f.close();
    return false;
    }

  if(this->GetIntAt(f,GE_ADW_VARIABLE_HDR_LENGTH,&varHdrSize,false) != 0)
    {
    f.close();
    return false;
    }

  imageSize = varHdrSize + GE_ADW_FIXED_HDR_LENGTH + (matrixX * matrixY * sizeof(short));

  if ( imageSize != itksys::SystemTools::FileLength(FileNameToRead) )
    {
    f.close();
    return false;
    }
  f.close();
  return true;
}

GEImageHeader *GEAdwImageIO::ReadHeader(const char *FileNameToRead)
{
  char tmpbuf[1024];

  if(!this->CanReadFile(FileNameToRead))
    {
    RAISE_EXCEPTION();
    }
  GEImageHeader *hdr = new GEImageHeader;
  if(hdr == 0)
    {
    RAISE_EXCEPTION();
    }
  //
  // Next, can you open it?
  std::ifstream f(FileNameToRead,std::ios::binary | std::ios::in);
  if(!f.is_open())
    {
    RAISE_EXCEPTION();
    }
  sprintf(hdr->scanner,"GE-ADW");
  this->GetStringAt(f,GE_ADW_EX_PATID,tmpbuf,12);
  tmpbuf[12] = '\0';
  hdr->patientId[0] = '\0';
  for(char *ptr = strtok(tmpbuf,"-"); ptr != NULL; ptr = strtok(NULL,"-"))
    {
    strcat(hdr->patientId,ptr);
    }

  this->GetStringAt(f,GE_ADW_EX_PATNAME,hdr->name,GE_ADW_EX_PATNAME_LEN);
  hdr->name[GE_ADW_EX_PATNAME_LEN] = '\0';

  this->GetStringAt(f,GE_ADW_EX_HOSPNAME,hdr->hospital,34);
  hdr->hospital[33] = '\0';


  int timeStamp;
  this->GetIntAt(f,GE_ADW_EX_DATETIME,&timeStamp);
  this->statTimeToAscii(&timeStamp,hdr->date);

  this->GetStringAt(f,GE_ADW_SU_PRODID,hdr->scanner,13);
  hdr->scanner[13] = '\0';

  this->GetShortAt(f,GE_ADW_SE_NO,&(hdr->seriesNumber));

  this->GetShortAt(f,GE_ADW_IM_NO,&(hdr->imageNumber));

  this->GetShortAt(f,GE_ADW_IM_CPHASENUM,&(hdr->imagesPerSlice));

  this->GetShortAt(f,GE_ADW_IM_CPHASENUM,&(hdr->turboFactor));

  this->GetFloatAt(f,GE_ADW_IM_SLTHICK,&(hdr->sliceThickness));
  hdr->sliceGap = 0.0f;

  this->GetShortAt(f,GE_ADW_IM_IMATRIX_X,&(hdr->imageXsize));

  this->GetShortAt(f,GE_ADW_IM_IMATRIX_Y,&(hdr->imageYsize));


  hdr->acqXsize = hdr->imageXsize;
  hdr->acqYsize = hdr->imageYsize;
    
  this->GetFloatAt(f,GE_ADW_IM_DFOV,&hdr->xFOV);
  hdr->yFOV = hdr->xFOV;

  this->GetFloatAt(f,GE_ADW_IM_PIXSIZE_X,&hdr->imageXres);

  this->GetFloatAt(f,GE_ADW_IM_PIXSIZE_Y,&hdr->imageYres);

  short tmpShort;
  this->GetShortAt(f,GE_ADW_IM_PLANE,&tmpShort);
  switch (tmpShort)
    {
    case GE_CORONAL:
      //hdr->imagePlane = itk::IOCommon::ITK_ANALYZE_ORIENTATION_IRP_CORONAL;
      //hdr->origin = itk::IOCommon::ITK_ORIGIN_SLA;
      hdr->coordinateOrientation = itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_RSP;
      break;
    case GE_SAGITTAL:
      //hdr->imagePlane = itk::SpatialOrientation::ITK_ANALYZE_ORIENTATION_IRP_SAGITTAL;
      //hdr->origin = itk::SpatialOrientation::ITK_ORIGIN_SLA;
      hdr->coordinateOrientation = itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_AIR;
      break;
    case GE_AXIAL:
      //hdr->imagePlane = itk::SpatialOrientation::ITK_ANALYZE_ORIENTATION_IRP_TRANSVERSE;
      //hdr->origin = itk::SpatialOrientation::ITK_ORIGIN_SLA;
      hdr->coordinateOrientation = itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_RAI;
      break;
    default:
      //hdr->imagePlane = itk::SpatialOrientation::ITK_ANALYZE_ORIENTATION_IRP_CORONAL;
      //hdr->origin = itk::SpatialOrientation::ITK_ORIGIN_SLA;
      hdr->coordinateOrientation = itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_RSP;
      break;
    }
  this->GetFloatAt(f,GE_ADW_IM_LOC,&(hdr->sliceLocation));

  int tmpInt;
  this->GetIntAt(f,GE_ADW_IM_TR,&tmpInt);
  hdr->TR = (float) tmpInt / 1000.0f;

  this->GetIntAt(f,GE_ADW_IM_TI,&tmpInt);
  hdr->TI = (float) tmpInt / 1000.0f;

  this->GetIntAt(f,GE_ADW_IM_TE,&tmpInt);
  hdr->TE = (float) tmpInt / 1000.0f;

  this->GetShortAt(f, GE_ADW_IM_NUMECHO,&(hdr->numberOfEchoes));

  this->GetShortAt(f, GE_ADW_IM_ECHONUM,&(hdr->echoNumber));

  float tmpFloat;
  this->GetFloatAt(f,GE_ADW_IM_NEX,&tmpFloat);
    
  hdr->NEX = (int) tmpFloat;
    
  this->GetShortAt(f,GE_ADW_IM_MR_FLIP,&hdr->flipAngle);

  this->GetStringAt(f,GE_ADW_IM_PSDNAME, hdr->pulseSequence, 31);
  hdr->pulseSequence[31] = '\0';
    
  this->GetShortAt(f,GE_ADW_IM_SLQUANT,&(hdr->numberOfSlices));

  this->GetIntAt(f,GE_ADW_VARIABLE_HDR_LENGTH,&tmpInt);
  hdr->offset = GE_ADW_FIXED_HDR_LENGTH + tmpInt;

  strncpy (hdr->filename, FileNameToRead, IOCommon::ITK_MAXPATHLEN);
  hdr->filename[IOCommon::ITK_MAXPATHLEN] = '\0';

  return hdr;
}

} // end namespace itk