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