File: gdcmMrProtocol.cxx

package info (click to toggle)
gdcm 2.8.8-9
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,300 kB
  • sloc: cpp: 189,550; ansic: 70,447; xml: 46,290; python: 3,412; cs: 2,202; java: 1,468; lex: 1,290; sh: 253; php: 128; makefile: 105
file content (222 lines) | stat: -rw-r--r-- 5,914 bytes parent folder | download
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
/*=========================================================================

  Program: GDCM (Grassroots DICOM). A DICOM library

  Copyright (c) 2006-2011 Mathieu Malaterre
  All rights reserved.
  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html 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 "gdcmMrProtocol.h"

#include <map>
#include <string>

namespace gdcm
{

struct MrProtocol::Element {
};

typedef std::map< std::string, std::string > MyMapType;
struct MrProtocol::Internals {
  MyMapType mymap;
  std::string csastr;
  int version;
};

static inline bool starts_with(const std::string& s1, const std::string& s2)
{
  return s2.size() <= s1.size() && s1.compare(0, s2.size(), s2) == 0;
}

MrProtocol::MrProtocol()
{
  Pimpl = new MrProtocol::Internals;
}

bool MrProtocol::Load( const ByteValue * bv, const char * csastr, int version )
{
  if( bv )
  {
    std::string str(bv->GetPointer(), bv->GetLength());
    std::istringstream is(str);
    std::string s;
    Pimpl->version = version;
    if( csastr ) Pimpl->csastr = csastr;
    else Pimpl->csastr = "";
    MyMapType &mymap = Pimpl->mymap;
    mymap.clear();
    // Need to handle both:
    // ### ASCCONV BEGIN ###
    // as well as:
    // ### ASCCONV BEGIN object=MrProtDataImpl@MrProtocolData version=41310008 converter=%MEASCONST%/ConverterList/Prot_Converter.txt ###
    static const char begin[] = "### ASCCONV BEGIN ";
    static const char end[] = "### ASCCONV END ###";
    bool hasstarted = false;
    while( std::getline(is, s ) )
    {
      if( !hasstarted )
      {
        hasstarted = starts_with(s, begin);
        if( hasstarted ) {
          if( version == -1 ) {
            // find version if not specified:
            static const char vers[] = "version=";
            std::string::size_type p = s.find(vers);
            if ( p != std::string::npos) {
              const char *v = s.c_str() + p + sizeof(vers) - 1;
              Pimpl->version = atoi(v);
            }
          }
          continue; // do not insert ASCCONV begin
        }
      }
      if( !hasstarted ) continue;
      if( starts_with(s, end) ) break;
      std::string::size_type pos = s.find( '=' );
      if( pos != std::string::npos )
      {
        std::string sub1 = s.substr(0, pos);
        sub1.erase( sub1.find_last_not_of(" \t") + 1);
        std::string sub2 = s.substr(pos+1); // skip the '=' char
        sub2.erase( 0, sub2.find_first_not_of(" \t"));
        mymap.insert( MyMapType::value_type(sub1, sub2) );
      }
      else
      {
        // ### ASCCONV BEGIN ###
        // ### ASCCONV END ###
      }
    }
  }
  else
  {
    Pimpl->version = 0;
    Pimpl->csastr = "";
    Pimpl->mymap.clear();
    return false;
  }
  return true;
}

MrProtocol::~MrProtocol()
{
  delete Pimpl;
}

int MrProtocol::GetVersion() const
{
  return Pimpl->version;
}

static inline std::string trim(std::string str)
{
  str.erase(0, str.find_first_not_of('"'));
  str.erase(str.find_last_not_of('"')+1);
  return str;
}

void MrProtocol::Print(std::ostream &os) const
{
  {
    os << Pimpl->csastr << " / Version: " << Pimpl->version << std::endl;
    os << std::endl;
    MyMapType &mymap = Pimpl->mymap;
    for( MyMapType::const_iterator it = mymap.begin();
        it != mymap.end(); ++it)
    {
      os << it->first << " : " << trim(std::string(it->second)) << std::endl;
    }
  }
}

const char * MrProtocol::GetMrProtocolByName(const char *name) const
{
  if( name )
  {
    MyMapType &mymap = Pimpl->mymap;
    MyMapType::const_iterator it = mymap.find ( name );
    if( it == mymap.end() ) return NULL;
    return it->second.c_str();
  }
  return NULL;
}

bool MrProtocol::FindMrProtocolByName(const char *name) const
{
  if( name )
  {
    MyMapType &mymap = Pimpl->mymap;
    MyMapType::const_iterator it = mymap.find ( name );
    if( it != mymap.end() ) return true;
  }
  return false;
}

bool MrProtocol::GetSliceArray( MrProtocol::SliceArray & sa ) const
{
  // Technically this is all boilerplate code, since we could infer the type
  // from the first few leters, eg:
  // us -> unsigned char
  // d -> double
  // ...
  sa.Slices.clear();
  static const char saSize[] = "sSliceArray.lSize";
  const char * sizestr = GetMrProtocolByName(saSize);
  if( sizestr == NULL ) return false;
  const int size = atoi( sizestr );
  sa.Slices.resize( size );

  // sSliceArray.asSlice[*].sPosition.dSag
  // sSliceArray.asSlice[*].sPosition.dCor
  // sSliceArray.asSlice[*].sPosition.dTra
  char buf[512];
  static const char templ1[] = "sSliceArray.asSlice[%d].sPosition.%s";
  static const char templ2[] = "sSliceArray.asSlice[%d].sNormal.%s";
  static const char *dir[3] = { "dSag", "dCor", "dTra"};
  for( int i = 0; i < size; ++i )
  {
    Slice & slice = sa.Slices[i];
    {
      double v[3];
      for( int j = 0; j < 3; ++j )
      {
        sprintf( buf, templ1, i, dir[j] );
        const char * valstr = GetMrProtocolByName(buf);
        // when not present this means 0.0
        double val = 0.0;
        if( valstr ) val = atof( valstr );
        v[j] = val;
      }
      Vector3 & pos = slice.Position;
      pos.dSag = v[0];
      pos.dCor = v[1];
      pos.dTra = v[2];
    }
    {
      double v[3];
      for( int j = 0; j < 3; ++j )
      {
        sprintf( buf, templ2, i, dir[j] );
        const char * valstr = GetMrProtocolByName(buf);
        // when not present this means 0.0
        double val = 0.0;
        if( valstr ) val = atof( valstr );
        v[j] = val;
      }
      Vector3 & pos = slice.Normal;
      pos.dSag = v[0];
      pos.dCor = v[1];
      pos.dTra = v[2];
    }
  }

  return true;
}

} // end namespace gdcm