]> Creatis software - gdcm.git/blob - src/gdcmDocEntrySet.cxx
* src/gdcmDictEntry.h : now, the IsVRUnknown is correct
[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 13:35:38 $
7   Version:   $Revision: 1.29 $
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 namespace gdcm 
31 {
32
33 //-----------------------------------------------------------------------------
34 // Public
35
36 /**
37  * \brief   Build a new Val Entry from all the low level arguments. 
38  *          Check for existence of dictionary entry, and build
39  *          a default one when absent.
40  * @param   group group   number of the underlying DictEntry
41  * @param   elem  element number of the underlying DictEntry
42  */
43 ValEntry *DocEntrySet::NewValEntryByNumber(uint16_t group,
44                                            uint16_t elem) 
45 {
46    // Find out if the tag we encountered is in the dictionaries:
47    DictEntry *dictEntry = GetDictEntryByNumber(group, elem);
48    if (!dictEntry)
49    {
50       dictEntry = NewVirtualDictEntry(group, elem);
51    }
52
53    ValEntry *newEntry = new ValEntry(dictEntry);
54    if (!newEntry) 
55    {
56       dbg.Verbose(1, "Document::NewValEntryByNumber",
57                   "failed to allocate ValEntry");
58       return 0;
59    }
60    return newEntry;
61 }
62
63
64 /**
65  * \brief   Build a new Bin Entry from all the low level arguments. 
66  *          Check for existence of dictionary entry, and build
67  *          a default one when absent.
68  * @param   group group   number of the underlying DictEntry
69  * @param   elem  element number of the underlying DictEntry
70  */
71 BinEntry *DocEntrySet::NewBinEntryByNumber(uint16_t group,
72                                            uint16_t elem) 
73 {
74    // Find out if the tag we encountered is in the dictionaries:
75    DictEntry *dictEntry = GetDictEntryByNumber(group, elem);
76    if (!dictEntry)
77    {
78       dictEntry = NewVirtualDictEntry(group, elem);
79    }
80
81    BinEntry *newEntry = new BinEntry(dictEntry);
82    if (!newEntry) 
83    {
84       dbg.Verbose(1, "Document::NewBinEntryByNumber",
85                   "failed to allocate BinEntry");
86       return 0;
87    }
88    return newEntry;
89 }
90
91 /**
92  * \brief   Build a new Seq Entry from all the low level arguments. 
93  *          Check for existence of dictionary entry, and build
94  *          a default one when absent.
95  * @param   Group group   number of the underlying DictEntry
96  * @param   Elem  element number of the underlying DictEntry
97  */
98 SeqEntry* DocEntrySet::NewSeqEntryByNumber(uint16_t Group,
99                                                    uint16_t Elem) 
100 {
101    // Find out if the tag we encountered is in the dictionaries:
102    DictEntry* DictEntry = GetDictEntryByNumber( Group, Elem );
103    if ( ! DictEntry )
104    {
105       DictEntry = NewVirtualDictEntry(Group, Elem);
106    }
107
108    SeqEntry *NewEntry = new SeqEntry( DictEntry );
109    if ( !NewEntry ) 
110    {
111       dbg.Verbose(1, "Document::NewSeqEntryByNumber",
112                   "failed to allocate SeqEntry");
113       return 0;
114    }
115    return NewEntry;
116 }
117
118 //-----------------------------------------------------------------------------
119 // Protected
120
121 /**
122  * \brief  Gets a Dicom Element inside a SQ Item Entry, by name
123  * @param  name of the element to be found.
124  * @return
125  */
126 DocEntry* DocEntrySet::GetDocEntryByName( TagName const & name )
127 {
128    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
129    DictEntry *dictEntry = pubDict->GetDictEntryByName(name);
130    if( !dictEntry )
131    {
132       return 0;
133    }
134
135    return GetDocEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement());      
136 }
137
138
139 /**
140  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by name
141  * @param   name : name of the searched element.
142  * @return
143  */ 
144
145 std::string DocEntrySet::GetEntryByName(TagName const & name)
146 {
147    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
148    DictEntry *dictEntry = pubDict->GetDictEntryByName(name); 
149
150    if( !dictEntry )
151    {
152       return GDCM_UNFOUND;
153    }
154
155    return GetEntryByNumber(dictEntry->GetGroup(), dictEntry->GetElement()); 
156 }
157
158
159 /**
160  * \brief   Request a new virtual dict entry to the dict set
161  * @param   group     group  number of the underlying DictEntry
162  * @param   element  element number of the underlying DictEntry
163  * @param   vr     VR of the underlying DictEntry
164  * @param   fourth owner group
165  * @param   name   english name
166  */
167 DictEntry* DocEntrySet::NewVirtualDictEntry( uint16_t group,
168                                              uint16_t element,
169                                              TagName const & vr,
170                                              TagName const & fourth,
171                                              TagName const & name )
172 {
173    return Global::GetDicts()->NewVirtualDictEntry(group,element,vr,fourth,name);
174 }
175
176 /** \brief 
177  * Creates a new DocEntry (without any 'value' ...)
178  * @param   group     group  number of the underlying DictEntry
179  * @param   elem  elem number of the underlying DictEntry 
180  */
181 DocEntry* DocEntrySet::NewDocEntryByNumber(uint16_t group,
182                                            uint16_t elem)
183 {
184    // Find out if the tag we encountered is in the dictionaries:
185    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
186    DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem);
187    if (!dictEntry)
188    {
189       dictEntry = NewVirtualDictEntry(group, elem);
190    }
191
192    DocEntry *newEntry = new DocEntry(dictEntry);
193    if (!newEntry) 
194    {
195       dbg.Verbose(1, "SQItem::NewDocEntryByNumber",
196                   "failed to allocate DocEntry");
197       return 0;
198    }
199    return newEntry;
200 }
201
202
203 /** \brief 
204  * Creates a new DocEntry (without any 'value' ...)
205  * @param   group     group  number of the underlying DictEntry
206  * @param   elem  elem number of the underlying DictEntry 
207  * @param   vr    V(alue) R(epresentation) of the Entry -if private Entry- 
208  */
209 DocEntry* DocEntrySet::NewDocEntryByNumber(uint16_t group, uint16_t elem,
210                                            TagName const & vr)
211 {
212    // Find out if the tag we encountered is in the dictionaries:
213    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
214    DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem);
215    std::string goodVR = vr;
216    DictEntry *goodDict = dictEntry;
217
218    // Check if the would VR is good
219    if (elem==0x0000)
220       goodVR="UL";
221
222    // Check if the DictEntry VR corresponds with the would VR
223    if (goodDict)
224       if (goodDict->GetVR() != goodVR && goodVR!=GDCM_UNKNOWN)
225          goodDict=NULL;
226
227    // Create a new virtual DictEntry if necessary
228    if (!goodDict)
229    {
230       if (dictEntry)
231          goodDict = NewVirtualDictEntry(group, elem, goodVR,"FIXME",dictEntry->GetName());
232       else
233          goodDict = NewVirtualDictEntry(group, elem, goodVR);
234    }
235
236    // Create the DocEntry
237    DocEntry *newEntry = new DocEntry(goodDict);
238    if (!newEntry) 
239    {
240       dbg.Verbose(1, "SQItem::NewDocEntryByNumber",
241                   "failed to allocate DocEntry");
242       return 0;
243    }
244    return newEntry;
245 }
246
247 /* \brief
248  * Probabely move, as is, to DocEntrySet, as a non virtual method
249  * and remove Document::NewDocEntryByName
250  */
251 DocEntry *DocEntrySet::NewDocEntryByName(TagName const & name)
252 {
253   Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
254   DictEntry *newTag = pubDict->GetDictEntryByName(name);
255    if (!newTag)
256    {
257       newTag = NewVirtualDictEntry(0xffff, 0xffff, "LO", GDCM_UNKNOWN, name);
258    }
259
260    DocEntry* newEntry = new DocEntry(newTag);
261    if (!newEntry) 
262    {
263       dbg.Verbose(1, "SQItem::ObtainDocEntryByName",
264                   "failed to allocate DocEntry");
265       return 0;
266    }
267
268    return newEntry;
269 }
270
271
272 /**
273  * \brief   Searches both the public and the shadow dictionary (when they
274  *          exist) for the presence of the DictEntry with given name.
275  *          The public dictionary has precedence on the shadow one.
276  * @param   name Name of the searched DictEntry
277  * @return  Corresponding DictEntry when it exists, NULL otherwise.
278  */
279 DictEntry *DocEntrySet::GetDictEntryByName(TagName const & name) 
280 {
281    DictEntry *found = 0;
282    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
283    if (!pubDict) 
284    {
285       dbg.Verbose(0, "Document::GetDictEntry",
286                      "we SHOULD have a default dictionary");
287    }
288    else
289    {
290       found = pubDict->GetDictEntryByName(name);  
291    }
292    return found;
293 }
294
295 /**
296  * \brief   Searches both the public and the shadow dictionary (when they
297  *          exist) for the presence of the DictEntry with given
298  *          group and element. The public dictionary has precedence on the
299  *          shadow one.
300  * @param   group   group number of the searched DictEntry
301  * @param   element element number of the searched DictEntry
302  * @return  Corresponding DictEntry when it exists, NULL otherwise.
303  */
304 DictEntry *DocEntrySet::GetDictEntryByNumber(uint16_t group, 
305                                              uint16_t element) 
306 {
307    DictEntry *found = 0;
308    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
309    if (!pubDict) 
310    {
311       dbg.Verbose(0, "Document::GetDictEntry",
312                      "we SHOULD have a default dictionary");
313    }
314    else
315    {
316       found = pubDict->GetDictEntryByNumber(group, element);  
317    }
318    return found;
319 }
320
321
322 //-----------------------------------------------------------------------------
323 // Private
324
325 } // end namespace gdcm
326
327 //-----------------------------------------------------------------------------