1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/01/11 00:21:48 $
7 Version: $Revision: 1.50 $
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"
24 #include "gdcmDictSet.h"
26 #include "gdcmDebug.h"
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
36 * \brief Constructor from a given SQItem
38 SQItem::SQItem(int depthLevel )
41 SQDepthLevel = depthLevel;
45 * \brief Canonical destructor.
49 for(ListDocEntry::iterator cc = DocEntries.begin();
50 cc != DocEntries.end();
58 //-----------------------------------------------------------------------------
61 * \brief canonical Printer
63 void SQItem::Print(std::ostream &os)
69 for (int i = 0; i < SQDepthLevel; ++i)
74 //std::cout << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
75 for (ListDocEntry::iterator i = DocEntries.begin();
76 i != DocEntries.end();
80 bool PrintEndLine = true;
83 Entry->SetPrintLevel(PrintLevel);
85 if ( SeqEntry *seqEntry = dynamic_cast<SeqEntry*>(Entry) )
87 (void)seqEntry; //not used
99 * \brief canonical Writer
101 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
104 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
105 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
107 //we force the writting of an 'Item' Start Element
108 // because we want to write the Item as a 'no Length' item
111 binary_write( *fp, item[j]); // fffe e000 ffff ffff
114 for (ListDocEntry::iterator it = DocEntries.begin();
115 it != DocEntries.end();
118 // we skip delimitors (start and end one) because
119 // we force them as 'no length'
120 if ( (*it)->GetGroup() == 0xfffe )
125 // Fix in order to make some MR PHILIPS images e-film readable
126 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
127 // we just *always* ignore spurious fffe|0000 tag !
128 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
130 break; // FIXME : continue; ?!?
133 (*it)->WriteContent(fp, filetype);
136 //we force the writting of an 'Item Delimitation' item
137 // because we wrote the Item as a 'no Length' item
140 binary_write( *fp, itemt[j]); // fffe e000 ffff ffff
145 //-----------------------------------------------------------------------------
148 * \brief adds any Entry (Dicom Element) to the Sequence Item
150 bool SQItem::AddEntry(DocEntry *entry)
152 DocEntries.push_back(entry);
153 //TODO : check if it worked
158 * \brief Sets Entry (Dicom Element) value of an element,
159 * specified by it's tag (Group, Number)
160 * and the length, too ... inside a SQ Item
161 * If the Element is not found, it's just created !
162 * \warning we suppose, right now, the element belongs to a Public Group
164 * @param val string value to set
165 * @param group Group number of the searched tag.
166 * @param elem Element number of the searched tag.
167 * @return true if element was found or created successfully
170 bool SQItem::SetEntry(std::string const &val, uint16_t group,
173 for(ListDocEntry::iterator i = DocEntries.begin();
174 i != DocEntries.end();
177 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
182 if ( ( group < (*i)->GetGroup() )
183 ||( group == (*i)->GetGroup() && elem < (*i)->GetElement()) )
185 // instead of ReplaceOrCreate
186 // that is a method of Document :-(
188 TagKey key = DictEntry::TranslateToKey(group, elem);
190 // we assume a Public Dictionnary *is* loaded
191 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
192 // if the invoked (group,elem) doesn't exist inside the Dictionary
193 // we create a VirtualDictEntry
194 DictEntry *dictEntry = pubDict->GetDictEntry(group, elem);
195 if (dictEntry == NULL)
198 Global::GetDicts()->NewVirtualDictEntry(group, elem,
202 // we assume the constructor didn't fail
203 entry = new ValEntry(dictEntry);
206 entry->SetValue(val);
208 DocEntries.insert(i,entry);
212 if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
214 if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
216 entry->SetValue(val);
225 * \brief Clear the hash table from given entry AND delete the entry.
226 * @param entryToRemove Entry to remove AND delete.
227 * \warning Some problems when using under Windows... prefer the use of
228 * Initialize / GetNext methods
230 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
232 for(ListDocEntry::iterator it = DocEntries.begin();
233 it != DocEntries.end();
236 if( *it == entryToRemove)
238 DocEntries.erase(it);
239 gdcmVerboseMacro( "One element erased.");
240 delete entryToRemove;
244 gdcmVerboseMacro( "Value not present.");
249 * \brief Clear the hash table from given entry BUT keep the entry.
250 * @param entryToRemove Entry to remove.
252 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
254 for(ListDocEntry::iterator it = DocEntries.begin();
255 it != DocEntries.end();
258 if( *it == entryToRemove)
260 DocEntries.erase(it);
261 gdcmVerboseMacro( "One element erased.");
266 gdcmVerboseMacro( "Value not present.");
271 * \brief Initialise the visit of the chained list
273 void SQItem::Initialize()
275 ItDocEntries = DocEntries.begin();
279 * \brief Get the next entry whil visiting the chained list
280 * \return The next DocEntry if found, otherwhise NULL
282 DocEntry *SQItem::GetNextEntry()
284 if (ItDocEntries != DocEntries.end())
286 DocEntry *tmp = *ItDocEntries;
297 //-----------------------------------------------------------------------------
300 * \brief Gets a Dicom Element inside a SQ Item Entry
301 * @param group Group number of the Entry
302 * @param elem Element number of the Entry
305 DocEntry* SQItem::GetDocEntry(uint16_t group, uint16_t elem)
307 for(ListDocEntry::iterator i = DocEntries.begin();
308 i != DocEntries.end(); ++i)
310 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
319 * \brief Get the value of a Dicom Element inside a SQ Item Entry
320 * @param group Group number of the Entry
321 * @param elem Element number of the Entry
325 std::string SQItem::GetEntry(uint16_t group, uint16_t elem)
327 for(ListDocEntry::iterator i = DocEntries.begin();
328 i != DocEntries.end(); ++i)
330 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
332 return ((ValEntry *)(*i))->GetValue(); //FIXME
337 //-----------------------------------------------------------------------------
341 //-----------------------------------------------------------------------------
343 } // end namespace gdcm