File: gdcmPresentationDataValue.cxx

package info (click to toggle)
gdcm 2.2.0-14.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 31,828 kB
  • sloc: cpp: 159,587; ansic: 124,519; xml: 44,180; sh: 7,019; python: 3,618; cs: 1,297; lex: 1,290; java: 1,242; tcl: 677; php: 111; makefile: 108
file content (225 lines) | stat: -rw-r--r-- 5,918 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
223
224
225
/*=========================================================================

  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 "gdcmPresentationDataValue.h"
#include "gdcmSwapper.h"
#include "gdcmFile.h"
#include "gdcmAttribute.h"
#include "gdcmCommandDataSet.h"
#include "gdcmPrinter.h"

#include <limits>

namespace gdcm
{
namespace network
{

PresentationDataValue::PresentationDataValue()
{
  MessageHeader = 0;
  PresentationContextID = 0; //MUST BE SET BY THE CALLER!

  // postcondition
  assert(Size() < std::numeric_limits<uint32_t>::max());
  ItemLength = (uint32_t)Size() - 4;
  assert (ItemLength + 4 == Size() );
}

std::istream &PresentationDataValue::Read(std::istream &is)
{
  uint32_t itemlength = ItemLength;
  is.read( (char*)&itemlength, sizeof(ItemLength) );
  SwapperDoOp::SwapArray(&itemlength,1);
  ItemLength = itemlength;
  is.read( (char*)&PresentationContextID, sizeof(PresentationContextID) );

  uint8_t mh;
  is.read( (char*)&mh, 1 );
  MessageHeader = mh;
  if ( MessageHeader > 3 )
    {
    gdcmDebugMacro( "Bizarre MessageHeader: " << MessageHeader );
    }

  assert( ItemLength > 2 );
  uint32_t vl = ItemLength - 2;
  Blob.resize( vl );
  is.read( &Blob[0], vl );

  assert (ItemLength + 4 == Size() );
  return is;
}

std::istream &PresentationDataValue::ReadInto(std::istream &is, std::ostream &os)
{
  uint32_t itemlength = ItemLength;
  is.read( (char*)&itemlength, sizeof(ItemLength) );
  SwapperDoOp::SwapArray(&itemlength,1);
  ItemLength = itemlength;
  is.read( (char*)&PresentationContextID, sizeof(PresentationContextID) );

  uint8_t mh;
  is.read( (char*)&mh, 1 );
  MessageHeader = mh;
  if ( MessageHeader > 3 )
    {
    gdcmDebugMacro( "Bizarre MessageHeader: " << MessageHeader );
    }

  assert( ItemLength > 2 );
  uint32_t vl = ItemLength - 2;
  Blob.resize( vl );
  is.read( &Blob[0], vl );
  os.write( &Blob[0], vl );

  assert (ItemLength + 4 == Size() );
  return is;
}

const std::ostream &PresentationDataValue::Write(std::ostream &os) const
{
  uint32_t copy = ItemLength;
  SwapperDoOp::SwapArray(&copy,1);
  os.write( (char*)&copy, sizeof(ItemLength) );
  assert( os.good() );
  os.write( (char*)&PresentationContextID, sizeof(PresentationContextID) );
  assert( os.good() );

  assert( MessageHeader <= 3 );
  uint8_t t = MessageHeader;
  os.write( (char*)&t, 1 );
  assert( os.good() );

  os.write( Blob.c_str(), Blob.size() );

  assert( Blob.size() == ItemLength - 2 );
  assert (ItemLength + 4 == Size() );

  return os;
}

size_t PresentationDataValue::Size() const
{
  size_t ret = 0;
  ret += sizeof(ItemLength);
  ret += sizeof(PresentationContextID);
  ret += sizeof(MessageHeader); // MESSAGE CONTROL HEADER ENCODING
  ret += Blob.size();

  return ret;
}

void PresentationDataValue::SetBlob(const std::string & partialblob)
{
  assert( !partialblob.empty() );
  Blob = partialblob;
  assert(Size() < std::numeric_limits<uint32_t>::max());
  ItemLength = (uint32_t)Size() - 4;
  assert (ItemLength + 4 == Size() );
}

void PresentationDataValue::SetDataSet(const DataSet & ds)
{
  std::stringstream ss;
  //!!FIXME-- have to make sure that the transfer syntax is known and accounted for!
  ds.Write<ImplicitDataElement,SwapperNoOp>( ss );
  SetBlob( ss.str() );
}

const std::string &PresentationDataValue::GetBlob() const
{
  return Blob;
}

//should only be one data set per chunk of pdvs.  So, only return one; the
//loop that gets the results from the scp will be clever and only enter this function
//when the pdu has its 'last bit' set to true (ie, when all the pdvs can be sent in at once,
//but the are all part of the same data set)
DataSet PresentationDataValue::ConcatenatePDVBlobs(const std::vector<PresentationDataValue>& inPDVs)
{
  //size_t s = inPDVs.size();

  std::string theEntireBuffer;//could do it as streams.  but apparently, std isn't letting me
  std::vector<PresentationDataValue>::const_iterator itor;
  for (itor = inPDVs.begin(); itor < inPDVs.end(); itor++){
    std::string theBlobString = itor->GetBlob();
    theEntireBuffer.insert(theEntireBuffer.end(), theBlobString.begin(), theBlobString.end());
  }

  DataSet outDataSet;

  std::stringstream ss;
  ss.str( theEntireBuffer );

#if 0
  std::ofstream d( "/tmp/debug" );
  d.write( theEntireBuffer.c_str(), theEntireBuffer.size() );
  d.close();
#endif

  outDataSet.Read<ImplicitDataElement,SwapperNoOp>( ss );
  //outDataSet.Read<ExplicitDataElement,SwapperNoOp>( ss );


  return outDataSet;
}

void PresentationDataValue::Print(std::ostream &os) const
{
  os << "ItemLength: " << ItemLength << std::endl;
  os << "PresentationContextID: " << (int)PresentationContextID << std::endl;
  os << "MessageHeader: " << (int)MessageHeader << std::endl;
  std::vector<PresentationDataValue> thePDVs;
  thePDVs.push_back(*this);
  DataSet ds = ConcatenatePDVBlobs(thePDVs);
  Printer thePrinter;
  thePrinter.PrintDataSet(ds, os);
}

void PresentationDataValue::SetCommand(bool inCommand)
{
  if (inCommand)
    {
    MessageHeader |= 1;
    }
  else
    {
    MessageHeader &= ~1;
    }
}

void PresentationDataValue::SetLastFragment(bool inLast)
{
  if (inLast)
    {
    MessageHeader |= 2;
    }
  else
    {
    MessageHeader &= ~2;//set the second field to zero
    }
}

bool PresentationDataValue::GetIsCommand() const
{
  return (MessageHeader & 1) == 1;
}

bool PresentationDataValue::GetIsLastFragment() const
{
  return (MessageHeader & 2) == 2;
}

} // end namespace network
} // end namespace gdcm