1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/01/24 16:10:53 $
7 Version: $Revision: 1.57 $
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
35 * \brief Constructor from a given SQItem
37 SQItem::SQItem(int depthLevel )
40 SQDepthLevel = depthLevel;
44 * \brief Canonical destructor.
48 for(ListDocEntry::iterator cc = DocEntries.begin();
49 cc != DocEntries.end();
57 //-----------------------------------------------------------------------------
60 * \brief canonical Printer
61 * @param os Stream to print to.
62 * @param indent Indentation string to be prepended during printing.
64 void SQItem::Print(std::ostream &os, std::string const &)
70 for (int i = 0; i < SQDepthLevel; ++i)
75 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
76 for (ListDocEntry::iterator i = DocEntries.begin();
77 i != DocEntries.end();
81 bool PrintEndLine = true;
84 Entry->SetPrintLevel(PrintLevel);
86 if ( dynamic_cast<SeqEntry*>(Entry) )
98 * \brief canonical Writer
99 * @param fp file pointer to an already open file.
100 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
102 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
105 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
106 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
108 //we force the writting of an 'Item' Start Element
109 // because we want to write the Item as a 'no Length' item
112 binary_write( *fp, item[j]); // fffe e000 ffff ffff
115 for (ListDocEntry::iterator it = DocEntries.begin();
116 it != DocEntries.end();
119 // we skip delimitors (start and end one) because
120 // we force them as 'no length'
121 if ( (*it)->GetGroup() == 0xfffe )
126 // Fix in order to make some MR PHILIPS images e-film readable
127 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
128 // we just *always* ignore spurious fffe|0000 tag !
129 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
131 break; // FIXME : continue; ?!?
134 (*it)->WriteContent(fp, filetype);
137 //we force the writting of an 'Item Delimitation' item
138 // because we wrote the Item as a 'no Length' item
141 binary_write( *fp, itemt[j]); // fffe e000 ffff ffff
146 //-----------------------------------------------------------------------------
149 * \brief adds any Entry (Dicom Element) to the Sequence Item
150 * @param entry Entry to add
152 bool SQItem::AddEntry(DocEntry *entry)
154 DocEntries.push_back(entry);
155 //TODO : check if it worked
160 * \brief Sets Entry (Dicom Element) value of an element,
161 * specified by it's tag (Group, Number)
162 * and the length, too ... inside a SQ Item
163 * If the Element is not found, it's just created !
164 * \warning we suppose, right now, the element belongs to a Public Group
166 * @param val string value to set
167 * @param group Group number of the searched tag.
168 * @param elem Element number of the searched tag.
169 * @return true if element was found or created successfully
172 bool SQItem::SetEntryValue(std::string const &val, uint16_t group,
175 for(ListDocEntry::iterator i = DocEntries.begin();
176 i != DocEntries.end();
179 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
184 if ( ( group < (*i)->GetGroup() )
185 ||( group == (*i)->GetGroup() && elem < (*i)->GetElement()) )
187 // instead of ReplaceOrCreate
188 // that is a method of Document :-(
190 TagKey key = DictEntry::TranslateToKey(group, elem);
192 // we assume a Public Dictionary *is* loaded
193 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
194 // if the invoked (group,elem) doesn't exist inside the Dictionary
195 // we create a VirtualDictEntry
196 DictEntry *dictEntry = pubDict->GetEntry(group, elem);
197 if (dictEntry == NULL)
200 Global::GetDicts()->NewVirtualDictEntry(group, elem,
204 // we assume the constructor didn't fail
205 entry = new ValEntry(dictEntry);
208 entry->SetValue(val);
210 DocEntries.insert(i,entry);
214 if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
216 if ( ValEntry *entry = dynamic_cast<ValEntry*>(*i) )
218 entry->SetValue(val);
227 * \brief Clear the std::list from given entry AND delete the entry.
228 * @param entryToRemove Entry to remove AND delete.
229 * @return true if the entry was found and removed; false otherwise
231 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
233 for(ListDocEntry::iterator it = DocEntries.begin();
234 it != DocEntries.end();
237 if( *it == entryToRemove)
239 DocEntries.erase(it);
240 gdcmVerboseMacro( "One element erased: " << entryToRemove->GetKey() );
241 delete entryToRemove;
245 gdcmVerboseMacro( "Entry not found: " << entryToRemove->GetKey() );
250 * \brief Clear the std::list from given entry BUT keep the entry.
251 * @param entryToRemove Entry to remove.
252 * @return true if the entry was found and removed; false otherwise
254 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
256 for(ListDocEntry::iterator it = DocEntries.begin();
257 it != DocEntries.end();
260 if( *it == entryToRemove)
262 DocEntries.erase(it);
263 gdcmVerboseMacro( "One element erased, no destroyed: "
264 << entryToRemove->GetKey() );
269 gdcmVerboseMacro( "Entry not found:" << entryToRemove->GetKey() );
274 * \brief Get the first Dicom entry while visiting the SQItem
275 * \return The first DocEntry if found, otherwhise 0
277 DocEntry * SQItem::GetFirstEntry()
279 ItDocEntries = DocEntries.begin();
280 if (ItDocEntries != DocEntries.end())
281 return *ItDocEntries;
286 * \brief Get the next Dicom entry while visiting the chained list
287 * \return The next DocEntry if found, otherwhise NULL
289 DocEntry *SQItem::GetNextEntry()
291 // gdcmAssertMacro (ItDocEntries != DocEntries.end());
294 if (ItDocEntries != DocEntries.end())
295 return *ItDocEntries;
300 //-----------------------------------------------------------------------------
303 * \brief Gets a Dicom Element inside a SQ Item Entry
304 * @param group Group number of the Entry
305 * @param elem Element number of the Entry
306 * @return Entry whose (group,elem) was passed. 0 if not found
308 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
310 for(ListDocEntry::iterator i = DocEntries.begin();
311 i != DocEntries.end(); ++i)
313 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
322 * \brief Gets a Dicom Element inside a SQ Item Entry
323 * @param group Group number of the Entry
324 * @param elem Element number of the Entry
325 * @return Entry whose (group,elem) was passed. 0 if not found
327 ValEntry* SQItem::GetValEntry(uint16_t group, uint16_t elem)
329 DocEntry *d = GetDocEntry(group, elem);
330 if ( ValEntry *e = dynamic_cast<ValEntry*>(d) )
336 * \brief Gets a Dicom Element inside a SQ Item Entry
337 * @param group Group number of the Entry
338 * @param elem Element number of the Entry
339 * @return Entry whose (group,elem) was passed. 0 if not found
341 BinEntry* SQItem::GetBinEntry(uint16_t group, uint16_t elem)
343 DocEntry *d = GetDocEntry(group, elem);
344 if ( BinEntry *e = dynamic_cast<BinEntry*>(d) )
350 * \brief Gets a Dicom Element inside a SQ Item Entry
351 * @param group Group number of the Entry
352 * @param elem Element number of the Entry
353 * @return Entry whose (group,elem) was passed. 0 if not found
355 SeqEntry* SQItem::GetSeqEntry(uint16_t group, uint16_t elem)
357 DocEntry *d = GetDocEntry(group, elem);
358 if ( SeqEntry *e = dynamic_cast<SeqEntry*>(d) )
365 * \brief Get the value of a Dicom Element inside a SQ Item Entry
366 * \note : meaningfull only if the required entry is NEITHER a SeqEntry
368 * @param group Group number of the Entry
369 * @param elem Element number of the Entry
370 * @return 'string value' of the entry whose (group,elem) was passed.
371 * GDCM_UNFOUND if not found
374 std::string SQItem::GetEntryValue(uint16_t group, uint16_t elem)
378 DocEntry *e = GetFirstEntry();
381 if ( e->GetGroup() == group && e->GetElement() == elem)
384 if (ValEntry *ve = dynamic_cast<ValEntry*>(e))
385 return ve->GetValue();
390 for(ListDocEntry::iterator i = DocEntries.begin();
391 i != DocEntries.end(); ++i)
393 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
395 if (ValEntry *ve = dynamic_cast<ValEntry*>(*i))
396 return ve->GetValue();
401 //-----------------------------------------------------------------------------
405 //-----------------------------------------------------------------------------
407 } // end namespace gdcm