]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
ENH: Adding 'gdcm' namespace. Be nice with me this was a ~13000 lines patch. Also...
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/12 04:35:45 $
7   Version:   $Revision: 1.27 $
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 "gdcmGlobal.h"
22 #include "gdcmUtil.h"
23
24 #include <iomanip> // for std::ios::left, ...
25 namespace gdcm 
26 {
27
28 // CLEAN ME
29 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
30
31 //-----------------------------------------------------------------------------
32 // Constructor / Destructor
33 /**
34  * \ingroup DocEntry
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    SetKey( in->GetKey( ) );
43    Offset     = 0 ; // To avoid further missprinting
44 }
45
46 //-----------------------------------------------------------------------------
47 // Print
48 /**
49  * \ingroup DocEntry
50  * \brief   Prints the common part of ValEntry, BinEntry, SeqEntry
51  * @param   os ostream we want to print in
52  */
53 void DocEntry::Print(std::ostream& os)
54 {
55    PrintLevel = 2; // FIXME
56    
57    size_t o;
58    unsigned short int g, e;
59    std::string st;
60    TSKey v;
61    std::string d2, vr;
62    std::ostringstream s;
63    uint32_t lgth;
64
65    g  = GetGroup();
66    e  = GetElement();
67    o  = GetOffset();
68    vr = GetVR();
69
70    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
71
72    if (PrintLevel >= 2)
73    {
74       s << " lg : ";
75       lgth = GetReadLength(); // ReadLength, as opposed to UsableLength
76       if (lgth == 0xffffffff)
77       {
78          st = Util::Format("x(ffff)");  // I said : "x(ffff)" !
79          s.setf(std::ios::left);
80          s << std::setw(10-st.size()) << " ";  
81          s << st << " ";
82          s.setf(std::ios::left);
83          s << std::setw(8) << "-1"; 
84       }
85       else
86       {
87          st = Util::Format("x(%x)",lgth);
88          s.setf(std::ios::left);
89          s << std::setw(10-st.size()) << " ";
90          s << st << " ";
91          s.setf(std::ios::left);
92          s << std::setw(8) << lgth; 
93       }
94       s << " Off.: ";
95       st = Util::Format("x(%x)",o); 
96       s << std::setw(10-st.size()) << " ";
97       s << st << " ";
98       s << std::setw(8) << o; 
99    }
100
101    s << "[" << vr  << "] ";
102
103    if (PrintLevel >= 1)
104    {
105       s.setf(std::ios::left);
106       s << std::setw(66-GetName().length()) << " ";
107    }
108     
109    s << "[" << GetName()<< "]";
110    os << s.str();      
111 }
112
113 /**
114  * \ingroup DocEntry
115  * \brief   Writes the common part of any ValEntry, BinEntry, SeqEntry
116  * @param fp already open file pointer
117  * @param filetype type of the file to be written
118  */
119 void DocEntry::Write(FILE* fp, FileType filetype)
120 {
121    uint32_t ffff  = 0xffffffff;
122    uint16_t group = GetGroup();
123    VRKey vr   = GetVR();
124    uint16_t el    = GetElement();
125    uint32_t lgr   = GetReadLength();
126
127    if ( group == 0xfffe && el == 0x0000 )
128    {
129      // Fix in order to make some MR PHILIPS images e-film readable
130      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
131      // we just *always* ignore spurious fffe|0000 tag !   
132       return;
133    }
134
135    //
136    // ----------- Writes the common part
137    //
138    fwrite ( &group,(size_t)2 ,(size_t)1 ,fp);  //group
139    fwrite ( &el,   (size_t)2 ,(size_t)1 ,fp);  //element
140       
141    if ( filetype == ExplicitVR )
142    {
143       // Special case of delimiters:
144       if (group == 0xfffe)
145       {
146          // Delimiters have NO Value Representation
147          // Hence we skip writing the VR.
148          // In order to avoid further troubles, we choose to write them
149          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
150          // The end of a given Item will be found when  :
151          //  - a new Item Delimitor Item is encountered (the Seq goes on)
152          //  - a Sequence Delimitor Item is encountered (the Seq just ended)
153
154          // TODO : verify if the Sequence Delimitor Item was forced during Parsing 
155
156          int ff = 0xffffffff;
157          fwrite (&ff,(size_t)4 ,(size_t)1 ,fp);
158          return;
159       }
160
161       uint16_t z = 0;
162       uint16_t shortLgr = lgr;
163
164       if (vr == "unkn")
165       {
166          // Unknown was 'written'
167          // deal with Little Endian            
168          fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
169          fwrite ( &z,       (size_t)2 ,(size_t)1 ,fp);
170       }
171       else
172       {
173          fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,fp); 
174                   
175          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
176          {
177             fwrite ( &z,  (size_t)2 ,(size_t)1 ,fp);
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                fwrite ( &ffff,(size_t)4 ,(size_t)1 ,fp);
184             }
185             else
186             {
187                fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
188             }
189          }
190          else
191          {
192             fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
193          }
194       }
195    } 
196    else // IMPLICIT VR 
197    { 
198       if (vr == "SQ")
199       {
200          fwrite ( &ffff,(size_t)4 ,(size_t)1 ,fp);
201       }
202       else
203       {
204          fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
205       }
206    }
207 }
208
209 //-----------------------------------------------------------------------------
210 // Public
211
212 /**
213  * \ingroup DocEntry
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()=="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  * \ingroup DocEntry
240  * \brief   Copies all the attributes from an other DocEntry 
241  */
242 void DocEntry::Copy (DocEntry* e)
243 {
244    DicomDict    = e->DicomDict;
245    UsableLength = e->UsableLength;
246    ReadLength   = e->ReadLength;
247    ImplicitVR   = e->ImplicitVR;
248    Offset       = e->Offset;
249    PrintLevel   = e->PrintLevel;
250    // TODO : remove DocEntry SQDepth
251 }
252
253 /**
254  * \ingroup DocEntry
255  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
256  *          (fffe,e00d) 
257  */
258 bool DocEntry::IsItemDelimitor()
259 {
260    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
261 }
262 /**
263  * \ingroup DocEntry
264  * \brief   tells us if entry is the last one of a 'no length' Sequence 
265  *          (fffe,e0dd) 
266  */
267 bool DocEntry::IsSequenceDelimitor()
268 {
269    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
270 }
271
272 //-----------------------------------------------------------------------------
273 // Protected
274
275
276 //-----------------------------------------------------------------------------
277 // Private
278
279 //-----------------------------------------------------------------------------
280
281 } // end namespace gdcm