]> Creatis software - gdcm.git/blob - src/gdcmObject.cxx
3f4b5101f4dc8c0e1bfda7f83661756bfe8713ad
[gdcm.git] / src / gdcmObject.cxx
1 // gdcmObject.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmObject.h"
4 #include "gdcmUtil.h"
5
6 //-----------------------------------------------------------------------------
7 // Constructor / Destructor
8 /**
9  * \ingroup gdcmObject
10  * \brief  Constructor 
11  * @param  begin  iterator on the first Header Entry (i.e Dicom Element)
12  *                related to this 'Object'
13  * @param  end  iterator on the last Header Entry 
14  *              (i.e Dicom Element) related to this 'Object'             
15  * @param ptagHT pointer to the HTable (gdcmObject needs it 
16  *               to build the gdcmHeaderEntries)
17  * @param plistEntries pointer to the chained List (gdcmObject needs it 
18  *               to build the gdcmHeaderEntries)
19  */
20 gdcmObject::gdcmObject(ListTag::iterator begin, ListTag::iterator end,
21               TagHeaderEntryHT *ptagHT, ListTag *plistEntries) {
22    beginObj = begin;
23    endObj   = end;
24    this->ptagHT = ptagHT;
25    this->plistEntries = plistEntries;
26    if(begin==end)
27       dbg.Verbose(0, "gdcmObject::gdcmObject empty list");
28 }
29
30 /**
31  * \ingroup gdcmObject
32  * \brief   Canonical destructor.
33  */
34 gdcmObject::~gdcmObject(void) {
35 }
36
37 //-----------------------------------------------------------------------------
38 // Print
39 /**
40  * \ingroup gdcmObject
41  * \brief   Prints the Object
42  * @return
43  */ 
44 void gdcmObject::Print(std::ostream &os) {
45    if(printLevel>=0) {
46       ListTag::iterator i;
47       //for(ListTag::iterator i=beginObj;i!=endObj;++i) // JPR
48       for(i=beginObj;;++i) {
49          (*i)->SetPrintLevel(printLevel);
50          (*i)->Print(os);
51          if (i == endObj) break;
52       }
53    }
54 }
55
56 //-----------------------------------------------------------------------------
57 // Public
58 /**
59  * \ingroup gdcmObject
60  * \brief   Get the value of an Header Entries (i.e Dicom Element) by number
61  * @return
62  */ 
63 std::string gdcmObject::GetEntryByNumber(guint16 group, guint16 element) {
64    //for(ListTag::iterator i=beginObj;i!=endObj;++i) // JPR
65    for(ListTag::iterator i=beginObj;;++i) {
66       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
67          return (*i)->GetValue();
68       if (i == endObj) break;  
69    }   
70    return GDCM_UNFOUND;
71 }
72
73 /**
74  * \ingroup gdcmObject
75  * \brief   Get the value of an Header Entries (i.e Dicom Element) by name
76  * @param   name : name of the searched element.
77  * @return
78  */ 
79 std::string gdcmObject::GetEntryByName(TagName name)  {
80    gdcmDict *PubDict=gdcmGlobal::GetDicts()->GetDefaultPubDict();
81    gdcmDictEntry *dictEntry = (*PubDict).GetDictEntryByName(name); 
82
83    if( dictEntry == NULL)
84       return GDCM_UNFOUND;
85    return GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()); 
86 }
87
88 /**
89  * \ingroup gdcmParser
90  * \brief   Sets Entry (Dicom Element) value of an element,
91  *          specified by it's tag (Group, Number) 
92  *          and the length, too ...
93  *          If the Element is not found, it's just created !
94  * \warning we suppose, right now, the element belongs to a Public Group
95  *          (NOT a shadow one)       
96  * @param   val string value to set
97  * @param   group Group of the searched tag.
98  * @param   element Element of the searched tag.
99  * @return  true if element was found or created successfully
100  */
101  bool gdcmObject::SetEntryByNumber(std::string val,guint16 group, 
102                                                    guint16 element) {
103
104    gdcmHeaderEntry *a;
105    for(ListTag::iterator i=beginObj;;++i) { 
106       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
107          continue;
108       if ( group   < (*i)->GetGroup() || 
109            (group == (*i)->GetGroup() && element < (*i)->GetElement()) ){
110          // instead of ReplaceOrCreateByNumber 
111          // that is a method of gdcmParser :-( 
112          gdcmHeaderEntry *Entry;
113          TagKey key = gdcmDictEntry::TranslateToKey(group, element);
114          if ( ! ptagHT->count(key)) {
115            // we assume the element is a belongs to a Public Group (not a shadow one)
116            // we assume a Public Dictionnary *is* loaded
117            gdcmDict *PubDict         = gdcmGlobal::GetDicts()->GetDefaultPubDict();
118            // we assume the invoqued (group,elem) exists
119            // inside the Public Dictionary
120            gdcmDictEntry *DictEntry  = PubDict->GetDictEntryByNumber(group, element); 
121            // we assume the constuctor didn't fail
122            Entry = new gdcmHeaderEntry(DictEntry);
123            // ----
124            // TODO
125            // ----
126            // better we don't assume too much !
127            // in the next release, gdcmObject will be used 
128            // to describe any Header Entry ...
129          } else {
130             Entry = ptagHT->find(key)->second;
131          }
132          Entry->SetValue(val); 
133          Entry->SetLength(val.length());
134          plistEntries->insert(i,Entry); 
135          return true;
136       }    
137       if (group == (*i)->GetGroup() && element == (*i)->GetElement() ) {
138          (*i)->SetValue(val);
139          (*i)->SetLength(val.length()); 
140          return true;    
141       }   
142    }                                                
143 }
144 /**
145  * \ingroup gdcmObject
146  * \brief   Builds a hash table (multimap) containing 
147  *          pointers to all Header Entries (i.e Dicom Element)
148  *          related to this 'object'
149  * @return
150  */ 
151 TagHeaderEntryHT gdcmObject::GetEntry(void) {
152    TagHeaderEntryHT HT;
153    //for(ListTag::iterator i=beginObj;i!=endObj;++i) // JPR
154    for(ListTag::iterator i=beginObj;;++i) {
155       HT.insert( PairHT( (*i)->GetKey(),(*i)) );
156       if (i == endObj) break;      
157    }
158    return(HT);
159 }
160
161 /**
162  * \ingroup gdcmObject
163  * \brief   Builds a Chained List containing 
164  *          pointers to all Header Entries (i.e Dicom Element)
165  *          related to this 'object'
166  * @return
167  */ 
168 ListTag gdcmObject::GetListEntry(void) {
169    ListTag list;
170    //for(ListTag::iterator i=beginObj;i!=endObj;++i) // JPR
171    for(ListTag::iterator i=beginObj;;++i) {
172       list.push_back(*i);
173       if (i == endObj) break;      
174    }
175    return(list);
176 }
177
178
179 //-----------------------------------------------------------------------------
180 // Protected
181 /*
182  * \ingroup gdcmObject
183  * \brief   add the 'Object' related Dicom Elements to the listEntries
184  *          of a partially created DICOMDIR
185  */
186 void gdcmObject::FillObject(std::list<gdcmElement> elemList) {
187    std::list<gdcmElement>::iterator it;
188    guint16 tmpGr,tmpEl;
189    gdcmDictEntry *dictEntry;
190    gdcmHeaderEntry *entry;
191       
192    debInsertion = this->fin(); 
193    ++debInsertion;
194    finInsertion=debInsertion;
195    
196    for(it=elemList.begin();it!=elemList.end();++it)
197    {
198       tmpGr=it->group;
199       tmpEl=it->elem;
200       dictEntry=gdcmGlobal::GetDicts()->GetDefaultPubDict()->GetDictEntryByNumber(tmpGr,tmpEl);
201       entry=new gdcmHeaderEntry(dictEntry);
202       entry->SetOffset(0); // just to avoid missprinting //JPR
203       entry->SetValue(it->value);
204
205       if(dictEntry->GetGroup()==0xfffe) 
206          {
207             entry->SetLength(entry->GetValue().length());        
208          }
209       else if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") ) 
210          {
211             entry->SetLength(4);
212          } 
213       else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") ) 
214          {
215             entry->SetLength(2); 
216          } 
217       else if(dictEntry->GetVR()=="SQ") 
218          {
219             entry->SetLength(0xffffffff);
220          }
221       else
222          {
223             entry->SetLength(entry->GetValue().length());        
224          }                                
225       ptagHT->insert( PairHT(entry->GetKey(),entry) ); // add in the (multimap) H Table
226       plistEntries->insert(debInsertion ,entry);       // en tete de liste des Patients
227       ++finInsertion;                                      
228    }
229      
230    i=fin();
231    i++;
232    j=debInsertion;
233    j--;
234 }   
235 //-----------------------------------------------------------------------------
236 // Private
237
238 //-----------------------------------------------------------------------------