]> Creatis software - gdcm.git/blob - src/gdcmDocEntrySet.cxx
* src/gdcmDocEntrySet.[h|cxx], gdcmDocument.[h|cxx] : amelioration of
[gdcm.git] / src / gdcmDocEntrySet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntrySet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/06 15:36:48 $
7   Version:   $Revision: 1.30 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmDocEntrySet.h"
19
20 #include "gdcmDebug.h"
21 #include "gdcmCommon.h"
22 #include "gdcmDictSet.h"
23 #include "gdcmGlobal.h"
24 #include "gdcmException.h"
25 #include "gdcmDocEntry.h"
26 #include "gdcmSeqEntry.h"
27 #include "gdcmValEntry.h"
28 #include "gdcmBinEntry.h"
29
30 #include <assert.h>
31
32 namespace gdcm 
33 {
34
35 //-----------------------------------------------------------------------------
36 // Public
37 /**
38  * \brief  Gets a Dicom Element inside a SQ Item Entry, by name
39  * @param  name of the element to be found.
40  * @return
41  */
42 DocEntry* DocEntrySet::GetDocEntryByName( TagName const & name )
43 {
44    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
45    DictEntry *dictEntry = pubDict->GetDictEntryByName(name);
46    if( !dictEntry )
47    {
48       return 0;
49    }
50
51    return GetDocEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement());      
52 }
53
54
55 /**
56  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by name
57  * @param   name : name of the searched element.
58  * @return
59  */ 
60
61 std::string DocEntrySet::GetEntryByName(TagName const & name)
62 {
63    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
64    DictEntry *dictEntry = pubDict->GetDictEntryByName(name); 
65
66    if( !dictEntry )
67    {
68       return GDCM_UNFOUND;
69    }
70
71    return GetEntryByNumber(dictEntry->GetGroup(), dictEntry->GetElement()); 
72 }
73
74 /**
75  * \brief   Request a new virtual dict entry to the dict set
76  * @param   group     group  number of the underlying DictEntry
77  * @param   element  element number of the underlying DictEntry
78  * @param   vr     VR of the underlying DictEntry
79  * @param   fourth owner group
80  * @param   name   english name
81  */
82 DictEntry* DocEntrySet::NewVirtualDictEntry( uint16_t group,uint16_t elem,
83                                              TagName const & vr,
84                                              TagName const & fourth,
85                                              TagName const & name )
86 {
87    return Global::GetDicts()->NewVirtualDictEntry(group,elem,vr,fourth,name);
88 }
89
90 //-----------------------------------------------------------------------------
91 // Protected
92 /**
93  * \brief   Build a new Val Entry from all the low level arguments. 
94  *          Check for existence of dictionary entry, and build
95  *          a default one when absent.
96  * @param   group group   number of the underlying DictEntry
97  * @param   elem  element number of the underlying DictEntry
98  */
99 ValEntry *DocEntrySet::NewValEntryByNumber(uint16_t group,uint16_t elem,
100                                            TagName const & vr) 
101 {
102    DictEntry *dictEntry = GetDictEntryByNumber(group, elem, vr);
103    assert(dictEntry);
104
105    ValEntry *newEntry = new ValEntry(dictEntry);
106    if (!newEntry) 
107    {
108       dbg.Verbose(1, "Document::NewValEntryByNumber",
109                   "failed to allocate ValEntry");
110       return 0;
111    }
112    return newEntry;
113 }
114
115
116 /**
117  * \brief   Build a new Bin Entry from all the low level arguments. 
118  *          Check for existence of dictionary entry, and build
119  *          a default one when absent.
120  * @param   group group   number of the underlying DictEntry
121  * @param   elem  element number of the underlying DictEntry
122  */
123 BinEntry *DocEntrySet::NewBinEntryByNumber(uint16_t group,uint16_t elem,
124                                            TagName const & vr) 
125 {
126    DictEntry *dictEntry = GetDictEntryByNumber(group, elem, vr);
127    assert(dictEntry);
128
129    BinEntry *newEntry = new BinEntry(dictEntry);
130    if (!newEntry) 
131    {
132       dbg.Verbose(1, "Document::NewBinEntryByNumber",
133                   "failed to allocate BinEntry");
134       return 0;
135    }
136    return newEntry;
137 }
138
139 /**
140  * \brief   Build a new Seq Entry from all the low level arguments. 
141  *          Check for existence of dictionary entry, and build
142  *          a default one when absent.
143  * @param   Group group   number of the underlying DictEntry
144  * @param   Elem  element number of the underlying DictEntry
145  */
146 SeqEntry* DocEntrySet::NewSeqEntryByNumber(uint16_t group,uint16_t elem) 
147 {
148    DictEntry *dictEntry = GetDictEntryByNumber(group, elem, "SQ");
149    assert(dictEntry);
150
151    SeqEntry *newEntry = new SeqEntry( dictEntry );
152    if (!newEntry)
153    {
154       dbg.Verbose(1, "Document::NewSeqEntryByNumber",
155                   "failed to allocate SeqEntry");
156       return 0;
157    }
158    return newEntry;
159 }
160
161 /** \brief 
162  * Creates a new DocEntry (without any 'value' ...)
163  * @param   group     group  number of the underlying DictEntry
164  * @param   elem  elem number of the underlying DictEntry 
165  * @param   vr    V(alue) R(epresentation) of the Entry -if private Entry- 
166  */
167 DocEntry* DocEntrySet::NewDocEntryByNumber(uint16_t group, uint16_t elem,
168                                            TagName const & vr)
169 {
170    DictEntry *dictEntry = GetDictEntryByNumber(group, elem, vr);
171    assert(dictEntry);
172
173    // Create the DocEntry
174    DocEntry *newEntry = new DocEntry(dictEntry);
175    if (!newEntry) 
176    {
177       dbg.Verbose(1, "SQItem::NewDocEntryByNumber",
178                   "failed to allocate DocEntry");
179       return 0;
180    }
181    return newEntry;
182 }
183
184 /**
185  * \brief   Searches both the public and the shadow dictionary (when they
186  *          exist) for the presence of the DictEntry with given name.
187  *          The public dictionary has precedence on the shadow one.
188  * @param   name Name of the searched DictEntry
189  * @return  Corresponding DictEntry when it exists, NULL otherwise.
190  */
191 DictEntry *DocEntrySet::GetDictEntryByName(TagName const & name) 
192 {
193    DictEntry *found = 0;
194    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
195    if (!pubDict) 
196    {
197       dbg.Verbose(0, "Document::GetDictEntry",
198                      "we SHOULD have a default dictionary");
199    }
200    else
201    {
202       found = pubDict->GetDictEntryByName(name);  
203    }
204    return found;
205 }
206
207 /**
208  * \brief   Searches both the public and the shadow dictionary (when they
209  *          exist) for the presence of the DictEntry with given
210  *          group and element. The public dictionary has precedence on the
211  *          shadow one.
212  * @param   group   group number of the searched DictEntry
213  * @param   element element number of the searched DictEntry
214  * @return  Corresponding DictEntry when it exists, NULL otherwise.
215  */
216 DictEntry *DocEntrySet::GetDictEntryByNumber(uint16_t group,uint16_t elem) 
217 {
218    DictEntry *found = 0;
219    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
220    if (!pubDict) 
221    {
222       dbg.Verbose(0, "Document::GetDictEntry",
223                      "we SHOULD have a default dictionary");
224    }
225    else
226    {
227       found = pubDict->GetDictEntryByNumber(group, elem);  
228    }
229    return found;
230 }
231
232 DictEntry *DocEntrySet::GetDictEntryByNumber(uint16_t group, uint16_t elem,
233                                              TagName const & vr)
234 {
235    DictEntry *dictEntry = GetDictEntryByNumber(group,elem);
236    DictEntry *goodEntry = dictEntry;
237    std::string goodVR=vr;
238
239    if (elem==0x0000)
240       goodVR="UL";
241
242    if (goodEntry)
243       if (goodEntry->GetVR() != goodVR && goodVR!=GDCM_UNKNOWN)
244          goodEntry=NULL;
245
246    // Create a new virtual DictEntry if necessary
247    if (!goodEntry)
248    {
249       if (dictEntry)
250          goodEntry = NewVirtualDictEntry(group, elem, goodVR,"FIXME",dictEntry->GetName());
251       else
252          goodEntry = NewVirtualDictEntry(group, elem, goodVR);
253    }
254
255    return goodEntry;
256 }
257
258 //-----------------------------------------------------------------------------
259 // Private
260
261 } // end namespace gdcm
262
263 //-----------------------------------------------------------------------------