1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2004/10/12 04:35:48 $
7 Version: $Revision: 1.30 $
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.
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"
28 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
33 * \brief Constructor from a given DictEntry
34 * @param e Pointer to existing dictionary entry
36 ValEntry::ValEntry(DictEntry* e) : DocEntry(e)
41 * \brief Constructor from a given DocEntry
42 * @param e Pointer to existing Doc entry
44 ValEntry::ValEntry(DocEntry* e)
45 : DocEntry(e->GetDictEntry())
47 UsableLength = e->GetLength();
48 ReadLength = e->GetReadLength();
49 ImplicitVR = e->IsImplicitVR();
50 Offset = e->GetOffset();
51 PrintLevel = e->GetPrintLevel();
56 * \brief Canonical destructor.
58 ValEntry::~ValEntry ()
62 //-----------------------------------------------------------------------------
65 * \brief canonical Printer
67 void ValEntry::Print(std::ostream & os)
69 uint16_t g = GetGroup();
70 uint16_t e = GetElement();
71 std::string vr = GetVR();
81 // just to avoid identing all the remaining code
85 TS * ts = Global::GetTS();
87 v = GetValue(); // not applicable for SQ ...
88 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
89 if( (GetLength()<=MAX_SIZE_PRINT_ELEMENT_VALUE) ||
90 //(PrintLevel>=3) || (d2.find("gdcm::NotLoaded.") < d2.length()) )
91 (PrintLevel>=3) || (d2.find(GDCM_NOTLOADED) < d2.length()) )
93 s << " [" << d2 << "]";
97 s << " [gdcm::too long for print (" << GetLength() << ") ]";
100 // Display the UID value (instead of displaying only the rough code)
101 // First 'clean' trailing character (space or zero)
104 // Any more to be displayed ?
105 if ( (e == 0x0010) || (e == 0x0002) )
107 if ( v.length() != 0 ) // for brain damaged headers
109 if ( ! isdigit(v[v.length()-1]) )
111 v.erase(v.length()-1, 1);
114 s << " ==>\t[" << ts->GetValue(v) << "]";
121 if ( e == 0x0016 || e == 0x1150 )
123 if ( v.length() != 0 ) // for brain damaged headers
125 if ( ! isdigit(v[v.length()-1]) )
127 v.erase(v.length()-1, 1);
130 s << " ==>\t[" << ts->GetValue(v) << "]";
137 if ( (e == 0x1510) || (e == 0x1512) )
139 if ( v.length() != 0 ) // for brain damaged headers
141 if ( ! isdigit(v[v.length()-1]) )
143 v.erase(v.length()-1, 1);
146 s << " ==>\t[" << ts->GetValue(v) << "]";
151 //if (e == 0x0000) { // elem 0x0000 --> group length
152 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
154 if (v == "4294967295") // to avoid troubles in convertion
156 st = Util::Format(" x(ffffffff)");
160 if ( GetLength() !=0 )
162 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
166 st = Util::Format(" ");
175 * \brief canonical Writer
177 void ValEntry::Write(FILE* fp, FileType filetype)
179 DocEntry::Write(fp, filetype);
181 //std::cout << "=====================================" << GetVR() << std::endl;
183 if ( GetGroup() == 0xfffe )
185 return; //delimitors have NO value
188 std::string vr = GetVR();
189 int lgr = GetReadLength();
190 if (vr == "US" || vr == "SS")
192 // some 'Short integer' fields may be mulivaluated
193 // each single value is separated from the next one by '\'
194 // we split the string and write each value as a short int
195 std::vector<std::string> tokens;
196 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
197 Util::Tokenize (GetValue(), tokens, "\\");
198 for (unsigned int i=0; i<tokens.size();i++)
200 uint16_t val_uint16 = atoi(tokens[i].c_str());
201 void* ptr = &val_uint16;
202 fwrite ( ptr,(size_t)2 ,(size_t)1 ,fp);
207 if (vr == "UL" || vr == "SL")
209 // Some 'Integer' fields may be multivaluated (multiple instances
210 // of integers). But each single integer value is separated from the
211 // next one by '\' (backslash character). Hence we split the string
212 // along the '\' and write each value as an int:
213 std::vector<std::string> tokens;
214 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
215 Util::Tokenize (GetValue(), tokens, "\\");
216 for (unsigned int i=0; i<tokens.size();i++)
218 uint32_t val_uint32 = atoi(tokens[i].c_str());
219 void* ptr = &val_uint32;
220 fwrite ( ptr,(size_t)4 ,(size_t)1 ,fp);
226 fwrite (GetValue().c_str(), (size_t)lgr ,(size_t)1, fp); // Elem value
229 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
239 } // end namespace gdcm