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