]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
Fix bug when Writing Meta Elements Group (0x0002) on Big Endian Proc,
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/09/07 08:52:58 $
7   Version:   $Revision: 1.67 $
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
33 // Constructor / Destructor
34 /**
35  * \brief   Constructor from a given DictEntry
36  * @param   in Pointer to existing dictionary entry
37  */
38 DocEntry::DocEntry(DictEntry *in)
39 {
40    ImplicitVR = false;
41    DicomDict  = in;
42    SetKey( in->GetKey( ) );
43    Offset     = 0 ; // To avoid further missprinting
44
45    // init some variables
46    ReadLength = 0;
47    Length = 0;
48 }
49
50 //-----------------------------------------------------------------------------
51 // Public
52 /**
53  * \brief   Writes the common part of any ValEntry, BinEntry, SeqEntry
54  * @param fp already open ofstream pointer
55  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
56  */
57 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
58 {
59    uint32_t ffff  = 0xffffffff;
60    uint16_t group = GetGroup();
61    VRKey vr       = GetVR();
62    uint16_t el    = GetElement();
63    uint32_t lgth  = GetLength();
64
65    if ( group == 0xfffe && el == 0x0000 )
66    {
67      // Fix in order to make some MR PHILIPS images e-film readable
68      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
69      // we just *always* ignore spurious fffe|0000 tag !   
70       return;
71    }
72    //
73    // ----------- Writes the common part
74    //
75    binary_write( *fp, group); //group number
76    binary_write( *fp, el);    //element number
77
78    // Dicom V3 group 0x0002 is *always* Explicit VR !
79    if ( filetype == ExplicitVR || group == 0x0002 )
80    {
81       // Special case of delimiters:
82       if (group == 0xfffe)
83       {
84          // Delimiters have NO Value Representation
85          // Hence we skip writing the VR.
86          //
87          // In order to avoid further troubles, we choose to write them
88          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
89          // We shall force the end of a given Item by writting 
90          //  a Item Delimitation Item (fffe, e00d)
91
92          uint32_t ff = 0xffffffff;
93          binary_write(*fp, ff);
94          return;
95       }
96
97       uint16_t z = 0;
98       uint16_t shortLgr = (uint16_t)lgth;
99
100       if (vr == GDCM_UNKNOWN)
101       {
102          // GDCM_UNKNOWN was stored in the Entry VR;
103          // deal with Entry as if TS were Implicit VR
104  
105          // FIXME : troubles expected on big endian processors :
106          // let lgth = 0x00001234
107          // we write 34 12 00 00 on little endian proc (OK)
108          // we write 12 34 00 00 on big endian proc (KO)          
109          //binary_write(*fp, shortLgr);
110          //binary_write(*fp, z);
111
112 binary_write(*fp, lgth);
113       }
114       else
115       {
116          binary_write(*fp, vr);
117          gdcmAssertMacro( vr.size() == 2 );
118                   
119          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") /*|| (vr == "UN")*/ )
120          {
121             binary_write(*fp, z);
122             if (vr == "SQ")
123             {
124                // we set SQ length to ffffffff
125                // and  we shall write a Sequence Delimitor Item 
126                // at the end of the Sequence! 
127                binary_write(*fp, ffff);
128             }
129             else
130             {
131                binary_write(*fp, lgth);
132             }
133          }
134          else
135          {
136             binary_write(*fp, shortLgr);
137          }
138       }
139    } 
140    else // IMPLICIT VR 
141    { 
142       if (vr == "SQ")
143       {
144          binary_write(*fp, ffff);
145       }
146       else
147       {
148          binary_write(*fp, lgth);
149       }
150    }
151 }
152
153 /**
154  * \brief   Gets the full length of the elementary DocEntry (not only value
155  *          length) depending on the VR.
156  */
157 uint32_t DocEntry::GetFullLength()
158 {
159    uint32_t l = GetReadLength();
160    if ( IsImplicitVR() )
161    {
162       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
163    }
164    else
165    {
166       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
167       {
168          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
169       }
170       else
171       {
172          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
173       }
174    }
175    return l;
176 }
177
178 /**
179  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
180  *          (fffe,e00d) 
181  */
182 bool DocEntry::IsItemDelimitor()
183 {
184    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
185 }
186
187 /**
188  * \brief   tells us if entry is the first one of an Item 
189  *          (fffe,e000) 
190  */
191 bool DocEntry::IsItemStarter()
192 {
193    return (GetGroup() == 0xfffe && GetElement() == 0xe000);
194 }
195
196 /**
197  * \brief   tells us if entry is the last one of a 'no length' Sequence 
198  *          (fffe,e0dd) 
199  */
200 bool DocEntry::IsSequenceDelimitor()
201 {
202    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
203 }
204
205 /**
206  * \brief   Copies all the attributes from an other DocEntry 
207  * @param doc entry to copy from
208  */
209 void DocEntry::Copy(DocEntry *doc)
210 {
211    Length     = doc->Length;
212    ReadLength = doc->ReadLength;
213    ImplicitVR = doc->ImplicitVR;
214    Offset     = doc->Offset;
215 }
216
217 //-----------------------------------------------------------------------------
218 // Protected
219
220 //-----------------------------------------------------------------------------
221 // Private
222
223 //-----------------------------------------------------------------------------
224 // Print
225 /**
226  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
227  * @param   os ostream we want to print in
228  * @param indent Indentation string to be prepended during printing
229  */
230 void DocEntry::Print(std::ostream &os, std::string const & )
231 {
232    size_t o;
233    std::string st;
234    TSKey v;
235    std::string d2, vr;
236    std::ostringstream s;
237    uint32_t lgth;
238
239    o  = GetOffset();
240    vr = GetVR();
241    if ( vr==GDCM_UNKNOWN )
242       vr="  ";
243
244    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
245
246    if (PrintLevel >= 2)
247    {
248       s << " lg : ";
249       lgth = GetReadLength(); // ReadLength, as opposed to (usable) Length
250       if (lgth == 0xffffffff)
251       {
252          st = " ffff ";
253          s.setf(std::ios::left);
254          s << std::setw(4);  
255          s << "    x(ffff) ";
256          s.setf(std::ios::left);
257          s << std::setw(8) << "-1"; 
258       }
259       else
260       {
261          st = Util::Format("x(%x)",lgth); // we may keep it
262          s.setf(std::ios::left);
263          s << std::setw(11-st.size()) << " ";
264          s << st << " ";
265          s.setf(std::ios::left);
266          s << std::setw(8) << lgth; 
267       }
268       s << " Off.: ";
269       st = Util::Format("x(%x)",o);  // we may keep it
270       s << std::setw(11-st.size()) << " ";
271       s << st << " ";
272       s << std::setw(8) << o; 
273    }
274    if (PrintLevel >= 1)
275       s << " ";
276
277    s << "[" << vr  << "] ";
278
279    std::string name;
280    if ( GetElement() == 0x0000 )
281       name = "Group Length";
282    else
283       name = GetName();
284
285    if (PrintLevel >= 1)
286    {
287       s.setf(std::ios::left);
288       s << std::setw(66-name.length()) << " ";
289    }
290     
291    s << "[" << name << "]";
292    os << s.str();      
293 }
294
295 //-----------------------------------------------------------------------------
296 } // end namespace gdcm