1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntry.cxx,v $
6 Date: $Date: 2005/03/11 11:08:31 $
7 Version: $Revision: 1.54 $
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 SetKey( in->GetKey( ) );
43 Offset = 0 ; // To avoid further missprinting
45 // init some variables
50 //-----------------------------------------------------------------------------
53 * \brief Writes the common part of any ValEntry, BinEntry, SeqEntry
54 * @param fp already open ofstream pointer
55 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
57 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
59 uint32_t ffff = 0xffffffff;
60 uint16_t group = GetGroup();
62 uint16_t el = GetElement();
63 uint32_t lgth = GetLength();
65 if ( group == 0xfffe && el == 0x0000 )
67 // Fix in order to make some MR PHILIPS images e-film readable
68 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
69 // we just *always* ignore spurious fffe|0000 tag !
73 // ----------- Writes the common part
75 binary_write( *fp, group); //group number
76 binary_write( *fp, el); //element number
78 if ( filetype == ExplicitVR )
80 // Special case of delimiters:
83 // Delimiters have NO Value Representation
84 // Hence we skip writing the VR.
85 // In order to avoid further troubles, we choose to write them
86 // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
87 // We shall force the end of a given Item by writting
88 // a Item Delimitation Item (fffe, e00d)
90 uint32_t ff = 0xffffffff;
91 binary_write(*fp, ff);
96 uint16_t shortLgr = lgth;
98 if (vr == GDCM_UNKNOWN)
100 // Unknown was 'written'
101 // deal with Little Endian
102 binary_write(*fp, shortLgr);
103 binary_write(*fp, z);
107 binary_write(*fp, vr);
108 gdcmAssertMacro( vr.size() == 2 );
110 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
112 binary_write(*fp, z);
115 // we set SQ length to ffffffff
116 // and we shall write a Sequence Delimitor Item
117 // at the end of the Sequence!
118 binary_write(*fp, ffff);
122 binary_write(*fp, lgth);
127 binary_write(*fp, shortLgr);
135 binary_write(*fp, ffff);
139 binary_write(*fp, lgth);
145 * \brief Gets the full length of the elementary DocEntry (not only value
146 * length) depending on the VR.
148 uint32_t DocEntry::GetFullLength()
150 uint32_t l = GetReadLength();
151 if ( IsImplicitVR() )
153 l = l + 8; // 2 (gr) + 2 (el) + 4 (lgth)
157 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
159 l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
163 l = l + 8; // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
170 * \brief tells us if entry is the last one of a 'no length' SequenceItem
173 bool DocEntry::IsItemDelimitor()
175 return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
179 * \brief tells us if entry is the last one of a 'no length' Sequence
182 bool DocEntry::IsSequenceDelimitor()
184 return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
188 * \brief Copies all the attributes from an other DocEntry
189 * @param doc entry to copy from
191 void DocEntry::Copy(DocEntry *doc)
193 Length = doc->Length;
194 ReadLength = doc->ReadLength;
195 ImplicitVR = doc->ImplicitVR;
196 Offset = doc->Offset;
199 //-----------------------------------------------------------------------------
202 //-----------------------------------------------------------------------------
205 //-----------------------------------------------------------------------------
208 * \brief Prints the common part of ValEntry, BinEntry, SeqEntry
209 * @param os ostream we want to print in
210 * @param indent Indentation string to be prepended during printing
212 void DocEntry::Print(std::ostream &os, std::string const & )
218 std::ostringstream s;
226 s << DictEntry::TranslateToKey(GetGroup(),GetElement());
231 lgth = GetReadLength(); // ReadLength, as opposed to Length
232 if (lgth == 0xffffffff)
234 st = Util::Format("x(ffff)"); // I said : "x(ffff)" !
235 s.setf(std::ios::left);
236 s << std::setw(10-st.size()) << " ";
238 s.setf(std::ios::left);
239 s << std::setw(8) << "-1";
243 st = Util::Format("x(%x)",lgth);
244 s.setf(std::ios::left);
245 s << std::setw(10-st.size()) << " ";
247 s.setf(std::ios::left);
248 s << std::setw(8) << lgth;
251 st = Util::Format("x(%x)",o);
252 s << std::setw(10-st.size()) << " ";
254 s << std::setw(8) << o;
257 s << "[" << vr << "] ";
261 s.setf(std::ios::left);
262 s << std::setw(66-GetName().length()) << " ";
265 s << "[" << GetName()<< "]";
269 //-----------------------------------------------------------------------------
270 } // end namespace gdcm