]> Creatis software - gdcm.git/blob - src/gdcmObject.cxx
66c40a80c6ef927779e32e6f504848945b28de26
[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  * @param   val string value to set
94  * @param   group Group of the searched tag.
95  * @param   element Element of the searched tag.
96  * @return  true if element was found, else false
97  */
98  bool gdcmObject::SetEntryByNumber(std::string val,guint16 group, 
99                                                    guint16 element) {
100                                                    
101    //for(ListTag::iterator i=beginObj;i!=endObj;++i) // JPR
102    for(ListTag::iterator i=beginObj;;++i) {  
103       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element) {
104          (*i)->SetValue(val);
105          (*i)->SetLength(val.length()+1);
106          return true;
107       }
108       if (i == endObj) break;      
109    }
110    return false;                                                    
111 }
112 /**
113  * \ingroup gdcmObject
114  * \brief   Builds a hash table (multimap) containing 
115  *          pointers to all Header Entries (i.e Dicom Element)
116  *          related to this 'object'
117  * @return
118  */ 
119 TagHeaderEntryHT gdcmObject::GetEntry(void) {
120    TagHeaderEntryHT HT;
121    //for(ListTag::iterator i=beginObj;i!=endObj;++i) // JPR
122    for(ListTag::iterator i=beginObj;;++i) {
123       HT.insert( PairHT( (*i)->GetKey(),(*i)) );
124       if (i == endObj) break;      
125    }
126    return(HT);
127 }
128
129 /**
130  * \ingroup gdcmObject
131  * \brief   Builds a Chained List containing 
132  *          pointers to all Header Entries (i.e Dicom Element)
133  *          related to this 'object'
134  * @return
135  */ 
136 ListTag gdcmObject::GetListEntry(void) {
137    ListTag list;
138    //for(ListTag::iterator i=beginObj;i!=endObj;++i) // JPR
139    for(ListTag::iterator i=beginObj;;++i) {
140       list.push_back(*i);
141       if (i == endObj) break;      
142    }
143    return(list);
144 }
145
146
147 //-----------------------------------------------------------------------------
148 // Protected
149 /*
150  * \ingroup gdcmObject
151  * \brief   add the 'Object' related Dicom Elements to the listEntries
152  *          of a partially created DICOMDIR
153  */
154 void gdcmObject::FillObject(std::list<gdcmElement> elemList) {
155    std::list<gdcmElement>::iterator it;
156    guint16 tmpGr,tmpEl;
157    gdcmDictEntry *dictEntry;
158    gdcmHeaderEntry *entry;
159       
160    debInsertion = this->fin(); 
161    ++debInsertion;
162    finInsertion=debInsertion;
163    
164    for(it=elemList.begin();it!=elemList.end();++it)
165    {
166       tmpGr=it->group;
167       tmpEl=it->elem;
168       dictEntry=gdcmGlobal::GetDicts()->GetDefaultPubDict()->GetDictEntryByNumber(tmpGr,tmpEl);
169       entry=new gdcmHeaderEntry(dictEntry);
170       entry->SetOffset(0); // just to avoid missprinting //JPR
171       entry->SetValue(it->value);
172
173       if(dictEntry->GetGroup()==0xfffe) 
174          {
175             entry->SetLength(entry->GetValue().length());        
176          }
177       else if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") ) 
178          {
179             entry->SetLength(4);
180          } 
181       else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") ) 
182          {
183             entry->SetLength(2); 
184          } 
185       else if(dictEntry->GetVR()=="SQ") 
186          {
187             entry->SetLength(0xffffffff);
188          }
189       else
190          {
191             entry->SetLength(entry->GetValue().length());        
192          }                                
193       ptagHT->insert( PairHT(entry->GetKey(),entry) ); // add in the (multimap) H Table
194       plistEntries->insert(debInsertion ,entry);       // en tete de liste des Patients
195       ++finInsertion;                                      
196    }
197      
198    i=fin();
199    i++;
200    j=debInsertion;
201    j--;
202 }   
203 //-----------------------------------------------------------------------------
204 // Private
205
206 //-----------------------------------------------------------------------------