5 #include "gdcmElValSet.h"
7 gdcmElValSet::~gdcmElValSet() {
8 for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
9 gdcmElValue* EntryToDelete = tag->second;
14 // Since Add() adds symetrical in both tagHt and NameHt we can
15 // assume all the pointed gdcmElValues are allready cleaned-up when
20 TagElValueHT & gdcmElValSet::GetTagHt(void) {
24 void gdcmElValSet::Add(gdcmElValue * newElValue) {
25 tagHt [newElValue->GetKey()] = newElValue;
26 NameHt[newElValue->GetName()] = newElValue;
29 void gdcmElValSet::Print(ostream & os) {
30 for (TagElValueHT::iterator tag = tagHt.begin();
33 os << tag->first << ": ";
34 os << "[" << tag->second->GetValue() << "]";
35 os << "[" << tag->second->GetName() << "]";
36 os << "[" << tag->second->GetVR() << "]";
37 os << " lgr : " << tag->second->GetLength();
42 void gdcmElValSet::PrintByName(ostream & os) {
43 for (TagElValueNameHT::iterator tag = NameHt.begin();
46 os << tag->first << ": ";
47 os << "[" << tag->second->GetValue() << "]";
48 os << "[" << tag->second->GetKey() << "]";
49 os << "[" << tag->second->GetVR() << "]" << endl;
53 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
54 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
55 if ( ! tagHt.count(key))
56 return (gdcmElValue*)0;
57 return tagHt.find(key)->second;
60 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
61 if ( ! NameHt.count(TagName))
62 return (gdcmElValue*)0;
63 return NameHt.find(TagName)->second;
66 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
67 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
68 if ( ! tagHt.count(key))
69 return "gdcm::Unfound";
70 return tagHt.find(key)->second->GetValue();
73 string gdcmElValSet::GetElValueByName(string TagName) {
74 if ( ! NameHt.count(TagName))
75 return "gdcm::Unfound";
76 return NameHt.find(TagName)->second->GetValue();
80 int gdcmElValSet::SetElValueByNumber(string content,
81 guint16 group, guint16 element) {
82 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
83 if ( ! tagHt.count(key))
85 tagHt[key]->SetValue(content);
86 tagHt[key]->SetLength(content.length());
90 int gdcmElValSet::SetElValueByName(string content, string TagName) {
91 if ( ! NameHt.count(TagName))
93 NameHt[TagName]->SetValue(content);
94 NameHt[TagName]->SetLength(content.length());
99 * \ingroup gdcmElValSet
100 * \brief Generate a free TagKey i.e. a TagKey that is not present
101 * in the TagHt dictionary.
102 * @param group The generated tag must belong to this group.
103 * @return The element of tag with given group which is fee.
105 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
106 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
107 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
108 if (tagHt.count(key) == 0)
114 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
115 guint16 group, guint16 element) {
116 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
117 if ( ! tagHt.count(key))
119 tagHt[key]->SetLength(length);
124 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
125 if ( ! NameHt.count(TagName))
127 NameHt.find(TagName)->second->SetLength(length);
133 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
145 for (TagElValueHT::iterator tag2 = tagHt.begin();
150 gr = elem->GetGroup();
151 el = elem->GetElement();
154 sprintf(trash, "%04x", gr);
157 if (SkipSequence && vr == "SQ") continue;
158 // pas SEQUENCE en ACR-NEMA
159 // WARNING : pb CERTAIN
160 // si on est descendu 'a l'interieur' des SQ
162 // --> la descente a l'interieur' des SQ
163 // devra etre faite avec une liste chainee, pas avec une HTable...
165 if ( groupHt.count(key) == 0) {
169 groupHt[key] =2 + 2 + 4 + elem->GetLength();
172 if (type = ExplicitVR) {
173 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
177 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
182 for (GroupHT::iterator g = groupHt.begin();
185 printf("groupKey %s : %d\n",g->first.c_str(),g->second);
189 unsigned short int gr_bid;
191 for (GroupHT::iterator g = groupHt.begin();
195 sscanf(g->first.c_str(),"%x",&gr_bid);
196 tk = g->first + "|0000";
198 if ( tagHt.count(tk) == 0) {
199 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
200 elemZ = new gdcmElValue(tagZ);
204 elemZ=GetElementByNumber(gr_bid, 0x0000);
206 sprintf(trash ,"%d",g->second);
208 elemZ->SetValue(str_trash);
212 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
220 vector<string> tokens;
224 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
225 // restent à tester les echecs en écriture (apres chaque fwrite)
227 for (TagElValueHT::iterator tag2=tagHt.begin();
231 gr = tag2->second->GetGroup();
232 el = tag2->second->GetElement();
233 lgr = tag2->second->GetLength();
234 val = tag2->second->GetValue().c_str();
235 vr = tag2->second->GetVR();
238 if (gr < 0x0008) continue;
240 if (vr == "SQ" ) continue;
243 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
244 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
246 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
247 // On est en EXPLICIT VR
248 guint16 z=0, shortLgr;
249 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
251 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
252 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
253 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
257 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
260 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
263 tokens.erase(tokens.begin(),tokens.end());
264 Tokenize (tag2->second->GetValue(), tokens, "\\");
266 if (vr == "US" || vr == "SS") {
267 for (unsigned int i=0; i<tokens.size();i++) {
268 val_uint16 = atoi(tokens[i].c_str());
270 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
274 if (vr == "UL" || vr == "SL") {
275 for (unsigned int i=0; i<tokens.size();i++) {
276 val_uint32 = atoi(tokens[i].c_str());
278 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
284 // Les pixels ne sont pas chargés dans l'element !
285 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
287 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
291 int gdcmElValSet::Write(FILE * _fp, FileType type) {
293 if (type == ImplicitVR) {
294 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
295 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
297 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
298 // values with a VR of UI shall be padded with a single trailing null
299 // Dans le cas suivant on doit pader manuellement avec un 0
301 SetElValueLengthByNumber(18, 0x0002, 0x0010);
305 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
306 // (FileType est un champ de gdcmHeader ...)
307 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
311 if (type == ExplicitVR) {
312 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
313 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
315 SetElValueLengthByNumber(20, 0x0002, 0x0010);
318 if ( (type == ImplicitVR) || (type == ExplicitVR) )
319 UpdateGroupLength(false,type);
321 UpdateGroupLength(true,ACR);
323 WriteElements(type, _fp);