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