File: NewSpmOutputter.cpp

package info (click to toggle)
mriconvert 1%3A2.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,488 kB
  • sloc: cpp: 17,029; makefile: 11
file content (234 lines) | stat: -rw-r----- 5,417 bytes parent folder | download | duplicates (4)
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
232
233
234
/// NewSpmOutputter.cpp
/**
*/

#include <string>
#include <vector>

#include "SeriesHandler.h"
#include "NewSpmVolume.h"
#include "NewSpmOutputter.h"

using namespace jcs;


///
/**
*/
NewSpmOutputter::NewSpmOutputter() : 
Basic3DOutputter(CreateOptions())
{

}


///
/**
*/
Options
NewSpmOutputter::CreateOptions()
{
  Options options = Get3DOptions();
  options.pathname = "SpmAnalyze";
  return options;
}


///
/**
*/
BasicVolumeFormat* 
NewSpmOutputter::GetOutputVolume(const char* file)
{
  return new NewSpmVolume(file, headerExtension.mb_str(wxConvLocal), rawExtension.mb_str(wxConvLocal));
}


///
/**
*/
int
NewSpmOutputter::ConvertSeries(SeriesHandler* handler)
{
  
  int bits_allocated, pixel_rep = 0;
  handler->Find("BitsAllocated", bits_allocated);
  handler->Find("PixelRepresentation", pixel_rep);

  switch (bits_allocated + pixel_rep) {

  case 8 : {
      SpmConversion<wxUint8> conversion(this, handler);
      conversion.Convert();
      break;
    }
  case 9 : {
      SpmConversion<wxInt8> conversion(this, handler);
      conversion.Convert();
      break;
    }
  case 16 : {
      SpmConversion<wxUint16> conversion(this, handler);
      conversion.Convert();
      break;
    }
  case 17 : {
      SpmConversion<wxInt16> conversion(this, handler);
      conversion.Convert();
      break;
    }
  case 32 : {
      SpmConversion<wxUint32> conversion(this, handler);
      conversion.Convert();
      break;
    }
  case 33 : {
      SpmConversion<wxInt32> conversion(this, handler);
      conversion.Convert();
      break;
    }
  default : {
      wxLogMessage(_("BitsAllocated and PixelRepresentation values (%d, %d) not supported."), bits_allocated, pixel_rep);
    }
  }

  return 1;
}


///
/**
*/
template <class T>
SpmConversion<T>::SpmConversion(Basic3DOutputter* outputter, SeriesHandler* handler)
: Basic3DConversion<T>(outputter, handler)
{
  mHeader = new NewSpmHeader();
}


///
/**
*/
template <class T>
SpmConversion<T>::~SpmConversion()
{
  delete mHeader;
}


///
/**
*/
template <class T> void
SpmConversion<T>::ProcessSlice(std::vector<T>& slice)
{
  std::reverse(slice.begin(), slice.end());
}


///
/**
*/
template <class T> void
SpmConversion<T>::GetHeaderForSeries()
{
  mHeader->InitHeader();
  std::string s;

  this->mHandler->Find("SeriesDate", s);
  s.copy(mHeader->hist.exp_date, sizeof(mHeader->hist.exp_date));
  
  this->mHandler->Find("SeriesTime", s);
  s.copy(mHeader->hist.exp_time, sizeof(mHeader->hist.exp_time));
  
  this->mHandler->Find("SeriesDescription", s);
  s.copy(mHeader->hist.descrip, sizeof(mHeader->hist.descrip));

  this->mHandler->Find("PatientName", s);
  s.copy(mHeader->hist.patient_id, sizeof(mHeader->hist.patient_id));

  float fvar;
  this->mHandler->Find("RepetitionTime", fvar);
  mHeader->dime.pixdim[4] = fvar;

  mHeader->dime.dim[1] = this->mHandler->GetColumns();
  mHeader->dime.dim[2] = this->mHandler->GetRows();
  mHeader->dime.dim[3] = this->mHandler->GetNumberOfSlices();
  // dim[3] recalculated in mConvert

  std::vector<double>voxel_size = this->mHandler->GetVoxelSize();
  mHeader->dime.pixdim[1] = voxel_size[0];
  mHeader->dime.pixdim[2] = voxel_size[1];
  mHeader->dime.pixdim[3] = voxel_size[2];
  // pixdim[3] recalculated in mConvert

  this->mHandler->Find("PhotometricInterpretation", s);
  if (s == "RGB") {
    mHeader->dime.datatype = 128; // DT_RGB
  }
  else {

    this->mHandler->Find("BitsAllocated", mHeader->dime.bitpix);

    // Standard Analyze data types don't include
    // either unsigned 16 bit or signed 8 bit
    switch (mHeader->dime.bitpix) {
    case 16 : 
      mHeader->dime.datatype = 4;	// DT_SIGNED_SHORT 
      break;

    case 8 :
      mHeader->dime.datatype = 8;  // DT_UNSIGNED_CHAR
      break;

      default :
      mHeader->dime.datatype = 0;  // DT_UNKNOWN
    }
  }

  // Write/calculate remaining fields
  mHeader->hk.sizeof_hdr = 348;
  mHeader->dime.dim[0] = 4;
  mHeader->dime.dim[4] = 1; // one volume per file by default

  int dimensionality = this->mOutputter->GetDimensionality(this->mHandler->GetSeriesUid());
  if (dimensionality == 4) {
    mHeader->dime.dim[4] = this->GetNumberOfVolumes();
  }

  mHeader->hk.regular = 'r'; // all volumes same size
  mHeader->dime.vox_offset = 0;
  mHeader->hist.origin[0] = mHeader->dime.dim[1]/2;
  mHeader->hist.origin[1] = mHeader->dime.dim[2]/2;
  mHeader->hist.origin[2] = mHeader->dime.dim[3]/2;
}


///
/**
    \param volPair A volume identifier and a volume.
*/
template <class T> void
SpmConversion<T>::CompleteHeaderForVolume(std::pair<VolId, Volume<T> > volPair)
{
  // Calculate number of slices for this volume
  int n_slices = volPair.second.size();
  mHeader->SetNumberOfSlices(n_slices);
  
  // Calculate voxel size in Z from slice spacing for multi-slice files
  if (n_slices > 1) {
    mHeader->SetSliceSpacing(volPair.second.GetSpacing());
  }

  double slope, intercept;
  if (this->mOutputter->rescale == false &&
      this->mHandler->GetRescaleSlopeAndIntercept(volPair.first, slope, intercept)) {
    mHeader->dime.scale = slope;
    mHeader->dime.intercept = intercept;
  }
  else {
    mHeader->dime.scale = 1;
    mHeader->dime.intercept = 0;
  }
}