1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/10/27 09:52:33 $
7 Version: $Revision: 1.78 $
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 "gdcmGlobal.h"
22 #include "gdcmDictSet.h"
24 #include "gdcmDebug.h"
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
33 * \brief Constructor from a given SQItem
35 SQItem::SQItem(int depthLevel )
38 SQDepthLevel = depthLevel;
43 * \brief Canonical destructor.
50 //-----------------------------------------------------------------------------
53 * \brief canonical Writer
54 * @param fp file pointer to an already open file.
55 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
57 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
60 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
61 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
63 //we force the writting of an 'Item' Start Element
64 // because we want to write the Item as a 'no Length' item
67 binary_write( *fp, item[j]); // fffe e000 ffff ffff
70 for (ListDocEntry::iterator it = DocEntries.begin();
71 it != DocEntries.end();
74 // we skip delimitors (start and end one) because
75 // we force them as 'no length'
76 if ( (*it)->GetGroup() == 0xfffe )
81 // Fix in order to make some MR PHILIPS images e-film readable
82 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
83 // we just *always* ignore spurious fffe|0000 tag !
84 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
86 break; // FIXME : break or continue; ?!?
87 // --> makes no difference since the only bugged file we have
88 // contains 'impossible tag' fffe|0000 in last position !
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);
117 ListDocEntry::iterator insertSpot;
118 ListDocEntry::iterator it = DocEntries.end();
123 if ( (*it)->IsItemDelimitor() )
127 if ( (*it)->GetGroup() < entry->GetGroup() )
130 if ( (*it)->GetGroup() == entry->GetGroup() &&
131 (*it)->GetElement() < entry->GetElement() )
133 } while (it != DocEntries.begin() );
137 //++insertSpot; // ?!?
138 DocEntries.insert(insertSpot, entry);
144 * \brief Clear the std::list from given entry AND delete the entry.
145 * @param entryToRemove Entry to remove AND delete.
146 * @return true if the entry was found and removed; false otherwise
148 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
150 for(ListDocEntry::iterator it = DocEntries.begin();
151 it != DocEntries.end();
154 if ( *it == entryToRemove )
156 DocEntries.erase(it);
157 gdcmDebugMacro( "One element erased: " << entryToRemove->GetKey() );
158 entryToRemove->Unregister();
162 gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
167 * \brief Remove all entry in the Sequence Item
169 void SQItem::ClearEntry()
171 for(ListDocEntry::iterator cc = DocEntries.begin();
172 cc != DocEntries.end();
181 * \brief Move all the entries from a given Sequence Item
183 void SQItem::MoveObject(SQItem *source)
185 DocEntries = source->DocEntries;
186 source->DocEntries.clear();
190 * \brief Get the first Dicom entry while visiting the SQItem
191 * \return The first DocEntry if found, otherwhise 0
193 DocEntry *SQItem::GetFirstEntry()
195 ItDocEntries = DocEntries.begin();
196 if ( ItDocEntries != DocEntries.end() )
197 return *ItDocEntries;
202 * \brief Get the next Dicom entry while visiting the SQItem
203 * \return The next DocEntry if found, otherwhise NULL
205 DocEntry *SQItem::GetNextEntry()
208 if ( ItDocEntries != DocEntries.end() )
209 return *ItDocEntries;
214 * \brief Gets a Dicom Element inside a SQ Item Entry
215 * @param group Group number of the Entry
216 * @param elem Element number of the Entry
217 * @return Entry whose (group,elem) was passed. 0 if not found
219 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
221 for(ListDocEntry::iterator i = DocEntries.begin();
222 i != DocEntries.end();
225 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
231 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
237 //-----------------------------------------------------------------------------
240 * \brief canonical Printer
241 * @param os Stream to print to.
242 * @param indent Indentation string to be prepended during printing.
244 void SQItem::Print(std::ostream &os, std::string const &)
246 std::ostringstream s;
248 if (SQDepthLevel > 0)
250 for (int i = 0; i < SQDepthLevel; ++i)
255 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
256 for (ListDocEntry::iterator i = DocEntries.begin();
257 i != DocEntries.end();
260 DocEntry *Entry = *i;
261 bool PrintEndLine = true;
264 Entry->SetPrintLevel(PrintLevel);
266 if ( dynamic_cast<SeqEntry*>(Entry) )
268 PrintEndLine = false;
277 //-----------------------------------------------------------------------------
278 } // end namespace gdcm