]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
* src/gdcmDictEntry.h : now, the IsVRUnknown is correct
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/06 13:35:38 $
7   Version:   $Revision: 1.37 $
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 }
52
53 //-----------------------------------------------------------------------------
54 // Print
55 /**
56  * \ingroup DocEntry
57  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
58  * @param   os ostream we want to print in
59  */
60 void DocEntry::Print(std::ostream& os)
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 UsableLength
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  * \ingroup DocEntry
119  * \brief   Writes the common part of any ValEntry, BinEntry, SeqEntry
120  * @param fp already open file pointer
121  * @param filetype type of the file to be written
122  */
123 void DocEntry::WriteContent(std::ofstream* fp, FileType filetype)
124 {
125    uint32_t ffff  = 0xffffffff;
126    uint16_t group = GetGroup();
127    VRKey vr   = GetVR();
128    uint16_t el    = GetElement();
129    uint32_t lgr   = GetReadLength();
130
131    if ( group == 0xfffe && el == 0x0000 )
132    {
133      // Fix in order to make some MR PHILIPS images e-film readable
134      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
135      // we just *always* ignore spurious fffe|0000 tag !   
136       return;
137    }
138
139    //
140    // ----------- Writes the common part
141    //
142    binary_write( *fp, group);  //group
143    binary_write( *fp, el);  //element
144
145    if ( filetype == ExplicitVR )
146    {
147       // Special case of delimiters:
148       if (group == 0xfffe)
149       {
150          // Delimiters have NO Value Representation
151          // Hence we skip writing the VR.
152          // In order to avoid further troubles, we choose to write them
153          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
154          // The end of a given Item will be found when  :
155          //  - a new Item Delimitor Item is encountered (the Seq goes on)
156          //  - a Sequence Delimitor Item is encountered (the Seq just ended)
157
158          // TODO : verify if the Sequence Delimitor Item was forced during Parsing 
159
160          uint32_t ff = 0xffffffff;
161          binary_write(*fp, ff);
162          return;
163       }
164
165       uint16_t z = 0;
166       uint16_t shortLgr = lgr;
167
168       if (vr == GDCM_UNKNOWN)
169       {
170          // Unknown was 'written'
171          // deal with Little Endian            
172          binary_write(*fp, shortLgr);
173          binary_write(*fp, z);
174       }
175       else
176       {
177          binary_write(*fp, vr);
178          assert( vr.size() == 2 );
179                   
180          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
181          {
182             binary_write(*fp, z);
183             if (vr == "SQ")
184             {
185                // we set SQ length to ffffffff
186                // and  we shall write a Sequence Delimitor Item 
187                // at the end of the Sequence! 
188                binary_write(*fp, ffff);
189             }
190             else
191             {
192                binary_write(*fp, lgr);
193             }
194          }
195          else
196          {
197             binary_write(*fp, shortLgr);
198          }
199       }
200    } 
201    else // IMPLICIT VR 
202    { 
203       if (vr == "SQ")
204       {
205          binary_write(*fp, ffff);
206       }
207       else
208       {
209          binary_write(*fp, lgr);
210       }
211    }
212 }
213
214 //-----------------------------------------------------------------------------
215 // Public
216
217 /**
218  * \ingroup DocEntry
219  * \brief   Gets the full length of the elementary DocEntry (not only value
220  *          length) depending on the VR.
221  */
222 uint32_t DocEntry::GetFullLength()
223 {
224    uint32_t l = GetReadLength();
225    if ( IsImplicitVR() )
226    {
227       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
228    }
229    else
230    {
231       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
232       {
233          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
234       }
235       else
236       {
237          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
238       }
239    }
240    return l;
241 }
242
243 /**
244  * \ingroup DocEntry
245  * \brief   Copies all the attributes from an other DocEntry 
246  */
247 void DocEntry::Copy (DocEntry* e)
248 {
249 //   DicomDict    = e->DicomDict;
250    UsableLength = e->UsableLength;
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