1 /*=========================================================================
4 Module: $RCSfile: gdcmSeqEntry.cxx,v $
6 Date: $Date: 2005/11/07 09:46:36 $
7 Version: $Revision: 1.62 $
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 * \brief Constructor from a given SeqEntry
37 SeqEntry::SeqEntry( DictEntry *e )
41 ReadLength = 0xffffffff;
44 DelimitorMode = false;
49 * \brief Constructor from a given DocEntry
50 * @param e Pointer to existing Doc entry
51 * @param depth depth level of the current Seq entry
53 SeqEntry::SeqEntry( DocEntry *e, int depth )
54 : DocEntry( e->GetDictEntry() )
57 ReadLength = 0xffffffff;
60 ImplicitVR = e->IsImplicitVR();
61 Offset = e->GetOffset();
66 * \brief Canonical destructor.
73 //-----------------------------------------------------------------------------
76 * \brief canonical Writer
77 * @param fp pointer to an already open file
78 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
80 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype)
82 uint16_t seq_term_gr = 0xfffe;
83 uint16_t seq_term_el = 0xe0dd;
84 uint32_t seq_term_lg = 0xffffffff;
86 // ignore 'Zero length' Sequences
87 if ( GetReadLength() == 0 )
90 DocEntry::WriteContent(fp, filetype);
91 for(ListSQItem::iterator cc = Items.begin();
95 (*cc)->WriteContent(fp, filetype);
98 // we force the writting of a Sequence Delimitation item
99 // because we wrote the Sequence as a 'no Length' sequence
100 binary_write(*fp, seq_term_gr);
101 binary_write(*fp, seq_term_el);
102 binary_write(*fp, seq_term_lg);
106 * \brief Compute the full length of the SeqEntry (not only value
107 * length) depending on the VR.
109 uint32_t SeqEntry::ComputeFullLength()
111 uint32_t l = 12; // Tag (4) + VR (explicit) 4 + 4 (length);
112 for(ListSQItem::iterator cc = Items.begin();
116 l += (*cc)->ComputeFullLength();
118 l += 8; // seq_term Tag (4) + seq_term_lg (4)
123 * \brief adds the passed ITEM to the ITEM chained List for this SeQuence.
124 * @param sqItem SQItem to be pushed back in the SeqEntry
125 * @param itemNumber ordinal number of the SQItem
126 * \note NOT end-user intendend method !
128 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
130 // FIXME : SQItemNumber is supposed to be the ordinal number of the SQItem
131 // within the Sequence.
132 // Either only 'push_back' is allowed,
133 // and we just have to do something like SeqEntry::lastNb++
134 // Or we can add (or remove) anywhere, and SQItemNumber will be broken
135 sqItem->SetSQItemNumber(itemNumber);
136 Items.push_back(sqItem);
141 * \brief Remove all SQItem.
143 void SeqEntry::ClearSQItem()
145 for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
151 SeqTerm->Unregister();
156 * \brief Get the first entry while visiting the SeqEntry
157 * \return The first SQItem if found, otherwhise NULL
159 SQItem *SeqEntry::GetFirstSQItem()
161 ItSQItem = Items.begin();
162 if (ItSQItem != Items.end())
168 * \brief Get the next SQItem while visiting the SeqEntry
169 * \note : meaningfull only if GetFirstEntry already called
170 * \return The next SQItem if found, otherwhise NULL
173 SQItem *SeqEntry::GetNextSQItem()
175 gdcmAssertMacro (ItSQItem != Items.end())
178 if (ItSQItem != Items.end())
185 * \brief return a pointer to the SQItem referenced by its ordinal number.
186 * Returns the first item when argument is negative.
187 * Returns the last item when argument is bigger than the total
190 SQItem *SeqEntry::GetSQItem(int nb)
194 return *(Items.begin());
197 for(ListSQItem::iterator cc = Items.begin();
206 return *(Items.end());
210 * \brief returns the number of SQItems within the current Sequence
212 unsigned int SeqEntry::GetNumberOfSQItems()
218 * \brief Sets the Sequence Delimitation Item
219 * \param e Delimitation item
221 void SeqEntry::SetDelimitationItem(DocEntry *e)
226 SeqTerm->Unregister();
233 //-----------------------------------------------------------------------------
237 //-----------------------------------------------------------------------------
240 //-----------------------------------------------------------------------------
243 * \brief canonical Printer
245 void SeqEntry::Print( std::ostream &os, std::string const & )
247 // First, Print the Dicom Element itself.
252 if (GetReadLength() == 0)
255 // Then, Print each SQ Item
256 for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
258 (*cc)->SetPrintLevel(PrintLevel);
262 // at end, print the sequence terminator item, if any
266 for ( i = 0; i < SQDepthLevel; i++ )
268 os << " --- " << std::endl;
269 for ( i = 0; i < SQDepthLevel; i++ )
273 SeqTerm->SetPrintLevel(PrintLevel);
280 gdcmWarningMacro(" -------- should have a sequence terminator item");
285 //-----------------------------------------------------------------------------
286 } // end namespace gdcm