]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
ENH: /binary_write/ gdcm source. Now even on big endian we are writting little endian...
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/03 20:52:13 $
7   Version:   $Revision: 1.29 $
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 "gdcmGlobal.h"
22 #include "gdcmUtil.h"
23
24 #include <iomanip> // for std::ios::left, ...
25 #include <fstream>
26
27 namespace gdcm 
28 {
29
30 // CLEAN ME
31 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
32
33 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
35 /**
36  * \ingroup DocEntry
37  * \brief   Constructor from a given DictEntry
38  * @param   in Pointer to existing dictionary entry
39  */
40 DocEntry::DocEntry(DictEntry* in)
41 {
42    ImplicitVR = false;
43    DicomDict  = in;
44    SetKey( in->GetKey( ) );
45    Offset     = 0 ; // To avoid further missprinting
46 }
47
48 //-----------------------------------------------------------------------------
49 // Print
50 /**
51  * \ingroup DocEntry
52  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
53  * @param   os ostream we want to print in
54  */
55 void DocEntry::Print(std::ostream& os)
56 {
57    PrintLevel = 2; // FIXME
58    
59    size_t o;
60    unsigned short int g, e;
61    std::string st;
62    TSKey v;
63    std::string d2, vr;
64    std::ostringstream s;
65    uint32_t lgth;
66
67    g  = GetGroup();
68    e  = GetElement();
69    o  = GetOffset();
70    vr = GetVR();
71
72    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
73
74    if (PrintLevel >= 2)
75    {
76       s << " lg : ";
77       lgth = GetReadLength(); // ReadLength, as opposed to UsableLength
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  * \ingroup DocEntry
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::Write(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   = GetReadLength();
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 == "unkn")
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          assert( 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  * \ingroup DocEntry
217  * \brief   Gets the full length of the elementary DocEntry (not only value
218  *          length) depending on the VR.
219  */
220 uint32_t DocEntry::GetFullLength()
221 {
222    uint32_t l = GetReadLength();
223    if ( IsImplicitVR() )
224    {
225       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
226    }
227    else
228    {
229       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
230       {
231          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
232       }
233       else
234       {
235          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
236       }
237    }
238    return l;
239 }
240
241 /**
242  * \ingroup DocEntry
243  * \brief   Copies all the attributes from an other DocEntry 
244  */
245 void DocEntry::Copy (DocEntry* e)
246 {
247    DicomDict    = e->DicomDict;
248    UsableLength = e->UsableLength;
249    ReadLength   = e->ReadLength;
250    ImplicitVR   = e->ImplicitVR;
251    Offset       = e->Offset;
252    PrintLevel   = e->PrintLevel;
253    // TODO : remove DocEntry SQDepth
254 }
255
256 /**
257  * \ingroup DocEntry
258  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
259  *          (fffe,e00d) 
260  */
261 bool DocEntry::IsItemDelimitor()
262 {
263    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
264 }
265 /**
266  * \ingroup DocEntry
267  * \brief   tells us if entry is the last one of a 'no length' Sequence 
268  *          (fffe,e0dd) 
269  */
270 bool DocEntry::IsSequenceDelimitor()
271 {
272    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
273 }
274
275 //-----------------------------------------------------------------------------
276 // Protected
277
278
279 //-----------------------------------------------------------------------------
280 // Private
281
282 //-----------------------------------------------------------------------------
283
284 } // end namespace gdcm