1 /*=========================================================================
4 Module: $RCSfile: gdcmSeqEntry.cxx,v $
6 Date: $Date: 2007/10/08 15:20:17 $
7 Version: $Revision: 1.72 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmSeqEntry.h"
20 #include "gdcmSQItem.h"
22 #include "gdcmGlobal.h"
24 #include "gdcmDebug.h"
30 namespace GDCM_NAME_SPACE
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
35 // Constructor / Destructor
37 * \brief Constructor from a given SeqEntry
39 SeqEntry::SeqEntry( uint16_t group,uint16_t elem )
40 : DocEntry(group, elem, "SQ")
43 ReadLength = 0xffffffff;
46 DelimitorMode = false;
51 * \brief Constructor from a given DocEntry
52 * @param e Pointer to existing Doc entry
53 * @param depth depth level of the current Seq entry
55 SeqEntry::SeqEntry( DocEntry *e, int depth )
56 //: DocEntry( e->GetDictEntry() )
57 : DocEntry( e->GetGroup(), e->GetElement(), "SQ" /*e->GetVR()*/ )
60 ReadLength = 0xffffffff;
63 ImplicitVR = e->IsImplicitVR();
64 Offset = e->GetOffset();
69 * \brief Canonical destructor.
76 //-----------------------------------------------------------------------------
79 * \brief canonical Writer
80 * @param fp pointer to an already open file
81 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
83 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype, bool , bool )
85 uint16_t seq_term_gr = 0xfffe;
86 uint16_t seq_term_el = 0xe0dd;
87 uint32_t seq_term_lg = 0x00000000;
89 // ignore 'Zero length' Sequences
90 if ( GetReadLength() == 0 )
92 // false : we are not in MetaElements
93 // true : we are inside a Sequence
94 DocEntry::WriteContent(fp, filetype, false, true);
95 for(ListSQItem::iterator cc = Items.begin();
99 (*cc)->WriteContent(fp, filetype, false, true);
102 // we force the writting of a Sequence Delimitation item
103 // because we wrote the Sequence as a 'no Length' sequence
104 binary_write(*fp, seq_term_gr);
105 binary_write(*fp, seq_term_el);
106 binary_write(*fp, seq_term_lg);
110 * \brief Compute the full length of the SeqEntry (not only value
111 * length) depending on the VR.
113 uint32_t SeqEntry::ComputeFullLength()
115 uint32_t l = 12; // Tag (4) + VR (explicit) 4 + 4 (length);
116 for(ListSQItem::iterator cc = Items.begin();
120 l += (*cc)->ComputeFullLength();
122 l += 8; // seq_term Tag (4) + seq_term_lg (4)
127 * \brief adds the passed ITEM to the ITEM chained List for this SeQuence.
128 * @param sqItem SQItem to be pushed back in the SeqEntry
129 * @param itemNumber ordinal number of the SQItem
130 * \note NOT end-user intendend method !
132 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
134 // FIXME : SQItemNumber is supposed to be the ordinal number of the SQItem
135 // within the Sequence.
136 // Either only 'push_back' is allowed,
137 // and we just have to do something like SeqEntry::lastNb++
138 // Or we can add (or remove) anywhere, and SQItemNumber will be broken
139 sqItem->SetSQItemNumber(itemNumber);
140 Items.push_back(sqItem);
145 * \brief Remove all SQItem.
147 void SeqEntry::ClearSQItem()
149 for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
155 SeqTerm->Unregister();
160 * \brief Get the first entry while visiting the SeqEntry
161 * \return The first SQItem if found, otherwhise NULL
163 SQItem *SeqEntry::GetFirstSQItem()
165 ItSQItem = Items.begin();
166 if (ItSQItem != Items.end())
172 * \brief Get the next SQItem while visiting the SeqEntry
173 * \note : meaningfull only if GetFirstEntry already called
174 * \return The next SQItem if found, otherwhise NULL
177 SQItem *SeqEntry::GetNextSQItem()
179 gdcmAssertMacro (ItSQItem != Items.end())
182 if (ItSQItem != Items.end())
189 * \brief return a pointer to the SQItem referenced by its ordinal number.
190 * Returns the first item when argument is negative.
191 * Returns the last item when argument is bigger than the total
194 SQItem *SeqEntry::GetSQItem(int nb)
198 return *(Items.begin());
201 for(ListSQItem::iterator cc = Items.begin();
210 return *(Items.end());
214 * \brief returns the number of SQItems within the current Sequence
216 unsigned int SeqEntry::GetNumberOfSQItems()
222 * \brief Sets the Sequence Delimitation Item
223 * \param e Delimitation item
225 void SeqEntry::SetDelimitationItem(DocEntry *e)
230 SeqTerm->Unregister();
238 * \brief Copies all the attributes from an other DocEntry
239 * @param doc entry to copy from
240 * @remarks The contained SQItems a not copied, only referenced
242 void SeqEntry::Copy(DocEntry *doc)
244 // Delete previous SQ items
248 SeqEntry *entry = dynamic_cast<SeqEntry *>(doc);
251 DelimitorMode = entry->DelimitorMode;
252 SQDepthLevel = entry->SQDepthLevel;
254 SeqTerm = entry->SeqTerm;
257 Items = entry->Items;
258 for(ItSQItem = Items.begin();ItSQItem != Items.end(); ++ItSQItem)
260 (*ItSQItem)->Register();
265 //-----------------------------------------------------------------------------
268 //-----------------------------------------------------------------------------
271 //-----------------------------------------------------------------------------
274 * \brief canonical Printer
276 void SeqEntry::Print( std::ostream &os, std::string const & )
280 // First, Print the common part (vr [length offset] name).
284 if (GetReadLength() == 0)
287 // Then, Print each SQ Item
288 for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
290 (*cc)->SetPrintLevel(PrintLevel);
294 // at end, print the sequence terminator item, if any
298 for ( i = 0; i < SQDepthLevel; i++ )
300 os << " --- " << std::endl;
301 for ( i = 0; i < SQDepthLevel; i++ )
305 SeqTerm->SetPrintLevel(PrintLevel);
312 gdcmWarningMacro(" -------- should have a sequence terminator item");
317 //-----------------------------------------------------------------------------
318 } // end namespace gdcm