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