]> Creatis software - gdcm.git/blob - src/gdcmDocEntry.cxx
gdcmDocEntry::PrintCommonPart() and ::WriteCommonPart() removed.
[gdcm.git] / src / gdcmDocEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/06/23 13:02:36 $
7   Version:   $Revision: 1.9 $
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    ImplicitVR = false;
38    entry = in;
39 }
40
41 //-----------------------------------------------------------------------------
42 // Print
43 /**
44  * \ingroup gdcmDocEntry
45  * \brief   Prints the common part of gdcmValEntry, gdcmBinEntry, gdcmSeqEntry
46  * @param   os ostream we want to print in
47  */
48 void gdcmDocEntry::Print(std::ostream & os) {
49
50    printLevel=2; // FIXME
51    
52    size_t o;
53    unsigned short int g, e;
54    char st[20];
55    TSKey v;
56    std::string d2, vr;
57    std::ostringstream s;
58    guint32 lgth;
59    char greltag[10];  //group element tag
60
61    g  = GetGroup();
62    e  = GetElement();
63    o  = GetOffset();
64    vr = GetVR();
65    sprintf(greltag,"%04x|%04x ",g,e);           
66    s << greltag ;
67        
68    if (printLevel>=2) { 
69       s << "lg : ";
70       lgth = GetReadLength(); // ReadLength, as opposed to UsableLength
71       if (lgth == 0xffffffff) {
72          sprintf(st,"x(ffff)");  // I said : "x(ffff)" !
73          s.setf(std::ios::left);
74          s << std::setw(10-strlen(st)) << " ";  
75          s << st << " ";
76          s.setf(std::ios::left);
77          s << std::setw(8) << "-1";      
78       } else {
79          sprintf(st,"x(%x)",lgth);
80          s.setf(std::ios::left);
81          s << std::setw(10-strlen(st)) << " ";  
82          s << st << " ";
83          s.setf(std::ios::left);
84          s << std::setw(8) << lgth; 
85       }
86       s << " Off.: ";
87       sprintf(st,"x(%x)",o); 
88       s << std::setw(10-strlen(st)) << " ";
89       s << st << " ";
90       s << std::setw(8) << o; 
91    }
92
93    s << "[" << vr  << "] ";
94
95    if (printLevel>=1) {      
96       s.setf(std::ios::left);
97       s << std::setw(66-GetName().length()) << " ";
98    }
99     
100    s << "[" << GetName()<< "]";
101    os << s.str();      
102 }
103
104 /**
105  * \ingroup gdcmDocEntry
106  * \brief   Writes the common part of any gdcmValEntry, gdcmBinEntry, gdcmSeqEntry
107  */
108 void gdcmDocEntry::Write(FILE *fp, FileType filetype) {
109
110    guint16 group  = GetGroup();
111    VRKey   vr     = GetVR();
112    guint16 el     = GetElement();
113    guint32 lgr    = GetReadLength();
114
115    if ( (group == 0xfffe) && (el == 0x0000) ) 
116      // Fix in order to make some MR PHILIPS images e-film readable
117      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
118      // we just *always* ignore spurious fffe|0000 tag !   
119       return; 
120
121 //
122 // ----------- Writes the common part
123 //
124    fwrite ( &group,(size_t)2 ,(size_t)1 ,fp);  //group
125    fwrite ( &el,   (size_t)2 ,(size_t)1 ,fp);  //element
126       
127    if ( filetype == gdcmExplicitVR ) {
128
129       // Special case of delimiters:
130       if (group == 0xfffe) {
131          // Delimiters have NO Value Representation
132          // Hence we skip writing the VR.
133          // In order to avoid further troubles, we choose to write them
134          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
135          // The end of a given Item will be found when  :
136          //  - a new Item Delimitor Item is encountered (the Seq goes on)
137          //  - a Sequence Delimitor Item is encountered (the Seq just ended)
138
139        // TODO : verify if the Sequence Delimitor Item was forced during Parsing 
140
141          int ff=0xffffffff;
142          fwrite (&ff,(size_t)4 ,(size_t)1 ,fp);
143          return;
144       }
145
146       guint16 z=0;
147       guint16 shortLgr = lgr;
148       if (vr == "unkn") {     // Unknown was 'written'
149          // deal with Little Endian            
150          fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
151          fwrite ( &z,  (size_t)2 ,(size_t)1 ,fp);
152       } else {
153          fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,fp); 
154
155 // TODO : better we set SQ length to ffffffff
156 //      and write a Sequence Delimitor Item at the end of the Sequence!                    
157          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
158          {
159             fwrite ( &z,  (size_t)2 ,(size_t)1 ,fp);
160             fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
161          } else {
162             fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,fp);
163          }
164       }
165    } 
166    else // IMPLICIT VR 
167    { 
168       fwrite ( &lgr,(size_t)4 ,(size_t)1 ,fp);
169    }
170 }
171
172 //-----------------------------------------------------------------------------
173 // Public
174
175 /**
176  * \ingroup gdcmDocEntry
177  * \brief   Gets the full length of the elementary DocEntry (not only value length)
178  */
179 guint32 gdcmDocEntry::GetFullLength(void) {
180    guint32 l;
181    l = GetReadLength();
182    if ( IsImplicitVR() ) 
183       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
184    else    
185       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
186          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
187       else
188          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
189    return(l);
190 }
191
192 /**
193  * \ingroup gdcmDocEntry
194  * \brief   Copies all the attributes from an other DocEntry 
195  */
196 void gdcmDocEntry::Copy (gdcmDocEntry* e) {
197    this->entry        = e->entry;
198    this->UsableLength = e->UsableLength;
199    this->ReadLength   = e->ReadLength;
200    this->ImplicitVR   = e->ImplicitVR;
201    this->Offset       = e->Offset;
202    this->printLevel   = e->printLevel;
203    // TODO : remove gdcmDocEntry SQDepth
204 }
205
206 /**
207  * \ingroup gdcmDocEntry
208  * \brief   tells us if entry is the first one of a Sequence Item (fffe,e00d) 
209  */
210 bool gdcmDocEntry::isItemDelimitor() {
211    if ( (GetGroup() == 0xfffe) && (GetElement() == 0xe00d) )
212       return true;
213    else
214       return false;      
215 }
216 /**
217  * \ingroup gdcmDocEntry
218  * \brief   tells us if entry is the last one of a 'no length' Sequence fffe,e0dd) 
219  */
220 bool gdcmDocEntry::isSequenceDelimitor() {
221    if (GetGroup() == 0xfffe && GetElement() == 0xe0dd)
222       return true;
223    else
224       return false; 
225 }
226
227 //-----------------------------------------------------------------------------
228 // Protected
229
230
231 //-----------------------------------------------------------------------------
232 // Private
233
234 //-----------------------------------------------------------------------------