1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntry.cxx,v $
6 Date: $Date: 2005/02/02 10:02:17 $
7 Version: $Revision: 1.51 $
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 //-----------------------------------------------------------------------------
32 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
37 * \brief Constructor from a given DictEntry
38 * @param in Pointer to existing dictionary entry
40 DocEntry::DocEntry(DictEntry *in)
44 SetKey( in->GetKey( ) );
45 Offset = 0 ; // To avoid further missprinting
47 // init some variables
52 //-----------------------------------------------------------------------------
55 * \brief Writes the common part of any ValEntry, BinEntry, SeqEntry
56 * @param fp already open ofstream pointer
57 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
59 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
61 uint32_t ffff = 0xffffffff;
62 uint16_t group = GetGroup();
64 uint16_t el = GetElement();
65 uint32_t lgr = GetLength();
67 if ( group == 0xfffe && el == 0x0000 )
69 // Fix in order to make some MR PHILIPS images e-film readable
70 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
71 // we just *always* ignore spurious fffe|0000 tag !
75 // ----------- Writes the common part
77 binary_write( *fp, group); //group number
78 binary_write( *fp, el); //element number
80 if ( filetype == ExplicitVR )
82 // Special case of delimiters:
85 // Delimiters have NO Value Representation
86 // Hence we skip writing the VR.
87 // In order to avoid further troubles, we choose to write them
88 // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
89 // We shall force the end of a given Item by writting
90 // a Item Delimitation Item (fffe, e00d)
92 uint32_t ff = 0xffffffff;
93 binary_write(*fp, ff);
98 uint16_t shortLgr = lgr;
100 if (vr == GDCM_UNKNOWN)
102 // Unknown was 'written'
103 // deal with Little Endian
104 binary_write(*fp, shortLgr);
105 binary_write(*fp, z);
109 binary_write(*fp, vr);
110 gdcmAssertMacro( vr.size() == 2 );
112 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
114 binary_write(*fp, z);
117 // we set SQ length to ffffffff
118 // and we shall write a Sequence Delimitor Item
119 // at the end of the Sequence!
120 binary_write(*fp, ffff);
124 binary_write(*fp, lgr);
129 binary_write(*fp, shortLgr);
137 binary_write(*fp, ffff);
141 binary_write(*fp, lgr);
147 * \brief Gets the full length of the elementary DocEntry (not only value
148 * length) depending on the VR.
150 uint32_t DocEntry::GetFullLength()
152 uint32_t l = GetReadLength();
153 if ( IsImplicitVR() )
155 l = l + 8; // 2 (gr) + 2 (el) + 4 (lgth)
159 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
161 l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
165 l = l + 8; // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
172 * \brief Copies all the attributes from an other DocEntry
173 * @param doc entry to copy from
175 void DocEntry::Copy(DocEntry *doc)
177 Length = doc->Length;
178 ReadLength = doc->ReadLength;
179 ImplicitVR = doc->ImplicitVR;
180 Offset = doc->Offset;
184 * \brief tells us if entry is the last one of a 'no length' SequenceItem
187 bool DocEntry::IsItemDelimitor()
189 return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
193 * \brief tells us if entry is the last one of a 'no length' Sequence
196 bool DocEntry::IsSequenceDelimitor()
198 return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
201 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
207 //-----------------------------------------------------------------------------
210 * \brief Prints the common part of ValEntry, BinEntry, SeqEntry
211 * @param os ostream we want to print in
212 * @param indent Indentation string to be prepended during printing
214 void DocEntry::Print(std::ostream &os, std::string const & )
220 std::ostringstream s;
228 s << DictEntry::TranslateToKey(GetGroup(),GetElement());
233 lgth = GetReadLength(); // ReadLength, as opposed to Length
234 if (lgth == 0xffffffff)
236 st = Util::Format("x(ffff)"); // I said : "x(ffff)" !
237 s.setf(std::ios::left);
238 s << std::setw(10-st.size()) << " ";
240 s.setf(std::ios::left);
241 s << std::setw(8) << "-1";
245 st = Util::Format("x(%x)",lgth);
246 s.setf(std::ios::left);
247 s << std::setw(10-st.size()) << " ";
249 s.setf(std::ios::left);
250 s << std::setw(8) << lgth;
253 st = Util::Format("x(%x)",o);
254 s << std::setw(10-st.size()) << " ";
256 s << std::setw(8) << o;
259 s << "[" << vr << "] ";
263 s.setf(std::ios::left);
264 s << std::setw(66-GetName().length()) << " ";
267 s << "[" << GetName()<< "]";
271 //-----------------------------------------------------------------------------
272 } // end namespace gdcm