File: NewBvOutputter.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 (252 lines) | stat: -rw-r----- 5,880 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/// NewBvOutputter.cpp
/**
*/

#include <wx/filename.h>

#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <math.h>

#include "StringConvert.h"
#include "DicomFile.h"
#include "SeriesHandler.h"
#include "Converter.h"
#include "NewBvOutputter.h"

using namespace jcs;


///
/**
 */
NewBvOutputter::NewBvOutputter():
OutputterBase(CreateOptions())
{
  mSaveV16 = mOptions.boolOptions["savev16"];
  mSkip = mOptions.intOptions["skip"];
}


///
/**
 */
NewBvOutputter::~NewBvOutputter()
{
  mOptions.boolOptions["savev16"] = mSaveV16;
  mOptions.intOptions["skip"] = mSkip;
}


///
/**
 */
Options
NewBvOutputter::CreateOptions()
{
  Options options = GetBaseOptions();
  options.pathname = "BrainVoyager";
  options.boolOptions["savev16"] = false;
  options.intOptions["skip"] = 0;
  return options;
}


///
/**
 */
void
NewBvOutputter::RemoveSeries(const std::string& seriesUid)
{
  std::set<OutputList::ListType::key_type> keys;

  OutputList::ListType::iterator it = mOutputList.fileNameList.begin();
  OutputList::ListType::iterator it_end = mOutputList.fileNameList.end();
  for (;it != it_end; ++it) {
    if (it->second.seriesUid == seriesUid) {
      keys.insert(it->first);
    }
  }

  std::set<OutputList::ListType::key_type>::iterator key_it = keys.begin();
  std::set<OutputList::ListType::key_type>::iterator key_it_end = keys.end();
  for (;key_it != key_it_end; ++key_it) {
    mOutputList.fileNameList.erase(*key_it);
  }
}


///
/**
    \param handler A pointer to a SeriesHandler.
    \return 1
*/
int
NewBvOutputter::ConvertSeries(SeriesHandler* handler)
{
  int bits_allocated, pixel_rep = 0;
  handler->Find("BitsAllocated", bits_allocated);
  handler->Find("PixelRepresentation", pixel_rep);

  // Note that mosaic == functional is assumed
  if (handler->IsMosaic()) {
    int nVolumes = handler->GetNumberOfVolumes();

    mConvertFmr(handler);

    switch (bits_allocated + pixel_rep) {
    case 8 : mConvertStc<wxUint8>(handler);
      break;
    case 9 : mConvertStc<wxInt8>(handler);
      break;
    case 16 : mConvertStc<wxUint16>(handler);
      break;
    case 17 : mConvertStc<wxInt16>(handler);
      break;
    case 32 : mConvertStc<wxUint32>(handler);
      break;
    case 33 : mConvertStc<wxInt32>(handler);
      break;
      default : mConvertStc<wxUint16>(handler);
    }
  }
  else {

    switch (bits_allocated + pixel_rep) {
    case 8 : mConvertAnat<wxUint8>(handler);
      break;
    case 9 : mConvertAnat<wxInt8>(handler);
      break;
    case 16 : mConvertAnat<wxUint16>(handler);
      break;
    case 17 : mConvertAnat<wxInt16>(handler);
      break;
    case 32 : mConvertAnat<wxUint32>(handler);
      break;
    case 33 : mConvertAnat<wxInt32>(handler);
      break;
      default : mConvertAnat<wxUint16>(handler);
    }
  }

  return 1;
}



///
/**
 */
void
NewBvOutputter::mConvertFmr(SeriesHandler* handler)
{
  wxFileName file = GetFileName(handler->GetSeriesUid());
  wxFileName::Mkdir(file.GetPath(wxPATH_GET_VOLUME), 0777, wxPATH_MKDIR_FULL);
  file.SetExt(_T(""));
  std::string fname(static_cast<const char*>(file.GetFullPath()));  //.mb_str(wxConvLocal)));

  FmrHeader header;
  header.volumes = handler->GetNumberOfVolumes() - mSkip;
  header.slices = handler->GetNumberOfSlices();
  header.skipVolumes = mSkip;
  header.prefix = fname + "_";
  handler->Find("RepetitionTime", header.tr);

  header.interSliceT = header.tr/header.slices;

  header.columns = handler->GetColumns();
  header.rows = handler->GetRows();
  std::vector<double> res = handler->GetVoxelSize();
  header.IpResX = static_cast<float>(res[0]);
  header.IpResY = static_cast<float>(res[1]);

  int layout = static_cast<int>(sqrtf(header.slices));
  if ((layout * layout) < header.slices) {
    ++layout;
  }
  header.layoutColumns = layout;
  header.layoutRows = layout;

  handler->Find("SliceThickness", header.sliceThickness);
  header.sliceGap = res[2] - header.sliceThickness;
  if (header.sliceGap < 0) {
    header.sliceGap = 0.0;
  }

  BvFmr fmr_file(fname);
  fmr_file.WriteFmr(header);

}


///
/**
 */
void
NewBvOutputter::UpdateOutputForSeries(SeriesHandler* handler)
{
  std::string series_uid(handler->GetSeriesUid());
  RemoveSeries(series_uid);
  
  ImageFileName name;
  name.seriesUid = series_uid;
  name.SetPrefix(GenerateDefaultPrefix(handler));

  // fill in default dirs
  FillInDefaultDirs(name, handler);

  std::string output_file_uid = series_uid;

  //Currently we assume that mosaics == BOLD images....
  if (handler->IsMosaic()) {

    name.SetExt("fmr");
    mOutputList.fileNameList.insert(make_pair(output_file_uid, name));
    name.SetExt("stc");
    int n_slices = handler->GetNumberOfSlices();
    int ndigits = itos(n_slices).size();
    for (int i = 1; i <= n_slices; ++i) {
      std::string postfix = "_";
      postfix.append(itos(i, ndigits));
      name.SetPostfix((postfix));
      mOutputList.fileNameList.insert(make_pair(output_file_uid + postfix, name));
    }
  }
  else {
    name.SetExt("vmr");
    mOutputList.fileNameList.insert(make_pair(output_file_uid, name));
    if (mSaveV16) {
      name.SetExt("v16");
      mOutputList.fileNameList.insert(make_pair(output_file_uid, name));
    }
  }
}


///
/**
 */
void
NewBvOutputter::SetOption(const std::string& name, int value)
{
  OutputterBase::SetOption(name, value);
  if (name.find("skip") != std::string::npos) {
    mSkip = value;
  }
}


///
/**
 */
void
NewBvOutputter::SetOption(const std::string& name, bool value)
{
  OutputterBase::SetOption(name, value);
  if (name.find("v16") != std::string::npos) {
    mSaveV16 = value;
  }
}