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