5 #include "gdcmElValSet.h"
9 gdcmElValSet::~gdcmElValSet() {
10 for (TagElValueHT::iterator tag = tagHt.begin(); tag != tagHt.end(); ++tag) {
11 gdcmElValue* EntryToDelete = tag->second;
16 // Since Add() adds symetrical in both tagHt and NameHt we can
17 // assume all the pointed gdcmElValues are allready cleaned-up when
22 TagElValueHT & gdcmElValSet::GetTagHt(void) {
27 * \ingroup gdcmElValSet
32 void gdcmElValSet::Add(gdcmElValue * newElValue) {
33 tagHt [newElValue->GetKey()] = newElValue;
34 NameHt[newElValue->GetName()] = newElValue;
39 * \ingroup gdcmElValSet
40 * \brief Checks if a given Dicom element exists
46 int gdcmElValSet::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
47 string key = TranslateToKey(Group, Elem );
48 return (tagHt.count(key));
52 * \ingroup gdcmElValSet
55 void gdcmElValSet::Print(ostream & os) {
62 gdcmTS * ts = gdcmGlobal::GetTS();
64 for (TagElValueHT::iterator tag = tagHt.begin();
67 g = tag->second->GetGroup();
68 e = tag->second->GetElement();
69 v = tag->second->GetValue();
70 o = tag->second->GetOffset();
71 d = _CreateCleanString(v); // TODO : trouver qq chose moins goret
74 os << tag->first << ": ";
75 //os << "[" << v << "]";
76 os << "[" << d2 << "]";
77 os << "[" << tag->second->GetName() << "]";
78 os << "[" << tag->second->GetVR() << "]";
80 if ( (g == 0x0002) && (e == 0x0010) ) {
81 os << " [" << ts->GetValue(v) << "]";
86 os << " lgr : " << tag->second->GetLength();
87 os << ", Offset : " << o;
88 os << " x(" << hex << o << dec << ") ";
94 * \ingroup gdcmElValSet
97 void gdcmElValSet::PrintByName(ostream & os) {
98 for (TagElValueNameHT::iterator tag = NameHt.begin();
101 os << tag->first << ": ";
102 os << "[" << tag->second->GetValue() << "]";
103 os << "[" << tag->second->GetKey() << "]";
104 os << "[" << tag->second->GetVR() << "]" << endl;
109 * \ingroup gdcmElValSet
115 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
116 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
117 if ( ! tagHt.count(key))
118 return (gdcmElValue*)0;
119 return tagHt.find(key)->second;
123 * \ingroup gdcmElValSet
127 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
128 if ( ! NameHt.count(TagName))
129 return (gdcmElValue*)0;
130 return NameHt.find(TagName)->second;
134 * \ingroup gdcmElValSet
140 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
141 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
142 if ( ! tagHt.count(key))
143 return "gdcm::Unfound";
144 return tagHt.find(key)->second->GetValue();
148 * \ingroup gdcmElValSet
152 string gdcmElValSet::GetElValueByName(string TagName) {
153 if ( ! NameHt.count(TagName))
154 return "gdcm::Unfound";
155 return NameHt.find(TagName)->second->GetValue();
159 * \ingroup gdcmElValSet
166 int gdcmElValSet::SetElValueByNumber(string content,
167 guint16 group, guint16 element) {
168 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
169 if ( ! tagHt.count(key))
171 tagHt[key]->SetValue(content);
172 string vr = tagHt[key]->GetVR();
175 if( (vr == "US") || (vr == "SS") )
177 else if( (vr == "UL") || (vr == "SL") )
180 lgr = content.length();
181 tagHt[key]->SetLength(lgr);
186 * \ingroup gdcmElValSet
192 int gdcmElValSet::SetElValueByName(string content, string TagName) {
193 if ( ! NameHt.count(TagName))
195 NameHt[TagName]->SetValue(content);
196 string vr = NameHt[TagName]->GetVR();
199 if( (vr == "US") || (vr == "SS") )
201 else if( (vr == "UL") || (vr == "SL") )
204 lgr = content.length();
206 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
207 // par SetElValueByName
208 // il faudra utiliser SetElValueByNumber
210 NameHt[TagName]->SetLength(lgr);
215 * \ingroup gdcmElValSet
216 * \brief Generate a free TagKey i.e. a TagKey that is not present
217 * in the TagHt dictionary.
218 * @param group The generated tag must belong to this group.
219 * @return The element of tag with given group which is fee.
221 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
222 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
223 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
224 if (tagHt.count(key) == 0)
231 * \ingroup gdcmElValSet
238 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
239 guint16 group, guint16 element) {
240 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
241 if ( ! tagHt.count(key))
243 tagHt[key]->SetLength(length);
249 * \ingroup gdcmElValSet
255 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
256 if ( ! NameHt.count(TagName))
258 NameHt.find(TagName)->second->SetLength(length);
263 * \ingroup gdcmElValSet
264 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
265 * @param SkipSequence
268 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
277 GroupHT groupHt; // to hold the length of each group
280 // typedef std::map<GroupKey, int> GroupHT;
284 // for each Tag in the DCM Header
286 for (TagElValueHT::iterator tag2 = tagHt.begin();
291 gr = elem->GetGroup();
292 el = elem->GetElement();
295 sprintf(trash, "%04x", gr);
296 key = trash; // generate 'group tag'
298 // if the caller decided not to take SEQUENCEs into account
299 // e.g : he wants to write an ACR-NEMA File
301 if (SkipSequence && vr == "SQ") continue;
303 // pas SEQUENCE en ACR-NEMA
304 // WARNING : pb CERTAIN
305 // si on est descendu 'a l'interieur' des SQ
307 // --> la descente a l'interieur' des SQ
308 // devra etre faite avec une liste chainee, pas avec une HTable...
310 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
311 if (el == 0x0000) { // the first elem is 0x0000
312 groupHt[key] = 0; // initialize group length
314 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
316 } else { // any elem but the first
317 if (type == ExplicitVR) {
318 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
319 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
322 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
325 unsigned short int gr_bid;
327 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
330 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
331 // warning: unsigned int format, different type arg
332 sscanf(g->first.c_str(),"%x",&gr_bid);
333 tk = g->first + "|0000"; // generate the element full tag
335 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
336 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
337 elemZ = new gdcmElValue(tagZ);
339 Add(elemZ); // create it
341 elemZ=GetElementByNumber(gr_bid, 0x0000);
343 sprintf(trash ,"%d",g->second);
345 elemZ->SetValue(str_trash);
350 * \ingroup gdcmElValSet
356 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
364 vector<string> tokens;
368 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
369 // restent à tester les echecs en écriture (apres chaque fwrite)
371 for (TagElValueHT::iterator tag2=tagHt.begin();
375 gr = tag2->second->GetGroup();
376 el = tag2->second->GetElement();
377 lgr = tag2->second->GetLength();
378 val = tag2->second->GetValue().c_str();
379 vr = tag2->second->GetVR();
382 if (gr < 0x0008) continue;
383 // if (gr %2) continue; // pour voir
384 if (vr == "SQ" ) continue;
387 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
388 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
390 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
391 // On est en EXPLICIT VR
392 guint16 z=0, shortLgr;
393 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
395 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
396 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
397 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
401 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
404 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
407 tokens.erase(tokens.begin(),tokens.end());
408 Tokenize (tag2->second->GetValue(), tokens, "\\");
410 if (vr == "US" || vr == "SS") {
411 for (unsigned int i=0; i<tokens.size();i++) {
412 val_uint16 = atoi(tokens[i].c_str());
414 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
418 if (vr == "UL" || vr == "SL") {
419 for (unsigned int i=0; i<tokens.size();i++) {
420 val_uint32 = atoi(tokens[i].c_str());
422 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
428 // Les pixels ne sont pas chargés dans l'element !
429 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
431 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
436 * \ingroup gdcmElValSet
442 int gdcmElValSet::Write(FILE * _fp, FileType type) {
444 if (type == ImplicitVR) {
445 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
446 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
448 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
449 // values with a VR of UI shall be padded with a single trailing null
450 // Dans le cas suivant on doit pader manuellement avec un 0
452 SetElValueLengthByNumber(18, 0x0002, 0x0010);
455 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
456 // (FileType est un champ de gdcmHeader ...)
457 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
460 if (type == ExplicitVR) {
461 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
462 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
464 SetElValueLengthByNumber(20, 0x0002, 0x0010);
467 if ( (type == ImplicitVR) || (type == ExplicitVR) )
468 UpdateGroupLength(false,type);
470 UpdateGroupLength(true,ACR);
472 WriteElements(type, _fp);