]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
Cosmetics
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/30 17:30:57 $
7   Version:   $Revision: 1.49 $
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 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
32
33 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
35 /**
36  * \brief   Constructor from a given DictEntry
37  * @param   in Pointer to existing dictionary entry
38  */
39 DocEntry::DocEntry(DictEntry *in)
40 {
41    ImplicitVR = false;
42    DicomDict  = in;
43    SetKey( in->GetKey( ) );
44    Offset     = 0 ; // To avoid further missprinting
45
46    // init some variables
47    ReadLength = 0;
48    Length = 0;
49 }
50
51 //-----------------------------------------------------------------------------
52 // Print
53 /**
54  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
55  * @param   os ostream we want to print in
56  * @param indent Indentation string to be prepended during printing
57  */
58 void DocEntry::Print(std::ostream &os, std::string const & )
59 {
60    size_t o;
61    std::string st;
62    TSKey v;
63    std::string d2, vr;
64    std::ostringstream s;
65    uint32_t lgth;
66
67    o  = GetOffset();
68    vr = GetVR();
69    if(vr==GDCM_UNKNOWN)
70       vr="  ";
71
72    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
73
74    if (PrintLevel >= 2)
75    {
76       s << " lg : ";
77       lgth = GetReadLength(); // ReadLength, as opposed to Length
78       if (lgth == 0xffffffff)
79       {
80          st = Util::Format("x(ffff)");  // I said : "x(ffff)" !
81          s.setf(std::ios::left);
82          s << std::setw(10-st.size()) << " ";  
83          s << st << " ";
84          s.setf(std::ios::left);
85          s << std::setw(8) << "-1"; 
86       }
87       else
88       {
89          st = Util::Format("x(%x)",lgth);
90          s.setf(std::ios::left);
91          s << std::setw(10-st.size()) << " ";
92          s << st << " ";
93          s.setf(std::ios::left);
94          s << std::setw(8) << lgth; 
95       }
96       s << " Off.: ";
97       st = Util::Format("x(%x)",o); 
98       s << std::setw(10-st.size()) << " ";
99       s << st << " ";
100       s << std::setw(8) << o; 
101    }
102
103    s << "[" << vr  << "] ";
104
105    if (PrintLevel >= 1)
106    {
107       s.setf(std::ios::left);
108       s << std::setw(66-GetName().length()) << " ";
109    }
110     
111    s << "[" << GetName()<< "]";
112    os << s.str();      
113 }
114
115 //-----------------------------------------------------------------------------
116 // Public
117 /**
118  * \brief   Writes the common part of any ValEntry, BinEntry, SeqEntry
119  * @param fp already open ofstream pointer
120  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
121  */
122 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
123 {
124    uint32_t ffff  = 0xffffffff;
125    uint16_t group = GetGroup();
126    VRKey vr   = GetVR();
127    uint16_t el    = GetElement();
128    uint32_t lgr   = GetLength();
129
130    if ( group == 0xfffe && el == 0x0000 )
131    {
132      // Fix in order to make some MR PHILIPS images e-film readable
133      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
134      // we just *always* ignore spurious fffe|0000 tag !   
135       return;
136    }
137    //
138    // ----------- Writes the common part
139    //
140    binary_write( *fp, group); //group
141    binary_write( *fp, el);    //element
142
143    if ( filetype == ExplicitVR )
144    {
145       // Special case of delimiters:
146       if (group == 0xfffe)
147       {
148          // Delimiters have NO Value Representation
149          // Hence we skip writing the VR.
150          // In order to avoid further troubles, we choose to write them
151          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
152          // We shall force the end of a given Item by writting 
153          //  a Item Delimitation Item (fffe, e00d)
154
155          uint32_t ff = 0xffffffff;
156          binary_write(*fp, ff);
157          return;
158       }
159
160       uint16_t z = 0;
161       uint16_t shortLgr = lgr;
162
163       if (vr == GDCM_UNKNOWN)
164       {
165          // Unknown was 'written'
166          // deal with Little Endian            
167          binary_write(*fp, shortLgr);
168          binary_write(*fp, z);
169       }
170       else
171       {
172          binary_write(*fp, vr);
173          gdcmAssertMacro( vr.size() == 2 );
174                   
175          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
176          {
177             binary_write(*fp, z);
178             if (vr == "SQ")
179             {
180                // we set SQ length to ffffffff
181                // and  we shall write a Sequence Delimitor Item 
182                // at the end of the Sequence! 
183                binary_write(*fp, ffff);
184             }
185             else
186             {
187                binary_write(*fp, lgr);
188             }
189          }
190          else
191          {
192             binary_write(*fp, shortLgr);
193          }
194       }
195    } 
196    else // IMPLICIT VR 
197    { 
198       if (vr == "SQ")
199       {
200          binary_write(*fp, ffff);
201       }
202       else
203       {
204          binary_write(*fp, lgr);
205       }
206    }
207 }
208
209 /**
210  * \brief   Gets the full length of the elementary DocEntry (not only value
211  *          length) depending on the VR.
212  */
213 uint32_t DocEntry::GetFullLength()
214 {
215    uint32_t l = GetReadLength();
216    if ( IsImplicitVR() )
217    {
218       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
219    }
220    else
221    {
222       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
223       {
224          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
225       }
226       else
227       {
228          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
229       }
230    }
231    return l;
232 }
233
234 /**
235  * \brief   Copies all the attributes from an other DocEntry 
236  * @param doc entry to copy from
237  */
238 void DocEntry::Copy(DocEntry *doc)
239 {
240    Length     = doc->Length;
241    ReadLength = doc->ReadLength;
242    ImplicitVR = doc->ImplicitVR;
243    Offset     = doc->Offset;
244 }
245
246 /**
247  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
248  *          (fffe,e00d) 
249  */
250 bool DocEntry::IsItemDelimitor()
251 {
252    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
253 }
254
255 /**
256  * \brief   tells us if entry is the last one of a 'no length' Sequence 
257  *          (fffe,e0dd) 
258  */
259 bool DocEntry::IsSequenceDelimitor()
260 {
261    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
262 }
263
264 //-----------------------------------------------------------------------------
265 // Protected
266
267 //-----------------------------------------------------------------------------
268 // Private
269
270 //-----------------------------------------------------------------------------
271 } // end namespace gdcm