]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
Comments
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/03 08:36:19 $
7   Version:   $Revision: 1.61 $
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    // ignore 'Zero length' Sequences
87    if ( GetReadLength() == 0 )
88       return;
89        
90    DocEntry::WriteContent(fp, filetype);
91    for(ListSQItem::iterator cc  = Items.begin();
92                             cc != Items.end();
93                           ++cc)
94    {        
95       (*cc)->WriteContent(fp, filetype);
96    }
97    
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);
103 }
104
105 /**
106  * \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
107  * @param sqItem SQItem to be pushed back in the SeqEntry
108  * @param itemNumber ordinal number of the SQItem
109  *  \note NOT end-user intendend method !
110  */
111 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
112 {
113 // FIXME : SQItemNumber is supposed to be the ordinal number of the SQItem
114 //         within the Sequence.
115 //         Either only 'push_back' is allowed, 
116 //                and we just have to do something like SeqEntry::lastNb++
117 //         Or we can add (or remove) anywhere, and SQItemNumber will be broken
118    sqItem->SetSQItemNumber(itemNumber);
119    Items.push_back(sqItem);
120    sqItem->Register();
121 }
122
123 /**
124  * \brief Remove all SQItem.
125  */
126 void SeqEntry::ClearSQItem()
127 {
128    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
129    {
130       (*cc)->Unregister();
131    }
132    if (SeqTerm)
133    {
134       SeqTerm->Unregister();
135    }
136 }
137
138 /**
139  * \brief   Get the first entry while visiting the SeqEntry
140  * \return  The first SQItem if found, otherwhise NULL
141  */ 
142 SQItem *SeqEntry::GetFirstSQItem()
143 {
144    ItSQItem = Items.begin();
145    if (ItSQItem != Items.end())
146       return *ItSQItem;
147    return NULL;
148
149
150 /**
151  * \brief   Get the next SQItem while visiting the SeqEntry
152  * \note : meaningfull only if GetFirstEntry already called
153  * \return  The next SQItem if found, otherwhise NULL
154  */
155
156 SQItem *SeqEntry::GetNextSQItem()
157 {
158    gdcmAssertMacro (ItSQItem != Items.end())
159    {
160       ++ItSQItem;
161       if (ItSQItem != Items.end())
162          return *ItSQItem;
163    }
164    return NULL;
165 }
166  
167 /**
168  * \brief return a pointer to the SQItem referenced by its ordinal number.
169  *        Returns the first item when argument is negative.
170  *        Returns the  last item when argument is bigger than the total
171  *        item number.
172  */
173 SQItem *SeqEntry::GetSQItem(int nb)
174 {
175    if (nb<0)
176    {
177       return *(Items.begin());
178    }
179    int count = 0 ;
180    for(ListSQItem::iterator cc = Items.begin();
181                            cc != Items.end();
182                            count ++, ++cc)
183    {
184       if (count == nb)
185       {
186          return *cc;
187       }
188    }
189    return *(Items.end());
190 }
191
192 /**
193  * \brief returns the number of SQItems within the current Sequence
194  */
195 unsigned int SeqEntry::GetNumberOfSQItems()
196 {
197    return Items.size();
198 }
199
200 /**
201  * \brief Sets the Sequence Delimitation Item
202  * \param e Delimitation item
203  */
204 void SeqEntry::SetDelimitationItem(DocEntry *e)
205 {
206    if( SeqTerm != e )
207    {
208       if( SeqTerm )
209          SeqTerm->Unregister();
210       SeqTerm = e;
211       if( SeqTerm )
212          SeqTerm->Register();
213    }
214 }
215
216 //-----------------------------------------------------------------------------
217 // Protected
218
219
220 //-----------------------------------------------------------------------------
221 // Private
222
223 //-----------------------------------------------------------------------------
224 // Print
225 /**
226  * \brief   canonical Printer
227  */
228 void SeqEntry::Print( std::ostream &os, std::string const & )
229 {
230    // First, Print the Dicom Element itself.
231    os << "S ";
232    DocEntry::Print(os);
233    os << std::endl;
234
235    if (GetReadLength() == 0)
236       return;
237
238    // Then, Print each SQ Item   
239    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
240    {
241       (*cc)->SetPrintLevel(PrintLevel);
242       (*cc)->Print(os);   
243    }
244
245    // at end, print the sequence terminator item, if any
246    if (DelimitorMode)
247    {
248       int i;
249       for ( i = 0; i < SQDepthLevel; i++ )
250          os << "   | " ;
251       os << " --- "  << std::endl;
252       for ( i = 0; i < SQDepthLevel; i++ )
253          os << "   | " ;
254       if (SeqTerm != NULL)
255       {
256          SeqTerm->SetPrintLevel(PrintLevel);
257          SeqTerm->Print(os);
258          os << std::endl;
259       } 
260       else 
261       {
262          // fuse
263          gdcmWarningMacro("  -------- should have a sequence terminator item");
264       }
265    }
266 }
267
268 //-----------------------------------------------------------------------------
269 } // end namespace gdcm