1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntry.cxx,v $
6 Date: $Date: 2006/07/06 16:57:06 $
7 Version: $Revision: 1.86 $
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"
20 #include "gdcmDataEntry.h"
23 #include "gdcmGlobal.h"
25 #include "gdcmDebug.h"
26 #include "gdcmDictSet.h"
27 #include <iomanip> // for std::ios::left, ...
32 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
36 * \brief Constructor from a given DictEntry
37 * @param group Group number
38 * @param elem Element number
41 DocEntry::DocEntry(uint16_t group, uint16_t elem, VRKey const &vr)
45 Offset = 0 ; // To avoid further missprinting
47 // init some variables
52 Key.SetGroupElem(group,elem);
56 * \brief Destructor from a given DictEntry
62 gdcmAssertMacro(DicomDict);
63 DicomDict->Unregister();
67 //-----------------------------------------------------------------------------
70 * \brief Writes the common part of any DataEntry, SeqEntry
71 * @param fp already open ofstream pointer
72 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
74 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
76 uint32_t ffff = 0xffffffff;
77 uint16_t group = GetGroup();
79 ///\todo allow skipping Shadow groups
82 uint16_t elem = GetElement();
83 uint32_t lgth = GetLength();
85 if ( group == 0xfffe && elem == 0x0000 )
87 // Fix in order to make some MR PHILIPS images e-film readable
88 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
89 // we just *always* ignore spurious fffe|0000 tag !
94 // ----------- Writes the common part
96 // To avoid gdcm to propagate oddities.
97 // --> Don't forget to *write* an even length value
101 // ----------- Writes the common part : the Tag
102 binary_write( *fp, group); //group number
103 binary_write( *fp, elem); //element number
105 // Dicom V3 group 0x0002 is *always* Explicit VR !
106 if ( filetype == ExplicitVR || filetype == JPEG || filetype == JPEG2000 || group == 0x0002 )
108 // ----------- Writes the common part : the VR + the length
110 // Special case of delimiters:
113 // Delimiters have NO Value Representation
114 // Hence we skip writing the VR.
116 // In order to avoid further troubles, we choose to write them
117 // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
118 // We shall force the end of a given SeqItem by writting
119 // a Item Delimitation Item (fffe, e00d)
121 uint32_t ff = 0xffffffff;
122 binary_write(*fp, ff);
126 uint16_t shortLgr = (uint16_t)lgth;
130 // GDCM_VRUNKNOWN was stored in the Entry VR;
131 // deal with Entry as if TS were Implicit VR
132 binary_write(*fp, lgth);
136 binary_write(*fp, vr.str());
137 // See PS 3.5-2004 page 33, 36
138 if ( (vr == "SQ") || (vr == "OB") || (vr == "OW") || (vr == "OF")
139 || (vr == "UN") || (vr == "UT") )
141 binary_write(*fp, zero);
142 if ( (filetype == JPEG || filetype == JPEG2000) && group == 0x7fe0 && elem == 0x0010)
144 gdcmAssertMacro( GetVR() == "OW" );
145 binary_write(*fp, ffff);
149 // we set SQ length to ffffffff
150 // and we shall write a Sequence Delimitor Item
151 // at the end of the Sequence!
152 binary_write(*fp, ffff);
156 binary_write(*fp, lgth);
161 binary_write(*fp, shortLgr);
167 // ----------- Writes the common part : the VR
170 binary_write(*fp, ffff);
174 binary_write(*fp, lgth);
179 /// \brief Returns the 'Name' '(e.g. "Patient's Name") found in the Dicom
180 /// Dictionnary of the current Dicom Header Entry
181 std::string const &DocEntry::GetName()
185 Global::GetDicts()->GetDefaultPubDict()->GetEntry(Key[0],Key[1]);
190 DicomDict->Register();
191 return DicomDict->GetName();
195 /// \brief Returns the 'Value Multiplicity' (e.g. "1", "6", "1-n", "3-n"),
196 /// found in the Dicom entry or in the Dicom Dictionnary
197 /// of the current Dicom entry
198 std::string const &DocEntry::GetVM()
202 Global::GetDicts()->GetDefaultPubDict()->GetEntry(Key[0],Key[1]);
207 DicomDict->Register();
208 return DicomDict->GetVM();
213 * \brief Gets the full length of the elementary DocEntry (not only value
214 * length) depending on the VR.
216 uint32_t DocEntry::GetFullLength()
218 uint32_t l = GetReadLength();
219 if ( IsImplicitVR() )
221 l = l + 8; // 2 (gr) + 2 (el) + 4 (lgth)
225 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
227 l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
231 l = l + 8; // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
238 * \brief Copies all the attributes from an other DocEntry
239 * @param doc entry to copy from
241 void DocEntry::Copy(DocEntry *doc)
243 Length = doc->Length;
244 ReadLength = doc->ReadLength;
245 ImplicitVR = doc->ImplicitVR;
246 Offset = doc->Offset;
249 //-----------------------------------------------------------------------------
252 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
258 * \brief Prints the common part of DataEntry, SeqEntry
259 * @param os ostream we want to print in
260 * @param indent Indentation string to be prepended during printing
262 void DocEntry::Print(std::ostream &os, std::string const & )
269 std::ostringstream s;
274 if ( vr == GDCM_VRUNKNOWN )
277 s << DictEntry::TranslateToKey(GetGroup(),GetElement());
282 lgth = GetReadLength(); // ReadLength, as opposed to (usable) Length
283 if (lgth == 0xffffffff)
286 s.setf(std::ios::left);
289 s.setf(std::ios::left);
290 s << std::setw(8) << "-1";
294 st = Util::Format("x(%x)",lgth); // we may keep it
295 s.setf(std::ios::left);
296 s << std::setw(11-st.size()) << " ";
298 s.setf(std::ios::left);
299 s << std::setw(8) << lgth;
302 st = Util::Format("x(%x)",o); // we may keep it
303 s << std::setw(11-st.size()) << " ";
305 s << std::setw(8) << o;
310 s << "[" << vr << "] ";
313 if ( GetElement() == 0x0000 )
314 name = "Group Length";
320 s.setf(std::ios::left);
321 s << std::setw(66-name.length()) << " ";
324 s << "[" << name << "]";
328 //-----------------------------------------------------------------------------
329 } // end namespace gdcm