GDCM  2.2.0
gdcmFragment.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: GDCM (Grassroots DICOM). A DICOM library
4 
5  Copyright (c) 2006-2011 Mathieu Malaterre
6  All rights reserved.
7  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without even
10  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  PURPOSE. See the above copyright notice for more information.
12 
13 =========================================================================*/
14 #ifndef GDCMFRAGMENT_H
15 #define GDCMFRAGMENT_H
16 
17 #include "gdcmDataElement.h"
18 #include "gdcmByteValue.h"
19 #include "gdcmSmartPointer.h"
20 #include "gdcmParseException.h"
21 
22 namespace gdcm
23 {
24 
25 // Implementation detail:
26 // I think Fragment should be a protected sublclass of DataElement:
27 // looking somewhat like this:
28 /*
29 class GDCM_EXPORT Fragment : protected DataElement
30 {
31 public:
32  using DataElement::GetTag;
33  using DataElement::GetVL;
34  using DataElement::SetByteValue;
35  using DataElement::GetByteValue;
36  using DataElement::GetValue;
37 */
38 // Instead I am only hiding the SetTag member...
39 
44 {
45 //protected:
46 // void SetTag(const Tag &t);
47 public:
48  Fragment() : DataElement(Tag(0xfffe, 0xe000), 0) {}
49  friend std::ostream &operator<<(std::ostream &os, const Fragment &val);
50 
51  VL GetLength() const {
52  assert( !ValueLengthField.IsUndefined() );
53  assert( !ValueField || ValueField->GetLength() == ValueLengthField );
54  return TagField.GetLength() + ValueLengthField.GetLength()
55  + ValueLengthField;
56  }
57 
58  template <typename TSwap>
59  std::istream &Read(std::istream &is)
60  {
61  TagField.Read<TSwap>(is);
62  return ReadValue<TSwap>(is);
63  }
64 
65  template <typename TSwap>
66  std::istream &ReadValue(std::istream &is)
67  {
68  // Superclass
69  const Tag itemStart(0xfffe, 0xe000);
70  const Tag seqDelItem(0xfffe,0xe0dd);
71  if( !is )
72  {
73  // BogusItemStartItemEnd.dcm
74  throw Exception( "Problem" );
75  return is;
76  }
77  if( !ValueLengthField.Read<TSwap>(is) )
78  {
79  // GENESIS_SIGNA-JPEG-CorruptFrag.dcm
80  // JPEG fragment is declared to have 61902, but infact really is only 61901
81  // so we end up reading 0xddff,0x00e0, and VL = 0x0 (1 byte)
82  throw Exception( "Problem" );
83  return is;
84  }
85 #ifdef GDCM_SUPPORT_BROKEN_IMPLEMENTATION
86  if( TagField != itemStart && TagField != seqDelItem )
87  {
88  throw Exception( "Problem" );
89  }
90 #endif
91  // Self
93  bv->SetLength(ValueLengthField);
94  if( !bv->Read<TSwap>(is) )
95  {
96  // Fragment is incomplete, but is a itemStart, let's try to push it anyway...
97  gdcmWarningMacro( "Fragment could not be read" );
98  //bv->SetLength(is.gcount());
99  ValueField = bv;
100  ParseException pe;
101  pe.SetLastElement( *this );
102  throw pe;
103  return is;
104  }
105  ValueField = bv;
106  return is;
107  }
108 
109 
110  template <typename TSwap>
111  std::ostream &Write(std::ostream &os) const {
112  const Tag itemStart(0xfffe, 0xe000);
113  const Tag seqDelItem(0xfffe,0xe0dd);
114  if( !TagField.Write<TSwap>(os) )
115  {
116  assert(0 && "Should not happen");
117  return os;
118  }
119  assert( TagField == itemStart
120  || TagField == seqDelItem );
121  const ByteValue *bv = GetByteValue();
122  // VL
123  // The following piece of code is hard to read in order to support such broken file as:
124  // CompressedLossy.dcm
125  if( IsEmpty() )
126  {
127  //assert( bv );
128  VL zero = 0;
129  if( !zero.Write<TSwap>(os) )
130  {
131  assert(0 && "Should not happen");
132  return os;
133  }
134  }
135  else
136  {
137  assert( ValueLengthField );
138  if( !ValueLengthField.Write<TSwap>(os) )
139  {
140  assert(0 && "Should not happen");
141  return os;
142  }
143  }
144  // Value
145  if( ValueLengthField && bv )
146  {
147  // Self
148  assert( bv );
149  assert( bv->GetLength() == ValueLengthField );
150  if( !bv->Write<TSwap>(os) )
151  {
152  assert(0 && "Should not happen");
153  return os;
154  }
155  }
156  return os;
157  }
158 };
159 //-----------------------------------------------------------------------------
160 inline std::ostream &operator<<(std::ostream &os, const Fragment &val)
161 {
162  os << "Tag: " << val.TagField;
163  os << "\tVL: " << val.ValueLengthField;
164  if( val.ValueField )
165  {
166  os << "\t" << *(val.ValueField);
167  }
168 
169  return os;
170 }
171 
172 } // end namespace gdcm
173 
174 #endif //GDCMFRAGMENT_H

Generated on Wed Jun 13 2012 20:40:37 for GDCM by doxygen 1.8.1
SourceForge.net Logo