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