1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/10/28 03:10:58 $
7 Version: $Revision: 1.33 $
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.
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"
25 #include "gdcmDebug.h"
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
34 * \brief Constructor from a given SQItem
36 SQItem::SQItem(int depthLevel )
39 SQDepthLevel = depthLevel;
43 * \brief Canonical destructor.
47 for(ListDocEntry::iterator cc = DocEntries.begin();
48 cc != DocEntries.end();
56 //-----------------------------------------------------------------------------
59 * \brief canonical Printer
61 void SQItem::Print(std::ostream& os)
67 for (int i = 0; i < SQDepthLevel; ++i)
72 //std::cout << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
73 for (ListDocEntry::iterator i = DocEntries.begin();
74 i != DocEntries.end();
78 bool PrintEndLine = true;
81 Entry->SetPrintLevel(2);
83 if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(Entry) )
85 (void)seqEntry; //not used
97 * \brief canonical Writer
99 void SQItem::Write(std::ofstream* fp, FileType filetype)
101 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
102 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
104 //we force the writting of an 'Item' Start Element
105 // because we want to write the Item as a 'no Length' item
106 fp->write((char*)&item[0],8); // fffe e000 ffff ffff
108 for (ListDocEntry::iterator i = DocEntries.begin();
109 i != DocEntries.end();
112 // we skip delimitors (start and end one) because
113 // we force them as 'no length'
114 if ( (*i)->GetGroup() == 0xfffe )
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 )
124 break; // FIXME : continue; ?!?
127 (*i)->Write(fp, filetype);
130 //we force the writting of an 'Item Delimitation' item
131 // because we wrote the Item as a 'no Length' item
132 fp->write((char*)&itemt[0],8); // fffe e000 ffff ffff
136 //-----------------------------------------------------------------------------
139 * \brief adds any Entry (Dicom Element) to the Sequence Item
141 bool SQItem::AddEntry(DocEntry* entry)
143 DocEntries.push_back(entry);
144 //TODO : check if it worked
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
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
161 bool SQItem::SetEntryByNumber(std::string const & val, uint16_t group,
164 for(ListDocEntry::iterator i = DocEntries.begin();
165 i != DocEntries.end();
168 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
173 if ( ( group < (*i)->GetGroup() )
174 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
176 // instead of ReplaceOrCreateByNumber
177 // that is a method of Document :-(
179 TagKey key = DictEntry::TranslateToKey(group, element);
181 if ( ! PtagHT->count(key))
183 // we assume a Public Dictionnary *is* loaded
184 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
185 // if the invoked (group,elem) doesn't exist inside the Dictionary
186 // we create a VirtualDictEntry
187 DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, element);
188 if (dictEntry == NULL)
191 Global::GetDicts()->NewVirtualDictEntry(group, element,
194 // we assume the constructor didn't fail
195 entry = new ValEntry(dictEntry);
198 /// better we don't assume too much !
199 /// SQItem is now used to describe any DICOMDIR related object
203 DocEntry* foundEntry = PtagHT->find(key)->second;
204 entry = dynamic_cast<ValEntry*>(foundEntry);
207 dbg.Verbose(0, "SQItem::SetEntryByNumber: docEntries"
208 " contains non ValEntry occurences");
213 entry->SetValue(val);
215 entry->SetLength(val.length());
216 DocEntries.insert(i,entry);
220 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
222 if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
224 entry->SetValue(val);
226 (*i)->SetLength(val.length());
232 //-----------------------------------------------------------------------------
237 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
240 DocEntry* SQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
242 for(ListDocEntry::iterator i = DocEntries.begin();
243 i != DocEntries.end(); ++i)
245 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
254 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
258 std::string SQItem::GetEntryByNumber(uint16_t group, uint16_t element)
260 for(ListDocEntry::iterator i = DocEntries.begin();
261 i != DocEntries.end(); ++i)
263 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
265 return ((ValEntry *)(*i))->GetValue(); //FIXME
270 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
276 } // end namespace gdcm