]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
ENH: Some more borland issues
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/09 22:15:36 $
7   Version:   $Revision: 1.30 $
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    std::string st;
61    TSKey v;
62    std::string d2, vr;
63    std::ostringstream s;
64    uint32_t lgth;
65
66    o  = GetOffset();
67    vr = GetVR();
68
69    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
70
71    if (PrintLevel >= 2)
72    {
73       s << " lg : ";
74       lgth = GetReadLength(); // ReadLength, as opposed to UsableLength
75       if (lgth == 0xffffffff)
76       {
77          st = Util::Format("x(ffff)");  // I said : "x(ffff)" !
78          s.setf(std::ios::left);
79          s << std::setw(10-st.size()) << " ";  
80          s << st << " ";
81          s.setf(std::ios::left);
82          s << std::setw(8) << "-1"; 
83       }
84       else
85       {
86          st = Util::Format("x(%x)",lgth);
87          s.setf(std::ios::left);
88          s << std::setw(10-st.size()) << " ";
89          s << st << " ";
90          s.setf(std::ios::left);
91          s << std::setw(8) << lgth; 
92       }
93       s << " Off.: ";
94       st = Util::Format("x(%x)",o); 
95       s << std::setw(10-st.size()) << " ";
96       s << st << " ";
97       s << std::setw(8) << o; 
98    }
99
100    s << "[" << vr  << "] ";
101
102    if (PrintLevel >= 1)
103    {
104       s.setf(std::ios::left);
105       s << std::setw(66-GetName().length()) << " ";
106    }
107     
108    s << "[" << GetName()<< "]";
109    os << s.str();      
110 }
111
112 /**
113  * \ingroup DocEntry
114  * \brief   Writes the common part of any ValEntry, BinEntry, SeqEntry
115  * @param fp already open file pointer
116  * @param filetype type of the file to be written
117  */
118 void DocEntry::Write(std::ofstream* fp, FileType filetype)
119 {
120    uint32_t ffff  = 0xffffffff;
121    uint16_t group = GetGroup();
122    VRKey vr   = GetVR();
123    uint16_t el    = GetElement();
124    uint32_t lgr   = GetReadLength();
125
126    if ( group == 0xfffe && el == 0x0000 )
127    {
128      // Fix in order to make some MR PHILIPS images e-film readable
129      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
130      // we just *always* ignore spurious fffe|0000 tag !   
131       return;
132    }
133
134    //
135    // ----------- Writes the common part
136    //
137    binary_write( *fp, group);  //group
138    binary_write( *fp, el);  //element
139
140    if ( filetype == ExplicitVR )
141    {
142       // Special case of delimiters:
143       if (group == 0xfffe)
144       {
145          // Delimiters have NO Value Representation
146          // Hence we skip writing the VR.
147          // In order to avoid further troubles, we choose to write them
148          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
149          // The end of a given Item will be found when  :
150          //  - a new Item Delimitor Item is encountered (the Seq goes on)
151          //  - a Sequence Delimitor Item is encountered (the Seq just ended)
152
153          // TODO : verify if the Sequence Delimitor Item was forced during Parsing 
154
155          uint32_t ff = 0xffffffff;
156          binary_write(*fp, ff);
157          return;
158       }
159
160       uint16_t z = 0;
161       uint16_t shortLgr = lgr;
162
163       if (vr == "unkn")
164       {
165          // Unknown was 'written'
166          // deal with Little Endian            
167          binary_write(*fp, shortLgr);
168          binary_write(*fp, z);
169       }
170       else
171       {
172          binary_write(*fp, vr);
173          assert( vr.size() == 2 );
174                   
175          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
176          {
177             binary_write(*fp, z);
178             if (vr == "SQ")
179             {
180                // we set SQ length to ffffffff
181                // and  we shall write a Sequence Delimitor Item 
182                // at the end of the Sequence! 
183                binary_write(*fp, ffff);
184             }
185             else
186             {
187                binary_write(*fp, lgr);
188             }
189          }
190          else
191          {
192             binary_write(*fp, shortLgr);
193          }
194       }
195    } 
196    else // IMPLICIT VR 
197    { 
198       if (vr == "SQ")
199       {
200          binary_write(*fp, ffff);
201       }
202       else
203       {
204          binary_write(*fp, lgr);
205       }
206    }
207 }
208
209 //-----------------------------------------------------------------------------
210 // Public
211
212 /**
213  * \ingroup DocEntry
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  * \ingroup DocEntry
240  * \brief   Copies all the attributes from an other DocEntry 
241  */
242 void DocEntry::Copy (DocEntry* e)
243 {
244    DicomDict    = e->DicomDict;
245    UsableLength = e->UsableLength;
246    ReadLength   = e->ReadLength;
247    ImplicitVR   = e->ImplicitVR;
248    Offset       = e->Offset;
249    PrintLevel   = e->PrintLevel;
250    // TODO : remove DocEntry SQDepth
251 }
252
253 /**
254  * \ingroup DocEntry
255  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
256  *          (fffe,e00d) 
257  */
258 bool DocEntry::IsItemDelimitor()
259 {
260    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
261 }
262 /**
263  * \ingroup DocEntry
264  * \brief   tells us if entry is the last one of a 'no length' Sequence 
265  *          (fffe,e0dd) 
266  */
267 bool DocEntry::IsSequenceDelimitor()
268 {
269    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
270 }
271
272 //-----------------------------------------------------------------------------
273 // Protected
274
275
276 //-----------------------------------------------------------------------------
277 // Private
278
279 //-----------------------------------------------------------------------------
280
281 } // end namespace gdcm