]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
* gdcmDocEntrySet::SQDepthLevel and gdcmDocEntrySet::BaseTagKey attributes
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/09/16 19:21:57 $
7   Version:   $Revision: 1.26 $
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.htm 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 "gdcmUtil.h"
25
26 #include "gdcmDebug.h"
27
28
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31 /**
32  * \ingroup gdcmSQItem
33  * \brief   Constructor from a given gdcmSQItem
34  */
35 gdcmSQItem::gdcmSQItem(int depthLevel ) 
36           : gdcmDocEntrySet( )
37 {
38    SQDepthLevel = depthLevel;
39 }
40
41 /**
42  * \brief   Canonical destructor.
43  */
44 gdcmSQItem::~gdcmSQItem() 
45 {
46    for(ListDocEntry::iterator cc = docEntries.begin();
47        cc != docEntries.end();
48        ++cc)
49    {
50       delete (*cc);
51    }
52    docEntries.clear();
53 }
54
55 //-----------------------------------------------------------------------------
56 // Print
57 /*
58  * \brief   canonical Printer
59  */
60  void gdcmSQItem::Print(std::ostream & os)
61  {
62    std::ostringstream s;
63
64    if (SQDepthLevel > 0)
65    {
66       for (int i = 0; i < SQDepthLevel; ++i)
67       {
68          s << "   | " ;
69       }
70    }
71    std::cout << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
72    for (ListDocEntry::iterator i  = docEntries.begin();
73                                i != docEntries.end();
74                              ++i)
75    {
76       gdcmDocEntry* Entry = *i;
77       bool PrintEndLine = true;
78
79       os << s.str();
80       Entry->SetPrintLevel(2);
81       Entry->Print(os); 
82       if ( gdcmSeqEntry* SeqEntry = dynamic_cast<gdcmSeqEntry*>(Entry) )
83       {
84          (void)SeqEntry;  //not used
85          PrintEndLine = false;
86       }
87       if (PrintEndLine)
88       {
89          os << std::endl;
90       }
91    } 
92 }
93
94 /*
95  * \ingroup gdcmSQItem
96  * \brief   canonical Writer
97  */
98 void gdcmSQItem::Write(FILE *fp,FileType filetype)
99 {
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    fwrite(&item[0],8,1,fp);  // fffe e000 ffff ffff 
106      
107    for (ListDocEntry::iterator i = docEntries.begin();  
108         i != docEntries.end();
109         ++i)
110    {   
111       // we skip delimitors (start and end one) because 
112       // we force them as 'no length'
113       if ( (*i)->GetGroup() == 0xfffe )
114       {
115          continue;
116       }
117
118       // Fix in order to make some MR PHILIPS images e-film readable
119       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
120       // we just *always* ignore spurious fffe|0000 tag ! 
121       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0x0000 )
122       {
123          break; // FIXME : continue; ?!?
124       }
125
126       (*i)->Write(fp, filetype);
127    }
128       
129     //we force the writting of an 'Item Delimitation' item
130     // because we wrote the Item as a 'no Length' item
131    fwrite(&itemt[0],8,1,fp);  // fffe e000 ffff ffff 
132
133 }
134
135 //-----------------------------------------------------------------------------
136 // Public
137 /**
138  * \brief   adds any Entry (Dicom Element) to the Sequence Item
139  */
140 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
141 {
142    docEntries.push_back(entry);
143    //TODO : check if it worked
144    return true;
145 }   
146
147 /**
148  * \brief   Sets Entry (Dicom Element) value of an element,
149  *          specified by it's tag (Group, Number) 
150  *          and the length, too ...  inside a SQ Item
151  *          If the Element is not found, it's just created !
152  * \warning we suppose, right now, the element belongs to a Public Group
153  *          (NOT a shadow one)       
154  * @param   val string value to set
155  * @param   group Group number of the searched tag.
156  * @param   element Element number of the searched tag.
157  * @return  true if element was found or created successfully
158  */
159
160 bool gdcmSQItem::SetEntryByNumber(std::string val,uint16_t group, 
161                                   uint16_t element)
162 {
163    for(ListDocEntry::iterator i = docEntries.begin(); i != docEntries.end(); ++i)
164    { 
165       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
166       {
167          continue;
168       }
169
170       if (  ( group   < (*i)->GetGroup() )
171           ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
172       {
173          // instead of ReplaceOrCreateByNumber 
174          // that is a method of gdcmDocument :-( 
175          gdcmValEntry* entry = 0;
176          gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
177
178          if ( ! PtagHT->count(key))
179          {
180             // we assume a Public Dictionnary *is* loaded
181             gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
182             // if the invoked (group,elem) doesn't exist inside the Dictionary
183             // we create a VirtualDictEntry
184             gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group,
185                                                                      element);
186             if (dictEntry == NULL)
187             {
188                dictEntry = 
189                   gdcmGlobal::GetDicts()->NewVirtualDictEntry(group, element,
190                                                               "UN", "??", "??");
191             } 
192             // we assume the constructor didn't fail
193             entry = new gdcmValEntry(dictEntry);
194             /// \todo
195             /// ----
196             /// better we don't assume too much !
197             /// gdcmSQItem is now used to describe any DICOMDIR related object
198          }
199          else
200          {
201             gdcmDocEntry* foundEntry = PtagHT->find(key)->second;
202             entry = dynamic_cast<gdcmValEntry*>(foundEntry);
203             if (!entry)
204             {
205                dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
206                               " contains non gdcmValEntry occurences");
207             }
208          }
209          if (entry)
210          {
211             entry->SetValue(val); 
212          }
213          entry->SetLength(val.length());
214          docEntries.insert(i,entry);
215
216          return true;
217       }   
218       if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
219       {
220          if ( gdcmValEntry* entry = dynamic_cast<gdcmValEntry*>(*i) )
221          {
222             entry->SetValue(val);
223          }
224          (*i)->SetLength(val.length()); 
225          return true;    
226       }
227    }
228    return false;
229 }
230 //-----------------------------------------------------------------------------
231 // Protected
232
233
234 /**
235  * \brief   Gets a Dicom Element inside a SQ Item Entry, by number
236  * @return
237  */
238 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
239 {
240    for(ListDocEntry::iterator i = docEntries.begin();
241                               i != docEntries.end(); ++i)
242    {
243       if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
244       {
245          return *i;
246       }
247    }
248    return 0;
249 }
250
251 /**
252  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by number
253  * @return
254  */ 
255
256 std::string gdcmSQItem::GetEntryByNumber(uint16_t group, uint16_t element)
257 {
258    for(ListDocEntry::iterator i = docEntries.begin();
259                               i != docEntries.end(); ++i)
260    {
261       if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
262       {
263          return ((gdcmValEntry *)(*i))->GetValue();   //FIXME
264       }
265    }
266    return GDCM_UNFOUND;
267 }
268 //-----------------------------------------------------------------------------
269 // Private
270
271
272 //-----------------------------------------------------------------------------