1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/09/16 19:21:57 $
7 Version: $Revision: 1.26 $
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.
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.
17 =========================================================================*/
19 #include "gdcmSQItem.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmGlobal.h"
26 #include "gdcmDebug.h"
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
33 * \brief Constructor from a given gdcmSQItem
35 gdcmSQItem::gdcmSQItem(int depthLevel )
38 SQDepthLevel = depthLevel;
42 * \brief Canonical destructor.
44 gdcmSQItem::~gdcmSQItem()
46 for(ListDocEntry::iterator cc = docEntries.begin();
47 cc != docEntries.end();
55 //-----------------------------------------------------------------------------
58 * \brief canonical Printer
60 void gdcmSQItem::Print(std::ostream & os)
66 for (int i = 0; i < SQDepthLevel; ++i)
71 std::cout << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
72 for (ListDocEntry::iterator i = docEntries.begin();
73 i != docEntries.end();
76 gdcmDocEntry* Entry = *i;
77 bool PrintEndLine = true;
80 Entry->SetPrintLevel(2);
82 if ( gdcmSeqEntry* SeqEntry = dynamic_cast<gdcmSeqEntry*>(Entry) )
84 (void)SeqEntry; //not used
96 * \brief canonical Writer
98 void gdcmSQItem::Write(FILE *fp,FileType filetype)
100 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
101 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
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
107 for (ListDocEntry::iterator i = docEntries.begin();
108 i != docEntries.end();
111 // we skip delimitors (start and end one) because
112 // we force them as 'no length'
113 if ( (*i)->GetGroup() == 0xfffe )
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 )
123 break; // FIXME : continue; ?!?
126 (*i)->Write(fp, filetype);
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
135 //-----------------------------------------------------------------------------
138 * \brief adds any Entry (Dicom Element) to the Sequence Item
140 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
142 docEntries.push_back(entry);
143 //TODO : check if it worked
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
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
160 bool gdcmSQItem::SetEntryByNumber(std::string val,uint16_t group,
163 for(ListDocEntry::iterator i = docEntries.begin(); i != docEntries.end(); ++i)
165 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
170 if ( ( group < (*i)->GetGroup() )
171 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
173 // instead of ReplaceOrCreateByNumber
174 // that is a method of gdcmDocument :-(
175 gdcmValEntry* entry = 0;
176 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
178 if ( ! PtagHT->count(key))
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,
186 if (dictEntry == NULL)
189 gdcmGlobal::GetDicts()->NewVirtualDictEntry(group, element,
192 // we assume the constructor didn't fail
193 entry = new gdcmValEntry(dictEntry);
196 /// better we don't assume too much !
197 /// gdcmSQItem is now used to describe any DICOMDIR related object
201 gdcmDocEntry* foundEntry = PtagHT->find(key)->second;
202 entry = dynamic_cast<gdcmValEntry*>(foundEntry);
205 dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
206 " contains non gdcmValEntry occurences");
211 entry->SetValue(val);
213 entry->SetLength(val.length());
214 docEntries.insert(i,entry);
218 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
220 if ( gdcmValEntry* entry = dynamic_cast<gdcmValEntry*>(*i) )
222 entry->SetValue(val);
224 (*i)->SetLength(val.length());
230 //-----------------------------------------------------------------------------
235 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
238 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
240 for(ListDocEntry::iterator i = docEntries.begin();
241 i != docEntries.end(); ++i)
243 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
252 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
256 std::string gdcmSQItem::GetEntryByNumber(uint16_t group, uint16_t element)
258 for(ListDocEntry::iterator i = docEntries.begin();
259 i != docEntries.end(); ++i)
261 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
263 return ((gdcmValEntry *)(*i))->GetValue(); //FIXME
268 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------