]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
0c1cb641eb58b267eb649b2dce72734c809eb74f
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/25 11:11:59 $
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 "gdcmSQItem.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmGlobal.h"
24 #include "gdcmDictSet.h"
25 #include "gdcmUtil.h"
26 #include "gdcmDebug.h"
27
28 #include <fstream>
29
30 namespace gdcm 
31 {
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \brief   Constructor from a given SQItem
36  */
37 SQItem::SQItem(int depthLevel ) 
38           : DocEntrySet( )
39 {
40    SQDepthLevel = depthLevel;
41    SQItemNumber = 0;
42 }
43
44 /**
45  * \brief   Canonical destructor.
46  */
47 SQItem::~SQItem() 
48 {
49    ClearEntry();
50 }
51
52 //-----------------------------------------------------------------------------
53 // Print
54 /*
55  * \brief   canonical Printer
56  * @param os     Stream to print to. 
57  * @param indent Indentation string to be prepended during printing.
58  */
59 void SQItem::Print(std::ostream &os, std::string const &)
60 {
61    std::ostringstream s;
62
63    if (SQDepthLevel > 0)
64    {
65       for (int i = 0; i < SQDepthLevel; ++i)
66       {
67          s << "   | " ;
68       }
69    }
70    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
71    for (ListDocEntry::iterator i  = DocEntries.begin();
72                                i != DocEntries.end();
73                              ++i)
74    {
75       DocEntry *Entry = *i;
76       bool PrintEndLine = true;
77
78       os << s.str();
79       Entry->SetPrintLevel(PrintLevel);
80       Entry->Print(os); 
81       if ( dynamic_cast<SeqEntry*>(Entry) )
82       {
83          PrintEndLine = false;
84       }
85       if (PrintEndLine)
86       {
87          os << std::endl;
88       }
89    } 
90 }
91
92 /*
93  * \brief   canonical Writer
94  * @param fp     file pointer to an already open file. 
95  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
96  */
97 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
98 {
99    int j;
100    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
101    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
102
103     //we force the writting of an 'Item' Start Element
104     // because we want to write the Item as a 'no Length' item
105    for(j=0;j<4;++j)
106    {
107       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
108    }
109      
110    for (ListDocEntry::iterator it = DocEntries.begin();  
111                               it != DocEntries.end();
112                              ++it)
113    {   
114       // we skip delimitors (start and end one) because 
115       // we force them as 'no length'
116       if ( (*it)->GetGroup() == 0xfffe )
117       {
118          continue;
119       }
120
121       // Fix in order to make some MR PHILIPS images e-film readable
122       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
123       // we just *always* ignore spurious fffe|0000 tag ! 
124       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
125       {
126          break; // FIXME : continue; ?!?
127       }
128
129       (*it)->WriteContent(fp, filetype);
130    }
131       
132     //we force the writting of an 'Item Delimitation' item
133     // because we wrote the Item as a 'no Length' item
134    for(j=0;j<4;++j)
135    {
136       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
137    }
138  
139 }
140
141 //-----------------------------------------------------------------------------
142 // Public
143 /**
144  * \brief  Remove all entry in the Sequence Item 
145  */
146 void SQItem::ClearEntry()
147 {
148    for(ListDocEntry::iterator cc = DocEntries.begin();
149                              cc != DocEntries.end();
150                              ++cc)
151    {
152       delete *cc;
153    }
154    DocEntries.clear();
155 }
156
157 /**
158  * \brief   adds any Entry (Dicom Element) to the Sequence Item
159  * @param entry Entry to add
160  */
161 bool SQItem::AddEntry(DocEntry *entry)
162 {
163    DocEntries.push_back(entry);
164    //TODO : check if it worked
165    return true;
166 }   
167
168 /**
169  * \brief   Sets Entry (Dicom Element) value of an element,
170  *          specified by it's tag (Group, Number) 
171  *          and the length, too ...  inside a SQ Item
172  *          If the Element is not found, it's just created !
173  * \warning we suppose, right now, the element belongs to a Public Group
174  *          (NOT a shadow one)       
175  * @param   val string value to set
176  * @param   group  Group number of the searched tag.
177  * @param   elem Element number of the searched tag.
178  * @return  true if element was found or created successfully
179  */
180
181 bool SQItem::SetEntryValue(std::string const &val, uint16_t group, 
182                       uint16_t elem)
183 {
184    for(ListDocEntry::iterator i = DocEntries.begin(); 
185                               i != DocEntries.end(); 
186                             ++i)
187    { 
188       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
189       {
190          continue;
191       }
192
193       if (  ( group  < (*i)->GetGroup() )
194           ||( group == (*i)->GetGroup() && elem < (*i)->GetElement()) )
195       {
196          // instead of Insert, that is a method of Document :-( 
197          ValEntry* entry = 0;
198          TagKey key = DictEntry::TranslateToKey(group, elem);
199
200          // we assume a Public Dictionary *is* loaded
201          Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
202          // if the invoked (group,elem) doesn't exist inside the Dictionary
203          // we create a VirtualDictEntry
204          DictEntry *dictEntry = pubDict->GetEntry(group, elem);
205          if (dictEntry == NULL)
206          {
207             dictEntry = 
208                Global::GetDicts()->NewVirtualDictEntry(group, elem,
209                                                        "UN", GDCM_UNKNOWN, 
210                                                         GDCM_UNKNOWN);
211          } 
212          // we assume the constructor didn't fail
213          entry = new ValEntry(dictEntry);
214          if (entry)
215          {
216             entry->SetValue(val); 
217          }
218          DocEntries.insert(i,entry);
219
220          return true;
221       }   
222       if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
223       {
224          if ( ValEntry *entry = dynamic_cast<ValEntry*>(*i) )
225          {
226             entry->SetValue(val);
227          }
228          return true;    
229       }
230    }
231    return false;
232 }
233
234 /**
235  * \brief   Clear the std::list from given entry AND delete the entry.
236  * @param   entryToRemove Entry to remove AND delete.
237  * @return true if the entry was found and removed; false otherwise
238  */
239 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
240 {
241    for(ListDocEntry::iterator it = DocEntries.begin();
242        it != DocEntries.end();
243        ++it)
244    {
245       if( *it == entryToRemove)
246       {
247          DocEntries.erase(it);
248          gdcmVerboseMacro( "One element erased: " << entryToRemove->GetKey() );
249          delete entryToRemove;
250          return true;
251       }
252    }
253    gdcmVerboseMacro( "Entry not found: " << entryToRemove->GetKey() );
254    return false ;
255 }
256
257 /**
258  * \brief   Clear the std::list from given entry BUT keep the entry.
259  * @param   entryToRemove Entry to remove.
260  * @return true if the entry was found and removed; false otherwise
261  */
262 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
263 {
264    for(ListDocEntry::iterator it = DocEntries.begin();
265        it != DocEntries.end();
266        ++it)
267    {
268       if( *it == entryToRemove)
269       {
270          DocEntries.erase(it);
271          gdcmVerboseMacro( "One element erased, no destroyed: "
272                             << entryToRemove->GetKey() );
273          return true;
274       }
275    }
276                                                                                 
277    gdcmVerboseMacro( "Entry not found:" << entryToRemove->GetKey() );
278    return false ;
279 }
280                                                                                 
281 /**
282  * \brief   Get the first Dicom entry while visiting the SQItem
283  * \return  The first DocEntry if found, otherwhise 0
284  */
285 DocEntry * SQItem::GetFirstEntry()
286 {
287    ItDocEntries = DocEntries.begin();
288    if (ItDocEntries != DocEntries.end())
289       return *ItDocEntries;
290    return 0;   
291 }
292                                                                                 
293 /**
294  * \brief   Get the next Dicom entry while visiting the chained list
295  * \return  The next DocEntry if found, otherwhise NULL
296  */
297 DocEntry *SQItem::GetNextEntry()
298 {
299   // gdcmAssertMacro (ItDocEntries != DocEntries.end());
300    {
301       ++ItDocEntries;
302       if (ItDocEntries != DocEntries.end())
303          return  *ItDocEntries;
304       return NULL;
305    }
306 }
307
308 //-----------------------------------------------------------------------------
309 // Protected
310 /**
311  * \brief   Gets a Dicom Element inside a SQ Item Entry
312  * @param   group Group number of the Entry
313  * @param   elem  Element number of the Entry
314  * @return Entry whose (group,elem) was passed. 0 if not found
315  */
316 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
317 {
318    for(ListDocEntry::iterator i = DocEntries.begin();
319                               i != DocEntries.end(); ++i)
320    {
321       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
322       {
323          return *i;
324       }
325    }
326    return 0;
327 }
328
329 /**
330  * \brief   Gets a Dicom Element inside a SQ Item Entry
331  * @param   group Group number of the Entry
332  * @param   elem  Element number of the Entry
333  * @return Entry whose (group,elem) was passed. 0 if not found
334  */
335 ValEntry* SQItem::GetValEntry(uint16_t group, uint16_t elem)
336 {
337    DocEntry *d = GetDocEntry(group, elem);
338    if ( ValEntry *e = dynamic_cast<ValEntry*>(d) )
339       return e;
340    return 0;
341 }
342
343 /**
344  * \brief   Gets a Dicom Element inside a SQ Item Entry
345  * @param   group   Group number of the Entry
346  * @param   elem  Element number of the Entry
347  * @return Entry whose (group,elem) was passed. 0 if not found
348  */
349 BinEntry* SQItem::GetBinEntry(uint16_t group, uint16_t elem)
350 {
351    DocEntry *d = GetDocEntry(group, elem);
352    if ( BinEntry *e = dynamic_cast<BinEntry*>(d) )
353       return e;
354    return 0;
355 }
356
357 /**
358  * \brief   Gets a Dicom Element inside a SQ Item Entry
359  * @param   group   Group number of the Entry
360  * @param   elem  Element number of the Entry
361  * @return Entry whose (group,elem) was passed. 0 if not found
362  */
363 SeqEntry* SQItem::GetSeqEntry(uint16_t group, uint16_t elem)
364 {
365    DocEntry *d = GetDocEntry(group, elem);
366    if ( SeqEntry *e = dynamic_cast<SeqEntry*>(d) )
367       return e;
368    return 0;
369 }
370
371
372 /**
373  * \brief   Get the value of a Dicom Element inside a SQ Item Entry
374  * \note : meaningfull only if the required entry is NEITHER a SeqEntry 
375  *                                                   NOR a BinEntry
376  * @param   group   Group number of the Entry
377  * @param   elem  Element number of the Entry 
378  * @return  'string value' of the entry whose (group,elem) was passed.
379  *           GDCM_UNFOUND if not found
380  */ 
381
382 std::string SQItem::GetEntryValue(uint16_t group, uint16_t elem)
383 {
384
385 /*
386    DocEntry *e = GetFirstEntry();
387    while (e)
388    {
389       if ( e->GetGroup() == group && e->GetElement() == elem)
390       {
391
392          if (ValEntry *ve = dynamic_cast<ValEntry*>(e))
393             return ve->GetValue();
394       }
395       e = GetNextEntry();
396    }   
397 */
398    for(ListDocEntry::iterator i = DocEntries.begin();
399                               i != DocEntries.end(); ++i)
400    {
401       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
402       {
403          if (ValEntry *ve = dynamic_cast<ValEntry*>(*i))
404            return ve->GetValue();
405       }
406    }
407    return GDCM_UNFOUND;
408 }
409 //-----------------------------------------------------------------------------
410 // Private
411
412
413 //-----------------------------------------------------------------------------
414
415 } // end namespace gdcm