5 #include "gdcmElValSet.h"
8 TagElValueHT & gdcmElValSet::GetTagHt(void) {
12 void gdcmElValSet::Add(gdcmElValue * newElValue) {
13 tagHt [newElValue->GetKey()] = newElValue;
14 NameHt[newElValue->GetName()] = newElValue;
17 void gdcmElValSet::Print(ostream & os) {
18 for (TagElValueHT::iterator tag = tagHt.begin();
21 os << tag->first << ": ";
22 os << "[" << tag->second->GetValue() << "]";
23 os << "[" << tag->second->GetName() << "]";
24 os << "[" << tag->second->GetVR() << "]";
25 os << " lgr : " << tag->second->GetLength();
30 void gdcmElValSet::PrintByName(ostream & os) {
31 for (TagElValueNameHT::iterator tag = NameHt.begin();
34 os << tag->first << ": ";
35 os << "[" << tag->second->GetValue() << "]";
36 os << "[" << tag->second->GetKey() << "]";
37 os << "[" << tag->second->GetVR() << "]" << endl;
41 gdcmElValue* gdcmElValSet::GetElementByNumber(guint16 group, guint16 element) {
42 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
43 if ( ! tagHt.count(key))
44 return (gdcmElValue*)0;
45 return tagHt.find(key)->second;
48 gdcmElValue* gdcmElValSet::GetElementByName(string TagName) {
49 if ( ! NameHt.count(TagName))
50 return (gdcmElValue*)0;
51 return NameHt.find(TagName)->second;
54 string gdcmElValSet::GetElValueByNumber(guint16 group, guint16 element) {
55 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
56 if ( ! tagHt.count(key))
57 return "gdcm::Unfound";
58 return tagHt.find(key)->second->GetValue();
61 string gdcmElValSet::GetElValueByName(string TagName) {
62 if ( ! NameHt.count(TagName))
63 return "gdcm::Unfound";
64 return NameHt.find(TagName)->second->GetValue();
68 int gdcmElValSet::SetElValueByNumber(string content,
69 guint16 group, guint16 element) {
70 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
71 if ( ! tagHt.count(key))
73 tagHt[key]->SetValue(content);
74 tagHt[key]->SetLength(content.length());
78 int gdcmElValSet::SetElValueByName(string content, string TagName) {
79 if ( ! NameHt.count(TagName))
81 NameHt[TagName]->SetValue(content);
82 NameHt[TagName]->SetLength(content.length());
87 * \ingroup gdcmElValSet
88 * \brief Generate a free TagKey i.e. a TagKey that is not present
89 * in the TagHt dictionary.
90 * @param group The generated tag must belong to this group.
91 * @return The element of tag with given group which is fee.
93 guint32 gdcmElValSet::GenerateFreeTagKeyInGroup(guint16 group) {
94 for (guint32 elem = 0; elem < UINT32_MAX; elem++) {
95 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
96 if (tagHt.count(key) == 0)
102 int gdcmElValSet::SetElValueLengthByNumber(guint32 length,
103 guint16 group, guint16 element) {
104 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
105 if ( ! tagHt.count(key))
107 tagHt[key]->SetLength(length);
112 int gdcmElValSet::SetElValueLengthByName(guint32 length, string TagName) {
113 if ( ! NameHt.count(TagName))
115 NameHt.find(TagName)->second->SetLength(length);
121 void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
133 for (TagElValueHT::iterator tag2 = tagHt.begin();
138 gr = elem->GetGroup();
139 el = elem->GetElement();
142 sprintf(trash, "%04x", gr);
145 if (SkipSequence && vr == "SQ") continue;
146 // pas SEQUENCE en ACR-NEMA
147 // WARNING : pb CERTAIN
148 // si on est descendu 'a l'interieur' des SQ
150 // --> la descente a l'interieur' des SQ
151 // devra etre faite avec une liste chainee, pas avec une HTable...
153 if ( groupHt.count(key) == 0) {
157 groupHt[key] =2 + 2 + 4 + elem->GetLength();
160 if (type = ExplicitVR) {
161 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
165 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
170 for (GroupHT::iterator g = groupHt.begin();
173 printf("groupKey %s : %d\n",g->first.c_str(),g->second);
177 unsigned short int gr_bid;
179 for (GroupHT::iterator g = groupHt.begin();
183 sscanf(g->first.c_str(),"%x",&gr_bid);
184 tk = g->first + "|0000";
186 if ( tagHt.count(tk) == 0) {
187 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
188 elemZ = new gdcmElValue(tagZ);
192 elemZ=GetElementByNumber(gr_bid, 0x0000);
194 sprintf(trash ,"%d",g->second);
196 elemZ->SetValue(str_trash);
200 void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
208 vector<string> tokens;
212 // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
213 // restent à tester les echecs en écriture (apres chaque fwrite)
215 for (TagElValueHT::iterator tag2=tagHt.begin();
219 gr = tag2->second->GetGroup();
220 el = tag2->second->GetElement();
221 lgr = tag2->second->GetLength();
222 val = tag2->second->GetValue().c_str();
223 vr = tag2->second->GetVR();
226 if (gr < 0x0008) continue;
228 if (vr == "SQ" ) continue;
231 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
232 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
234 if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
235 // On est en EXPLICIT VR
236 guint16 z=0, shortLgr;
237 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
239 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
240 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
241 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
245 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
248 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
251 tokens.erase(tokens.begin(),tokens.end());
252 Tokenize (tag2->second->GetValue(), tokens, "\\");
254 if (vr == "US" || vr == "SS") {
255 for (unsigned int i=0; i<tokens.size();i++) {
256 val_uint16 = atoi(tokens[i].c_str());
258 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
262 if (vr == "UL" || vr == "SL") {
263 for (unsigned int i=0; i<tokens.size();i++) {
264 val_uint32 = atoi(tokens[i].c_str());
266 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
271 // Les pixels ne sont pas chargés dans l'element !
272 if ((gr == 0x7fe0) && (el == 0x0010) ) break;
274 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); //valeur Elem
278 int gdcmElValSet::Write(FILE * _fp, FileType type) {
280 if (type == ImplicitVR) {
281 string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
282 SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
284 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
285 // values with a VR of UI shall be padded with a single trailing null
286 // Dans le cas suivant on doit pader manuellement avec un 0
288 SetElValueLengthByNumber(18, 0x0002, 0x0010);
292 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
293 // (FileType est un champ de gdcmHeader ...)
294 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
298 if (type == ExplicitVR) {
299 string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
300 SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
302 SetElValueLengthByNumber(20, 0x0002, 0x0010);
305 if ( (type == ImplicitVR) || (type == ExplicitVR) )
306 UpdateGroupLength(false,type);
308 UpdateGroupLength(true,ACR);
310 WriteElements(type, _fp);