]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
* src/gdcmBase.[h|cxx] : new base class. Contains the PrintLevel and an
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/16 13:46:37 $
7   Version:   $Revision: 1.35 $
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
25 #include <iomanip> // for std::ios::left, ...
26 #include <fstream>
27
28 namespace gdcm 
29 {
30
31 // CLEAN ME
32 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
33
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
36 /**
37  * \ingroup DocEntry
38  * \brief   Constructor from a given DictEntry
39  * @param   in Pointer to existing dictionary entry
40  */
41 DocEntry::DocEntry(DictEntry* in)
42 {
43    ImplicitVR = false;
44    DicomDict  = in;
45    SetKey( in->GetKey( ) );
46    Offset     = 0 ; // To avoid further missprinting
47
48    // init some variables
49    ReadLength = 0;
50    UsableLength = 0;
51 }
52
53 //-----------------------------------------------------------------------------
54 // Print
55 /**
56  * \ingroup DocEntry
57  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
58  * @param   os ostream we want to print in
59  */
60 void DocEntry::Print(std::ostream& os)
61 {
62    size_t o;
63    std::string st;
64    TSKey v;
65    std::string d2, vr;
66    std::ostringstream s;
67    uint32_t lgth;
68
69    o  = GetOffset();
70    vr = GetVR();
71
72    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
73
74    if (PrintLevel >= 2)
75    {
76       s << " lg : ";
77       lgth = GetReadLength(); // ReadLength, as opposed to UsableLength
78       if (lgth == 0xffffffff)
79       {
80          st = Util::Format("x(ffff)");  // I said : "x(ffff)" !
81          s.setf(std::ios::left);
82          s << std::setw(10-st.size()) << " ";  
83          s << st << " ";
84          s.setf(std::ios::left);
85          s << std::setw(8) << "-1"; 
86       }
87       else
88       {
89          st = Util::Format("x(%x)",lgth);
90          s.setf(std::ios::left);
91          s << std::setw(10-st.size()) << " ";
92          s << st << " ";
93          s.setf(std::ios::left);
94          s << std::setw(8) << lgth; 
95       }
96       s << " Off.: ";
97       st = Util::Format("x(%x)",o); 
98       s << std::setw(10-st.size()) << " ";
99       s << st << " ";
100       s << std::setw(8) << o; 
101    }
102
103    s << "[" << vr  << "] ";
104
105    if (PrintLevel >= 1)
106    {
107       s.setf(std::ios::left);
108       s << std::setw(66-GetName().length()) << " ";
109    }
110     
111    s << "[" << GetName()<< "]";
112    os << s.str();      
113 }
114
115 /**
116  * \ingroup DocEntry
117  * \brief   Writes the common part of any ValEntry, BinEntry, SeqEntry
118  * @param fp already open file pointer
119  * @param filetype type of the file to be written
120  */
121 void DocEntry::WriteContent(std::ofstream* fp, FileType filetype)
122 {
123    uint32_t ffff  = 0xffffffff;
124    uint16_t group = GetGroup();
125    VRKey vr   = GetVR();
126    uint16_t el    = GetElement();
127    uint32_t lgr   = GetReadLength();
128
129    if ( group == 0xfffe && el == 0x0000 )
130    {
131      // Fix in order to make some MR PHILIPS images e-film readable
132      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
133      // we just *always* ignore spurious fffe|0000 tag !   
134       return;
135    }
136
137    //
138    // ----------- Writes the common part
139    //
140    binary_write( *fp, group);  //group
141    binary_write( *fp, el);  //element
142
143    if ( filetype == ExplicitVR )
144    {
145       // Special case of delimiters:
146       if (group == 0xfffe)
147       {
148          // Delimiters have NO Value Representation
149          // Hence we skip writing the VR.
150          // In order to avoid further troubles, we choose to write them
151          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
152          // The end of a given Item will be found when  :
153          //  - a new Item Delimitor Item is encountered (the Seq goes on)
154          //  - a Sequence Delimitor Item is encountered (the Seq just ended)
155
156          // TODO : verify if the Sequence Delimitor Item was forced during Parsing 
157
158          uint32_t ff = 0xffffffff;
159          binary_write(*fp, ff);
160          return;
161       }
162
163       uint16_t z = 0;
164       uint16_t shortLgr = lgr;
165
166       if (vr == "unkn")
167       {
168          // Unknown was 'written'
169          // deal with Little Endian            
170          binary_write(*fp, shortLgr);
171          binary_write(*fp, z);
172       }
173       else
174       {
175          binary_write(*fp, vr);
176          assert( vr.size() == 2 );
177                   
178          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
179          {
180             binary_write(*fp, z);
181             if (vr == "SQ")
182             {
183                // we set SQ length to ffffffff
184                // and  we shall write a Sequence Delimitor Item 
185                // at the end of the Sequence! 
186                binary_write(*fp, ffff);
187             }
188             else
189             {
190                binary_write(*fp, lgr);
191             }
192          }
193          else
194          {
195             binary_write(*fp, shortLgr);
196          }
197       }
198    } 
199    else // IMPLICIT VR 
200    { 
201       if (vr == "SQ")
202       {
203          binary_write(*fp, ffff);
204       }
205       else
206       {
207          binary_write(*fp, lgr);
208       }
209    }
210 }
211
212 //-----------------------------------------------------------------------------
213 // Public
214
215 /**
216  * \ingroup DocEntry
217  * \brief   Gets the full length of the elementary DocEntry (not only value
218  *          length) depending on the VR.
219  */
220 uint32_t DocEntry::GetFullLength()
221 {
222    uint32_t l = GetReadLength();
223    if ( IsImplicitVR() )
224    {
225       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
226    }
227    else
228    {
229       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
230       {
231          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
232       }
233       else
234       {
235          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
236       }
237    }
238    return l;
239 }
240
241 /**
242  * \ingroup DocEntry
243  * \brief   Copies all the attributes from an other DocEntry 
244  */
245 void DocEntry::Copy (DocEntry* e)
246 {
247 //   DicomDict    = e->DicomDict;
248    UsableLength = e->UsableLength;
249    ReadLength   = e->ReadLength;
250    ImplicitVR   = e->ImplicitVR;
251    Offset       = e->Offset;
252    // TODO : remove DocEntry SQDepth
253 }
254
255 /**
256  * \ingroup DocEntry
257  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
258  *          (fffe,e00d) 
259  */
260 bool DocEntry::IsItemDelimitor()
261 {
262    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
263 }
264 /**
265  * \ingroup DocEntry
266  * \brief   tells us if entry is the last one of a 'no length' Sequence 
267  *          (fffe,e0dd) 
268  */
269 bool DocEntry::IsSequenceDelimitor()
270 {
271    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
272 }
273
274 //-----------------------------------------------------------------------------
275 // Protected
276
277
278 //-----------------------------------------------------------------------------
279 // Private
280
281 //-----------------------------------------------------------------------------
282
283 } // end namespace gdcm