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