1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntry.cxx,v $
6 Date: $Date: 2005/12/21 14:52:12 $
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 "gdcmDocEntry.h"
20 #include "gdcmDataEntry.h"
23 #include "gdcmGlobal.h"
25 #include "gdcmDebug.h"
27 #include <iomanip> // for std::ios::left, ...
32 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
36 * \brief Constructor from a given DictEntry
37 * @param in Pointer to existing dictionary entry
39 DocEntry::DocEntry(DictEntry *in)
43 Offset = 0 ; // To avoid further missprinting
45 // init some variables
49 gdcmAssertMacro(DicomDict);
50 DicomDict->Register();
54 * \brief Destructor from a given DictEntry
58 gdcmAssertMacro(DicomDict);
60 DicomDict->Unregister();
63 //-----------------------------------------------------------------------------
66 * \brief Writes the common part of any DataEntry, SeqEntry
67 * @param fp already open ofstream pointer
68 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
70 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
72 uint32_t ffff = 0xffffffff;
73 uint16_t group = GetGroup();
75 ///\todo allow skipping Shadow groups
78 uint16_t elem = GetElement();
79 uint32_t lgth = GetLength();
81 if ( group == 0xfffe && elem == 0x0000 )
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 !
90 // ----------- Writes the common part
92 // To avoid gdcm to propagate oddities.
93 // --> Don't forget to *write* an even length value
97 // ----------- Writes the common part : the Tag
98 binary_write( *fp, group); //group number
99 binary_write( *fp, elem); //element number
101 // Dicom V3 group 0x0002 is *always* Explicit VR !
102 if ( filetype == ExplicitVR || filetype == JPEG || group == 0x0002 )
105 // ----------- Writes the common part : the VR + the length
107 // Special case of delimiters:
110 // Delimiters have NO Value Representation
111 // Hence we skip writing the VR.
113 // In order to avoid further troubles, we choose to write them
114 // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
115 // We shall force the end of a given SeqItem by writting
116 // a Item Delimitation Item (fffe, e00d)
118 uint32_t ff = 0xffffffff;
119 binary_write(*fp, ff);
124 uint16_t shortLgr = (uint16_t)lgth;
128 // GDCM_VRUNKNOWN was stored in the Entry VR;
129 // deal with Entry as if TS were Implicit VR
131 binary_write(*fp, lgth);
135 binary_write(*fp, vr.str());
137 // See PS 3.5-2004 page 33, 36
138 if ( (vr == "SQ") || (vr == "OB") || (vr == "OW") || (vr == "OF")
139 || (vr == "UN") || (vr == "UT") )
141 binary_write(*fp, zero);
144 // we set SQ length to ffffffff
145 // and we shall write a Sequence Delimitor Item
146 // at the end of the Sequence!
147 binary_write(*fp, ffff);
151 binary_write(*fp, lgth);
156 binary_write(*fp, shortLgr);
162 // ----------- Writes the common part : the VR
165 binary_write(*fp, ffff);
169 binary_write(*fp, lgth);
175 * \brief Gets the full length of the elementary DocEntry (not only value
176 * length) depending on the VR.
178 uint32_t DocEntry::GetFullLength()
180 uint32_t l = GetReadLength();
181 if ( IsImplicitVR() )
183 l = l + 8; // 2 (gr) + 2 (el) + 4 (lgth)
187 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
189 l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
193 l = l + 8; // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
200 * \brief tells us if entry is the last one of a 'no length' SequenceItem
203 bool DocEntry::IsItemDelimitor()
205 return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
209 * \brief tells us if entry is the first one of an Item
212 bool DocEntry::IsItemStarter()
214 return (GetGroup() == 0xfffe && GetElement() == 0xe000);
218 * \brief tells us if entry is the last one of a 'no length' Sequence
221 bool DocEntry::IsSequenceDelimitor()
223 return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
227 * \brief Copies all the attributes from an other DocEntry
228 * @param doc entry to copy from
230 void DocEntry::Copy(DocEntry *doc)
232 Length = doc->Length;
233 ReadLength = doc->ReadLength;
234 ImplicitVR = doc->ImplicitVR;
235 Offset = doc->Offset;
238 //-----------------------------------------------------------------------------
241 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
247 * \brief Prints the common part of DataEntry, SeqEntry
248 * @param os ostream we want to print in
249 * @param indent Indentation string to be prepended during printing
251 void DocEntry::Print(std::ostream &os, std::string const & )
258 std::ostringstream s;
263 if ( vr == GDCM_VRUNKNOWN )
266 s << DictEntry::TranslateToKey(GetGroup(),GetElement());
271 lgth = GetReadLength(); // ReadLength, as opposed to (usable) Length
272 if (lgth == 0xffffffff)
275 s.setf(std::ios::left);
278 s.setf(std::ios::left);
279 s << std::setw(8) << "-1";
283 st = Util::Format("x(%x)",lgth); // we may keep it
284 s.setf(std::ios::left);
285 s << std::setw(11-st.size()) << " ";
287 s.setf(std::ios::left);
288 s << std::setw(8) << lgth;
291 st = Util::Format("x(%x)",o); // we may keep it
292 s << std::setw(11-st.size()) << " ";
294 s << std::setw(8) << o;
299 s << "[" << vr << "] ";
302 if ( GetElement() == 0x0000 )
303 name = "Group Length";
309 s.setf(std::ios::left);
310 s << std::setw(66-name.length()) << " ";
313 s << "[" << name << "]";
317 //-----------------------------------------------------------------------------
318 } // end namespace gdcm