1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/07/03 12:47:23 $
7 Version: $Revision: 1.72 $
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
107 bool SQItem::AddEntry(DocEntry *entry)
109 if (DocEntries.empty() )
111 DocEntries.push_back(entry);
115 ListDocEntry::iterator insertSpot;
116 ListDocEntry::iterator it = DocEntries.end();
121 if ( (*it)->IsItemDelimitor() )
125 if ( (*it)->GetGroup() < entry->GetGroup() )
128 if ( (*it)->GetGroup() == entry->GetGroup() &&
129 (*it)->GetElement() < entry->GetElement() )
131 } while (it != DocEntries.begin() );
135 DocEntries.insert(insertSpot, entry);
140 * \brief Clear the std::list from given entry AND delete the entry.
141 * @param entryToRemove Entry to remove AND delete.
142 * @return true if the entry was found and removed; false otherwise
144 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
146 for(ListDocEntry::iterator it = DocEntries.begin();
147 it != DocEntries.end();
150 if ( *it == entryToRemove )
152 DocEntries.erase(it);
153 gdcmWarningMacro( "One element erased: " << entryToRemove->GetKey() );
154 delete entryToRemove;
158 gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
163 * \brief Clear the std::list from given entry BUT keep the entry.
164 * @param entryToRemove Entry to remove.
165 * @return true if the entry was found and removed; false otherwise
167 bool SQItem::RemoveEntryNoDestroy(DocEntry *entryToRemove)
169 for(ListDocEntry::iterator it = DocEntries.begin();
170 it != DocEntries.end();
173 if ( *it == entryToRemove )
175 DocEntries.erase(it);
176 gdcmWarningMacro( "One element removed, no destroyed: "
177 << entryToRemove->GetKey() );
182 gdcmWarningMacro( "Entry not found:" << entryToRemove->GetKey() );
187 * \brief Remove all entry in the Sequence Item
189 void SQItem::ClearEntry()
191 for(ListDocEntry::iterator cc = DocEntries.begin();
192 cc != DocEntries.end();
201 * \brief Get the first Dicom entry while visiting the SQItem
202 * \return The first DocEntry if found, otherwhise 0
204 DocEntry *SQItem::GetFirstEntry()
206 ItDocEntries = DocEntries.begin();
207 if ( ItDocEntries != DocEntries.end() )
208 return *ItDocEntries;
213 * \brief Get the next Dicom entry while visiting the SQItem
214 * \return The next DocEntry if found, otherwhise NULL
216 DocEntry *SQItem::GetNextEntry()
219 if ( ItDocEntries != DocEntries.end() )
220 return *ItDocEntries;
225 * \brief Gets a Dicom Element inside a SQ Item Entry
226 * @param group Group number of the Entry
227 * @param elem Element number of the Entry
228 * @return Entry whose (group,elem) was passed. 0 if not found
230 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
232 for(ListDocEntry::iterator i = DocEntries.begin();
233 i != DocEntries.end();
236 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
242 //-----------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
248 //-----------------------------------------------------------------------------
251 * \brief canonical Printer
252 * @param os Stream to print to.
253 * @param indent Indentation string to be prepended during printing.
255 void SQItem::Print(std::ostream &os, std::string const &)
257 std::ostringstream s;
259 if (SQDepthLevel > 0)
261 for (int i = 0; i < SQDepthLevel; ++i)
266 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
267 for (ListDocEntry::iterator i = DocEntries.begin();
268 i != DocEntries.end();
271 DocEntry *Entry = *i;
272 bool PrintEndLine = true;
275 Entry->SetPrintLevel(PrintLevel);
277 if ( dynamic_cast<SeqEntry*>(Entry) )
279 PrintEndLine = false;
288 //-----------------------------------------------------------------------------
289 } // end namespace gdcm