]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
* Add the Copy method in all datas
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/29 12:48:47 $
7   Version:   $Revision: 1.63 $
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   Compute the full length of the SeqEntry (not only value
107  *          length) depending on the VR.
108  */
109 uint32_t SeqEntry::ComputeFullLength()
110 {
111    uint32_t l = 12; // Tag (4) + VR (explicit) 4 + 4 (length);   
112    for(ListSQItem::iterator cc  = Items.begin();
113                             cc != Items.end();
114                           ++cc)
115    {        
116       l += (*cc)->ComputeFullLength();
117    }   
118    l += 8; // seq_term Tag (4) +  seq_term_lg (4)
119    return l;
120 }
121
122 /**
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 !
127  */
128 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
129 {
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);
137    sqItem->Register();
138 }
139
140 /**
141  * \brief Remove all SQItem.
142  */
143 void SeqEntry::ClearSQItem()
144 {
145    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
146    {
147       (*cc)->Unregister();
148    }
149    if (SeqTerm)
150    {
151       SeqTerm->Unregister();
152    }
153 }
154
155 /**
156  * \brief   Get the first entry while visiting the SeqEntry
157  * \return  The first SQItem if found, otherwhise NULL
158  */ 
159 SQItem *SeqEntry::GetFirstSQItem()
160 {
161    ItSQItem = Items.begin();
162    if (ItSQItem != Items.end())
163       return *ItSQItem;
164    return NULL;
165
166
167 /**
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
171  */
172
173 SQItem *SeqEntry::GetNextSQItem()
174 {
175    gdcmAssertMacro (ItSQItem != Items.end())
176    {
177       ++ItSQItem;
178       if (ItSQItem != Items.end())
179          return *ItSQItem;
180    }
181    return NULL;
182 }
183  
184 /**
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
188  *        item number.
189  */
190 SQItem *SeqEntry::GetSQItem(int nb)
191 {
192    if (nb<0)
193    {
194       return *(Items.begin());
195    }
196    int count = 0 ;
197    for(ListSQItem::iterator cc = Items.begin();
198                            cc != Items.end();
199                            count ++, ++cc)
200    {
201       if (count == nb)
202       {
203          return *cc;
204       }
205    }
206    return *(Items.end());
207 }
208
209 /**
210  * \brief returns the number of SQItems within the current Sequence
211  */
212 unsigned int SeqEntry::GetNumberOfSQItems()
213 {
214    return Items.size();
215 }
216
217 /**
218  * \brief Sets the Sequence Delimitation Item
219  * \param e Delimitation item
220  */
221 void SeqEntry::SetDelimitationItem(DocEntry *e)
222 {
223    if( SeqTerm != e )
224    {
225       if( SeqTerm )
226          SeqTerm->Unregister();
227       SeqTerm = e;
228       if( SeqTerm )
229          SeqTerm->Register();
230    }
231 }
232
233 /**
234  * \brief Copies all the attributes from an other DocEntry 
235  * @param doc entry to copy from
236  * @remarks The contained SQItems a not copied, only referenced
237  */
238 void SeqEntry::Copy(DocEntry *doc)
239 {
240    // Delete previous SQ items
241    ClearSQItem();
242
243    DocEntry::Copy(doc);
244    SeqEntry *entry = dynamic_cast<SeqEntry *>(doc);
245    if ( entry )
246    {
247       DelimitorMode = entry->DelimitorMode;
248       SQDepthLevel = entry->SQDepthLevel;
249
250       SeqTerm = entry->SeqTerm;
251       if(SeqTerm)
252          SeqTerm->Register();
253       Items = entry->Items;
254       for(ItSQItem = Items.begin();ItSQItem != Items.end(); ++ItSQItem)
255       {
256          (*ItSQItem)->Register();
257       }
258    }
259 }
260
261 //-----------------------------------------------------------------------------
262 // Protected
263
264 //-----------------------------------------------------------------------------
265 // Private
266
267 //-----------------------------------------------------------------------------
268 // Print
269 /**
270  * \brief   canonical Printer
271  */
272 void SeqEntry::Print( std::ostream &os, std::string const & )
273 {
274    // First, Print the Dicom Element itself.
275    os << "S ";
276    DocEntry::Print(os);
277    os << std::endl;
278
279    if (GetReadLength() == 0)
280       return;
281
282    // Then, Print each SQ Item   
283    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
284    {
285       (*cc)->SetPrintLevel(PrintLevel);
286       (*cc)->Print(os);   
287    }
288
289    // at end, print the sequence terminator item, if any
290    if (DelimitorMode)
291    {
292       int i;
293       for ( i = 0; i < SQDepthLevel; i++ )
294          os << "   | " ;
295       os << " --- "  << std::endl;
296       for ( i = 0; i < SQDepthLevel; i++ )
297          os << "   | " ;
298       if (SeqTerm != NULL)
299       {
300          SeqTerm->SetPrintLevel(PrintLevel);
301          SeqTerm->Print(os);
302          os << std::endl;
303       } 
304       else 
305       {
306          // fuse
307          gdcmWarningMacro("  -------- should have a sequence terminator item");
308       }
309    }
310 }
311
312 //-----------------------------------------------------------------------------
313 } // end namespace gdcm