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