1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/11/21 09:46:27 $
7 Version: $Revision: 1.80 $
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 Compute the full length of the SQItem (not only value length)
104 * depending on the VR.
106 uint32_t SQItem::ComputeFullLength()
108 uint32_t l = 8; // Item Starter length
109 for (ListDocEntry::iterator it = DocEntries.begin();
110 it != DocEntries.end();
113 // we skip delimitors (start and end one) because
114 // we force them as 'no length'
115 if ( (*it)->GetGroup() == 0xfffe )
119 l += (*it)->ComputeFullLength();
121 l += 8; // 'Item Delimitation' item
126 * \brief Inserts *in the right place* any Entry (Dicom Element)
127 * into the Sequence Item
128 * @param entry Entry to add
129 * @return always true
131 bool SQItem::AddEntry(DocEntry *entry)
133 if (DocEntries.empty() )
135 DocEntries.push_back(entry);
140 ListDocEntry::iterator insertSpot;
141 ListDocEntry::iterator it = DocEntries.end();
146 if ( (*it)->IsItemDelimitor() )
150 if ( (*it)->GetGroup() < entry->GetGroup() )
153 if ( (*it)->GetGroup() == entry->GetGroup() &&
154 (*it)->GetElement() < entry->GetElement() )
156 } while (it != DocEntries.begin() );
160 //++insertSpot; // ?!?
161 DocEntries.insert(insertSpot, entry);
167 * \brief Clear the std::list from given entry AND delete the entry.
168 * @param entryToRemove Entry to remove AND delete.
169 * @return true if the entry was found and removed; false otherwise
171 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
173 for(ListDocEntry::iterator it = DocEntries.begin();
174 it != DocEntries.end();
177 if ( *it == entryToRemove )
179 DocEntries.erase(it);
180 gdcmDebugMacro( "One element erased: " << entryToRemove->GetKey() );
181 entryToRemove->Unregister();
185 gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
190 * \brief Remove all entry in the Sequence Item
192 void SQItem::ClearEntry()
194 for(ListDocEntry::iterator cc = DocEntries.begin();
195 cc != DocEntries.end();
204 * \brief Move all the entries from a given Sequence Item
206 void SQItem::MoveObject(SQItem *source)
208 DocEntries = source->DocEntries;
209 source->DocEntries.clear();
213 * \brief Get the first Dicom entry while visiting the SQItem
214 * \return The first DocEntry if found, otherwhise 0
216 DocEntry *SQItem::GetFirstEntry()
218 ItDocEntries = DocEntries.begin();
219 if ( ItDocEntries != DocEntries.end() )
220 return *ItDocEntries;
225 * \brief Get the next Dicom entry while visiting the SQItem
226 * \return The next DocEntry if found, otherwhise NULL
228 DocEntry *SQItem::GetNextEntry()
231 if ( ItDocEntries != DocEntries.end() )
232 return *ItDocEntries;
237 * \brief Gets a Dicom Element inside a SQ Item Entry
238 * @param group Group number of the Entry
239 * @param elem Element number of the Entry
240 * @return Entry whose (group,elem) was passed. 0 if not found
242 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
244 for(ListDocEntry::iterator i = DocEntries.begin();
245 i != DocEntries.end();
248 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
254 //-----------------------------------------------------------------------------
257 //-----------------------------------------------------------------------------
260 //-----------------------------------------------------------------------------
263 * \brief canonical Printer
264 * @param os Stream to print to.
265 * @param indent Indentation string to be prepended during printing.
267 void SQItem::Print(std::ostream &os, std::string const &)
269 std::ostringstream s;
271 if (SQDepthLevel > 0)
273 for (int i = 0; i < SQDepthLevel; ++i)
278 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
279 for (ListDocEntry::iterator i = DocEntries.begin();
280 i != DocEntries.end();
283 DocEntry *Entry = *i;
284 bool PrintEndLine = true;
287 Entry->SetPrintLevel(PrintLevel);
289 if ( dynamic_cast<SeqEntry*>(Entry) )
291 PrintEndLine = false;
300 //-----------------------------------------------------------------------------
301 } // end namespace gdcm