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