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