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