1 /*=========================================================================
4 Module: $RCSfile: gdcmDocEntrySet.cxx,v $
6 Date: $Date: 2005/02/02 10:02:17 $
7 Version: $Revision: 1.51 $
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.
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.
17 =========================================================================*/
18 #include "gdcmDocEntrySet.h"
20 #include "gdcmDebug.h"
21 #include "gdcmCommon.h"
22 #include "gdcmDictSet.h"
23 #include "gdcmGlobal.h"
24 #include "gdcmDocEntry.h"
25 #include "gdcmSeqEntry.h"
26 #include "gdcmValEntry.h"
27 #include "gdcmBinEntry.h"
31 //-----------------------------------------------------------------------------
32 // Constructor / Destructor
34 //-----------------------------------------------------------------------------
37 * \brief Get the "std::string representable" value of the Dicom entry
38 * @param group Group number of the searched tag.
39 * @param elem Element number of the searched tag.
40 * @return Corresponding element value when it exists,
41 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
43 std::string DocEntrySet::GetEntryValue(uint16_t group, uint16_t elem)
45 ContentEntry *entry = dynamic_cast<ContentEntry *>(GetDocEntry(group,elem));
47 return entry->GetValue();
52 * \brief Gets (from Header) a 'non string' element value
53 * @param group group number of the Entry
54 * @param elem element number of the Entry
55 * @return Pointer to the 'non string' area
57 void *DocEntrySet::GetEntryBinArea(uint16_t group, uint16_t elem)
59 BinEntry *entry = GetBinEntry(group, elem);
61 return entry->GetBinArea();
66 * \brief Searches within Header Entries (Dicom Elements) parsed with
67 * the public and private dictionaries
68 * for the value length of a given tag..
69 * @param group Group number of the searched tag.
70 * @param elem Element number of the searched tag.
71 * @return Corresponding element length; -2 if not found
73 int DocEntrySet::GetEntryLength(uint16_t group, uint16_t elem)
75 DocEntry *entry = GetDocEntry(group, elem);
77 return entry->GetLength();
82 * \brief Searches within Header Entries (Dicom Elements) parsed with
83 * the public [and private dictionaries]
84 * for the element value representation of a given tag..
85 * Obtaining the VR (Value Representation) might be needed by caller
86 * to convert the string typed content to caller's native type
87 * (think of C++ vs Python). The VR is actually of a higher level
88 * of semantics than just the native C++ type.
89 * @param group Group number of the searched tag.
90 * @param elem Element number of the searched tag.
91 * @return Corresponding element value representation when it exists,
92 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
94 std::string DocEntrySet::GetEntryVR(uint16_t group, uint16_t elem)
96 DocEntry *entry = GetDocEntry(group, elem);
98 return entry->GetVR();
103 * \brief Same as \ref Document::GetDocEntry except it only
104 * returns a result when the corresponding entry is of type
106 * @param group Group number of the searched Dicom Element
107 * @param elem Element number of the searched Dicom Element
108 * @return When present, the corresponding ValEntry.
110 ValEntry *DocEntrySet::GetValEntry(uint16_t group, uint16_t elem)
112 DocEntry *currentEntry = GetDocEntry(group, elem);
116 return dynamic_cast<ValEntry*>(currentEntry);
120 * \brief Same as \ref Document::GetDocEntry except it only
121 * returns a result when the corresponding entry is of type
123 * @param group Group number of the searched Dicom Element
124 * @param elem Element number of the searched Dicom Element
125 * @return When present, the corresponding BinEntry.
127 BinEntry *DocEntrySet::GetBinEntry(uint16_t group, uint16_t elem)
129 DocEntry *currentEntry = GetDocEntry(group, elem);
132 gdcmVerboseMacro( "No corresponding BinEntry " << std::hex << group <<
137 return dynamic_cast<BinEntry*>(currentEntry);
141 * \brief Same as \ref Document::GetDocEntry except it only
142 * returns a result when the corresponding entry is of type
144 * @param group Group number of the searched Dicom Element
145 * @param elem Element number of the searched Dicom Element
146 * @return When present, the corresponding SeqEntry.
148 SeqEntry *DocEntrySet::GetSeqEntry(uint16_t group, uint16_t elem)
150 DocEntry *currentEntry = GetDocEntry(group, elem);
153 gdcmVerboseMacro( "No corresponding SeqEntry " << std::hex << group <<
158 return dynamic_cast<SeqEntry*>(currentEntry);
162 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
163 * through it's (group, element) and modifies it's content with
165 * @param content new value (string) to substitute with
166 * @param group group number of the Dicom Element to modify
167 * @param elem element number of the Dicom Element to modify
169 bool DocEntrySet::SetValEntry(std::string const &content,
170 uint16_t group, uint16_t elem)
172 ValEntry *entry = GetValEntry(group, elem);
175 gdcmVerboseMacro( "No corresponding ValEntry " << std::hex << group <<
176 "," << elem << " element (try promotion first).");
179 return SetValEntry(content,entry);
183 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
184 * through it's (group, element) and modifies it's content with
186 * @param content new value (void* -> uint8_t*) to substitute with
187 * @param lgth new value length
188 * @param group group number of the Dicom Element to modify
189 * @param elem element number of the Dicom Element to modify
191 bool DocEntrySet::SetBinEntry(uint8_t *content, int lgth,
192 uint16_t group, uint16_t elem)
194 BinEntry *entry = GetBinEntry(group, elem);
197 gdcmVerboseMacro( "No corresponding ValEntry " << std::hex << group <<
198 "," << elem << " element (try promotion first).");
202 return SetBinEntry(content,lgth,entry);
206 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
207 * and modifies it's content with the given value.
208 * @param content new value (string) to substitute with
209 * @param entry Entry to be modified
211 bool DocEntrySet::SetValEntry(std::string const &content, ValEntry *entry)
215 entry->SetValue(content);
222 * \brief Accesses an existing BinEntry (i.e. a Dicom Element)
223 * and modifies it's content with the given value.
224 * @param content new value (void* -> uint8_t*) to substitute with
225 * @param entry Entry to be modified
226 * @param lgth new value length
228 bool DocEntrySet::SetBinEntry(uint8_t *content, int lgth, BinEntry *entry)
232 entry->SetBinArea(content);
233 entry->SetLength(lgth);
234 entry->SetValue(GDCM_BINLOADED);
241 * \brief Modifies the value of a given Doc Entry (Dicom Element)
242 * when it exists. Create it with the given value when unexistant.
243 * @param value (string) Value to be set
244 * @param group Group number of the Entry
245 * @param elem Element number of the Entry
246 * @param vr V(alue) R(epresentation) of the Entry -if private Entry-
247 * \return pointer to the modified/created Header Entry (NULL when creation
250 ValEntry *DocEntrySet::InsertValEntry(std::string const &value,
251 uint16_t group, uint16_t elem,
254 ValEntry *valEntry = 0;
255 DocEntry *currentEntry = GetDocEntry( group, elem);
259 valEntry = dynamic_cast<ValEntry *>(currentEntry);
263 if( valEntry->GetVR()!=vr )
266 // if currentEntry doesn't correspond to the requested valEntry
269 if( !RemoveEntry(currentEntry) )
271 gdcmVerboseMacro( "Removal of previous DocEntry failed.");
278 // Create a new valEntry if necessary
281 valEntry = NewValEntry(group, elem, vr);
283 if ( !AddEntry(valEntry) )
285 gdcmVerboseMacro("AddEntry failed although this is a creation.");
292 // Set the binEntry value
293 SetValEntry(value, valEntry); // The std::string value
298 * \brief Modifies the value of a given Header Entry (Dicom Element)
299 * when it exists. Create it with the given value when unexistant.
300 * A copy of the binArea is made to be kept in the Document.
301 * @param binArea (binary) value to be set
302 * @param group Group number of the Entry
303 * @param elem Element number of the Entry
304 * @param vr V(alue) R(epresentation) of the Entry -if private Entry-
305 * \return pointer to the modified/created Header Entry (NULL when creation
308 BinEntry *DocEntrySet::InsertBinEntry(uint8_t *binArea, int lgth,
309 uint16_t group, uint16_t elem,
312 BinEntry *binEntry = 0;
313 DocEntry *currentEntry = GetDocEntry( group, elem);
315 // Verify the currentEntry
318 binEntry = dynamic_cast<BinEntry *>(currentEntry);
322 if( binEntry->GetVR()!=vr )
325 // if currentEntry doesn't correspond to the requested valEntry
328 if( !RemoveEntry(currentEntry) )
330 gdcmVerboseMacro( "Removal of previous DocEntry failed.");
337 // Create a new binEntry if necessary
340 binEntry = NewBinEntry(group, elem, vr);
342 if ( !AddEntry(binEntry) )
344 gdcmVerboseMacro( "AddEntry failed allthough this is a creation.");
351 // Set the binEntry value
353 if( lgth>0 && binArea )
355 tmpArea = new uint8_t[lgth];
356 memcpy(tmpArea,binArea,lgth);
362 if( !SetBinEntry(tmpArea,lgth,binEntry) )
374 * \brief Modifies the value of a given Header Entry (Dicom Element)
375 * when it exists. Creates it when unexistant.
376 * @param group Group number of the Entry
377 * @param elem Element number of the Entry
378 * \return pointer to the modified/created SeqEntry (NULL when creation
381 SeqEntry *DocEntrySet::InsertSeqEntry(uint16_t group, uint16_t elem)
383 SeqEntry *seqEntry = 0;
384 DocEntry *currentEntry = GetDocEntry( group, elem);
386 // Verify the currentEntry
389 seqEntry = dynamic_cast<SeqEntry *>(currentEntry);
393 if( seqEntry->GetVR()!="SQ" )
396 // if currentEntry doesn't correspond to the requested valEntry
399 if (!RemoveEntry(currentEntry))
401 gdcmVerboseMacro( "Removal of previous DocEntry failed.");
407 // Create a new seqEntry if necessary
410 seqEntry = NewSeqEntry(group, elem);
412 if( !AddEntry(seqEntry) )
414 gdcmVerboseMacro( "AddEntry failed allthough this is a creation.");
424 * \brief Checks if a given Dicom Element exists within the H table
425 * @param group Group number of the searched Dicom Element
426 * @param elem Element number of the searched Dicom Element
427 * @return true is found
429 bool DocEntrySet::CheckIfEntryExist(uint16_t group, uint16_t elem )
431 return GetDocEntry(group,elem)!=NULL;
435 * \brief Request a new virtual dict entry to the dict set
436 * @param group group number of the underlying DictEntry
437 * @param elem element number of the underlying DictEntry
438 * @param vr VR (Value Representation) of the underlying DictEntry
439 * @param vm VM (Value Multiplicity) of the underlying DictEntry
440 * @param name english name
442 DictEntry* DocEntrySet::NewVirtualDictEntry( uint16_t group,uint16_t elem,
445 TagName const & name )
447 return Global::GetDicts()->NewVirtualDictEntry(group,elem,vr,vm,name);
451 * \brief Build a new Val Entry from all the low level arguments.
452 * Check for existence of dictionary entry, and build
453 * a default one when absent.
454 * @param group group number of the new Entry
455 * @param elem element number of the new Entry
456 * @param vr VR of the new Entry
458 ValEntry *DocEntrySet::NewValEntry(uint16_t group,uint16_t elem,
461 DictEntry *dictEntry = GetDictEntry(group, elem, vr);
462 gdcmAssertMacro(dictEntry);
464 ValEntry *newEntry = new ValEntry(dictEntry);
467 gdcmVerboseMacro( "Failed to allocate ValEntry");
475 * \brief Build a new Bin Entry from all the low level arguments.
476 * Check for existence of dictionary entry, and build
477 * a default one when absent.
478 * @param group group number of the new Entry
479 * @param elem element number of the new Entry
480 * @param vr VR of the new Entry
482 BinEntry *DocEntrySet::NewBinEntry(uint16_t group, uint16_t elem,
485 DictEntry *dictEntry = GetDictEntry(group, elem, vr);
486 gdcmAssertMacro(dictEntry);
488 BinEntry *newEntry = new BinEntry(dictEntry);
491 gdcmVerboseMacro( "Failed to allocate BinEntry");
498 * \brief Build a new Seq Entry from all the low level arguments.
499 * Check for existence of dictionary entry, and build
500 * a default one when absent.
501 * @param group group number of the new Entry
502 * @param elem element number of the new Entry
504 SeqEntry* DocEntrySet::NewSeqEntry(uint16_t group, uint16_t elem)
506 DictEntry *dictEntry = GetDictEntry(group, elem, "SQ");
507 gdcmAssertMacro(dictEntry);
509 SeqEntry *newEntry = new SeqEntry( dictEntry );
512 gdcmVerboseMacro( "Failed to allocate SeqEntry");
518 //-----------------------------------------------------------------------------
521 * \brief Searches [both] the public [and the shadow dictionary (when they
522 * exist)] for the presence of the DictEntry with given
523 * group and element. The public dictionary has precedence on the
525 * @param group group number of the searched DictEntry
526 * @param elem element number of the searched DictEntry
527 * @return Corresponding DictEntry when it exists, NULL otherwise.
529 DictEntry *DocEntrySet::GetDictEntry(uint16_t group,uint16_t elem)
531 DictEntry *found = 0;
532 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
535 gdcmVerboseMacro( "We SHOULD have a default dictionary");
539 found = pubDict->GetEntry(group, elem);
545 * \brief Searches [both] the public [and the shadow dictionary (when they
546 * exist)] for the presence of the DictEntry with given
547 * group and element, and create a new virtual DictEntry if necessary
548 * @param group group number of the searched DictEntry
549 * @param elem element number of the searched DictEntry
550 * @param vr Value Representation to use, if necessary
551 * @return Corresponding DictEntry when it exists, NULL otherwise.
553 DictEntry *DocEntrySet::GetDictEntry(uint16_t group, uint16_t elem,
556 DictEntry *dictEntry = GetDictEntry(group,elem);
557 DictEntry *goodEntry = dictEntry;
558 std::string goodVR = vr;
560 if (elem == 0x0000) goodVR="UL";
564 if ( goodVR != goodEntry->GetVR()
565 && goodVR != GDCM_UNKNOWN )
571 // Create a new virtual DictEntry if necessary
576 goodEntry = NewVirtualDictEntry(group, elem, goodVR, "FIXME",
577 dictEntry->GetName() );
581 goodEntry = NewVirtualDictEntry(group, elem, goodVR);
587 //-----------------------------------------------------------------------------
590 //-----------------------------------------------------------------------------
593 //-----------------------------------------------------------------------------
594 } // end namespace gdcm