1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2004/11/05 20:23:14 $
7 Version: $Revision: 1.33 $
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"
30 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
35 * \brief Constructor from a given DictEntry
36 * @param e Pointer to existing dictionary entry
38 ValEntry::ValEntry(DictEntry* e) : DocEntry(e)
43 * \brief Constructor from a given DocEntry
44 * @param e Pointer to existing Doc entry
46 ValEntry::ValEntry(DocEntry* e)
47 : DocEntry(e->GetDictEntry())
49 UsableLength = e->GetLength();
50 ReadLength = e->GetReadLength();
51 ImplicitVR = e->IsImplicitVR();
52 Offset = e->GetOffset();
53 PrintLevel = e->GetPrintLevel();
58 * \brief Canonical destructor.
60 ValEntry::~ValEntry ()
64 //-----------------------------------------------------------------------------
67 * \brief canonical Printer
69 void ValEntry::Print(std::ostream & os)
71 uint16_t g = GetGroup();
72 uint16_t e = GetElement();
73 std::string vr = GetVR();
83 // just to avoid identing all the remaining code
87 TS * ts = Global::GetTS();
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()) )
95 s << " [" << d2 << "]";
99 s << " [gdcm::too long for print (" << GetLength() << ") ]";
102 // Display the UID value (instead of displaying only the rough code)
103 // First 'clean' trailing character (space or zero)
106 // Any more to be displayed ?
107 if ( (e == 0x0010) || (e == 0x0002) )
109 if ( v.length() != 0 ) // for brain damaged headers
111 if ( ! isdigit(v[v.length()-1]) )
113 v.erase(v.length()-1, 1);
116 s << " ==>\t[" << ts->GetValue(v) << "]";
123 if ( e == 0x0016 || e == 0x1150 )
125 if ( v.length() != 0 ) // for brain damaged headers
127 if ( ! isdigit(v[v.length()-1]) )
129 v.erase(v.length()-1, 1);
132 s << " ==>\t[" << ts->GetValue(v) << "]";
139 if ( (e == 0x1510) || (e == 0x1512) )
141 if ( v.length() != 0 ) // for brain damaged headers
143 if ( ! isdigit(v[v.length()-1]) )
145 v.erase(v.length()-1, 1);
148 s << " ==>\t[" << ts->GetValue(v) << "]";
153 //if (e == 0x0000) { // elem 0x0000 --> group length
154 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
156 if (v == "4294967295") // to avoid troubles in convertion
158 st = Util::Format(" x(ffffffff)");
162 if ( GetLength() !=0 )
164 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
168 st = Util::Format(" ");
177 * \brief canonical Writer
179 void ValEntry::Write(std::ofstream* fp, FileType filetype)
181 DocEntry::Write(fp, filetype);
183 //std::cout << "=====================================" << GetVR() << std::endl;
185 if ( GetGroup() == 0xfffe )
187 return; //delimitors have NO value
190 std::string vr = GetVR();
191 int lgr = GetReadLength();
192 if (vr == "US" || vr == "SS")
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++)
202 uint16_t val_uint16 = atoi(tokens[i].c_str());
203 binary_write( *fp, val_uint16);
208 if (vr == "UL" || vr == "SL")
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++)
219 uint32_t val_uint32 = atoi(tokens[i].c_str());
220 binary_write( *fp, val_uint32);
226 //Due to seriously broken Theralys images I cannot put that here.
227 //assert( lgr == GetValue().size() );
228 binary_write(*fp, GetValue());
231 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
237 //-----------------------------------------------------------------------------
240 //-----------------------------------------------------------------------------
241 } // end namespace gdcm