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