1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2004/09/09 17:49:25 $
7 Version: $Revision: 1.23 $
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.
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.
17 =========================================================================*/
19 #include "gdcmValEntry.h"
21 #include "gdcmGlobal.h"
25 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
27 //-----------------------------------------------------------------------------
28 // Constructor / Destructor
30 * \brief Constructor from a given gdcmDictEntry
31 * @param e Pointer to existing dictionary entry
33 gdcmValEntry::gdcmValEntry(gdcmDictEntry* e) : gdcmDocEntry(e)
35 VoidArea = NULL; // will be in BinEntry ?
39 * \brief Constructor from a given gdcmDocEntry
40 * @param e Pointer to existing Doc entry
42 gdcmValEntry::gdcmValEntry(gdcmDocEntry* e) : gdcmDocEntry(e->GetDictEntry())
44 UsableLength = e->GetLength();
45 ReadLength = e->GetReadLength();
46 ImplicitVR = e->IsImplicitVR();
47 Offset = e->GetOffset();
48 PrintLevel = e->GetPrintLevel();
49 SQDepthLevel = e->GetDepthLevel();
51 VoidArea = NULL; // will be in BinEntry ?
56 * \brief Canonical destructor.
58 gdcmValEntry::~gdcmValEntry ()
60 if (!VoidArea) // will be in BinEntry
63 VoidArea = NULL; // let's be carefull !
67 //-----------------------------------------------------------------------------
70 * \brief canonical Printer
72 void gdcmValEntry::Print(std::ostream & os)
74 uint16_t g = GetGroup();
75 uint16_t e = GetElement();
76 std::string vr = GetVR();
82 gdcmDocEntry::Print(os);
86 // just to avoid identing all the remaining code
90 gdcmTS * ts = gdcmGlobal::GetTS();
92 v = GetValue(); // not applicable for SQ ...
93 d2 = 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()) )
98 s << " [" << d2 << "]";
102 s << " [gdcm::too long for print (" << GetLength() << ") ]";
105 // Display the UID value (instead of displaying only the rough code)
106 // First 'clean' trailing character (space or zero)
109 // Any more to be displayed ?
110 if ( (e == 0x0010) || (e == 0x0002) )
112 if ( v.length() != 0 ) // for brain damaged headers
114 if ( ! isdigit(v[v.length()-1]) )
116 v.erase(v.length()-1, 1);
119 s << " ==>\t[" << ts->GetValue(v) << "]";
126 if ( e == 0x0016 || e == 0x1150 )
128 if ( v.length() != 0 ) // for brain damaged headers
130 if ( ! isdigit(v[v.length()-1]) )
132 v.erase(v.length()-1, 1);
135 s << " ==>\t[" << ts->GetValue(v) << "]";
142 if ( (e == 0x1510) || (e == 0x1512) )
144 if ( v.length() != 0 ) // for brain damaged headers
146 if ( ! isdigit(v[v.length()-1]) )
148 v.erase(v.length()-1, 1);
151 s << " ==>\t[" << ts->GetValue(v) << "]";
156 //if (e == 0x0000) { // elem 0x0000 --> group length
157 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
159 if (v == "4294967295") // to avoid troubles in convertion
161 sprintf (st," x(ffffffff)");
165 if ( GetLength() !=0 )
167 sprintf(st," x(%x)", atoi(v.c_str()));//FIXME
180 * \brief canonical Writer
182 void gdcmValEntry::Write(FILE *fp, FileType filetype)
184 gdcmDocEntry::Write(fp, filetype);
186 if ( GetGroup() == 0xfffe )
188 return; //delimitors have NO value
191 std::string vr = GetVR();
192 int lgr = GetReadLength();
193 if (vr == "US" || vr == "SS")
195 // some 'Short integer' fields may be mulivaluated
196 // each single value is separated from the next one by '\'
197 // we split the string and write each value as a short int
198 std::vector<std::string> tokens;
199 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
200 Tokenize (GetValue(), tokens, "\\");
201 for (unsigned int i=0; i<tokens.size();i++)
203 uint16_t val_uint16 = atoi(tokens[i].c_str());
204 void *ptr = &val_uint16;
205 fwrite ( ptr,(size_t)2 ,(size_t)1 ,fp);
210 if (vr == "UL" || vr == "SL")
212 // Some 'Integer' fields may be multivaluated (multiple instances
213 // of integers). But each single integer value is separated from the
214 // next one by '\' (backslash character). Hence we split the string
215 // along the '\' and write each value as an int:
216 std::vector<std::string> tokens;
217 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
218 Tokenize (GetValue(), tokens, "\\");
219 for (unsigned int i=0; i<tokens.size();i++)
221 uint32_t val_uint32 = atoi(tokens[i].c_str());
222 void *ptr = &val_uint32;
223 fwrite ( ptr,(size_t)4 ,(size_t)1 ,fp);
229 fwrite (GetValue().c_str(), (size_t)lgr ,(size_t)1, fp); // Elem value
232 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
241 //-----------------------------------------------------------------------------