]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
c0647f9ded94d166f1bcea0f79c146e0d725bd02
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/24 16:00:48 $
7   Version:   $Revision: 1.59 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmSeqEntry.h"
20 #include "gdcmSQItem.h"
21 #include "gdcmTS.h"
22 #include "gdcmGlobal.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDebug.h"
25
26 #include <iostream>
27 #include <iomanip>
28 #include <fstream>
29
30 namespace gdcm 
31 {
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \brief   Constructor from a given SeqEntry
36  */
37 SeqEntry::SeqEntry( DictEntry *e ) 
38              : DocEntry(e)
39 {
40    Length       = 0;
41    ReadLength   = 0xffffffff;
42    SQDepthLevel = -1;
43
44    DelimitorMode = false;
45    SeqTerm  = NULL;
46 }
47
48 /**
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
52  */
53 SeqEntry::SeqEntry( DocEntry *e, int depth )
54              : DocEntry( e->GetDictEntry() )
55 {
56    Length       = 0;
57    ReadLength   = 0xffffffff;
58    SQDepthLevel = depth;
59
60    ImplicitVR   = e->IsImplicitVR();
61    Offset       = e->GetOffset();
62    SeqTerm = NULL;
63 }
64
65 /**
66  * \brief   Canonical destructor.
67  */
68 SeqEntry::~SeqEntry()
69 {
70    ClearSQItem();
71 }
72
73 //-----------------------------------------------------------------------------
74 // Public
75 /*
76  * \brief   canonical Writer
77  * @param fp pointer to an already open file
78  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
79  */
80 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype)
81 {
82    uint16_t seq_term_gr = 0xfffe;
83    uint16_t seq_term_el = 0xe0dd;
84    uint32_t seq_term_lg = 0xffffffff;
85
86    //uint16_t item_term_gr = 0xfffe;
87    //uint16_t item_term_el = 0xe00d;
88    
89    DocEntry::WriteContent(fp, filetype);
90    for(ListSQItem::iterator cc  = Items.begin();
91                             cc != Items.end();
92                           ++cc)
93    {        
94       (*cc)->WriteContent(fp, filetype);
95    }
96    
97    // we force the writting of a Sequence Delimitation item
98    // because we wrote the Sequence as a 'no Length' sequence
99    binary_write(*fp, seq_term_gr);
100    binary_write(*fp, seq_term_el);
101    binary_write(*fp, seq_term_lg);
102 }
103
104 /**
105  * \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
106  * @param sqItem SQItem to be pushed back in the SeqEntry
107  * @param itemNumber ordinal number of the SQItem
108  *  \note NOT end-user intendend method !
109  */
110 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
111 {
112 // FIXME : SQItemNumber is supposed to be the ordinal number of the SQItem
113 //         within the Sequence.
114 //         Either only 'push_back' is allowed, 
115 //                and we just have to do something like SeqEntry::lastNb++
116 //         Or we can add (or remove) anywhere, and SQItemNumber will be broken
117    sqItem->SetSQItemNumber(itemNumber);
118    Items.push_back(sqItem);
119 }
120
121 /**
122  * \brief Remove all SQItem.
123  */
124 void SeqEntry::ClearSQItem()
125 {
126    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
127    {
128       delete *cc;
129    }
130    if (SeqTerm)
131    {
132       SeqTerm->Unregister();
133    }
134 }
135
136 /**
137  * \brief   Get the first entry while visiting the SeqEntry
138  * \return  The first SQItem if found, otherwhise NULL
139  */ 
140 SQItem *SeqEntry::GetFirstSQItem()
141 {
142    ItSQItem = Items.begin();
143    if (ItSQItem != Items.end())
144       return *ItSQItem;
145    return NULL;
146
147
148 /**
149  * \brief   Get the next SQItem while visiting the SeqEntry
150  * \note : meaningfull only if GetFirstEntry already called
151  * \return  The next SQItem if found, otherwhise NULL
152  */
153
154 SQItem *SeqEntry::GetNextSQItem()
155 {
156    gdcmAssertMacro (ItSQItem != Items.end())
157    {
158       ++ItSQItem;
159       if (ItSQItem != Items.end())
160          return *ItSQItem;
161    }
162    return NULL;
163 }
164  
165 /**
166  * \brief return a pointer to the SQItem referenced by its ordinal number.
167  *        Returns the first item when argument is negative.
168  *        Returns the  last item when argument is bigger than the total
169  *        item number.
170  */
171 SQItem *SeqEntry::GetSQItem(int nb)
172 {
173    if (nb<0)
174    {
175       return *(Items.begin());
176    }
177    int count = 0 ;
178    for(ListSQItem::iterator cc = Items.begin();
179                            cc != Items.end();
180                            count ++, ++cc)
181    {
182       if (count == nb)
183       {
184          return *cc;
185       }
186    }
187    return *(Items.end());
188 }
189
190 /**
191  * \brief returns the number of SQItems within the current Sequence
192  */
193 unsigned int SeqEntry::GetNumberOfSQItems()
194 {
195    return Items.size();
196 }
197
198 /**
199  * \brief Sets the Sequence Delimitation Item
200  * \param e Delimitation item
201  */
202 void SeqEntry::SetDelimitationItem(DocEntry *e)
203 {
204    if( SeqTerm != e )
205    {
206       if( SeqTerm )
207          SeqTerm->Unregister();
208       SeqTerm = e;
209       if( SeqTerm )
210          SeqTerm->Register();
211    }
212 }
213
214 //-----------------------------------------------------------------------------
215 // Protected
216
217
218 //-----------------------------------------------------------------------------
219 // Private
220
221 //-----------------------------------------------------------------------------
222 // Print
223 /**
224  * \brief   canonical Printer
225  */
226 void SeqEntry::Print( std::ostream &os, std::string const & )
227 {
228    // First, Print the Dicom Element itself.
229    os << "S ";
230    DocEntry::Print(os);
231    os << std::endl;
232
233    if (GetReadLength() == 0)
234       return;
235
236    // Then, Print each SQ Item   
237    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
238    {
239       (*cc)->SetPrintLevel(PrintLevel);
240       (*cc)->Print(os);   
241    }
242
243    // at end, print the sequence terminator item, if any
244    if (DelimitorMode)
245    {
246       int i;
247       for ( i = 0; i < SQDepthLevel; i++ )
248          os << "   | " ;
249       os << " --- "  << std::endl;
250       for ( i = 0; i < SQDepthLevel; i++ )
251          os << "   | " ;
252       if (SeqTerm != NULL)
253       {
254          SeqTerm->SetPrintLevel(PrintLevel);
255          SeqTerm->Print(os);
256          os << std::endl;
257       } 
258       else 
259       {
260          // fuse
261          gdcmWarningMacro("  -------- should have a sequence terminator item");
262       }
263    }
264 }
265
266 //-----------------------------------------------------------------------------
267 } // end namespace gdcm