1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/08/01 00:59:21 $
7 Version: $Revision: 1.22 $
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 )
36 : gdcmDocEntrySet(depthLevel)
38 SQDepthLevel = depthLevel +1;
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 for (ListDocEntry::iterator i = docEntries.begin();
101 i != docEntries.end();
104 // Item Delimitor Item IS the last one of a 'no length' SQItem
105 // (when it exists) we don't write it right now
106 // It will be written outside, because ALL the SQItems are written
108 if ( (*i)->isItemDelimitor() )
113 // Fix in order to make some MR PHILIPS images e-film readable
114 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
115 // we just *always* ignore spurious fffe|0000 tag !
116 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0x0000 )
121 // It's up to the gdcmDocEntry Writter to write the SQItem begin element
122 // (fffe|e000) as a 'no length' one
123 (*i)->Write(fp, filetype);
127 //-----------------------------------------------------------------------------
130 * \brief adds any Entry (Dicom Element) to the Sequence Item
132 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
134 docEntries.push_back(entry);
135 //TODO : check if it worked
140 * \brief Sets Entry (Dicom Element) value of an element,
141 * specified by it's tag (Group, Number)
142 * and the length, too ... inside a SQ Item
143 * If the Element is not found, it's just created !
144 * \warning we suppose, right now, the element belongs to a Public Group
146 * @param val string value to set
147 * @param group Group number of the searched tag.
148 * @param element Element number of the searched tag.
149 * @return true if element was found or created successfully
152 bool gdcmSQItem::SetEntryByNumber(std::string val,uint16_t group,
155 for(ListDocEntry::iterator i = docEntries.begin(); i != docEntries.end(); ++i)
157 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
162 if ( ( group < (*i)->GetGroup() )
163 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
165 // instead of ReplaceOrCreateByNumber
166 // that is a method of gdcmDocument :-(
167 gdcmValEntry* entry = 0;
168 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
170 if ( ! PtagHT->count(key))
172 // we assume a Public Dictionnary *is* loaded
173 gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
174 // if the invoked (group,elem) doesn't exist inside the Dictionary
175 // we create a VirtualDictEntry
176 gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group,
178 if (dictEntry == NULL)
181 gdcmGlobal::GetDicts()->NewVirtualDictEntry(group, element,
184 // we assume the constructor didn't fail
185 entry = new gdcmValEntry(dictEntry);
188 /// better we don't assume too much !
189 /// gdcmSQItem is now used to describe any DICOMDIR related object
193 gdcmDocEntry* foundEntry = PtagHT->find(key)->second;
194 entry = dynamic_cast<gdcmValEntry*>(foundEntry);
197 dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
198 " contains non gdcmValEntry occurences");
203 entry->SetValue(val);
205 entry->SetLength(val.length());
206 docEntries.insert(i,entry);
210 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
212 if ( gdcmValEntry* entry = dynamic_cast<gdcmValEntry*>(*i) )
214 entry->SetValue(val);
216 (*i)->SetLength(val.length());
222 //-----------------------------------------------------------------------------
227 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
230 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
232 for(ListDocEntry::iterator i = docEntries.begin();
233 i != docEntries.end(); ++i)
235 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
244 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
248 std::string gdcmSQItem::GetEntryByNumber(uint16_t group, uint16_t element)
250 for(ListDocEntry::iterator i = docEntries.begin();
251 i != docEntries.end(); ++i)
253 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
255 return ((gdcmValEntry *)(*i))->GetValue(); //FIXME
260 //-----------------------------------------------------------------------------
264 //-----------------------------------------------------------------------------