1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntry.cxx,v $
6 Date: $Date: 2004/09/17 08:54:26 $
7 Version: $Revision: 1.20 $
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.htm 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"
21 #include "gdcmGlobal.h"
24 #include <iomanip> // for std::ios::left, ...
27 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
32 * \ingroup gdcmDocEntry
33 * \brief Constructor from a given gdcmDictEntry
34 * @param in Pointer to existing dictionary entry
36 gdcmDocEntry::gdcmDocEntry(gdcmDictEntry* in)
40 SetKey( in->GetKey( ) );
41 Offset = 0 ; // To avoid further missprinting
44 //-----------------------------------------------------------------------------
47 * \ingroup gdcmDocEntry
48 * \brief Prints the common part of gdcmValEntry, gdcmBinEntry, gdcmSeqEntry
49 * @param os ostream we want to print in
51 void gdcmDocEntry::Print(std::ostream & os)
53 PrintLevel = 2; // FIXME
56 unsigned short int g, e;
73 lgth = GetReadLength(); // ReadLength, as opposed to UsableLength
74 if (lgth == 0xffffffff)
76 sprintf(st,"x(ffff)"); // I said : "x(ffff)" !
77 s.setf(std::ios::left);
78 s << std::setw(10-strlen(st)) << " ";
80 s.setf(std::ios::left);
81 s << std::setw(8) << "-1";
85 sprintf(st,"x(%x)",lgth);
86 s.setf(std::ios::left);
87 s << std::setw(10-strlen(st)) << " ";
89 s.setf(std::ios::left);
90 s << std::setw(8) << lgth;
93 sprintf(st,"x(%x)",o);
94 s << std::setw(10-strlen(st)) << " ";
96 s << std::setw(8) << o;
99 s << "[" << vr << "] ";
103 s.setf(std::ios::left);
104 s << std::setw(66-GetName().length()) << " ";
107 s << "[" << GetName()<< "]";
112 * \ingroup gdcmDocEntry
113 * \brief Writes the common part of any gdcmValEntry, gdcmBinEntry, gdcmSeqEntry
114 * @param fp already open file pointer
115 * @param filetype type of the file to be written
117 void gdcmDocEntry::Write(FILE *fp, FileType filetype)
119 uint32_t FFFF = 0xffffffff;
120 uint16_t group = GetGroup();
121 gdcmVRKey vr = GetVR();
122 uint16_t el = GetElement();
123 uint32_t lgr = GetReadLength();
125 if ( group == 0xfffe && el == 0x0000 )
127 // Fix in order to make some MR PHILIPS images e-film readable
128 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
129 // we just *always* ignore spurious fffe|0000 tag !
134 // ----------- Writes the common part
136 fwrite ( &group,(size_t)2 ,(size_t)1 ,fp); //group
137 fwrite ( &el, (size_t)2 ,(size_t)1 ,fp); //element
139 if ( filetype == gdcmExplicitVR )
141 // Special case of delimiters:
144 // Delimiters have NO Value Representation
145 // Hence we skip writing the VR.
146 // In order to avoid further troubles, we choose to write them
147 // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
148 // The end of a given Item will be found when :
149 // - a new Item Delimitor Item is encountered (the Seq goes on)
150 // - a Sequence Delimitor Item is encountered (the Seq just ended)
152 // TODO : verify if the Sequence Delimitor Item was forced during Parsing
155 fwrite (&ff,(size_t)4 ,(size_t)1 ,fp);
160 uint16_t shortLgr = lgr;
164 // Unknown was 'written'
165 // deal with Little Endian
166 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
167 fwrite ( &z, (size_t)2 ,(size_t)1 ,fp);
171 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,fp);
173 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
175 fwrite ( &z, (size_t)2 ,(size_t)1 ,fp);
178 // we set SQ length to ffffffff
179 // and we shall write a Sequence Delimitor Item
180 // at the end of the Sequence!
181 fwrite ( &FFFF,(size_t)4 ,(size_t)1 ,fp);
185 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
190 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
198 fwrite ( &FFFF,(size_t)4 ,(size_t)1 ,fp);
202 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
207 //-----------------------------------------------------------------------------
211 * \ingroup gdcmDocEntry
212 * \brief Gets the full length of the elementary DocEntry (not only value length)
214 uint32_t gdcmDocEntry::GetFullLength()
216 uint32_t l = GetReadLength();
217 if ( IsImplicitVR() )
219 l = l + 8; // 2 (gr) + 2 (el) + 4 (lgth)
223 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
225 l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
229 l = l + 8; // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
236 * \ingroup gdcmDocEntry
237 * \brief Copies all the attributes from an other DocEntry
239 void gdcmDocEntry::Copy (gdcmDocEntry* e)
241 DictEntry = e->DictEntry;
242 UsableLength = e->UsableLength;
243 ReadLength = e->ReadLength;
244 ImplicitVR = e->ImplicitVR;
246 PrintLevel = e->PrintLevel;
247 // TODO : remove gdcmDocEntry SQDepth
251 * \ingroup gdcmDocEntry
252 * \brief tells us if entry is the last one of a 'no length' SequenceItem
255 bool gdcmDocEntry::IsItemDelimitor()
257 return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
260 * \ingroup gdcmDocEntry
261 * \brief tells us if entry is the last one of a 'no length' Sequence
264 bool gdcmDocEntry::IsSequenceDelimitor()
266 return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
269 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
276 //-----------------------------------------------------------------------------