]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
2005-01-18 Jean-Pierre Roux <jpr@creatis.univ-lyon1.fr>
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/18 08:01:41 $
7   Version:   $Revision: 1.42 $
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 // CLEAN ME
33 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
34
35 //-----------------------------------------------------------------------------
36 // Constructor / Destructor
37 /**
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    Length = 0;
51 }
52
53 //-----------------------------------------------------------------------------
54 // Print
55 /**
56  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
57  * @param   os ostream we want to print in
58  */
59 void DocEntry::Print(std::ostream &os, std::string const & )
60 {
61    size_t o;
62    std::string st;
63    TSKey v;
64    std::string d2, vr;
65    std::ostringstream s;
66    uint32_t lgth;
67
68    o  = GetOffset();
69    vr = GetVR();
70    if(vr==GDCM_UNKNOWN)
71       vr="  ";
72
73    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
74
75    if (PrintLevel >= 2)
76    {
77       s << " lg : ";
78       lgth = GetReadLength(); // ReadLength, as opposed to Length
79       if (lgth == 0xffffffff)
80       {
81          st = Util::Format("x(ffff)");  // I said : "x(ffff)" !
82          s.setf(std::ios::left);
83          s << std::setw(10-st.size()) << " ";  
84          s << st << " ";
85          s.setf(std::ios::left);
86          s << std::setw(8) << "-1"; 
87       }
88       else
89       {
90          st = Util::Format("x(%x)",lgth);
91          s.setf(std::ios::left);
92          s << std::setw(10-st.size()) << " ";
93          s << st << " ";
94          s.setf(std::ios::left);
95          s << std::setw(8) << lgth; 
96       }
97       s << " Off.: ";
98       st = Util::Format("x(%x)",o); 
99       s << std::setw(10-st.size()) << " ";
100       s << st << " ";
101       s << std::setw(8) << o; 
102    }
103
104    s << "[" << vr  << "] ";
105
106    if (PrintLevel >= 1)
107    {
108       s.setf(std::ios::left);
109       s << std::setw(66-GetName().length()) << " ";
110    }
111     
112    s << "[" << GetName()<< "]";
113    os << s.str();      
114 }
115
116 /**
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   = GetLength();
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 == GDCM_UNKNOWN)
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          gdcmAssertMacro( 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  * \brief   Gets the full length of the elementary DocEntry (not only value
217  *          length) depending on the VR.
218  */
219 uint32_t DocEntry::GetFullLength()
220 {
221    uint32_t l = GetReadLength();
222    if ( IsImplicitVR() )
223    {
224       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
225    }
226    else
227    {
228       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
229       {
230          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
231       }
232       else
233       {
234          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
235       }
236    }
237    return l;
238 }
239
240 /**
241  * \brief   Copies all the attributes from an other DocEntry 
242  * @param e entry to copy from
243  */
244 void DocEntry::Copy (DocEntry *e)
245 {
246    Length     = e->Length;
247    ReadLength = e->ReadLength;
248    ImplicitVR = e->ImplicitVR;
249    Offset     = e->Offset;
250 }
251
252 /**
253  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
254  *          (fffe,e00d) 
255  */
256 bool DocEntry::IsItemDelimitor()
257 {
258    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
259 }
260 /**
261  * \brief   tells us if entry is the last one of a 'no length' Sequence 
262  *          (fffe,e0dd) 
263  */
264 bool DocEntry::IsSequenceDelimitor()
265 {
266    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
267 }
268
269 //-----------------------------------------------------------------------------
270 // Protected
271
272
273 //-----------------------------------------------------------------------------
274 // Private
275
276 //-----------------------------------------------------------------------------
277
278 } // end namespace gdcm