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