1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntry.cxx,v $
6 Date: $Date: 2005/09/21 09:44:59 $
7 Version: $Revision: 1.69 $
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 // Dicom V3 group 0x0002 is *always* Explicit VR !
79 if ( filetype == ExplicitVR || group == 0x0002 )
81 // Special case of delimiters:
84 // Delimiters have NO Value Representation
85 // 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 SeqItem by writting
90 // a Item Delimitation Item (fffe, e00d)
92 uint32_t ff = 0xffffffff;
93 binary_write(*fp, ff);
98 uint16_t shortLgr = (uint16_t)lgth;
100 if (vr == GDCM_UNKNOWN)
102 // GDCM_UNKNOWN was stored in the Entry VR;
103 // deal with Entry as if TS were Implicit VR
105 // FIXME : troubles expected on big endian processors :
106 // let lgth = 0x00001234
107 // we write 34 12 00 00 on little endian proc (OK)
108 // we write 12 34 00 00 on big endian proc (KO)
109 //binary_write(*fp, shortLgr);
110 //binary_write(*fp, zero);
112 binary_write(*fp, lgth);
116 binary_write(*fp, vr);
117 gdcmAssertMacro( vr.size() == 2 );
119 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") /*|| (vr == "UN")*/ )
121 binary_write(*fp, zero);
124 // we set SQ length to ffffffff
125 // and we shall write a Sequence Delimitor Item
126 // at the end of the Sequence!
127 binary_write(*fp, ffff);
131 binary_write(*fp, lgth);
136 binary_write(*fp, shortLgr);
144 binary_write(*fp, ffff);
148 binary_write(*fp, lgth);
154 * \brief Gets the full length of the elementary DocEntry (not only value
155 * length) depending on the VR.
157 uint32_t DocEntry::GetFullLength()
159 uint32_t l = GetReadLength();
160 if ( IsImplicitVR() )
162 l = l + 8; // 2 (gr) + 2 (el) + 4 (lgth)
166 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
168 l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
172 l = l + 8; // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
179 * \brief tells us if entry is the last one of a 'no length' SequenceItem
182 bool DocEntry::IsItemDelimitor()
184 return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
188 * \brief tells us if entry is the first one of an Item
191 bool DocEntry::IsItemStarter()
193 return (GetGroup() == 0xfffe && GetElement() == 0xe000);
197 * \brief tells us if entry is the last one of a 'no length' Sequence
200 bool DocEntry::IsSequenceDelimitor()
202 return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
206 * \brief Copies all the attributes from an other DocEntry
207 * @param doc entry to copy from
209 void DocEntry::Copy(DocEntry *doc)
211 Length = doc->Length;
212 ReadLength = doc->ReadLength;
213 ImplicitVR = doc->ImplicitVR;
214 Offset = doc->Offset;
217 //-----------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------
223 //-----------------------------------------------------------------------------
226 * \brief Prints the common part of ValEntry, BinEntry, SeqEntry
227 * @param os ostream we want to print in
228 * @param indent Indentation string to be prepended during printing
230 void DocEntry::Print(std::ostream &os, std::string const & )
236 std::ostringstream s;
241 if ( vr==GDCM_UNKNOWN )
244 s << DictEntry::TranslateToKey(GetGroup(),GetElement());
249 lgth = GetReadLength(); // ReadLength, as opposed to (usable) Length
250 if (lgth == 0xffffffff)
253 s.setf(std::ios::left);
256 s.setf(std::ios::left);
257 s << std::setw(8) << "-1";
261 st = Util::Format("x(%x)",lgth); // we may keep it
262 s.setf(std::ios::left);
263 s << std::setw(11-st.size()) << " ";
265 s.setf(std::ios::left);
266 s << std::setw(8) << lgth;
269 st = Util::Format("x(%x)",o); // we may keep it
270 s << std::setw(11-st.size()) << " ";
272 s << std::setw(8) << o;
277 s << "[" << vr << "] ";
280 if ( GetElement() == 0x0000 )
281 name = "Group Length";
287 s.setf(std::ios::left);
288 s << std::setw(66-name.length()) << " ";
291 s << "[" << name << "]";
295 //-----------------------------------------------------------------------------
296 } // end namespace gdcm