]> Creatis software - gdcm.git/blob - src/gdcmValEntry.cxx
BUG: Fix bug for Philips NTSCAN, this was fixed everywhere except the main tree...
[gdcm.git] / src / gdcmValEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmValEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/07 22:19:48 $
7   Version:   $Revision: 1.44 $
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 "gdcmValEntry.h"
20 #include "gdcmVR.h"
21 #include "gdcmTS.h"
22 #include "gdcmGlobal.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDebug.h"
25
26 #include <fstream>
27
28 namespace gdcm 
29 {
30
31 // CLEAN ME
32 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
33
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
36 /**
37  * \brief   Constructor from a given DictEntry
38  * @param   e Pointer to existing dictionary entry
39  */
40 ValEntry::ValEntry(DictEntry *e) : DocEntry(e)
41 {
42 }
43
44 /**
45  * \brief   Constructor from a given DocEntry
46  * @param   e Pointer to existing Doc entry
47  */
48 ValEntry::ValEntry(DocEntry *e)
49              : DocEntry(e->GetDictEntry())
50 {
51    Copy(e);
52 /*   Length     = e->GetLength();
53    ReadLength = e->GetReadLength();
54    ImplicitVR = e->IsImplicitVR();
55    Offset     = e->GetOffset();*/
56 }
57
58
59 /**
60  * \brief   Canonical destructor.
61  */
62 ValEntry::~ValEntry ()
63 {
64 }
65
66 //-----------------------------------------------------------------------------
67 // Print
68 /**
69  * \brief   canonical Printer
70  */
71 void ValEntry::Print(std::ostream &os)
72 {
73    uint16_t g = GetGroup();
74    uint16_t e = GetElement();
75    std::string vr = GetVR();
76    std::ostringstream s; 
77    std::string st;
78    TSKey v;
79    std::string d2;
80      
81    os << "V ";
82    DocEntry::Print(os); 
83
84    if (g == 0xfffe)
85    {
86       // just to avoid identing all the remaining code     
87       return;
88    }
89    
90    TS * ts = Global::GetTS();
91     
92    v  = GetValue();  // not applicable for SQ ...     
93    d2 = Util::CreateCleanString(v);  // replace non printable characters by '.'            
94    if( (GetLength()<=MAX_SIZE_PRINT_ELEMENT_VALUE) || 
95        //(PrintLevel>=3)  || (d2.find("gdcm::NotLoaded.") < d2.length()) )
96        (PrintLevel>=3)  || (d2.find(GDCM_NOTLOADED) < d2.length()) )
97    {
98       s << " [" << d2 << "]";
99    }
100    else
101    {
102       s << " [gdcm::too long for print (" << GetLength() << ") ]";
103    }
104    
105    // Display the UID value (instead of displaying only the rough code)
106    // First 'clean' trailing character (space or zero) 
107    if (g == 0x0002)
108    {
109       // Any more to be displayed ?
110       if ( (e == 0x0010) || (e == 0x0002) )
111       {
112          if ( v.length() != 0 )  // for brain damaged headers
113          {
114             if ( ! isdigit((unsigned char)v[v.length()-1]) )
115             {
116                v.erase(v.length()-1, 1);
117             }
118          }
119          s << "  ==>\t[" << ts->GetValue(v) << "]";
120       }
121    }
122    else
123    {
124       if (g == 0x0008)
125       {
126          if ( e == 0x0016 || e == 0x1150 )
127          {
128             if ( v.length() != 0 )  // for brain damaged headers
129             {
130                if ( ! isdigit((unsigned char)v[v.length()-1]) )
131                {
132                   v.erase(v.length()-1, 1);
133                }
134             }
135             s << "  ==>\t[" << ts->GetValue(v) << "]";
136          }
137       }
138       else
139       {
140          if (g == 0x0004)
141          {
142             if ( (e == 0x1510) || (e == 0x1512)  )
143             {
144                if ( v.length() != 0 )  // for brain damaged headers  
145                {
146                   if ( ! isdigit((unsigned char)v[v.length()-1]) )
147                   {
148                      v.erase(v.length()-1, 1);  
149                   }
150                }
151               s << "  ==>\t[" << ts->GetValue(v) << "]";
152             }
153          }     
154       }
155    }
156    //if (e == 0x0000) {        // elem 0x0000 --> group length 
157    if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
158    {
159       if (v == "4294967295") // to avoid troubles in convertion 
160       {
161          st = Util::Format(" x(ffffffff)");
162       }
163       else
164       {
165          if ( GetLength() !=0 )
166          {
167             st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
168          }
169          else
170          {
171             st = Util::Format(" ");
172          }
173       }
174       s << st;
175    }
176    os << s.str();
177 }
178
179 //-----------------------------------------------------------------------------
180 // Public
181 void ValEntry::SetValue(std::string const &val)
182 {
183    // Integers have a special treatement for their length:
184    int l = val.length();
185    if ( l != 0) // To avoid to be cheated by 'zero length' integers
186    {   
187       VRKey vr = GetVR();
188       if( vr == "US" || vr == "SS" )
189       {
190          // for multivaluated items
191          l = (Util::CountSubstring(val, "\\") + 1) * 2;
192          SetValueOnly(val);
193       }
194       else if( vr == "UL" || vr == "SL" )
195       {
196          // for multivaluated items
197          l = (Util::CountSubstring(val, "\\") + 1) * 4;;
198          SetValueOnly(val);
199       }
200       else
201       {
202          std::string finalVal = Util::DicomString( val.c_str() );
203          gdcmAssertMacro( !(finalVal.size() % 2) );
204
205          l = finalVal.length();
206          SetValueOnly(finalVal);
207       }
208    }
209    else
210    {
211       std::string finalVal = Util::DicomString( val.c_str() );
212       gdcmAssertMacro( !(finalVal.size() % 2) );
213
214       l = finalVal.length();
215       SetValueOnly(finalVal);
216    }
217
218    SetLength(l);
219 }
220
221 /*
222  * \brief   canonical Writer
223  */
224 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
225 {
226    DocEntry::WriteContent(fp, filetype);
227
228    if ( GetGroup() == 0xfffe )
229    {
230       return; //delimitors have NO value
231    }
232
233    std::string vr = GetVR();
234    unsigned int lgr = GetLength();
235    //std::cout<<std::hex<<GetGroup()<<"|"<<GetElement()<<std::dec<<" : "<<GetReadLength()<<" / "<<GetLength()<<"\n";
236    if (vr == "US" || vr == "SS")
237    {
238       // some 'Short integer' fields may be mulivaluated
239       // each single value is separated from the next one by '\'
240       // we split the string and write each value as a short int
241       std::vector<std::string> tokens;
242       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
243       Util::Tokenize (GetValue(), tokens, "\\");
244       for (unsigned int i=0; i<tokens.size();i++)
245       {
246          uint16_t val_uint16 = atoi(tokens[i].c_str());
247          binary_write( *fp, val_uint16);
248       }
249       tokens.clear();
250       return;
251    }
252    if (vr == "UL" || vr == "SL")
253    {
254       // Some 'Integer' fields may be multivaluated (multiple instances 
255       // of integers). But each single integer value is separated from the
256       // next one by '\' (backslash character). Hence we split the string
257       // along the '\' and write each value as an int:
258       std::vector<std::string> tokens;
259       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
260       Util::Tokenize (GetValue(), tokens, "\\");
261       for (unsigned int i=0; i<tokens.size();i++)
262       {
263          uint32_t val_uint32 = atoi(tokens[i].c_str());
264          binary_write( *fp, val_uint32);
265       }
266       tokens.clear();
267       return;
268    } 
269
270    gdcmAssertMacro( lgr == GetValue().length() );
271    binary_write(*fp, GetValue());
272
273
274 //-----------------------------------------------------------------------------
275 // Protected
276
277 //-----------------------------------------------------------------------------
278 // Private
279
280 //-----------------------------------------------------------------------------
281 } // end namespace gdcm
282