]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
Deal with element 0x0001 of Private Groups
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/07/27 09:49:31 $
7   Version:   $Revision: 1.91 $
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 DictEntry
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 missprinting
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 DictEntry
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, ...)
73  */
74 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype, bool insideMetaElements)
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 /*
129       if( IsVRUnknown() )
130       { 
131          // GDCM_VRUNKNOWN was stored in the Entry VR;
132          // deal with Entry as if TS were Implicit VR
133          binary_write(*fp, lgth);
134       }
135       else
136 */
137       if( IsVRUnknown() )  
138       {
139       // if VR was not set, we set it to "UN"
140       // (FileHelper::Write has no longer to switch to ImplicitVR 
141       // when undocumented VR DataElements exist!)
142          SetVR("UN");
143          vr=GetVR();
144       }      
145      
146       //{
147          binary_write(*fp, vr.str());
148          // See PS 3.5-2004 page 33, 36                  
149          if ( (vr == "SQ") || (vr == "OB") || (vr == "OW") || (vr == "OF") 
150           ||  (vr == "UN") || (vr == "UT") )
151          {
152             binary_write(*fp, zero);
153            if ( (filetype == JPEG || filetype == JPEG2000) && group == 0x7fe0 && elem == 0x0010)
154             {
155                gdcmAssertMacro( GetVR() == "OW" );
156                binary_write(*fp, ffff);
157             }  
158             else if (vr == "SQ")
159             {
160                // we set SQ length to ffffffff
161                // and  we shall write a Sequence Delimitor Item 
162                // at the end of the Sequence! 
163                binary_write(*fp, ffff);
164             }
165             else
166             {
167                binary_write(*fp, lgth);
168             }
169          }
170          else
171          {
172             binary_write(*fp, shortLgr);
173          }
174       //}
175    } 
176    else // IMPLICIT VR 
177    { 
178 // ----------- Writes the common part : the VR  
179       if (vr == "SQ")
180       {
181          binary_write(*fp, ffff);
182       }
183       else
184       {
185          binary_write(*fp, lgth);
186       }
187    }
188 }
189
190 /// \brief Returns the 'Name' '(e.g. "Patient's Name") found in the Dicom
191 /// Dictionnary of the current Dicom Header Entry
192 std::string const &DocEntry::GetName() 
193
194    if (DicomDict == 0)
195       DicomDict =
196                  Global::GetDicts()->GetDefaultPubDict()->GetEntry(Key[0],Key[1]);
197    if (DicomDict == 0)
198       return GDCM_UNKNOWN;
199    else
200    {
201       DicomDict->Register();
202       return DicomDict->GetName();
203    }
204 }
205
206    /// \brief Returns the 'Value Multiplicity' (e.g. "1", "6", "1-n", "3-n"),
207    /// found in the Dicom entry or in the Dicom Dictionnary
208    /// of the current Dicom entry
209 std::string const &DocEntry::GetVM()
210 {
211    if (DicomDict == 0)
212       DicomDict =
213                  Global::GetDicts()->GetDefaultPubDict()->GetEntry(Key[0],Key[1]);
214    if (DicomDict == 0)
215       return GDCM_UNKNOWN;
216    else
217    {
218       DicomDict->Register();
219       return DicomDict->GetVM();
220    }
221 }
222
223 /**
224  * \brief   Gets the full length of the elementary DocEntry (not only value
225  *          length) depending on the VR.
226  */
227 uint32_t DocEntry::GetFullLength()
228 {
229    uint32_t l = GetReadLength();
230    if ( IsImplicitVR() )
231    {
232       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
233    }
234    else
235    {
236       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
237       {
238          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
239       }
240       else
241       {
242          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
243       }
244    }
245    return l;
246 }
247
248 /**
249  * \brief Copies all the attributes from an other DocEntry 
250  * @param doc entry to copy from
251  */
252 void DocEntry::Copy(DocEntry *doc)
253 {
254    Length     = doc->Length;
255    ReadLength = doc->ReadLength;
256    ImplicitVR = doc->ImplicitVR;
257    Offset     = doc->Offset;
258 }
259
260 //-----------------------------------------------------------------------------
261 // Protected
262
263 //-----------------------------------------------------------------------------
264 // Private
265
266 //-----------------------------------------------------------------------------
267 // Print
268 /**
269  * \brief   Prints the common part of DataEntry, SeqEntry
270  * @param   os ostream we want to print in
271  * @param indent Indentation string to be prepended during printing
272  */
273 void DocEntry::Print(std::ostream &os, std::string const & )
274 {
275    size_t o;
276    std::string st;
277    TSKey v;
278    std::string d2;
279    VRKey vr;
280    std::ostringstream s;
281    uint32_t lgth;
282
283    o  = GetOffset();
284    vr = GetVR();
285    if ( vr == GDCM_VRUNKNOWN )
286       vr = "  ";
287
288    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
289
290    if (PrintLevel >= 2)
291    {
292       s << " lg : ";
293       lgth = GetReadLength(); // ReadLength, as opposed to (usable) Length
294       if (lgth == 0xffffffff)
295       {
296          st = " ffff ";
297          s.setf(std::ios::left);
298          s << std::setw(4);  
299          s << "    x(ffff) ";
300          s.setf(std::ios::left);
301          s << std::setw(8) << "-1"; 
302       }
303       else
304       {
305          st = Util::Format("x(%x)",lgth); // we may keep it
306          s.setf(std::ios::left);
307          s << std::setw(11-st.size()) << " ";
308          s << st << " ";
309          s.setf(std::ios::left);
310          s << std::setw(8) << lgth; 
311       }
312       s << " Off.: ";
313       st = Util::Format("x(%x)",o);  // we may keep it
314       s << std::setw(11-st.size()) << " ";
315       s << st << " ";
316       s << std::setw(8) << o; 
317    }
318    if (PrintLevel >= 1)
319       s << " ";
320
321    s << "[" << vr  << "] ";
322
323    std::string name;
324    uint16_t e = GetElement();
325    if ( e == 0x0000 )
326       name = "Group Length";
327    else if ( GetGroup()%2 == 1 )
328    {
329       if ( e >= 0x0010 && e <= 0x00ff )
330          name = "Private Creator";
331       else if (e == 0x0001)
332          name = "Private Group Length To End";
333    }
334    else
335    {
336       name = GetName();
337       // prevent Print from any CR at end of name (hope it's enought!)
338       if (name[name.length()-1] == 0x0d || name[name.length()-1] == 0x0a)
339       {  
340          name.replace(name.length()-1, 1, 1, ' ');
341       }
342    }
343    if (PrintLevel >= 1)
344    {
345       s.setf(std::ios::left);
346       s << std::setw(66-name.length()) << " ";
347    }
348     
349    s << "[" << name << "]";
350    os << s.str();      
351 }
352
353 //-----------------------------------------------------------------------------
354 } // end namespace gdcm