1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntry.cxx,v $
6 Date: $Date: 2004/06/23 13:02:36 $
7 Version: $Revision: 1.9 $
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) {
41 //-----------------------------------------------------------------------------
44 * \ingroup gdcmDocEntry
45 * \brief Prints the common part of gdcmValEntry, gdcmBinEntry, gdcmSeqEntry
46 * @param os ostream we want to print in
48 void gdcmDocEntry::Print(std::ostream & os) {
50 printLevel=2; // FIXME
53 unsigned short int g, e;
59 char greltag[10]; //group element tag
65 sprintf(greltag,"%04x|%04x ",g,e);
70 lgth = GetReadLength(); // ReadLength, as opposed to UsableLength
71 if (lgth == 0xffffffff) {
72 sprintf(st,"x(ffff)"); // I said : "x(ffff)" !
73 s.setf(std::ios::left);
74 s << std::setw(10-strlen(st)) << " ";
76 s.setf(std::ios::left);
77 s << std::setw(8) << "-1";
79 sprintf(st,"x(%x)",lgth);
80 s.setf(std::ios::left);
81 s << std::setw(10-strlen(st)) << " ";
83 s.setf(std::ios::left);
84 s << std::setw(8) << lgth;
87 sprintf(st,"x(%x)",o);
88 s << std::setw(10-strlen(st)) << " ";
90 s << std::setw(8) << o;
93 s << "[" << vr << "] ";
96 s.setf(std::ios::left);
97 s << std::setw(66-GetName().length()) << " ";
100 s << "[" << GetName()<< "]";
105 * \ingroup gdcmDocEntry
106 * \brief Writes the common part of any gdcmValEntry, gdcmBinEntry, gdcmSeqEntry
108 void gdcmDocEntry::Write(FILE *fp, FileType filetype) {
110 guint16 group = GetGroup();
112 guint16 el = GetElement();
113 guint32 lgr = GetReadLength();
115 if ( (group == 0xfffe) && (el == 0x0000) )
116 // Fix in order to make some MR PHILIPS images e-film readable
117 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
118 // we just *always* ignore spurious fffe|0000 tag !
122 // ----------- Writes the common part
124 fwrite ( &group,(size_t)2 ,(size_t)1 ,fp); //group
125 fwrite ( &el, (size_t)2 ,(size_t)1 ,fp); //element
127 if ( filetype == gdcmExplicitVR ) {
129 // Special case of delimiters:
130 if (group == 0xfffe) {
131 // Delimiters have NO Value Representation
132 // Hence we skip writing the VR.
133 // In order to avoid further troubles, we choose to write them
134 // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
135 // The end of a given Item will be found when :
136 // - a new Item Delimitor Item is encountered (the Seq goes on)
137 // - a Sequence Delimitor Item is encountered (the Seq just ended)
139 // TODO : verify if the Sequence Delimitor Item was forced during Parsing
142 fwrite (&ff,(size_t)4 ,(size_t)1 ,fp);
147 guint16 shortLgr = lgr;
148 if (vr == "unkn") { // Unknown was 'written'
149 // deal with Little Endian
150 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
151 fwrite ( &z, (size_t)2 ,(size_t)1 ,fp);
153 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,fp);
155 // TODO : better we set SQ length to ffffffff
156 // and write a Sequence Delimitor Item at the end of the Sequence!
157 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
159 fwrite ( &z, (size_t)2 ,(size_t)1 ,fp);
160 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
162 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
168 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
172 //-----------------------------------------------------------------------------
176 * \ingroup gdcmDocEntry
177 * \brief Gets the full length of the elementary DocEntry (not only value length)
179 guint32 gdcmDocEntry::GetFullLength(void) {
182 if ( IsImplicitVR() )
183 l = l + 8; // 2 (gr) + 2 (el) + 4 (lgth)
185 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
186 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)
193 * \ingroup gdcmDocEntry
194 * \brief Copies all the attributes from an other DocEntry
196 void gdcmDocEntry::Copy (gdcmDocEntry* e) {
197 this->entry = e->entry;
198 this->UsableLength = e->UsableLength;
199 this->ReadLength = e->ReadLength;
200 this->ImplicitVR = e->ImplicitVR;
201 this->Offset = e->Offset;
202 this->printLevel = e->printLevel;
203 // TODO : remove gdcmDocEntry SQDepth
207 * \ingroup gdcmDocEntry
208 * \brief tells us if entry is the first one of a Sequence Item (fffe,e00d)
210 bool gdcmDocEntry::isItemDelimitor() {
211 if ( (GetGroup() == 0xfffe) && (GetElement() == 0xe00d) )
217 * \ingroup gdcmDocEntry
218 * \brief tells us if entry is the last one of a 'no length' Sequence fffe,e0dd)
220 bool gdcmDocEntry::isSequenceDelimitor() {
221 if (GetGroup() == 0xfffe && GetElement() == 0xe0dd)
227 //-----------------------------------------------------------------------------
231 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------