1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/11/04 15:08:50 $
7 Version: $Revision: 1.35 $
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"
25 #include "gdcmDebug.h"
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
34 * \brief Constructor from a given SQItem
36 SQItem::SQItem(int depthLevel )
39 SQDepthLevel = depthLevel;
43 * \brief Canonical destructor.
47 for(ListDocEntry::iterator cc = DocEntries.begin();
48 cc != DocEntries.end();
56 //-----------------------------------------------------------------------------
59 * \brief canonical Printer
61 void SQItem::Print(std::ostream& os)
67 for (int i = 0; i < SQDepthLevel; ++i)
72 //std::cout << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
73 for (ListDocEntry::iterator i = DocEntries.begin();
74 i != DocEntries.end();
78 bool PrintEndLine = true;
81 Entry->SetPrintLevel(2);
83 if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(Entry) )
85 (void)seqEntry; //not used
97 * \brief canonical Writer
99 void SQItem::Write(std::ofstream* fp, FileType filetype)
102 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
103 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
105 //we force the writting of an 'Item' Start Element
106 // because we want to write the Item as a 'no Length' item
109 binary_write( *fp, item[j]); // fffe e000 ffff ffff
112 for (ListDocEntry::iterator i = DocEntries.begin();
113 i != DocEntries.end();
116 // we skip delimitors (start and end one) because
117 // we force them as 'no length'
118 if ( (*i)->GetGroup() == 0xfffe )
123 // Fix in order to make some MR PHILIPS images e-film readable
124 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
125 // we just *always* ignore spurious fffe|0000 tag !
126 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0x0000 )
128 break; // FIXME : continue; ?!?
131 (*i)->Write(fp, filetype);
134 //we force the writting of an 'Item Delimitation' item
135 // because we wrote the Item as a 'no Length' item
138 binary_write( *fp, itemt[j]); // fffe e000 ffff ffff
143 //-----------------------------------------------------------------------------
146 * \brief adds any Entry (Dicom Element) to the Sequence Item
148 bool SQItem::AddEntry(DocEntry* entry)
150 DocEntries.push_back(entry);
151 //TODO : check if it worked
156 * \brief Sets Entry (Dicom Element) value of an element,
157 * specified by it's tag (Group, Number)
158 * and the length, too ... inside a SQ Item
159 * If the Element is not found, it's just created !
160 * \warning we suppose, right now, the element belongs to a Public Group
162 * @param val string value to set
163 * @param group Group number of the searched tag.
164 * @param element Element number of the searched tag.
165 * @return true if element was found or created successfully
168 bool SQItem::SetEntryByNumber(std::string const & val, uint16_t group,
171 for(ListDocEntry::iterator i = DocEntries.begin();
172 i != DocEntries.end();
175 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
180 if ( ( group < (*i)->GetGroup() )
181 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
183 // instead of ReplaceOrCreateByNumber
184 // that is a method of Document :-(
186 TagKey key = DictEntry::TranslateToKey(group, element);
188 if ( ! PtagHT->count(key))
190 // we assume a Public Dictionnary *is* loaded
191 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
192 // if the invoked (group,elem) doesn't exist inside the Dictionary
193 // we create a VirtualDictEntry
194 DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, element);
195 if (dictEntry == NULL)
198 Global::GetDicts()->NewVirtualDictEntry(group, element,
201 // we assume the constructor didn't fail
202 entry = new ValEntry(dictEntry);
205 /// better we don't assume too much !
206 /// SQItem is now used to describe any DICOMDIR related object
210 DocEntry* foundEntry = PtagHT->find(key)->second;
211 entry = dynamic_cast<ValEntry*>(foundEntry);
214 dbg.Verbose(0, "SQItem::SetEntryByNumber: docEntries"
215 " contains non ValEntry occurences");
220 entry->SetValue(val);
222 entry->SetLength(val.length());
223 DocEntries.insert(i,entry);
227 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
229 if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
231 entry->SetValue(val);
233 (*i)->SetLength(val.length());
239 //-----------------------------------------------------------------------------
244 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
247 DocEntry* SQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
249 for(ListDocEntry::iterator i = DocEntries.begin();
250 i != DocEntries.end(); ++i)
252 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
261 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
265 std::string SQItem::GetEntryByNumber(uint16_t group, uint16_t element)
267 for(ListDocEntry::iterator i = DocEntries.begin();
268 i != DocEntries.end(); ++i)
270 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
272 return ((ValEntry *)(*i))->GetValue(); //FIXME
277 //-----------------------------------------------------------------------------
281 //-----------------------------------------------------------------------------
283 } // end namespace gdcm