1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntry.cxx,v $
6 Date: $Date: 2005/11/21 12:15:06 $
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 "gdcmDocEntry.h"
22 #include "gdcmGlobal.h"
24 #include "gdcmDebug.h"
26 #include <iomanip> // for std::ios::left, ...
31 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
35 * \brief Constructor from a given DictEntry
36 * @param in Pointer to existing dictionary entry
38 DocEntry::DocEntry(DictEntry *in)
42 Offset = 0 ; // To avoid further missprinting
44 // init some variables
48 gdcmAssertMacro(DicomDict);
49 DicomDict->Register();
53 * \brief Destructor from a given DictEntry
57 gdcmAssertMacro(DicomDict);
59 DicomDict->Unregister();
62 //-----------------------------------------------------------------------------
65 * \brief Writes the common part of any DataEntry, SeqEntry
66 * @param fp already open ofstream pointer
67 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
69 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
71 uint32_t ffff = 0xffffffff;
72 uint16_t group = GetGroup();
74 uint16_t el = GetElement();
75 uint32_t lgth = GetLength();
77 if ( group == 0xfffe && el == 0x0000 )
79 // Fix in order to make some MR PHILIPS images e-film readable
80 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
81 // we just *always* ignore spurious fffe|0000 tag !
85 // ----------- Writes the common part
87 // To avoid gdcm to propagate oddities.
88 // --> Don't forget to *write* an even length value
92 // ----------- Writes the common part : the Tag
93 binary_write( *fp, group); //group number
94 binary_write( *fp, el); //element number
96 // Dicom V3 group 0x0002 is *always* Explicit VR !
97 if ( filetype == ExplicitVR || filetype == JPEG || group == 0x0002 )
100 // ----------- Writes the common part : the VR + the length
102 // Special case of delimiters:
105 // Delimiters have NO Value Representation
106 // Hence we skip writing the VR.
108 // In order to avoid further troubles, we choose to write them
109 // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
110 // We shall force the end of a given SeqItem by writting
111 // a Item Delimitation Item (fffe, e00d)
113 uint32_t ff = 0xffffffff;
114 binary_write(*fp, ff);
119 uint16_t shortLgr = (uint16_t)lgth;
123 // GDCM_VRUNKNOWN was stored in the Entry VR;
124 // deal with Entry as if TS were Implicit VR
126 binary_write(*fp, lgth);
130 binary_write(*fp, vr.str());
132 // See PS 3.5-2004 page 33, 36
133 if ( (vr == "SQ") || (vr == "OB") || (vr == "OW") || (vr == "OF")
134 || (vr == "UN") || (vr == "UT") )
136 binary_write(*fp, zero);
139 // we set SQ length to ffffffff
140 // and we shall write a Sequence Delimitor Item
141 // at the end of the Sequence!
142 binary_write(*fp, ffff);
146 binary_write(*fp, lgth);
151 binary_write(*fp, shortLgr);
157 // ----------- Writes the common part : the VR
160 binary_write(*fp, ffff);
164 binary_write(*fp, lgth);
170 * \brief Gets the full length of the elementary DocEntry (not only value
171 * length) depending on the VR.
173 uint32_t DocEntry::GetFullLength()
175 uint32_t l = GetReadLength();
176 if ( IsImplicitVR() )
178 l = l + 8; // 2 (gr) + 2 (el) + 4 (lgth)
182 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
184 l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
188 l = l + 8; // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
195 * \brief tells us if entry is the last one of a 'no length' SequenceItem
198 bool DocEntry::IsItemDelimitor()
200 return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
204 * \brief tells us if entry is the first one of an Item
207 bool DocEntry::IsItemStarter()
209 return (GetGroup() == 0xfffe && GetElement() == 0xe000);
213 * \brief tells us if entry is the last one of a 'no length' Sequence
216 bool DocEntry::IsSequenceDelimitor()
218 return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
222 * \brief Copies all the attributes from an other DocEntry
223 * @param doc entry to copy from
225 void DocEntry::Copy(DocEntry *doc)
227 Length = doc->Length;
228 ReadLength = doc->ReadLength;
229 ImplicitVR = doc->ImplicitVR;
230 Offset = doc->Offset;
233 //-----------------------------------------------------------------------------
236 //-----------------------------------------------------------------------------
239 //-----------------------------------------------------------------------------
242 * \brief Prints the common part of DataEntry, SeqEntry
243 * @param os ostream we want to print in
244 * @param indent Indentation string to be prepended during printing
246 void DocEntry::Print(std::ostream &os, std::string const & )
253 std::ostringstream s;
258 if ( vr == GDCM_VRUNKNOWN )
261 s << DictEntry::TranslateToKey(GetGroup(),GetElement());
266 lgth = GetReadLength(); // ReadLength, as opposed to (usable) Length
267 if (lgth == 0xffffffff)
270 s.setf(std::ios::left);
273 s.setf(std::ios::left);
274 s << std::setw(8) << "-1";
278 st = Util::Format("x(%x)",lgth); // we may keep it
279 s.setf(std::ios::left);
280 s << std::setw(11-st.size()) << " ";
282 s.setf(std::ios::left);
283 s << std::setw(8) << lgth;
286 st = Util::Format("x(%x)",o); // we may keep it
287 s << std::setw(11-st.size()) << " ";
289 s << std::setw(8) << o;
294 s << "[" << vr << "] ";
297 if ( GetElement() == 0x0000 )
298 name = "Group Length";
304 s.setf(std::ios::left);
305 s << std::setw(66-name.length()) << " ";
308 s << "[" << name << "]";
312 //-----------------------------------------------------------------------------
313 } // end namespace gdcm