1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2004/07/19 15:16:19 $
7 Version: $Revision: 1.17 $
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 this->UsableLength = e->GetLength();
45 this->ReadLength = e->GetReadLength();
46 this->ImplicitVR = e->IsImplicitVR();
47 this->Offset = e->GetOffset();
48 this->printLevel = e->GetPrintLevel();
49 this->SQDepthLevel = e->GetDepthLevel();
51 this->voidArea = NULL; // will be in BinEntry ?
56 * \brief Canonical destructor.
58 gdcmValEntry::~gdcmValEntry (void)
60 if (!voidArea) // will be in BinEntry
65 //-----------------------------------------------------------------------------
68 * \brief canonical Printer
70 void gdcmValEntry::Print(std::ostream & os)
72 uint16_t g = GetGroup();
73 uint16_t e = GetElement();
74 std::string vr = GetVR();
80 gdcmDocEntry::Print(os);
84 // just to avoid identing all the remaining code
88 gdcmTS * ts = gdcmGlobal::GetTS();
90 v = GetValue(); // not applicable for SQ ...
91 d2 = CreateCleanString(v); // replace non printable characters by '.'
92 if( (GetLength()<=MAX_SIZE_PRINT_ELEMENT_VALUE) ||
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)
104 if (g == 0x0002) { // Any more to be displayed ?
105 if ( (e == 0x0010) || (e == 0x0002) ) {
106 if ( ! isdigit(v[v.length()-1]) )
107 v.erase(v.length()-1, 1);
108 s << " ==>\t[" << ts->GetValue(v) << "]";
112 if ( (e == 0x0016) || (e == 0x1150) ) {
113 if ( ! isdigit(v[v.length()-1]) )
114 v.erase(v.length()-1, 1);
115 s << " ==>\t[" << ts->GetValue(v) << "]";
119 if ( (e == 0x1510) || (e == 0x1512) ) {
120 if ( ! isdigit(v[v.length()-1]) )
121 v.erase(v.length()-1, 1);
122 s << " ==>\t[" << ts->GetValue(v) << "]";
127 //if (e == 0x0000) { // elem 0x0000 --> group length
128 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
130 if (v == "4294967295") // to avoid troubles in convertion
131 sprintf (st," x(ffffffff)");
133 if ( GetLength() !=0 )
134 sprintf(st," x(%x)", atoi(v.c_str()));//FIXME
144 * \brief canonical Writer
146 void gdcmValEntry::Write(FILE *fp, FileType filetype)
148 gdcmDocEntry::Write(fp, filetype);
149 std::string vr=GetVR();
150 int lgr=GetReadLength();
151 if (vr == "US" || vr == "SS")
153 // some 'Short integer' fields may be mulivaluated
154 // each single value is separated from the next one by '\'
155 // we split the string and write each value as a short int
156 std::vector<std::string> tokens;
157 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
158 Tokenize (GetValue(), tokens, "\\");
159 for (unsigned int i=0; i<tokens.size();i++) {
160 uint16_t val_uint16 = atoi(tokens[i].c_str());
161 void *ptr = &val_uint16;
162 fwrite ( ptr,(size_t)2 ,(size_t)1 ,fp);
167 if (vr == "UL" || vr == "SL")
169 // Some 'Integer' fields may be multivaluated (multiple instances
170 // of integers). But each single integer value is separated from the
171 // next one by '\' (backslash character). Hence we split the string
172 // along the '\' and write each value as an int:
173 std::vector<std::string> tokens;
174 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
175 Tokenize (GetValue(), tokens, "\\");
176 for (unsigned int i=0; i<tokens.size();i++){
177 uint32_t val_uint32 = atoi(tokens[i].c_str());
178 void *ptr = &val_uint32;
179 fwrite ( ptr,(size_t)4 ,(size_t)1 ,fp);
185 fwrite (GetValue().c_str(), (size_t)lgr ,(size_t)1, fp); // Elem value
188 //-----------------------------------------------------------------------------
191 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
197 //-----------------------------------------------------------------------------