4 #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 already cleaned-up when
21 TagElValueHT & gdcmElValSet::GetTagHt(void) {
26 * \ingroup gdcmElValSet
31 void gdcmElValSet::Add(gdcmElValue * newElValue) {
32 tagHt [newElValue->GetKey()] = newElValue;
33 NameHt[newElValue->GetName()] = newElValue;
37 * \ingroup gdcmElValSet
38 * \brief Checks if a given Dicom element exists
44 int gdcmElValSet::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
45 std::string key = TranslateToKey(Group, Elem );
46 return (tagHt.count(key));
50 * \ingroup gdcmElValSet
53 void gdcmElValSet::Print(std::ostream & os) {
59 gdcmTS * ts = gdcmGlobal::GetTS();
61 for (TagElValueHT::iterator tag = tagHt.begin();
64 g = tag->second->GetGroup();
65 e = tag->second->GetElement();
66 v = tag->second->GetValue();
67 o = tag->second->GetOffset();
68 d2 = _CreateCleanString(v); // replace non printable characters by '.'
70 os << tag->first << ": ";
71 os << " lgr : " << tag->second->GetLength();
72 os << ", Offset : " << o;
73 os << " x(" << std::hex << o << std::dec << ") ";
74 os << "\t[" << tag->second->GetVR() << "]";
75 os << "\t[" << tag->second->GetName() << "]";
76 os << "\t[" << d2 << "]";
78 // Display the UID value (instead of displaying the rough code)
79 if (g == 0x0002) { // Some more to be displayed ?
80 if ( (e == 0x0010) || (e == 0x0002) )
81 os << " ==>\t[" << ts->GetValue(v) << "]";
84 if ( (e == 0x0016) || (e == 0x1150) )
85 os << " ==>\t[" << ts->GetValue(v) << "]";
93 * \ingroup gdcmElValSet
96 void gdcmElValSet::PrintByName(std::ostream & os) {
97 for (TagElValueNameHT::iterator tag = NameHt.begin();
100 os << tag->first << ": ";
101 os << "[" << tag->second->GetValue() << "]";
102 os << "[" << tag->second->GetKey() << "]";
103 os << "[" << tag->second->GetVR() << "]" << std::endl;
108 * \ingroup gdcmElValSet
114 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
115 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
116 if ( ! tagHt.count(key))
117 return (gdcmElValue*)0;
118 return tagHt.find(key)->second;
122 * \ingroup gdcmElValSet
126 gdcmElValue* gdcmElValSet::GetElementByName(std::string TagName) {
127 if ( ! NameHt.count(TagName))
128 return (gdcmElValue*)0;
129 return NameHt.find(TagName)->second;
133 * \ingroup gdcmElValSet
139 std::string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
140 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
141 if ( ! tagHt.count(key))
143 return tagHt.find(key)->second->GetValue();
147 * \ingroup gdcmElValSet
151 std::string gdcmElValSet::GetElValueByName(std::string TagName) {
152 if ( ! NameHt.count(TagName))
154 return NameHt.find(TagName)->second->GetValue();
158 * \ingroup gdcmElValSet
165 int gdcmElValSet::SetElValueByNumber(std::string content,
166 guint16 group, guint16 element) {
167 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
168 if ( ! tagHt.count(key))
170 tagHt[key]->SetValue(content);
171 std::string vr = tagHt[key]->GetVR();
174 if( (vr == "US") || (vr == "SS") )
176 else if( (vr == "UL") || (vr == "SL") )
179 lgr = content.length();
180 tagHt[key]->SetLength(lgr);
185 * \ingroup gdcmElValSet
191 int gdcmElValSet::SetElValueByName(std::string content, std::string TagName) {
192 if ( ! NameHt.count(TagName))
194 NameHt[TagName]->SetValue(content);
195 std::string vr = NameHt[TagName]->GetVR();
198 if( (vr == "US") || (vr == "SS") )
200 else if( (vr == "UL") || (vr == "SL") )
203 lgr = content.length();
205 // TODO : WARNING: le cas de l'element des pixels (7fe0,0010) n'est pas traite
206 // par SetElValueByName
207 // il faudra utiliser SetElValueByNumber
209 NameHt[TagName]->SetLength(lgr);
214 * \ingroup gdcmElValSet
215 * \brief Generate a free TagKey i.e. a TagKey that is not present
216 * in the TagHt dictionary.
217 * @param group The generated tag must belong to this group.
218 * @return The element of tag with given group which is fee.
220 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
221 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
222 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
223 if (tagHt.count(key) == 0)
230 * \ingroup gdcmElValSet
237 int gdcmElValSet::SetVoidAreaByNumber(void * area,
238 guint16 group, guint16 element) {
239 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
240 if ( ! tagHt.count(key))
242 tagHt[key]->SetVoidArea(area);
247 * \ingroup gdcmElValSet
252 * @return int acts as a boolean
254 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
255 guint16 group, guint16 element) {
256 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
257 if ( ! tagHt.count(key))
259 tagHt[key]->SetLength(length);
263 * \ingroup gdcmElValSet
269 int gdcmElValSet::SetElValueLengthByName(guint32 length, std::string TagName) {
270 if ( ! NameHt.count(TagName))
272 NameHt.find(TagName)->second->SetLength(length);
277 * \ingroup gdcmElValSet
278 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
279 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
282 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
288 std::string str_trash;
291 GroupHT groupHt; // to hold the length of each group
294 // typedef std::map<GroupKey, int> GroupHT;
298 // for each Tag in the DCM Header
300 for (TagElValueHT::iterator tag2 = tagHt.begin();
305 gr = elem->GetGroup();
306 el = elem->GetElement();
309 sprintf(trash, "%04x", gr);
310 key = trash; // generate 'group tag'
312 // if the caller decided not to take SEQUENCEs into account
313 // e.g : he wants to write an ACR-NEMA File
315 if (SkipSequence && vr == "SQ") continue;
317 // Still unsolved problem :
318 // we cannot find the 'Sequence Delimitation Item'
319 // since it's at the end of the Hash Table
322 // pas SEQUENCE en ACR-NEMA
324 // --> la descente a l'interieur' des SQ
325 // devrait etre faite avec une liste chainee, pas avec une HTable...
327 if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
328 if (el == 0x0000) { // the first elem is 0x0000
329 groupHt[key] = 0; // initialize group length
331 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
333 } else { // any elem but the first
334 if (type == ExplicitVR) {
335 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
336 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
339 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
343 unsigned short int gr_bid;
345 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
348 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
349 // warning: unsigned int format, different type arg
350 sscanf(g->first.c_str(),"%x",&gr_bid);
351 tk = g->first + "|0000"; // generate the element full tag
353 if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
354 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
355 elemZ = new gdcmElValue(tagZ);
357 Add(elemZ); // create it
359 elemZ=GetElementByNumber(gr_bid, 0x0000);
361 sprintf(trash ,"%d",g->second);
363 elemZ->SetValue(str_trash);
368 * \ingroup gdcmElValSet
374 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
382 std::vector<std::string> tokens;
386 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
387 // restent à tester les echecs en écriture (apres chaque fwrite)
389 for (TagElValueHT::iterator tag2=tagHt.begin();
393 gr = tag2->second->GetGroup();
394 el = tag2->second->GetElement();
395 lgr = tag2->second->GetLength();
396 val = tag2->second->GetValue().c_str();
397 vr = tag2->second->GetVR();
399 // std::cout << "Tag "<< std::hex << gr << " " << el << std::endl;
402 if (gr < 0x0008) continue; // ignore pure DICOM V3 groups
403 if (gr %2) continue; // ignore shadow groups
404 if (vr == "SQ" ) continue; // ignore Sequences
405 if (gr == 0xfffe ) continue; // ignore delimiters
408 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
409 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
411 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
413 guint16 z=0, shortLgr;
414 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
416 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
417 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
418 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
422 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
424 } else { // IMPLICIT VR
425 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
428 if (vr == "US" || vr == "SS") {
429 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
430 Tokenize (tag2->second->GetValue(), tokens, "\\");
431 for (unsigned int i=0; i<tokens.size();i++) {
432 val_uint16 = atoi(tokens[i].c_str());
434 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
439 if (vr == "UL" || vr == "SL") {
440 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
441 Tokenize (tag2->second->GetValue(), tokens, "\\");
442 for (unsigned int i=0; i<tokens.size();i++) {
443 val_uint32 = atoi(tokens[i].c_str());
445 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
450 // Pixels are never loaded in the element !
451 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
453 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
458 * \ingroup gdcmElValSet
464 int gdcmElValSet::Write(FILE * _fp, FileType type) {
466 if (type == ImplicitVR) {
467 std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
468 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
470 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
471 // values with a VR of UI shall be padded with a single trailing null
472 // Dans le cas suivant on doit pader manuellement avec un 0
474 SetElValueLengthByNumber(18, 0x0002, 0x0010);
477 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
478 // (FileType est un champ de gdcmHeader ...)
479 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
482 if (type == ExplicitVR) {
483 std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
484 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
486 SetElValueLengthByNumber(20, 0x0002, 0x0010);
489 if ( (type == ImplicitVR) || (type == ExplicitVR) )
490 UpdateGroupLength(false,type);
492 UpdateGroupLength(true,ACR);
494 WriteElements(type, _fp);