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