1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/06/25 03:06:38 $
7 Version: $Revision: 1.18 $
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) {
37 SQDepthLevel = depthLevel +1;
41 * \brief Canonical destructor.
43 gdcmSQItem::~gdcmSQItem()
45 for(ListDocEntry::iterator cc = docEntries.begin();
46 cc != docEntries.end();
54 //-----------------------------------------------------------------------------
57 * \brief canonical Printer
59 void gdcmSQItem::Print(std::ostream & os) {
64 for (int i=0;i<SQDepthLevel;i++)
67 std::cout << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
68 for (ListDocEntry::iterator i = docEntries.begin();
69 i != docEntries.end();
72 gdcmDocEntry* Entry = *i;
73 bool PrintEndLine = true;
76 Entry->SetPrintLevel(2);
78 if ( gdcmSeqEntry* SeqEntry = dynamic_cast<gdcmSeqEntry*>(Entry) )
80 (void)SeqEntry; //not used
90 * \brief canonical Writer
92 void gdcmSQItem::Write(FILE *fp,FileType filetype)
94 for (ListDocEntry::iterator i = docEntries.begin();
95 i != docEntries.end();
98 // Item Delimitor Item IS the last one of a 'no length' SQItem
99 // (when it exists) we don't write it right now
100 // It will be written outside, because ALL the SQItems are written
102 if ( (*i)->isItemDelimitor() )
104 if ( ((*i)->GetGroup() == 0xfffe) && ((*i)->GetElement() == 0x0000) )
105 // Fix in order to make some MR PHILIPS images e-film readable
106 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
107 // we just *always* ignore spurious fffe|0000 tag !
109 // It's up to the gdcmDocEntry Writter to write the SQItem begin element
110 // (fffe|e000) as a 'no length' one
111 (*i)->Write(fp, filetype);
115 //-----------------------------------------------------------------------------
118 * \brief adds any Entry (Dicom Element) to the Sequence Item
120 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
122 docEntries.push_back(entry);
123 //TODO : check if it worked
128 * \brief Sets Entry (Dicom Element) value of an element,
129 * specified by it's tag (Group, Number)
130 * and the length, too ... inside a SQ Item
131 * If the Element is not found, it's just created !
132 * \warning we suppose, right now, the element belongs to a Public Group
134 * @param val string value to set
135 * @param group Group number of the searched tag.
136 * @param element Element number of the searched tag.
137 * @return true if element was found or created successfully
140 bool gdcmSQItem::SetEntryByNumber(std::string val,guint16 group,
143 for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
145 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
148 if ( ( group < (*i)->GetGroup() )
149 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
151 // instead of ReplaceOrCreateByNumber
152 // that is a method of gdcmDocument :-(
153 gdcmValEntry* Entry = (gdcmValEntry*)0;
154 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
156 if ( ! ptagHT->count(key))
158 // we assume a Public Dictionnary *is* loaded
159 gdcmDict *PubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
160 // if the invoked (group,elem) doesn't exist inside the Dictionary
161 // we create a VirtualDictEntry
162 gdcmDictEntry *DictEntry = PubDict->GetDictEntryByNumber(group,
164 if (DictEntry == NULL)
166 DictEntry=gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,
171 // we assume the constructor didn't fail
172 Entry = new gdcmValEntry(DictEntry);
175 /// better we don't assume too much !
176 /// gdcmSQItem is now used to describe any DICOMDIR related object
178 gdcmDocEntry* FoundEntry = ptagHT->find(key)->second;
179 Entry = dynamic_cast<gdcmValEntry*>(FoundEntry);
181 dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
182 " contains non gdcmValEntry occurences");
185 Entry->SetValue(val);
186 Entry->SetLength(val.length());
187 docEntries.insert(i,Entry);
190 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
192 if ( gdcmValEntry* Entry = dynamic_cast<gdcmValEntry*>(*i) )
193 Entry->SetValue(val);
194 (*i)->SetLength(val.length());
200 //-----------------------------------------------------------------------------
205 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
208 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(guint16 group, guint16 element) {
209 for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
210 if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
217 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
221 std::string gdcmSQItem::GetEntryByNumber(guint16 group, guint16 element) {
222 for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
223 if ( (*i)->GetGroup()==group && (*i)->GetElement()==element) {
224 return ((gdcmValEntry *)(*i))->GetValue();
229 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------