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