]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
In order to allow to use current version (1.3) of gdcm *and* ITK (that includes
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:11 $
7   Version:   $Revision: 1.85 $
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 "gdcmSQItem.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmGlobal.h"
22 #include "gdcmDictSet.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDebug.h"
25
26 #include <fstream>
27
28 namespace GDCM_NAME_SPACE 
29 {
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
32 /**
33  * \brief   Constructor from a given SQItem
34  */
35 SQItem::SQItem(int depthLevel ) 
36           : DocEntrySet( )
37 {
38    SQDepthLevel = depthLevel;
39    SQItemNumber = 0;
40 }
41
42 /**
43  * \brief   Canonical destructor.
44  */
45 SQItem::~SQItem() 
46 {
47    ClearEntry();
48 }
49
50 //-----------------------------------------------------------------------------
51 // Public
52 /*
53  * \brief   canonical Writer
54  * @param fp     file pointer to an already open file. 
55  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
56  */
57 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
58 {
59    int j;
60    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
61    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0x0000, 0x0000 };
62
63     //we force the writting of an 'Item' Start Element
64     // because we want to write the Item as a 'No Length' item
65    for(j=0;j<4;++j)
66    {
67       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
68    }
69      
70    for (ListDocEntry::iterator it = DocEntries.begin();  
71                                it != DocEntries.end();
72                              ++it)
73    {   
74       // we skip delimitors (start and end one) because 
75       // we force them as 'no length'
76       if ( (*it)->GetGroup() == 0xfffe )
77       {
78          continue;
79       }
80
81       // Fix in order to make some MR PHILIPS images e-film readable
82       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
83       // we just *always* ignore spurious fffe|0000 tag ! 
84       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
85       {
86           break; // FIXME : break or continue; ?!?  
87                  // --> makes no difference since the only bugged file we have
88                  // contains 'impossible tag' fffe|0000 in last position !                            
89       }
90
91       (*it)->WriteContent(fp, filetype);
92    }
93       
94     //we force the writting of an 'Item Delimitation' item
95     // because we wrote the Item as a 'no Length' item
96    for(j=0;j<4;++j)
97    {
98       binary_write( *fp, itemt[j]);  // fffe e000 0000 0000
99    } 
100 }
101
102 /**
103  * \brief   Compute the full length of the SQItem (not only value length)
104  *           depending on the VR.
105  */
106 uint32_t SQItem::ComputeFullLength()
107 {
108    uint32_t l = 8;  // Item Starter length
109    for (ListDocEntry::iterator it = DocEntries.begin();  
110                                it != DocEntries.end();
111                              ++it)
112    {   
113       // we skip delimitors (start and end one) because 
114       // we force them as 'no length'
115       if ( (*it)->GetGroup() == 0xfffe )
116       {
117          continue;
118       }
119       l += (*it)->ComputeFullLength();
120    }
121    l += 8; // 'Item Delimitation' item 
122    return l;  
123 }
124
125 /**
126  * \brief   Inserts *in the right place* any Entry (Dicom Element)
127  *          into the Sequence Item
128  * @param entry Entry to add
129  * @return always true 
130  */
131 bool SQItem::AddEntry(DocEntry *entry)
132 {   
133    if (DocEntries.empty() )
134    {
135       DocEntries.push_back(entry);
136       entry->Register();
137       return true;
138    }
139  
140    ListDocEntry::iterator insertSpot;
141    ListDocEntry::iterator it = DocEntries.end();
142    do
143    {
144       it--;
145
146       if ( (*it)->IsItemDelimitor() )
147       {
148          continue;
149       }
150       if ( (*it)->GetGroup() < entry->GetGroup() )
151          break;
152       else
153          if ( (*it)->GetGroup() == entry->GetGroup() &&
154               (*it)->GetElement() < entry->GetElement() )
155             break;
156    } while (it != DocEntries.begin() );
157   
158    ++it;
159    insertSpot = it;
160    DocEntries.insert(insertSpot, entry); 
161    entry->Register();
162    return true;
163 }   
164
165 /**
166  * \brief   Clear the std::list from given entry AND delete the entry.
167  * @param   entryToRemove Entry to remove AND delete.
168  * @return true if the entry was found and removed; false otherwise
169  */
170 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
171 {
172    for(ListDocEntry::iterator it = DocEntries.begin();
173                               it != DocEntries.end();
174                             ++it)
175    {
176       if ( *it == entryToRemove )
177       {
178          DocEntries.erase(it);
179          gdcmDebugMacro( "One element erased: " << entryToRemove->GetKey() );
180          entryToRemove->Unregister();
181          return true;
182       }
183    }
184    gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
185    return false ;
186 }
187
188 /**
189  * \brief  Remove all entry in the Sequence Item 
190  */
191 void SQItem::ClearEntry()
192 {
193    for(ListDocEntry::iterator cc = DocEntries.begin();
194                               cc != DocEntries.end();
195                             ++cc)
196    {
197       (*cc)->Unregister();
198    }
199    DocEntries.clear();
200 }
201
202 /**
203  * \brief   Get the first Dicom entry while visiting the SQItem
204  * \return  The first DocEntry if found, otherwhise 0
205  */
206 DocEntry *SQItem::GetFirstEntry()
207 {
208    ItDocEntries = DocEntries.begin();
209    if ( ItDocEntries != DocEntries.end() )
210       return *ItDocEntries;
211    return 0;   
212 }
213                                                                                 
214 /**
215  * \brief   Get the next Dicom entry while visiting the SQItem
216  * \return  The next DocEntry if found, otherwhise NULL
217  */
218 DocEntry *SQItem::GetNextEntry()
219 {
220    ++ItDocEntries;
221    if ( ItDocEntries != DocEntries.end() )
222       return  *ItDocEntries;
223    return NULL;
224 }
225
226 /**
227  * \brief   Gets a Dicom Element inside a SQ Item Entry
228  * @param   group   Group number of the Entry
229  * @param   elem  Element number of the Entry
230  * @return Entry whose (group,elem) was passed. 0 if not found
231  */
232 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
233 {
234    for(ListDocEntry::iterator i =  DocEntries.begin();
235                               i != DocEntries.end(); 
236                             ++i)
237    {
238       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
239          return *i;
240    }
241    return NULL;
242 }
243
244 /**
245  * \brief Copies all the attributes from an other DocEntrySet 
246  * @param set entry to copy from
247  * @remarks The contained DocEntries a not copied, only referenced
248  */
249 void SQItem::Copy(DocEntrySet *set)
250 {
251    // Remove all previous entries
252    ClearEntry();
253
254    DocEntrySet::Copy(set);
255
256    SQItem *sq = dynamic_cast<SQItem *>(set);
257    if( sq )
258    {
259       SQDepthLevel = sq->SQDepthLevel;
260       SQItemNumber = sq->SQItemNumber;
261
262       DocEntries = sq->DocEntries;
263       for(ItDocEntries = DocEntries.begin();ItDocEntries != DocEntries.end();++ItDocEntries)
264          (*ItDocEntries)->Register();
265    }
266 }
267
268 //-----------------------------------------------------------------------------
269 // Protected
270
271 //-----------------------------------------------------------------------------
272 // Private
273
274 //-----------------------------------------------------------------------------
275 // Print
276 /*
277  * \brief   canonical Printer
278  * @param os     Stream to print to. 
279  * @param indent Indentation string to be prepended during printing.
280  */
281 void SQItem::Print(std::ostream &os, std::string const &)
282 {
283    std::ostringstream s;
284
285    if (SQDepthLevel > 0)
286    {
287       for (int i = 0; i < SQDepthLevel; ++i)
288       {
289          s << "   | " ;
290       }
291    }
292    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
293    for (ListDocEntry::iterator i  = DocEntries.begin();
294                                i != DocEntries.end();
295                              ++i)
296    {
297       DocEntry *Entry = *i;
298       bool PrintEndLine = true;
299
300       os << s.str();
301       Entry->SetPrintLevel(PrintLevel);
302       Entry->Print(os); 
303       if ( dynamic_cast<SeqEntry*>(Entry) )
304       {
305          PrintEndLine = false;
306       }
307       if (PrintEndLine)
308       {
309          os << std::endl;
310       }
311    } 
312 }
313
314 //-----------------------------------------------------------------------------
315 } // end namespace gdcm