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