]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
* Remove friend classes between DocEntry and File
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/28 15:10:56 $
7   Version:   $Revision: 1.48 $
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 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
32
33 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
35 /**
36  * \brief   Constructor from a given DictEntry
37  * @param   in Pointer to existing dictionary entry
38  */
39 DocEntry::DocEntry(DictEntry *in)
40 {
41    ImplicitVR = false;
42    DicomDict  = in;
43    SetKey( in->GetKey( ) );
44    Offset     = 0 ; // To avoid further missprinting
45
46    // init some variables
47    ReadLength = 0;
48    Length = 0;
49 }
50
51 //-----------------------------------------------------------------------------
52 // Print
53 /**
54  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
55  * @param   os ostream we want to print in
56  * @param indent Indentation string to be prepended during printing
57  */
58 void DocEntry::Print(std::ostream &os, std::string const & )
59 {
60    size_t o;
61    std::string st;
62    TSKey v;
63    std::string d2, vr;
64    std::ostringstream s;
65    uint32_t lgth;
66
67    o  = GetOffset();
68    vr = GetVR();
69    if(vr==GDCM_UNKNOWN)
70       vr="  ";
71
72    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
73
74    if (PrintLevel >= 2)
75    {
76       s << " lg : ";
77       lgth = GetReadLength(); // ReadLength, as opposed to Length
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 // Public
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  * \brief   Gets the full length of the elementary DocEntry (not only value
212  *          length) depending on the VR.
213  */
214 uint32_t DocEntry::GetFullLength()
215 {
216    uint32_t l = GetReadLength();
217    if ( IsImplicitVR() )
218    {
219       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
220    }
221    else
222    {
223       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
224       {
225          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
226       }
227       else
228       {
229          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
230       }
231    }
232    return l;
233 }
234
235 /**
236  * \brief   Copies all the attributes from an other DocEntry 
237  * @param e entry to copy from
238  */
239 void DocEntry::Copy(DocEntry *e)
240 {
241    Length     = e->Length;
242    ReadLength = e->ReadLength;
243    ImplicitVR = e->ImplicitVR;
244    Offset     = e->Offset;
245 }
246
247 /**
248  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
249  *          (fffe,e00d) 
250  */
251 bool DocEntry::IsItemDelimitor()
252 {
253    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
254 }
255 /**
256  * \brief   tells us if entry is the last one of a 'no length' Sequence 
257  *          (fffe,e0dd) 
258  */
259 bool DocEntry::IsSequenceDelimitor()
260 {
261    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
262 }
263
264 //-----------------------------------------------------------------------------
265 // Protected
266
267 //-----------------------------------------------------------------------------
268 // Private
269
270 //-----------------------------------------------------------------------------
271 } // end namespace gdcm