GDCM  2.4.5
gdcmSequenceOfItems.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 
15 #ifndef GDCMSEQUENCEOFITEMS_H
16 #define GDCMSEQUENCEOFITEMS_H
17 
18 #include "gdcmValue.h"
19 #include "gdcmItem.h"
20 
21 #include <vector>
22 #include <cstring> // strcmp
23 
24 namespace gdcm_ns
25 {
26 
40 {
41 public:
42  // Typdefs:
43  typedef std::vector< Item > ItemVector;
44  typedef ItemVector::size_type SizeType;
45  typedef ItemVector::iterator Iterator;
46  typedef ItemVector::const_iterator ConstIterator;
47  Iterator Begin() { return Items.begin(); }
48  Iterator End() { return Items.end(); }
49  ConstIterator Begin() const { return Items.begin(); }
50  ConstIterator End() const { return Items.end(); }
51 
53  SequenceOfItems():SequenceLengthField(0xFFFFFFFF) { }
54  //SequenceOfItems(VL const &vl = 0xFFFFFFFF):SequenceLengthField(vl),NType(type) { }
55 
57  VL GetLength() const { return SequenceLengthField; }
59  void SetLength(VL length) {
60  SequenceLengthField = length;
61  }
63  void SetLengthToUndefined();
65  bool IsUndefinedLength() const {
66  return SequenceLengthField.IsUndefined();
67  }
68 
69  template <typename TDE>
70  VL ComputeLength() const;
71 
73  void Clear();
74 
76  void AddItem(Item const &item);
77 
80  bool RemoveItemByIndex( const SizeType index );
81 
82  SizeType GetNumberOfItems() const { return Items.size(); }
83  void SetNumberOfItems(SizeType n) { Items.resize(n); }
84 
85  /* WARNING: first item is #1 (see DICOM standard)
86  * Each Item shall be implicitly assigned an ordinal position starting with the value 1 for the
87  * first Item in the Sequence, and incremented by 1 with each subsequent Item. The last Item in the
88  * Sequence shall have an ordinal position equal to the number of Items in the Sequence.
89  */
90  const Item &GetItem(SizeType position) const;
91  Item &GetItem(SizeType position);
92 
94  SequenceLengthField = val.SequenceLengthField;
95  Items = val.Items;
96  return *this;
97  }
98 
99  template <typename TDE, typename TSwap>
100  std::istream &Read(std::istream &is, bool readvalues = true)
101  {
102  (void)readvalues;
103  const Tag seqDelItem(0xfffe,0xe0dd);
104  if( SequenceLengthField.IsUndefined() )
105  {
106  Item item;
107  while( item.Read<TDE,TSwap>(is) && item.GetTag() != seqDelItem )
108  {
109  //gdcmDebugMacro( "Item: " << item );
110  assert( item.GetTag() != seqDelItem );
111  Items.push_back( item );
112  item.Clear();
113  }
114  //assert( item.GetTag() == seqDelItem && item.GetVL() == 0 );
115  }
116  else
117  {
118  Item item;
119  VL l = 0;
120  //is.seekg( SequenceLengthField, std::ios::cur ); return is;
121  while( l != SequenceLengthField )
122  {
123  try
124  {
125  item.Read<TDE,TSwap>(is);
126  }
127  catch( Exception &ex )
128  {
129  if( strcmp( ex.GetDescription(), "Changed Length" ) == 0 )
130  {
131  VL newlength = l + item.template GetLength<TDE>();
132  if( newlength > SequenceLengthField )
133  {
134  // BogugsItemAndSequenceLength.dcm
135  gdcmWarningMacro( "SQ length is wrong" );
136  SequenceLengthField = newlength;
137  }
138  }
139  else
140  {
141  throw ex;
142  }
143  }
144 #ifdef GDCM_SUPPORT_BROKEN_IMPLEMENTATION
145  if( item.GetTag() == seqDelItem )
146  {
147  gdcmWarningMacro( "SegDelItem found in defined length Sequence. Skipping" );
148  assert( item.GetVL() == 0 );
149  assert( item.GetNestedDataSet().Size() == 0 );
150  // we need to pay attention that the length of the Sequence of Items will be wrong
151  // this way. Indeed by not adding this item we are changing the size of this sqi
152  }
153  else // Not a seq del item marker
154 #endif
155  {
156  // By design we never load them. If we were to load those attribute
157  // as normal item it would become very complex to convert a sequence
158  // from defined length to undefined length with the risk to write two
159  // seq del marker
160  Items.push_back( item );
161  }
162  l += item.template GetLength<TDE>();
163  if( l > SequenceLengthField )
164  {
165  gdcmDebugMacro( "Found: Length of Item larger than expected" )
166  throw "Length of Item larger than expected";
167  }
168  assert( l <= SequenceLengthField );
169  //std::cerr << "sqi debug len: " << is.tellg() << " " << l << " " << SequenceLengthField << std::endl;
170 #ifdef GDCM_SUPPORT_BROKEN_IMPLEMENTATION
171  // MR_Philips_Intera_No_PrivateSequenceImplicitVR.dcm
172  // (0x2005, 0x1080): for some reason computation of length fails...
173  if( SequenceLengthField == 778 && l == 774 )
174  {
175  gdcmWarningMacro( "PMS: Super bad hack" );
176  SequenceLengthField = l;
177  throw Exception( "Wrong Length" );
178  //l = SequenceLengthField;
179  }
180  // Bug_Philips_ItemTag_3F3F
181  // (0x2005, 0x1080): Because we do not handle fully the bug at the item
182  // level we need to check here too
183  else if ( SequenceLengthField == 444 && l == 3*71 )
184  {
185  // This one is a double bug. Item length is wrong and impact SQ length
186  gdcmWarningMacro( "PMS: Super bad hack" );
187  l = SequenceLengthField;
188  }
189 #endif
190  }
191  assert( l == SequenceLengthField );
192  }
193  return is;
194  }
195 
196  template <typename TDE,typename TSwap>
197  std::ostream const &Write(std::ostream &os) const
198  {
199  typename ItemVector::const_iterator it = Items.begin();
200  for(;it != Items.end(); ++it)
201  {
202  it->Write<TDE,TSwap>(os);
203  }
204  if( SequenceLengthField.IsUndefined() )
205  {
206  // seq del item is not stored, write it !
207  const Tag seqDelItem(0xfffe,0xe0dd);
208  seqDelItem.Write<TSwap>(os);
209  VL zero = 0;
210  zero.Write<TSwap>(os);
211  }
212 
213  return os;
214  }
215 
216 //protected:
217  void Print(std::ostream &os) const {
218  os << "\t(" << SequenceLengthField << ")\n";
219  ItemVector::const_iterator it =
220  Items.begin();
221  for(;it != Items.end(); ++it)
222  {
223  os << " " << *it;
224  }
225  if( SequenceLengthField.IsUndefined() )
226  {
227  const Tag seqDelItem(0xfffe,0xe0dd);
228  VL zero = 0;
229  os << seqDelItem;
230  os << "\t" << zero;
231  }
232  }
233 
235  {
236  return new SequenceOfItems;
237  }
238  bool FindDataElement(const Tag &t) const;
239 
240  bool operator==(const Value &val) const
241  {
242  const SequenceOfItems &sqi = dynamic_cast<const SequenceOfItems&>(val);
243  return SequenceLengthField == sqi.SequenceLengthField &&
244  Items == sqi.Items;
245  }
246 
247 private:
248 public:
252  ItemVector Items;
253 };
254 
255 } // end namespace gdcm_ns
256 
257 #include "gdcmSequenceOfItems.txx"
258 
259 #endif //GDCMSEQUENCEOFITEMS_H
ItemVector::iterator Iterator
Definition: gdcmSequenceOfItems.h:45
Class to represent a Sequence Of Items (value representation : SQ)
Definition: gdcmSequenceOfItems.h:39
void Print(std::ostream &os) const
Definition: gdcmSequenceOfItems.h:217
void SetLength(VL length)
Sets the actual SQ length.
Definition: gdcmSequenceOfItems.h:59
std::istream & Read(std::istream &is)
Definition: gdcmItem.h:97
Class to represent the value of a Data Element.
Definition: gdcmValue.h:31
ItemVector::const_iterator ConstIterator
Definition: gdcmSequenceOfItems.h:46
std::ostream const & Write(std::ostream &os) const
Definition: gdcmSequenceOfItems.h:197
const VL & GetVL() const
Get VL.
Definition: gdcmDataElement.h:74
ConstIterator End() const
Definition: gdcmSequenceOfItems.h:50
#define GDCM_EXPORT
Definition: gdcmWin32.h:34
SequenceOfItems & operator=(const SequenceOfItems &val)
Definition: gdcmSequenceOfItems.h:93
ItemVector Items
Vector of Sequence Items.
Definition: gdcmSequenceOfItems.h:252
#define gdcmWarningMacro(msg)
Warning.
Definition: gdcmTrace.h:141
Value Length.
Definition: gdcmVL.h:29
#define gdcmDebugMacro(msg)
Debug.
Definition: gdcmTrace.h:119
static SmartPointer< SequenceOfItems > New()
Definition: gdcmSequenceOfItems.h:234
const std::ostream & Write(std::ostream &os) const
Write a tag in binary rep.
Definition: gdcmTag.h:169
Class to represent an Item A component of the value of a Data Element that is of Value Representation...
Definition: gdcmItem.h:45
VL GetLength() const
Returns the SQ length, as read from disk.
Definition: gdcmSequenceOfItems.h:57
Iterator Begin()
Definition: gdcmSequenceOfItems.h:47
ItemVector::size_type SizeType
Definition: gdcmSequenceOfItems.h:44
SizeType Size() const
Definition: gdcmDataSet.h:75
const std::ostream & Write(std::ostream &os) const
Definition: gdcmVL.h:99
void SetNumberOfItems(SizeType n)
Definition: gdcmSequenceOfItems.h:83
Class for Smart Pointer.
Definition: gdcmObject.h:26
bool operator==(const Value &val) const
Definition: gdcmSequenceOfItems.h:240
const char * GetDescription() const
Return the Description.
Definition: gdcmException.h:92
const Tag & GetTag() const
Get Tag.
Definition: gdcmDataElement.h:67
ConstIterator Begin() const
Definition: gdcmSequenceOfItems.h:49
VL SequenceLengthField
Total length of the Sequence (or 0xffffffff) if undefined.
Definition: gdcmSequenceOfItems.h:250
bool IsUndefinedLength() const
return if Value Length if of undefined length
Definition: gdcmSequenceOfItems.h:65
std::istream & Read(std::istream &is, bool readvalues=true)
Definition: gdcmSequenceOfItems.h:100
const DataSet & GetNestedDataSet() const
Definition: gdcmItem.h:80
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element). Basically an uint32_t which...
Definition: gdcmTag.h:38
void Clear()
Definition: gdcmItem.h:51
Exception.
Definition: gdcmException.h:43
std::vector< Item > ItemVector
Definition: gdcmSequenceOfItems.h:43
Iterator End()
Definition: gdcmSequenceOfItems.h:48
SequenceOfItems()
constructor (UndefinedLength by default)
Definition: gdcmSequenceOfItems.h:53
SizeType GetNumberOfItems() const
Definition: gdcmSequenceOfItems.h:82

Generated on Fri Sep 25 2015 17:58:24 for GDCM by doxygen 1.8.9.1
SourceForge.net Logo