1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/01/24 16:44:54 $
7 Version: $Revision: 1.58 $
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 Insert, that is a method of Document :-(
189 TagKey key = DictEntry::TranslateToKey(group, elem);
191 // we assume a Public Dictionary *is* loaded
192 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
193 // if the invoked (group,elem) doesn't exist inside the Dictionary
194 // we create a VirtualDictEntry
195 DictEntry *dictEntry = pubDict->GetEntry(group, elem);
196 if (dictEntry == NULL)
199 Global::GetDicts()->NewVirtualDictEntry(group, elem,
203 // we assume the constructor didn't fail
204 entry = new ValEntry(dictEntry);
207 entry->SetValue(val);
209 DocEntries.insert(i,entry);
213 if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
215 if ( ValEntry *entry = dynamic_cast<ValEntry*>(*i) )
217 entry->SetValue(val);
226 * \brief Clear the std::list from given entry AND delete the entry.
227 * @param entryToRemove Entry to remove AND delete.
228 * @return true if the entry was found and removed; false otherwise
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: " << entryToRemove->GetKey() );
240 delete entryToRemove;
244 gdcmVerboseMacro( "Entry not found: " << entryToRemove->GetKey() );
249 * \brief Clear the std::list from given entry BUT keep the entry.
250 * @param entryToRemove Entry to remove.
251 * @return true if the entry was found and removed; false otherwise
253 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
255 for(ListDocEntry::iterator it = DocEntries.begin();
256 it != DocEntries.end();
259 if( *it == entryToRemove)
261 DocEntries.erase(it);
262 gdcmVerboseMacro( "One element erased, no destroyed: "
263 << entryToRemove->GetKey() );
268 gdcmVerboseMacro( "Entry not found:" << entryToRemove->GetKey() );
273 * \brief Get the first Dicom entry while visiting the SQItem
274 * \return The first DocEntry if found, otherwhise 0
276 DocEntry * SQItem::GetFirstEntry()
278 ItDocEntries = DocEntries.begin();
279 if (ItDocEntries != DocEntries.end())
280 return *ItDocEntries;
285 * \brief Get the next Dicom entry while visiting the chained list
286 * \return The next DocEntry if found, otherwhise NULL
288 DocEntry *SQItem::GetNextEntry()
290 // gdcmAssertMacro (ItDocEntries != DocEntries.end());
293 if (ItDocEntries != DocEntries.end())
294 return *ItDocEntries;
299 //-----------------------------------------------------------------------------
302 * \brief Gets a Dicom Element inside a SQ Item Entry
303 * @param group Group number of the Entry
304 * @param elem Element number of the Entry
305 * @return Entry whose (group,elem) was passed. 0 if not found
307 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
309 for(ListDocEntry::iterator i = DocEntries.begin();
310 i != DocEntries.end(); ++i)
312 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
321 * \brief Gets a Dicom Element inside a SQ Item Entry
322 * @param group Group number of the Entry
323 * @param elem Element number of the Entry
324 * @return Entry whose (group,elem) was passed. 0 if not found
326 ValEntry* SQItem::GetValEntry(uint16_t group, uint16_t elem)
328 DocEntry *d = GetDocEntry(group, elem);
329 if ( ValEntry *e = dynamic_cast<ValEntry*>(d) )
335 * \brief Gets a Dicom Element inside a SQ Item Entry
336 * @param group Group number of the Entry
337 * @param elem Element number of the Entry
338 * @return Entry whose (group,elem) was passed. 0 if not found
340 BinEntry* SQItem::GetBinEntry(uint16_t group, uint16_t elem)
342 DocEntry *d = GetDocEntry(group, elem);
343 if ( BinEntry *e = dynamic_cast<BinEntry*>(d) )
349 * \brief Gets a Dicom Element inside a SQ Item Entry
350 * @param group Group number of the Entry
351 * @param elem Element number of the Entry
352 * @return Entry whose (group,elem) was passed. 0 if not found
354 SeqEntry* SQItem::GetSeqEntry(uint16_t group, uint16_t elem)
356 DocEntry *d = GetDocEntry(group, elem);
357 if ( SeqEntry *e = dynamic_cast<SeqEntry*>(d) )
364 * \brief Get the value of a Dicom Element inside a SQ Item Entry
365 * \note : meaningfull only if the required entry is NEITHER a SeqEntry
367 * @param group Group number of the Entry
368 * @param elem Element number of the Entry
369 * @return 'string value' of the entry whose (group,elem) was passed.
370 * GDCM_UNFOUND if not found
373 std::string SQItem::GetEntryValue(uint16_t group, uint16_t elem)
377 DocEntry *e = GetFirstEntry();
380 if ( e->GetGroup() == group && e->GetElement() == elem)
383 if (ValEntry *ve = dynamic_cast<ValEntry*>(e))
384 return ve->GetValue();
389 for(ListDocEntry::iterator i = DocEntries.begin();
390 i != DocEntries.end(); ++i)
392 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
394 if (ValEntry *ve = dynamic_cast<ValEntry*>(*i))
395 return ve->GetValue();
400 //-----------------------------------------------------------------------------
404 //-----------------------------------------------------------------------------
406 } // end namespace gdcm