]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
ENH: Minor patch, cosmetic -again-. Remove all this->. I might have discover the...
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/08/01 00:59:21 $
7   Version:   $Revision: 1.14 $
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    entry = 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  */
114 void gdcmDocEntry::Write(FILE *fp, FileType filetype)
115 {
116    uint32_t FFFF  = 0xffffffff;
117    uint16_t group = GetGroup();
118    gdcmVRKey vr   = GetVR();
119    uint16_t el    = GetElement();
120    uint32_t lgr   = GetReadLength();
121
122    if ( (group == 0xfffe) && (el == 0x0000) )
123    {
124      // Fix in order to make some MR PHILIPS images e-film readable
125      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
126      // we just *always* ignore spurious fffe|0000 tag !   
127       return;
128    }
129
130 //
131 // ----------- Writes the common part
132 //
133    fwrite ( &group,(size_t)2 ,(size_t)1 ,fp);  //group
134    fwrite ( &el,   (size_t)2 ,(size_t)1 ,fp);  //element
135       
136    if ( filetype == gdcmExplicitVR )
137    {
138       // Special case of delimiters:
139       if (group == 0xfffe)
140       {
141          // Delimiters have NO Value Representation
142          // Hence we skip writing the VR.
143          // In order to avoid further troubles, we choose to write them
144          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
145          // The end of a given Item will be found when  :
146          //  - a new Item Delimitor Item is encountered (the Seq goes on)
147          //  - a Sequence Delimitor Item is encountered (the Seq just ended)
148
149        // TODO : verify if the Sequence Delimitor Item was forced during Parsing 
150
151          int ff = 0xffffffff;
152          fwrite (&ff,(size_t)4 ,(size_t)1 ,fp);
153          return;
154       }
155
156       uint16_t z = 0;
157       uint16_t shortLgr = lgr;
158
159       if (vr == "unkn")
160       {
161          // Unknown was 'written'
162          // deal with Little Endian            
163          fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
164          fwrite ( &z,  (size_t)2 ,(size_t)1 ,fp);
165       }
166       else
167       {
168          fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,fp); 
169                   
170          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") )
171          {
172             fwrite ( &z,  (size_t)2 ,(size_t)1 ,fp);
173             if (vr == "SQ")
174             {
175                // we set SQ length to ffffffff
176                // and  we shall write a Sequence Delimitor Item 
177                // at the end of the Sequence! 
178                fwrite ( &FFFF,(size_t)4 ,(size_t)1 ,fp);
179             }
180             else
181             {
182                fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
183             }
184          }
185          else
186          {
187             fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
188          }
189       }
190    } 
191    else // IMPLICIT VR 
192    { 
193       if (vr == "SQ")
194       {
195          fwrite ( &FFFF,(size_t)4 ,(size_t)1 ,fp);
196       }
197       else
198       {
199          fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
200       }
201    }
202 }
203
204 //-----------------------------------------------------------------------------
205 // Public
206
207 /**
208  * \ingroup gdcmDocEntry
209  * \brief   Gets the full length of the elementary DocEntry (not only value length)
210  */
211 uint32_t gdcmDocEntry::GetFullLength()
212 {
213    uint32_t l = GetReadLength();
214    if ( IsImplicitVR() )
215    {
216       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
217    }
218    else
219    {
220       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
221       {
222          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
223       }
224       else
225       {
226          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
227       }
228    }
229    return l;
230 }
231
232 /**
233  * \ingroup gdcmDocEntry
234  * \brief   Copies all the attributes from an other DocEntry 
235  */
236 void gdcmDocEntry::Copy (gdcmDocEntry* e)
237 {
238    entry        = e->entry;
239    UsableLength = e->UsableLength;
240    ReadLength   = e->ReadLength;
241    ImplicitVR   = e->ImplicitVR;
242    Offset       = e->Offset;
243    printLevel   = e->printLevel;
244    // TODO : remove gdcmDocEntry SQDepth
245 }
246
247 /**
248  * \ingroup gdcmDocEntry
249  * \brief   tells us if entry is the last one of a 'no length' SequenceItem 
250  *          (fffe,e00d) 
251  */
252 bool gdcmDocEntry::isItemDelimitor()
253 {
254    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
255 }
256 /**
257  * \ingroup gdcmDocEntry
258  * \brief   tells us if entry is the last one of a 'no length' Sequence 
259  *          (fffe,e0dd) 
260  */
261 bool gdcmDocEntry::isSequenceDelimitor()
262 {
263    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
264 }
265
266 //-----------------------------------------------------------------------------
267 // Protected
268
269
270 //-----------------------------------------------------------------------------
271 // Private
272
273 //-----------------------------------------------------------------------------