1 /*=========================================================================
4 Module: $RCSfile: gdcmSeqEntry.cxx,v $
6 Date: $Date: 2006/04/13 08:05:52 $
7 Version: $Revision: 1.67 $
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"
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)
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 )
93 DocEntry::WriteContent(fp, filetype);
94 for(ListSQItem::iterator cc = Items.begin();
98 (*cc)->WriteContent(fp, filetype);
101 // we force the writting of a Sequence Delimitation item
102 // because we wrote the Sequence as a 'no Length' sequence
103 binary_write(*fp, seq_term_gr);
104 binary_write(*fp, seq_term_el);
105 binary_write(*fp, seq_term_lg);
109 * \brief Compute the full length of the SeqEntry (not only value
110 * length) depending on the VR.
112 uint32_t SeqEntry::ComputeFullLength()
114 uint32_t l = 12; // Tag (4) + VR (explicit) 4 + 4 (length);
115 for(ListSQItem::iterator cc = Items.begin();
119 l += (*cc)->ComputeFullLength();
121 l += 8; // seq_term Tag (4) + seq_term_lg (4)
126 * \brief adds the passed ITEM to the ITEM chained List for this SeQuence.
127 * @param sqItem SQItem to be pushed back in the SeqEntry
128 * @param itemNumber ordinal number of the SQItem
129 * \note NOT end-user intendend method !
131 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
133 // FIXME : SQItemNumber is supposed to be the ordinal number of the SQItem
134 // within the Sequence.
135 // Either only 'push_back' is allowed,
136 // and we just have to do something like SeqEntry::lastNb++
137 // Or we can add (or remove) anywhere, and SQItemNumber will be broken
138 sqItem->SetSQItemNumber(itemNumber);
139 Items.push_back(sqItem);
144 * \brief Remove all SQItem.
146 void SeqEntry::ClearSQItem()
148 for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
154 SeqTerm->Unregister();
159 * \brief Get the first entry while visiting the SeqEntry
160 * \return The first SQItem if found, otherwhise NULL
162 SQItem *SeqEntry::GetFirstSQItem()
164 ItSQItem = Items.begin();
165 if (ItSQItem != Items.end())
171 * \brief Get the next SQItem while visiting the SeqEntry
172 * \note : meaningfull only if GetFirstEntry already called
173 * \return The next SQItem if found, otherwhise NULL
176 SQItem *SeqEntry::GetNextSQItem()
178 gdcmAssertMacro (ItSQItem != Items.end())
181 if (ItSQItem != Items.end())
188 * \brief return a pointer to the SQItem referenced by its ordinal number.
189 * Returns the first item when argument is negative.
190 * Returns the last item when argument is bigger than the total
193 SQItem *SeqEntry::GetSQItem(int nb)
197 return *(Items.begin());
200 for(ListSQItem::iterator cc = Items.begin();
209 return *(Items.end());
213 * \brief returns the number of SQItems within the current Sequence
215 unsigned int SeqEntry::GetNumberOfSQItems()
221 * \brief Sets the Sequence Delimitation Item
222 * \param e Delimitation item
224 void SeqEntry::SetDelimitationItem(DocEntry *e)
229 SeqTerm->Unregister();
237 * \brief Copies all the attributes from an other DocEntry
238 * @param doc entry to copy from
239 * @remarks The contained SQItems a not copied, only referenced
241 void SeqEntry::Copy(DocEntry *doc)
243 // Delete previous SQ items
247 SeqEntry *entry = dynamic_cast<SeqEntry *>(doc);
250 DelimitorMode = entry->DelimitorMode;
251 SQDepthLevel = entry->SQDepthLevel;
253 SeqTerm = entry->SeqTerm;
256 Items = entry->Items;
257 for(ItSQItem = Items.begin();ItSQItem != Items.end(); ++ItSQItem)
259 (*ItSQItem)->Register();
264 //-----------------------------------------------------------------------------
267 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
273 * \brief canonical Printer
275 void SeqEntry::Print( std::ostream &os, std::string const & )
277 // First, Print the Dicom Element itself.
282 if (GetReadLength() == 0)
285 // Then, Print each SQ Item
286 for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
288 (*cc)->SetPrintLevel(PrintLevel);
292 // at end, print the sequence terminator item, if any
296 for ( i = 0; i < SQDepthLevel; i++ )
298 os << " --- " << std::endl;
299 for ( i = 0; i < SQDepthLevel; i++ )
303 SeqTerm->SetPrintLevel(PrintLevel);
310 gdcmWarningMacro(" -------- should have a sequence terminator item");
315 //-----------------------------------------------------------------------------
316 } // end namespace gdcm