5 #include "gdcmElValSet.h"
8 gdcmElValSet::~gdcmElValSet() {
9 for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
10 gdcmElValue* EntryToDelete = tag->second;
15 // Since Add() adds symetrical in both tagHt and NameHt we can
16 // assume all the pointed gdcmElValues are allready cleaned-up when
21 TagElValueHT & gdcmElValSet::GetTagHt(void) {
25 void gdcmElValSet::Add(gdcmElValue * newElValue) {
26 tagHt [newElValue->GetKey()] = newElValue;
27 NameHt[newElValue->GetName()] = newElValue;
30 void gdcmElValSet::Print(ostream & os) {
31 for (TagElValueHT::iterator tag = tagHt.begin();
34 os << tag->first << ": ";
35 os << "[" << tag->second->GetValue() << "]";
36 os << "[" << tag->second->GetName() << "]";
37 os << "[" << tag->second->GetVR() << "]";
38 os << " lgr : " << tag->second->GetLength();
43 void gdcmElValSet::PrintByName(ostream & os) {
44 for (TagElValueNameHT::iterator tag = NameHt.begin();
47 os << tag->first << ": ";
48 os << "[" << tag->second->GetValue() << "]";
49 os << "[" << tag->second->GetKey() << "]";
50 os << "[" << tag->second->GetVR() << "]" << endl;
54 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
55 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
56 if ( ! tagHt.count(key))
57 return (gdcmElValue*)0;
58 return tagHt.find(key)->second;
61 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
62 if ( ! NameHt.count(TagName))
63 return (gdcmElValue*)0;
64 return NameHt.find(TagName)->second;
67 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
68 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
69 if ( ! tagHt.count(key))
70 return "gdcm::Unfound";
71 return tagHt.find(key)->second->GetValue();
74 string gdcmElValSet::GetElValueByName(string TagName) {
75 if ( ! NameHt.count(TagName))
76 return "gdcm::Unfound";
77 return NameHt.find(TagName)->second->GetValue();
81 int gdcmElValSet::SetElValueByNumber(string content,
82 guint16 group, guint16 element) {
83 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
84 if ( ! tagHt.count(key))
86 tagHt[key]->SetValue(content);
87 string vr = tagHt[key]->GetVR();
90 if( (vr == "US") || (vr == "SS") )
92 else if( (vr == "UL") || (vr == "SL") )
95 lgr = content.length();
96 tagHt[key]->SetLength(lgr);
100 int gdcmElValSet::SetElValueByName(string content, string TagName) {
101 if ( ! NameHt.count(TagName))
103 NameHt[TagName]->SetValue(content);
104 string vr = NameHt[TagName]->GetVR();
107 if( (vr == "US") || (vr == "SS") )
109 else if( (vr == "UL") || (vr == "SL") )
112 lgr = content.length();
114 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
115 // par SetElValueByName
116 // il faudra utiliser SetElValueByNumber
118 NameHt[TagName]->SetLength(lgr);
123 * \ingroup gdcmElValSet
124 * \brief Generate a free TagKey i.e. a TagKey that is not present
125 * in the TagHt dictionary.
126 * @param group The generated tag must belong to this group.
127 * @return The element of tag with given group which is fee.
129 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
130 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
131 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
132 if (tagHt.count(key) == 0)
138 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
139 guint16 group, guint16 element) {
140 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
141 if ( ! tagHt.count(key))
143 tagHt[key]->SetLength(length);
148 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
149 if ( ! NameHt.count(TagName))
151 NameHt.find(TagName)->second->SetLength(length);
157 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
169 for (TagElValueHT::iterator tag2 = tagHt.begin();
174 gr = elem->GetGroup();
175 el = elem->GetElement();
178 sprintf(trash, "%04x", gr);
181 if (SkipSequence && vr == "SQ") continue;
182 // pas SEQUENCE en ACR-NEMA
183 // WARNING : pb CERTAIN
184 // si on est descendu 'a l'interieur' des SQ
186 // --> la descente a l'interieur' des SQ
187 // devra etre faite avec une liste chainee, pas avec une HTable...
189 if ( groupHt.count(key) == 0) {
193 groupHt[key] =2 + 2 + 4 + elem->GetLength();
196 if (type = ExplicitVR) {
197 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
201 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
206 for (GroupHT::iterator g = groupHt.begin();
209 printf("groupKey %s : %d\n",g->first.c_str(),g->second);
213 unsigned short int gr_bid;
215 for (GroupHT::iterator g = groupHt.begin();
219 sscanf(g->first.c_str(),"%x",&gr_bid);
220 tk = g->first + "|0000";
222 if ( tagHt.count(tk) == 0) {
223 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
224 elemZ = new gdcmElValue(tagZ);
228 elemZ=GetElementByNumber(gr_bid, 0x0000);
230 sprintf(trash ,"%d",g->second);
232 elemZ->SetValue(str_trash);
236 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
244 vector<string> tokens;
248 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
249 // restent à tester les echecs en écriture (apres chaque fwrite)
251 for (TagElValueHT::iterator tag2=tagHt.begin();
255 gr = tag2->second->GetGroup();
256 el = tag2->second->GetElement();
257 lgr = tag2->second->GetLength();
258 val = tag2->second->GetValue().c_str();
259 vr = tag2->second->GetVR();
262 if (gr < 0x0008) continue;
264 if (vr == "SQ" ) continue;
267 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
268 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
270 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
271 // On est en EXPLICIT VR
272 guint16 z=0, shortLgr;
273 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
275 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
276 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
277 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
281 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
284 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
287 tokens.erase(tokens.begin(),tokens.end());
288 Tokenize (tag2->second->GetValue(), tokens, "\\");
290 if (vr == "US" || vr == "SS") {
291 for (unsigned int i=0; i<tokens.size();i++) {
292 val_uint16 = atoi(tokens[i].c_str());
294 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
298 if (vr == "UL" || vr == "SL") {
299 for (unsigned int i=0; i<tokens.size();i++) {
300 val_uint32 = atoi(tokens[i].c_str());
302 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
308 // Les pixels ne sont pas chargés dans l'element !
309 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
311 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
315 int gdcmElValSet::Write(FILE * _fp, FileType type) {
317 if (type == ImplicitVR) {
318 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
319 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
321 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
322 // values with a VR of UI shall be padded with a single trailing null
323 // Dans le cas suivant on doit pader manuellement avec un 0
325 SetElValueLengthByNumber(18, 0x0002, 0x0010);
329 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
330 // (FileType est un champ de gdcmHeader ...)
331 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
335 if (type == ExplicitVR) {
336 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
337 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
339 SetElValueLengthByNumber(20, 0x0002, 0x0010);
342 if ( (type == ImplicitVR) || (type == ExplicitVR) )
343 UpdateGroupLength(false,type);
345 UpdateGroupLength(true,ACR);
347 WriteElements(type, _fp);