1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/07/06 08:43:18 $
7 Version: $Revision: 1.73 $
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;
45 * \brief Canonical destructor.
52 //-----------------------------------------------------------------------------
55 * \brief canonical Writer
56 * @param fp file pointer to an already open file.
57 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
59 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
62 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
63 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
65 //we force the writting of an 'Item' Start Element
66 // because we want to write the Item as a 'no Length' item
69 binary_write( *fp, item[j]); // fffe e000 ffff ffff
72 for (ListDocEntry::iterator it = DocEntries.begin();
73 it != DocEntries.end();
76 // we skip delimitors (start and end one) because
77 // we force them as 'no length'
78 if ( (*it)->GetGroup() == 0xfffe )
83 // Fix in order to make some MR PHILIPS images e-film readable
84 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
85 // we just *always* ignore spurious fffe|0000 tag !
86 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
88 break; // FIXME : continue; ?!?
91 (*it)->WriteContent(fp, filetype);
94 //we force the writting of an 'Item Delimitation' item
95 // because we wrote the Item as a 'no Length' item
98 binary_write( *fp, itemt[j]); // fffe e000 ffff ffff
103 * \brief Inserts *in the right place* any Entry (Dicom Element)
104 * into the Sequence Item
105 * @param entry Entry to add
106 * @return always true
108 bool SQItem::AddEntry(DocEntry *entry)
110 if (DocEntries.empty() )
112 DocEntries.push_back(entry);
116 ListDocEntry::iterator insertSpot;
117 ListDocEntry::iterator it = DocEntries.end();
122 if ( (*it)->IsItemDelimitor() )
126 if ( (*it)->GetGroup() < entry->GetGroup() )
129 if ( (*it)->GetGroup() == entry->GetGroup() &&
130 (*it)->GetElement() < entry->GetElement() )
132 } while (it != DocEntries.begin() );
136 DocEntries.insert(insertSpot, entry);
141 * \brief Clear the std::list from given entry AND delete the entry.
142 * @param entryToRemove Entry to remove AND delete.
143 * @return true if the entry was found and removed; false otherwise
145 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
147 for(ListDocEntry::iterator it = DocEntries.begin();
148 it != DocEntries.end();
151 if ( *it == entryToRemove )
153 DocEntries.erase(it);
154 gdcmWarningMacro( "One element erased: " << entryToRemove->GetKey() );
155 delete entryToRemove;
159 gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
164 * \brief Clear the std::list from given entry BUT keep the entry.
165 * @param entryToRemove Entry to remove.
166 * @return true if the entry was found and removed; false otherwise
168 bool SQItem::RemoveEntryNoDestroy(DocEntry *entryToRemove)
170 for(ListDocEntry::iterator it = DocEntries.begin();
171 it != DocEntries.end();
174 if ( *it == entryToRemove )
176 DocEntries.erase(it);
177 gdcmWarningMacro( "One element removed, no destroyed: "
178 << entryToRemove->GetKey() );
183 gdcmWarningMacro( "Entry not found:" << entryToRemove->GetKey() );
188 * \brief Remove all entry in the Sequence Item
190 void SQItem::ClearEntry()
192 for(ListDocEntry::iterator cc = DocEntries.begin();
193 cc != DocEntries.end();
202 * \brief Get the first Dicom entry while visiting the SQItem
203 * \return The first DocEntry if found, otherwhise 0
205 DocEntry *SQItem::GetFirstEntry()
207 ItDocEntries = DocEntries.begin();
208 if ( ItDocEntries != DocEntries.end() )
209 return *ItDocEntries;
214 * \brief Get the next Dicom entry while visiting the SQItem
215 * \return The next DocEntry if found, otherwhise NULL
217 DocEntry *SQItem::GetNextEntry()
220 if ( ItDocEntries != DocEntries.end() )
221 return *ItDocEntries;
226 * \brief Gets a Dicom Element inside a SQ Item Entry
227 * @param group Group number of the Entry
228 * @param elem Element number of the Entry
229 * @return Entry whose (group,elem) was passed. 0 if not found
231 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
233 for(ListDocEntry::iterator i = DocEntries.begin();
234 i != DocEntries.end();
237 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
243 //-----------------------------------------------------------------------------
246 //-----------------------------------------------------------------------------
249 //-----------------------------------------------------------------------------
252 * \brief canonical Printer
253 * @param os Stream to print to.
254 * @param indent Indentation string to be prepended during printing.
256 void SQItem::Print(std::ostream &os, std::string const &)
258 std::ostringstream s;
260 if (SQDepthLevel > 0)
262 for (int i = 0; i < SQDepthLevel; ++i)
267 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
268 for (ListDocEntry::iterator i = DocEntries.begin();
269 i != DocEntries.end();
272 DocEntry *Entry = *i;
273 bool PrintEndLine = true;
276 Entry->SetPrintLevel(PrintLevel);
278 if ( dynamic_cast<SeqEntry*>(Entry) )
280 PrintEndLine = false;
289 //-----------------------------------------------------------------------------
290 } // end namespace gdcm