1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/12/03 20:16:58 $
7 Version: $Revision: 1.40 $
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
36 * \brief Constructor from a given SQItem
38 SQItem::SQItem(int depthLevel )
41 SQDepthLevel = depthLevel;
45 * \brief Canonical destructor.
49 for(ListDocEntry::iterator cc = DocEntries.begin();
50 cc != DocEntries.end();
58 //-----------------------------------------------------------------------------
61 * \brief canonical Printer
63 void SQItem::Print(std::ostream& os)
70 for (int i = 0; i < SQDepthLevel; ++i)
75 //std::cout << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
76 for (ListDocEntry::iterator i = DocEntries.begin();
77 i != DocEntries.end();
81 bool PrintEndLine = true;
84 Entry->SetPrintLevel(2);
86 if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(Entry) )
88 (void)seqEntry; //not used
100 * \brief canonical Writer
102 void SQItem::WriteContent(std::ofstream* fp, FileType filetype)
105 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
106 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
108 //we force the writting of an 'Item' Start Element
109 // because we want to write the Item as a 'no Length' item
112 binary_write( *fp, item[j]); // fffe e000 ffff ffff
115 for (ListDocEntry::iterator it = DocEntries.begin();
116 it != DocEntries.end();
119 // we skip delimitors (start and end one) because
120 // we force them as 'no length'
121 if ( (*it)->GetGroup() == 0xfffe )
126 // Fix in order to make some MR PHILIPS images e-film readable
127 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
128 // we just *always* ignore spurious fffe|0000 tag !
129 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
131 break; // FIXME : continue; ?!?
134 (*it)->WriteContent(fp, filetype);
137 //we force the writting of an 'Item Delimitation' item
138 // because we wrote the Item as a 'no Length' item
141 binary_write( *fp, itemt[j]); // fffe e000 ffff ffff
146 //-----------------------------------------------------------------------------
149 * \brief adds any Entry (Dicom Element) to the Sequence Item
151 bool SQItem::AddEntry(DocEntry* entry)
153 DocEntries.push_back(entry);
154 //TODO : check if it worked
159 * \brief Sets Entry (Dicom Element) value of an element,
160 * specified by it's tag (Group, Number)
161 * and the length, too ... inside a SQ Item
162 * If the Element is not found, it's just created !
163 * \warning we suppose, right now, the element belongs to a Public Group
165 * @param val string value to set
166 * @param group Group number of the searched tag.
167 * @param element Element number of the searched tag.
168 * @return true if element was found or created successfully
171 bool SQItem::SetEntryByNumber(std::string const & val, uint16_t group,
174 for(ListDocEntry::iterator i = DocEntries.begin();
175 i != DocEntries.end();
178 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
183 if ( ( group < (*i)->GetGroup() )
184 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
186 // instead of ReplaceOrCreateByNumber
187 // that is a method of Document :-(
189 TagKey key = DictEntry::TranslateToKey(group, element);
191 if ( ! PtagHT->count(key))
193 // we assume a Public Dictionnary *is* loaded
194 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
195 // if the invoked (group,elem) doesn't exist inside the Dictionary
196 // we create a VirtualDictEntry
197 DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, element);
198 if (dictEntry == NULL)
201 Global::GetDicts()->NewVirtualDictEntry(group, element,
204 // we assume the constructor didn't fail
205 entry = new ValEntry(dictEntry);
208 /// better we don't assume too much !
209 /// SQItem is now used to describe any DICOMDIR related object
213 DocEntry* foundEntry = PtagHT->find(key)->second;
214 entry = dynamic_cast<ValEntry*>(foundEntry);
217 dbg.Verbose(0, "SQItem::SetEntryByNumber: docEntries"
218 " contains non ValEntry occurences");
223 entry->SetValue(val);
225 entry->SetLength(val.length());
226 DocEntries.insert(i,entry);
230 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
232 if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
234 entry->SetValue(val);
236 (*i)->SetLength(val.length());
244 * \brief Clear the hash table from given entry AND delete the entry.
245 * @param entryToRemove Entry to remove AND delete.
246 * \warning Some problems when using under Windows... prefer the use of
247 * Initialize / GetNext methods
249 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
251 for(ListDocEntry::iterator it = DocEntries.begin();
252 it != DocEntries.end();
255 if( *it == entryToRemove)
257 DocEntries.erase(it);
258 dbg.Verbose(0, "SQItem::RemoveEntry: one element erased.");
259 delete entryToRemove;
264 dbg.Verbose(0, "SQItem::RemoveEntry: value not present ");
269 * \brief Clear the hash table from given entry BUT keep the entry.
270 * @param entryToRemove Entry to remove.
272 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
274 for(ListDocEntry::iterator it = DocEntries.begin();
275 it != DocEntries.end();
278 if( *it == entryToRemove)
280 DocEntries.erase(it);
281 dbg.Verbose(0, "SQItem::RemoveEntry: one element erased.");
286 dbg.Verbose(0, "SQItem::RemoveEntry: value not present ");
291 * \brief Initialise the visit of the chained list
293 void SQItem::Initialize()
295 ItDocEntries = DocEntries.begin();
299 * \brief Get the next entry whil visiting the chained list
300 * \return The next DocEntry if found, otherwhise NULL
302 DocEntry *SQItem::GetNextEntry()
304 if (ItDocEntries != DocEntries.end())
306 DocEntry *tmp = *ItDocEntries;
317 //-----------------------------------------------------------------------------
320 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
323 DocEntry* SQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
325 for(ListDocEntry::iterator i = DocEntries.begin();
326 i != DocEntries.end(); ++i)
328 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
337 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
341 std::string SQItem::GetEntryByNumber(uint16_t group, uint16_t element)
343 for(ListDocEntry::iterator i = DocEntries.begin();
344 i != DocEntries.end(); ++i)
346 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
348 return ((ValEntry *)(*i))->GetValue(); //FIXME
353 //-----------------------------------------------------------------------------
357 //-----------------------------------------------------------------------------
359 } // end namespace gdcm