1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/02/01 10:29:56 $
7 Version: $Revision: 1.64 $
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
104 * \brief Remove all entry in the Sequence Item
106 void SQItem::ClearEntry()
108 for(ListDocEntry::iterator cc = DocEntries.begin();
109 cc != DocEntries.end();
118 * \brief Adds any Entry (Dicom Element) to the Sequence Item
119 * @param entry Entry to add
121 bool SQItem::AddEntry(DocEntry *entry)
123 DocEntries.push_back(entry);
128 * \brief Clear the std::list from given entry AND delete the entry.
129 * @param entryToRemove Entry to remove AND delete.
130 * @return true if the entry was found and removed; false otherwise
132 bool SQItem::RemoveEntry( DocEntry *entryToRemove)
134 for(ListDocEntry::iterator it = DocEntries.begin();
135 it != DocEntries.end();
138 if( *it == entryToRemove)
140 DocEntries.erase(it);
141 gdcmVerboseMacro( "One element erased: " << entryToRemove->GetKey() );
142 delete entryToRemove;
146 gdcmVerboseMacro( "Entry not found: " << entryToRemove->GetKey() );
151 * \brief Clear the std::list from given entry BUT keep the entry.
152 * @param entryToRemove Entry to remove.
153 * @return true if the entry was found and removed; false otherwise
155 bool SQItem::RemoveEntryNoDestroy(DocEntry *entryToRemove)
157 for(ListDocEntry::iterator it = DocEntries.begin();
158 it != DocEntries.end();
161 if( *it == entryToRemove)
163 DocEntries.erase(it);
164 gdcmVerboseMacro( "One element erased, no destroyed: "
165 << entryToRemove->GetKey() );
170 gdcmVerboseMacro( "Entry not found:" << entryToRemove->GetKey() );
175 * \brief Get the first Dicom entry while visiting the SQItem
176 * \return The first DocEntry if found, otherwhise 0
178 DocEntry *SQItem::GetFirstEntry()
180 ItDocEntries = DocEntries.begin();
181 if (ItDocEntries != DocEntries.end())
182 return *ItDocEntries;
187 * \brief Get the next Dicom entry while visiting the chained list
188 * \return The next DocEntry if found, otherwhise NULL
190 DocEntry *SQItem::GetNextEntry()
193 if (ItDocEntries != DocEntries.end())
194 return *ItDocEntries;
199 * \brief Gets a Dicom Element inside a SQ Item Entry
200 * @param group Group number of the Entry
201 * @param elem Element number of the Entry
202 * @return Entry whose (group,elem) was passed. 0 if not found
204 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
206 for(ListDocEntry::iterator i = DocEntries.begin();
207 i != DocEntries.end(); ++i)
209 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
215 //-----------------------------------------------------------------------------
218 //-----------------------------------------------------------------------------
221 //-----------------------------------------------------------------------------
224 * \brief canonical Printer
225 * @param os Stream to print to.
226 * @param indent Indentation string to be prepended during printing.
228 void SQItem::Print(std::ostream &os, std::string const &)
230 std::ostringstream s;
232 if (SQDepthLevel > 0)
234 for (int i = 0; i < SQDepthLevel; ++i)
239 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
240 for (ListDocEntry::iterator i = DocEntries.begin();
241 i != DocEntries.end();
244 DocEntry *Entry = *i;
245 bool PrintEndLine = true;
248 Entry->SetPrintLevel(PrintLevel);
250 if ( dynamic_cast<SeqEntry*>(Entry) )
252 PrintEndLine = false;
261 //-----------------------------------------------------------------------------
262 } // end namespace gdcm