]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
Print output fixed for level==1
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/09/05 08:32:57 $
7   Version:   $Revision: 1.66 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDocEntry.h"
20 #include "gdcmTS.h"
21 #include "gdcmVR.h"
22 #include "gdcmGlobal.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDebug.h"
25
26 #include <iomanip> // for std::ios::left, ...
27 #include <fstream>
28
29 namespace gdcm 
30 {
31 //-----------------------------------------------------------------------------
32
33 // Constructor / Destructor
34 /**
35  * \brief   Constructor from a given DictEntry
36  * @param   in Pointer to existing dictionary entry
37  */
38 DocEntry::DocEntry(DictEntry *in)
39 {
40    ImplicitVR = false;
41    DicomDict  = in;
42    SetKey( in->GetKey( ) );
43    Offset     = 0 ; // To avoid further missprinting
44
45    // init some variables
46    ReadLength = 0;
47    Length = 0;
48 }
49
50 //-----------------------------------------------------------------------------
51 // Public
52 /**
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, ...)
56  */
57 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
58 {
59    uint32_t ffff  = 0xffffffff;
60    uint16_t group = GetGroup();
61    VRKey vr       = GetVR();
62    uint16_t el    = GetElement();
63    uint32_t lgth  = GetLength();
64
65    if ( group == 0xfffe && el == 0x0000 )
66    {
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 !   
70       return;
71    }
72    //
73    // ----------- Writes the common part
74    //
75    binary_write( *fp, group); //group number
76    binary_write( *fp, el);    //element number
77
78    if ( filetype == ExplicitVR )
79    {
80       // Special case of delimiters:
81       if (group == 0xfffe)
82       {
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)
89
90          uint32_t ff = 0xffffffff;
91          binary_write(*fp, ff);
92          return;
93       }
94
95       uint16_t z = 0;
96       uint16_t shortLgr = (uint16_t)lgth;
97
98       if (vr == GDCM_UNKNOWN)
99       {
100          // Unknown was 'written'
101          // deal with Little Endian            
102          binary_write(*fp, shortLgr);
103          binary_write(*fp, z);
104       }
105       else
106       {
107          binary_write(*fp, vr);
108          gdcmAssertMacro( vr.size() == 2 );
109                   
110          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
111          {
112             binary_write(*fp, z);
113             if (vr == "SQ")
114             {
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);
119             }
120             else
121             {
122                binary_write(*fp, lgth);
123             }
124          }
125          else
126          {
127             binary_write(*fp, shortLgr);
128          }
129       }
130    } 
131    else // IMPLICIT VR 
132    { 
133       if (vr == "SQ")
134       {
135          binary_write(*fp, ffff);
136       }
137       else
138       {
139          binary_write(*fp, lgth);
140       }
141    }
142 }
143
144 /**
145  * \brief   Gets the full length of the elementary DocEntry (not only value
146  *          length) depending on the VR.
147  */
148 uint32_t DocEntry::GetFullLength()
149 {
150    uint32_t l = GetReadLength();
151    if ( IsImplicitVR() )
152    {
153       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
154    }
155    else
156    {
157       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
158       {
159          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
160       }
161       else
162       {
163          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
164       }
165    }
166    return l;
167 }
168
169 /**
170  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
171  *          (fffe,e00d) 
172  */
173 bool DocEntry::IsItemDelimitor()
174 {
175    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
176 }
177
178 /**
179  * \brief   tells us if entry is the first one of an Item 
180  *          (fffe,e000) 
181  */
182 bool DocEntry::IsItemStarter()
183 {
184    return (GetGroup() == 0xfffe && GetElement() == 0xe000);
185 }
186
187 /**
188  * \brief   tells us if entry is the last one of a 'no length' Sequence 
189  *          (fffe,e0dd) 
190  */
191 bool DocEntry::IsSequenceDelimitor()
192 {
193    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
194 }
195
196 /**
197  * \brief   Copies all the attributes from an other DocEntry 
198  * @param doc entry to copy from
199  */
200 void DocEntry::Copy(DocEntry *doc)
201 {
202    Length     = doc->Length;
203    ReadLength = doc->ReadLength;
204    ImplicitVR = doc->ImplicitVR;
205    Offset     = doc->Offset;
206 }
207
208 //-----------------------------------------------------------------------------
209 // Protected
210
211 //-----------------------------------------------------------------------------
212 // Private
213
214 //-----------------------------------------------------------------------------
215 // Print
216 /**
217  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
218  * @param   os ostream we want to print in
219  * @param indent Indentation string to be prepended during printing
220  */
221 void DocEntry::Print(std::ostream &os, std::string const & )
222 {
223    size_t o;
224    std::string st;
225    TSKey v;
226    std::string d2, vr;
227    std::ostringstream s;
228    uint32_t lgth;
229
230    o  = GetOffset();
231    vr = GetVR();
232    if ( vr==GDCM_UNKNOWN )
233       vr="  ";
234
235    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
236
237    if (PrintLevel >= 2)
238    {
239       s << " lg : ";
240       lgth = GetReadLength(); // ReadLength, as opposed to Length
241       if (lgth == 0xffffffff)
242       {
243          st = " ffff ";
244          s.setf(std::ios::left);
245          s << std::setw(4);  
246          s << "    x(ffff) ";
247          s.setf(std::ios::left);
248          s << std::setw(8) << "-1"; 
249       }
250       else
251       {
252          st = Util::Format("x(%x)",lgth); // we may keep it
253          s.setf(std::ios::left);
254          s << std::setw(11-st.size()) << " ";
255          s << st << " ";
256          s.setf(std::ios::left);
257          s << std::setw(8) << lgth; 
258       }
259       s << " Off.: ";
260       st = Util::Format("x(%x)",o);  // we may keep it
261       s << std::setw(11-st.size()) << " ";
262       s << st << " ";
263       s << std::setw(8) << o; 
264    }
265    if (PrintLevel >= 1)
266       s << " ";
267
268    s << "[" << vr  << "] ";
269
270    std::string name;
271    if ( GetElement() == 0x0000 )
272       name = "Group Length";
273    else
274       name = GetName();
275
276    if (PrintLevel >= 1)
277    {
278       s.setf(std::ios::left);
279       s << std::setw(66-name.length()) << " ";
280    }
281     
282    s << "[" << name << "]";
283    os << s.str();      
284 }
285
286 //-----------------------------------------------------------------------------
287 } // end namespace gdcm