]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
b1c276d20ea4158691ae4c55c29697c7a06cf3c4
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/07 22:19:48 $
7   Version:   $Revision: 1.40 $
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  * \ingroup DocEntry
39  * \brief   Constructor from a given DictEntry
40  * @param   in Pointer to existing dictionary entry
41  */
42 DocEntry::DocEntry(DictEntry *in)
43 {
44    ImplicitVR = false;
45    DicomDict  = in;
46    SetKey( in->GetKey( ) );
47    Offset     = 0 ; // To avoid further missprinting
48
49    // init some variables
50    ReadLength = 0;
51    Length = 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    size_t o;
64    std::string st;
65    TSKey v;
66    std::string d2, vr;
67    std::ostringstream s;
68    uint32_t lgth;
69
70    o  = GetOffset();
71    vr = GetVR();
72    if(vr==GDCM_UNKNOWN)
73       vr="  ";
74
75    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
76
77    if (PrintLevel >= 2)
78    {
79       s << " lg : ";
80       lgth = GetReadLength(); // ReadLength, as opposed to Length
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   = GetLength();
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 == GDCM_UNKNOWN)
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          gdcmAssertMacro( 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    Length     = e->Length;
251    ReadLength = e->ReadLength;
252    ImplicitVR = e->ImplicitVR;
253    Offset     = e->Offset;
254    // TODO : remove DocEntry SQDepth
255 }
256
257 /**
258  * \ingroup DocEntry
259  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
260  *          (fffe,e00d) 
261  */
262 bool DocEntry::IsItemDelimitor()
263 {
264    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
265 }
266 /**
267  * \ingroup DocEntry
268  * \brief   tells us if entry is the last one of a 'no length' Sequence 
269  *          (fffe,e0dd) 
270  */
271 bool DocEntry::IsSequenceDelimitor()
272 {
273    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
274 }
275
276 //-----------------------------------------------------------------------------
277 // Protected
278
279
280 //-----------------------------------------------------------------------------
281 // Private
282
283 //-----------------------------------------------------------------------------
284
285 } // end namespace gdcm