]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
In order to allow to use current version (1.3) of gdcm *and* ITK (that includes
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:11 $
7   Version:   $Revision: 1.68 $
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_NAME_SPACE 
31 {
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34
35 // Constructor / Destructor
36 /**
37  * \brief   Constructor from a given SeqEntry
38  */
39 SeqEntry::SeqEntry( uint16_t group,uint16_t elem ) 
40              : DocEntry(group, elem, "SQ")
41 {
42    Length       = 0;
43    ReadLength   = 0xffffffff;
44    SQDepthLevel = -1;
45
46    DelimitorMode = false;
47    SeqTerm  = NULL;
48 }
49
50 /**
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
54  */
55 SeqEntry::SeqEntry( DocEntry *e, int depth )
56              //: DocEntry( e->GetDictEntry() )
57             : DocEntry( e->GetGroup(), e->GetElement(), "SQ" /*e->GetVR()*/ )
58 {
59    Length       = 0;
60    ReadLength   = 0xffffffff;
61    SQDepthLevel = depth;
62
63    ImplicitVR   = e->IsImplicitVR();
64    Offset       = e->GetOffset();
65    SeqTerm = NULL;
66 }
67
68 /**
69  * \brief   Canonical destructor.
70  */
71 SeqEntry::~SeqEntry()
72 {
73    ClearSQItem();
74 }
75
76 //-----------------------------------------------------------------------------
77 // Public
78 /*
79  * \brief   canonical Writer
80  * @param fp pointer to an already open file
81  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
82  */
83 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype)
84 {
85    uint16_t seq_term_gr = 0xfffe;
86    uint16_t seq_term_el = 0xe0dd;
87    uint32_t seq_term_lg = 0x00000000;
88  
89    // ignore 'Zero length' Sequences
90    if ( GetReadLength() == 0 )
91       return;
92
93    DocEntry::WriteContent(fp, filetype);
94    for(ListSQItem::iterator cc  = Items.begin();
95                             cc != Items.end();
96                           ++cc)
97    {        
98       (*cc)->WriteContent(fp, filetype);
99    }
100    
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);
106 }
107
108 /**
109  * \brief   Compute the full length of the SeqEntry (not only value
110  *          length) depending on the VR.
111  */
112 uint32_t SeqEntry::ComputeFullLength()
113 {
114    uint32_t l = 12; // Tag (4) + VR (explicit) 4 + 4 (length);   
115    for(ListSQItem::iterator cc  = Items.begin();
116                             cc != Items.end();
117                           ++cc)
118    {        
119       l += (*cc)->ComputeFullLength();
120    }   
121    l += 8; // seq_term Tag (4) +  seq_term_lg (4)
122    return l;
123 }
124
125 /**
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 !
130  */
131 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
132 {
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);
140    sqItem->Register();
141 }
142
143 /**
144  * \brief Remove all SQItem.
145  */
146 void SeqEntry::ClearSQItem()
147 {
148    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
149    {
150       (*cc)->Unregister();
151    }
152    if (SeqTerm)
153    {
154       SeqTerm->Unregister();
155    }
156 }
157
158 /**
159  * \brief   Get the first entry while visiting the SeqEntry
160  * \return  The first SQItem if found, otherwhise NULL
161  */ 
162 SQItem *SeqEntry::GetFirstSQItem()
163 {
164    ItSQItem = Items.begin();
165    if (ItSQItem != Items.end())
166       return *ItSQItem;
167    return NULL;
168
169
170 /**
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
174  */
175
176 SQItem *SeqEntry::GetNextSQItem()
177 {
178    gdcmAssertMacro (ItSQItem != Items.end())
179    {
180       ++ItSQItem;
181       if (ItSQItem != Items.end())
182          return *ItSQItem;
183    }
184    return NULL;
185 }
186  
187 /**
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
191  *        item number.
192  */
193 SQItem *SeqEntry::GetSQItem(int nb)
194 {
195    if (nb<0)
196    {
197       return *(Items.begin());
198    }
199    int count = 0 ;
200    for(ListSQItem::iterator cc = Items.begin();
201                            cc != Items.end();
202                            count ++, ++cc)
203    {
204       if (count == nb)
205       {
206          return *cc;
207       }
208    }
209    return *(Items.end());
210 }
211
212 /**
213  * \brief returns the number of SQItems within the current Sequence
214  */
215 unsigned int SeqEntry::GetNumberOfSQItems()
216 {
217    return Items.size();
218 }
219
220 /**
221  * \brief Sets the Sequence Delimitation Item
222  * \param e Delimitation item
223  */
224 void SeqEntry::SetDelimitationItem(DocEntry *e)
225 {
226    if( SeqTerm != e )
227    {
228       if( SeqTerm )
229          SeqTerm->Unregister();
230       SeqTerm = e;
231       if( SeqTerm )
232          SeqTerm->Register();
233    }
234 }
235
236 /**
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
240  */
241 void SeqEntry::Copy(DocEntry *doc)
242 {
243    // Delete previous SQ items
244    ClearSQItem();
245
246    DocEntry::Copy(doc);
247    SeqEntry *entry = dynamic_cast<SeqEntry *>(doc);
248    if ( entry )
249    {
250       DelimitorMode = entry->DelimitorMode;
251       SQDepthLevel = entry->SQDepthLevel;
252
253       SeqTerm = entry->SeqTerm;
254       if(SeqTerm)
255          SeqTerm->Register();
256       Items = entry->Items;
257       for(ItSQItem = Items.begin();ItSQItem != Items.end(); ++ItSQItem)
258       {
259          (*ItSQItem)->Register();
260       }
261    }
262 }
263
264 //-----------------------------------------------------------------------------
265 // Protected
266
267 //-----------------------------------------------------------------------------
268 // Private
269
270 //-----------------------------------------------------------------------------
271 // Print
272 /**
273  * \brief   canonical Printer
274  */
275 void SeqEntry::Print( std::ostream &os, std::string const & )
276 {
277    // First, Print the Dicom Element itself.
278    os << "S ";
279    DocEntry::Print(os);
280    os << std::endl;
281
282    if (GetReadLength() == 0)
283       return;
284
285    // Then, Print each SQ Item   
286    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
287    {
288       (*cc)->SetPrintLevel(PrintLevel);
289       (*cc)->Print(os);   
290    }
291
292    // at end, print the sequence terminator item, if any
293    if (DelimitorMode)
294    {
295       int i;
296       for ( i = 0; i < SQDepthLevel; i++ )
297          os << "   | " ;
298       os << " --- "  << std::endl;
299       for ( i = 0; i < SQDepthLevel; i++ )
300          os << "   | " ;
301       if (SeqTerm != NULL)
302       {
303          SeqTerm->SetPrintLevel(PrintLevel);
304          SeqTerm->Print(os);
305          os << std::endl;
306       } 
307       else 
308       {
309          // fuse
310          gdcmWarningMacro("  -------- should have a sequence terminator item");
311       }
312    }
313 }
314
315 //-----------------------------------------------------------------------------
316 } // end namespace gdcm