2 //-----------------------------------------------------------------------------
3 #include "gdcmParser.h"
11 #include <netinet/in.h>
14 #ifdef GDCM_NO_ANSI_STRING_STREAM
16 # define ostringstream ostrstream
22 #define UI1_2_840_10008_1_2 "1.2.840.10008.1.2"
23 #define UI1_2_840_10008_1_2_1 "1.2.840.10008.1.2.1"
24 #define UI1_2_840_10008_1_2_2 "1.2.840.10008.1.2.2"
25 #define UI1_2_840_10008_1_2_1_99 "1.2.840.10008.1.2.1.99"
29 // ---> Warning : This fourth field is NOT part
30 // of the 'official' Dicom Dictionnary
31 // and should NOT be used.
32 // (Not defined for all the groups
33 // may be removed in a future release)
36 // META Meta Information
48 // NMI Nuclear Medicine
50 // BFS Basic Film Session
52 // BIB Basic Image Box
67 // Other usefull abreviations :
68 // Radiographic view associated with Patient Position (0018,5100).
71 // AP = Anterior/Posterior
72 // PA = Posterior/Anterior
75 // RLD = Right Lateral Decubitus
76 // LLD = Left Lateral Decubitus
77 // RLO = Right Lateral Oblique
78 // LLO = Left Lateral Oblique
80 //-----------------------------------------------------------------------------
81 // Refer to gdcmParser::CheckSwap()
82 const unsigned int gdcmParser::HEADER_LENGTH_TO_READ = 256;
84 // Refer to gdcmParser::SetMaxSizeLoadEntry()
85 const unsigned int gdcmParser::MAX_SIZE_LOAD_ELEMENT_VALUE = 4096;
87 // Refer to gdcmParser::SetMaxSizePrintEntry()
88 // TODO : Right now, better see "define, in gdcmHederEntry.cxx
89 const unsigned int gdcmParser::MAX_SIZE_PRINT_ELEMENT_VALUE = 64;
91 //-----------------------------------------------------------------------------
92 // Constructor / Destructor
98 * @param exception_on_error whether we throw an exception or not
99 * @param enable_sequences = true to allow the header
100 * to be parsed *inside* the SeQuences,
101 * when they have an actual length
102 * @param ignore_shadow to allow skipping the shadow elements,
103 * to save memory space.
104 * \warning The TRUE value for this param has to be used
105 * with a FALSE value for the 'enable_sequence' param.
106 * ('public elements' may be embedded in 'shadow Sequences')
108 gdcmParser::gdcmParser(const char *inFilename,
109 bool exception_on_error,
110 bool enable_sequences,
111 bool ignore_shadow) {
112 enableSequences=enable_sequences;
113 ignoreShadow =ignore_shadow;
115 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
116 filename = inFilename;
119 if ( !OpenFile(exception_on_error))
126 wasUpdated = 0; // will be set to 1 if user adds an entry
127 printLevel = 1; // 'Medium' print level by default
131 * \ingroup gdcmParser
133 * @param exception_on_error
135 gdcmParser::gdcmParser(bool exception_on_error) {
138 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
141 wasUpdated = 0; // will be set to 1 if user adds an entry
142 printLevel = 1; // 'Medium' print level by default
146 * \ingroup gdcmParser
147 * \brief Canonical destructor.
149 gdcmParser::~gdcmParser (void) {
154 //-----------------------------------------------------------------------------
157 * \ingroup gdcmParser
158 * \brief Prints the Header Entries (Dicom Elements)
159 * both from the H Table and the chained list
162 void gdcmParser::PrintEntry(std::ostream & os) {
163 std::ostringstream s;
165 for (ListTag::iterator i = listEntries.begin();
166 i != listEntries.end();
169 (*i)->SetPrintLevel(printLevel);
176 * \ingroup gdcmParser
177 * \brief Prints The Dict Entries of THE public Dicom Dictionnry
180 void gdcmParser::PrintPubDict(std::ostream & os) {
181 RefPubDict->Print(os);
185 * \ingroup gdcmParser
186 * \brief Prints The Dict Entries of THE shadow Dicom Dictionnry
189 void gdcmParser::PrintShaDict(std::ostream & os) {
190 RefShaDict->Print(os);
193 //-----------------------------------------------------------------------------
196 * \ingroup gdcmParser
197 * \brief Get the public dictionary used
199 gdcmDict *gdcmParser::GetPubDict(void) {
204 * \ingroup gdcmParser
205 * \brief Get the shadow dictionary used
207 gdcmDict *gdcmParser::GetShaDict(void) {
212 * \ingroup gdcmParser
213 * \brief Set the shadow dictionary used
214 * \param dict dictionary to use in shadow
216 bool gdcmParser::SetShaDict(gdcmDict *dict){
222 * \ingroup gdcmParser
223 * \brief Set the shadow dictionary used
224 * \param dictName name of the dictionary to use in shadow
226 bool gdcmParser::SetShaDict(DictKey dictName){
227 RefShaDict=gdcmGlobal::GetDicts()->GetDict(dictName);
232 * \ingroup gdcmParser
233 * \brief This predicate, based on hopefully reasonable heuristics,
234 * decides whether or not the current gdcmParser was properly parsed
235 * and contains the mandatory information for being considered as
236 * a well formed and usable Dicom/Acr File.
237 * @return true when gdcmParser is the one of a reasonable Dicom/Acr file,
240 bool gdcmParser::IsReadable(void) {
241 if(filetype==Unknown) {
244 if(listEntries.size()<=0) {
252 * \ingroup gdcmParser
253 * \brief Determines if the Transfer Syntax was already encountered
254 * and if it corresponds to a ImplicitVRLittleEndian one.
255 * @return True when ImplicitVRLittleEndian found. False in all other cases.
257 bool gdcmParser::IsImplicitVRLittleEndianTransferSyntax(void) {
258 gdcmHeaderEntry *Element = GetHeaderEntryByNumber(0x0002, 0x0010);
261 LoadHeaderEntrySafe(Element);
263 std::string Transfer = Element->GetValue();
264 if ( Transfer == UI1_2_840_10008_1_2 )
270 * \ingroup gdcmParser
271 * \brief Determines if the Transfer Syntax was already encountered
272 * and if it corresponds to a ExplicitVRLittleEndian one.
273 * @return True when ExplicitVRLittleEndian found. False in all other cases.
275 bool gdcmParser::IsExplicitVRLittleEndianTransferSyntax(void) {
276 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
279 LoadHeaderEntrySafe(Element);
281 std::string Transfer = Element->GetValue();
282 if ( Transfer == UI1_2_840_10008_1_2_1 )
288 * \ingroup gdcmParser
289 * \brief Determines if the Transfer Syntax was already encountered
290 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
291 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
293 bool gdcmParser::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
294 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
297 LoadHeaderEntrySafe(Element);
299 std::string Transfer = Element->GetValue();
300 if ( Transfer == UI1_2_840_10008_1_2_1_99 )
306 * \ingroup gdcmParser
307 * \brief Determines if the Transfer Syntax was already encountered
308 * and if it corresponds to a Explicit VR Big Endian one.
309 * @return True when big endian found. False in all other cases.
311 bool gdcmParser::IsExplicitVRBigEndianTransferSyntax(void) {
312 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
315 LoadHeaderEntrySafe(Element);
317 std::string Transfer = Element->GetValue();
318 if ( Transfer == UI1_2_840_10008_1_2_2 ) //1.2.2 ??? A verifier !
324 * \ingroup gdcmParser
325 * \brief returns the File Type
326 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
327 * @return the FileType code
329 FileType gdcmParser::GetFileType(void) {
334 * \ingroup gdcmParser
335 * \brief opens the file
336 * @param exception_on_error
339 FILE *gdcmParser::OpenFile(bool exception_on_error)
342 fp=fopen(filename.c_str(),"rb");
343 if(exception_on_error)
346 throw gdcmFileError("gdcmParser::gdcmParser(const char *, bool)");
352 fread(&zero, (size_t)2, (size_t)1, fp);
354 //ACR -- or DICOM with no Preamble --
355 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
359 fseek(fp, 126L, SEEK_CUR);
361 fread(dicm, (size_t)4, (size_t)1, fp);
362 if( memcmp(dicm, "DICM", 4) == 0 )
366 dbg.Verbose(0, "gdcmParser::OpenFile not DICOM/ACR", filename.c_str());
369 dbg.Verbose(0, "gdcmParser::OpenFile cannot open file", filename.c_str());
375 * \ingroup gdcmParser
376 * \brief closes the file
377 * @return TRUE if the close was successfull
379 bool gdcmParser::CloseFile(void) {
380 int closed = fclose(fp);
388 * \ingroup gdcmParser
389 * \brief writes on disc all the Header Entries (Dicom Elements)
390 * of the Chained List
391 * @param fp file pointer on an already open file
392 * @param type type of the File to be written
393 * (ACR-NEMA, ExplicitVR, ImplicitVR)
394 * @return always "True" ?!
396 bool gdcmParser::Write(FILE *fp, FileType type) {
398 // TODO The stuff was rewritten using the chained list instead
400 // so we could remove the GroupHT from the gdcmParser
404 // TODO : move the following lines (and a lot of others, to be written)
405 // to a future function CheckAndCorrectHeader
408 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non
409 // (FileType est un champ de gdcmParser ...)
410 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
412 // a moins de se livrer a un tres complique ajout des champs manquants.
413 // faire un CheckAndCorrectHeader (?)
416 if (type == ImplicitVR)
418 std::string implicitVRTransfertSyntax = UI1_2_840_10008_1_2;
419 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
421 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
422 // values with a VR of UI shall be padded with a single trailing null
423 // Dans le cas suivant on doit pader manuellement avec un 0
425 SetEntryLengthByNumber(18, 0x0002, 0x0010);
428 if (type == ExplicitVR)
430 std::string explicitVRTransfertSyntax = UI1_2_840_10008_1_2_1;
431 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
433 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
434 // values with a VR of UI shall be padded with a single trailing null
435 // Dans le cas suivant on doit pader manuellement avec un 0
437 SetEntryLengthByNumber(20, 0x0002, 0x0010);
440 /* TODO : rewrite later
442 if ( (type == ImplicitVR) || (type == ExplicitVR) )
443 UpdateGroupLength(false,type);
445 UpdateGroupLength(true,ACR);
448 WriteEntries(fp,type);
453 * \ingroup gdcmParser
454 * \brief Modifies the value of a given Header Entry (Dicom Element)
455 * if it exists; Creates it with the given value if it doesn't
456 * @param Value passed as a std::string
459 * \return false only if new element creation fails
461 bool gdcmParser::ReplaceOrCreateByNumber(std::string Value,
464 if (CheckIfEntryExistByNumber(Group, Elem) == 0) {
465 gdcmHeaderEntry *a =NewHeaderEntryByNumber(Group, Elem);
470 SetEntryByNumber(Value, Group, Elem);
475 * \ingroup gdcmParser
476 * \brief Modifies the value of a given Header Entry (Dicom Element)
477 * if it exists; Creates it with the given value if it doesn't
478 * @param Value passed as a char*
479 * @param Group group of the Entry
480 * @param Elem element of the Entry
484 bool gdcmParser::ReplaceOrCreateByNumber(char* Value, guint16 Group, guint16 Elem ) {
485 gdcmHeaderEntry* nvHeaderEntry=NewHeaderEntryByNumber(Group, Elem);
490 AddHeaderEntry(nvHeaderEntry);
492 std::string v = Value;
493 SetEntryByNumber(v, Group, Elem);
498 * \ingroup gdcmParser
499 * \brief Set a new value if the invoked element exists
500 * Seems to be useless !!!
501 * @param Value new element value
502 * @param Group group of the Entry
503 * @param Elem element of the Entry
506 bool gdcmParser::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem )
508 std::string v = Value;
509 SetEntryByNumber(v, Group, Elem);
513 //-----------------------------------------------------------------------------
517 * \ingroup gdcmParser
518 * \brief Checks if a given Dicom Element exists
520 * @param group Group number of the searched Dicom Element
521 * @param element Element number of the searched Dicom Element
522 * @return number of occurences
524 int gdcmParser::CheckIfEntryExistByNumber(guint16 group, guint16 element ) {
525 std::string key = gdcmDictEntry::TranslateToKey(group, element );
526 return (tagHT.count(key));
530 * \ingroup gdcmParser
531 * \brief Searches within Header Entries (Dicom Elements) parsed with
532 * the public and private dictionaries
533 * for the element value of a given tag.
534 * \warning Don't use any longer : use GetPubEntryByName
535 * @param tagName name of the searched element.
536 * @return Corresponding element value when it exists,
537 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
539 std::string gdcmParser::GetEntryByName(std::string tagName) {
540 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
541 if( dictEntry == NULL)
544 return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
548 * \ingroup gdcmParser
549 * \brief Searches within Header Entries (Dicom Elements) parsed with
550 * the public and private dictionaries
551 * for the element value representation of a given tag.
553 * Obtaining the VR (Value Representation) might be needed by caller
554 * to convert the string typed content to caller's native type
555 * (think of C++ vs Python). The VR is actually of a higher level
556 * of semantics than just the native C++ type.
557 * @param tagName name of the searched element.
558 * @return Corresponding element value representation when it exists,
559 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
561 std::string gdcmParser::GetEntryVRByName(std::string tagName) {
562 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
563 if( dictEntry == NULL)
566 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(dictEntry->GetGroup(),
567 dictEntry->GetElement());
568 return elem->GetVR();
573 * \ingroup gdcmParser
574 * \brief Searches within Header Entries (Dicom Elements) parsed with
575 * the public and private dictionaries
576 * for the element value representation of a given tag.
577 * @param group Group of the searched tag.
578 * @param element Element of the searched tag.
579 * @return Corresponding element value representation when it exists,
580 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
582 std::string gdcmParser::GetEntryByNumber(guint16 group, guint16 element){
583 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
584 if ( ! tagHT.count(key))
586 return tagHT.find(key)->second->GetValue();
590 * \ingroup gdcmParser
591 * \brief Searches within Header Entries (Dicom Elements) parsed with
592 * the public and private dictionaries
593 * for the element value representation of a given tag..
595 * Obtaining the VR (Value Representation) might be needed by caller
596 * to convert the string typed content to caller's native type
597 * (think of C++ vs Python). The VR is actually of a higher level
598 * of semantics than just the native C++ type.
599 * @param group Group of the searched tag.
600 * @param element Element of the searched tag.
601 * @return Corresponding element value representation when it exists,
602 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
604 std::string gdcmParser::GetEntryVRByNumber(guint16 group, guint16 element) {
605 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
608 return elem->GetVR();
612 * \ingroup gdcmParser
613 * \brief Searches within Header Entries (Dicom Elements) parsed with
614 * the public and private dictionaries
615 * for the value length of a given tag..
616 * @param group Group of the searched tag.
617 * @param element Element of the searched tag.
618 * @return Corresponding element length; -2 if not found
620 int gdcmParser::GetEntryLengthByNumber(guint16 group, guint16 element) {
621 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
624 return elem->GetLength();
627 * \ingroup gdcmParser
628 * \brief Sets the value (string) of the Header Entry (Dicom Element)
629 * @param content string value of the Dicom Element
630 * @param tagName name of the searched Dicom Element.
631 * @return true when found
633 bool gdcmParser::SetEntryByName(std::string content,std::string tagName) {
634 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
635 if( dictEntry == NULL)
638 return(SetEntryByNumber(content,dictEntry->GetGroup(),
639 dictEntry->GetElement()));
643 * \ingroup gdcmParser
644 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
645 * through it's (group, element) and modifies it's content with
647 * \warning Don't use any longer : use SetPubEntryByNumber
648 * @param content new value to substitute with
649 * @param group group of the Dicom Element to modify
650 * @param element element of the Dicom Element to modify
652 bool gdcmParser::SetEntryByNumber(std::string content,
656 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
657 if ( ! tagHT.count(key))
659 int l = content.length();
660 if(l%2) // Non even length are padded with a space (020H).
663 content = content + '\0';
668 TagHeaderEntryHT::iterator p2;
669 // DO NOT remove the following lines : they explain the stuff
670 //p= tagHT.equal_range(key); // get a pair of iterators first-last synonym
671 //p2=p.first; // iterator on the first synonym
672 //a=p2->second; // H Table target column (2-nd col)
675 a = ((tagHT.equal_range(key)).first)->second;
677 a-> SetValue(content);
679 std::string vr = a->GetVR();
682 if( (vr == "US") || (vr == "SS") )
684 else if( (vr == "UL") || (vr == "SL") )
694 * \ingroup gdcmParser
695 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
696 * in the PubHeaderEntrySet of this instance
697 * through it's (group, element) and modifies it's length with
699 * \warning Use with extreme caution.
700 * @param l new length to substitute with
701 * @param group group of the Entry to modify
702 * @param element element of the Entry to modify
703 * @return true on success, false otherwise.
705 bool gdcmParser::SetEntryLengthByNumber(guint32 l,
709 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
710 if ( ! tagHT.count(key))
712 if (l%2) l++; // length must be even
713 ( ((tagHT.equal_range(key)).first)->second )->SetLength(l);
719 * \ingroup gdcmParser
720 * \brief Gets (from Header) the offset of a 'non string' element value
721 * (LoadElementValues has already be executed)
722 * @param Group group of the Entry
723 * @param Elem element of the Entry
724 * @return File Offset of the Element Value
726 size_t gdcmParser::GetEntryOffsetByNumber(guint16 Group, guint16 Elem)
728 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
731 dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
732 "failed to Locate gdcmHeaderEntry");
735 return Entry->GetOffset();
739 * \ingroup gdcmParser
740 * \brief Gets (from Header) a 'non string' element value
741 * (LoadElementValues has already be executed)
742 * @param Group group of the Entry
743 * @param Elem element of the Entry
744 * @return Pointer to the 'non string' area
746 void * gdcmParser::GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem)
748 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
751 dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
752 "failed to Locate gdcmHeaderEntry");
755 return Entry->GetVoidArea();
759 * \ingroup gdcmParser
760 * \brief Loads (from disk) the element content
761 * when a string is not suitable
762 * @param Group group of the Entry
763 * @param Elem element of the Entry
765 void *gdcmParser::LoadEntryVoidArea(guint16 Group, guint16 Elem)
767 gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
770 size_t o =(size_t)Element->GetOffset();
771 fseek(fp, o, SEEK_SET);
772 int l=Element->GetLength();
773 void * a = malloc(l);
777 SetEntryVoidAreaByNumber(a, Group, Elem);
778 // TODO check the result
779 size_t l2 = fread(a, 1, l ,fp);
790 * \ingroup gdcmParser
791 * \brief Sets a 'non string' value to a given Dicom Element
793 * @param group Group number of the searched Dicom Element
794 * @param element Element number of the searched Dicom Element
797 bool gdcmParser::SetEntryVoidAreaByNumber(void * area,
801 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
802 if ( ! tagHT.count(key))
804 ( ((tagHT.equal_range(key)).first)->second )->SetVoidArea(area);
809 * \ingroup gdcmParser
810 * \brief Update the entries with the shadow dictionary.
811 * Only non even entries are analyzed
813 void gdcmParser::UpdateShaEntries(void) {
814 gdcmDictEntry *entry;
817 for(ListTag::iterator it=listEntries.begin();
818 it!=listEntries.end();
821 // Odd group => from public dictionary
822 if((*it)->GetGroup()%2==0)
825 // Peer group => search the corresponding dict entry
827 entry=RefShaDict->GetDictEntryByNumber((*it)->GetGroup(),(*it)->GetElement());
831 if((*it)->IsImplicitVR())
836 (*it)->SetValue(GetHeaderEntryUnvalue(*it));
838 // Set the new entry and the new value
839 (*it)->SetDictEntry(entry);
840 CheckHeaderEntryVR(*it,vr);
842 (*it)->SetValue(GetHeaderEntryValue(*it));
846 // Remove precedent value transformation
847 (*it)->SetDictEntry(NewVirtualDictEntry((*it)->GetGroup(),(*it)->GetElement(),vr));
853 * \ingroup gdcmParser
854 * \brief Searches within the Header Entries for a Dicom Element of
856 * @param tagName name of the searched Dicom Element.
857 * @return Corresponding Dicom Element when it exists, and NULL
860 gdcmHeaderEntry *gdcmParser::GetHeaderEntryByName(std::string tagName) {
861 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
862 if( dictEntry == NULL)
865 return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
869 * \ingroup gdcmParser
870 * \brief retrieves a Dicom Element (the first one) using (group, element)
871 * \warning (group, element) IS NOT an identifier inside the Dicom Header
872 * if you think it's NOT UNIQUE, check the count number
873 * and use iterators to retrieve ALL the Dicoms Elements within
874 * a given couple (group, element)
875 * @param group Group number of the searched Dicom Element
876 * @param element Element number of the searched Dicom Element
879 gdcmHeaderEntry* gdcmParser::GetHeaderEntryByNumber(guint16 group, guint16 element)
881 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
882 if ( ! tagHT.count(key))
884 return tagHT.find(key)->second;
888 * \ingroup gdcmParser
889 * \brief retrieves the Dicom Elements (all of them) using (group, element)
890 * @param group Group number of the searched Dicom Element.
891 * @param element Element number of the searched Dicom Element.
892 * @return a range (i.e.pair<,>) containing all elements whose key is group|element)
895 IterHT gdcmParser::GetHeaderEntrySameNumber(guint16 group, guint16 element){
896 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
897 return (tagHT.equal_range(key));
901 * \ingroup gdcmParser
902 * \brief Loads the element while preserving the current
903 * underlying file position indicator as opposed to
904 * to LoadHeaderEntry that modifies it.
905 * @param entry Header Entry whose value shall be loaded.
908 void gdcmParser::LoadHeaderEntrySafe(gdcmHeaderEntry * entry) {
909 long PositionOnEntry = ftell(fp);
910 LoadHeaderEntry(entry);
911 fseek(fp, PositionOnEntry, SEEK_SET);
915 * \ingroup gdcmParser
916 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
917 * \warning : to be re-written using the chained list instead of the H table.
918 * \warning : DO NOT use (doesn't work any longer because of the multimap)
919 * \todo : to be re-written using the chained list instead of the H table
920 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
921 * @param type Type of the File (ExplicitVR,ImplicitVR, ACR, ...)
923 void gdcmParser::UpdateGroupLength(bool SkipSequence, FileType type) {
927 gdcmHeaderEntry *elem;
929 std::string str_trash;
932 GroupHT groupHt; // to hold the length of each group
935 // typedef std::map<GroupKey, int> GroupHT;
937 gdcmHeaderEntry *elemZ;
939 // for each Tag in the DCM Header
941 for (TagHeaderEntryHT::iterator tag2 = tagHT.begin();
946 gr = elem->GetGroup();
947 el = elem->GetElement();
950 sprintf(trash, "%04x", gr);
951 key = trash; // generate 'group tag'
953 // if the caller decided not to take SEQUENCEs into account
954 // e.g : he wants to write an ACR-NEMA File
956 if (SkipSequence && vr == "SQ")
959 // Still unsolved problem :
960 // we cannot find the 'Sequence Delimitation Item'
961 // since it's at the end of the Hash Table
964 // pas SEQUENCE en ACR-NEMA
966 // --> la descente a l'interieur' des SQ
967 // devrait etre faite avec une liste chainee, pas avec une HTable...
969 if ( groupHt.count(key) == 0) // we just read the first elem of a given group
971 if (el == 0x0000) // the first elem is 0x0000
973 groupHt[key] = 0; // initialize group length
977 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
980 else // any elem but the first
982 if (type == ExplicitVR)
984 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
986 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
989 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
993 unsigned short int gr_bid;
995 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
999 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
1000 // warning: unsigned int format, different type arg
1001 sscanf(g->first.c_str(),"%x",&gr_bid);
1002 tk = g->first + "|0000"; // generate the element full tag
1004 if ( tagHT.count(tk) == 0) // if element 0x0000 not found
1006 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
1007 elemZ = new gdcmHeaderEntry(tagZ);
1008 elemZ->SetLength(4);
1009 AddHeaderEntry(elemZ); // create it
1013 elemZ=GetHeaderEntryByNumber(gr_bid, 0x0000);
1015 sprintf(trash ,"%d",g->second);
1017 elemZ->SetValue(str_trash);
1022 * \ingroup gdcmParser
1023 * \brief writes on disc according to the requested format
1024 * (ACR-NEMA, ExplicitVR, ImplicitVR) ONE
1026 * @param tag pointer on the gdcmHeaderEntry to be written
1027 * @param type type of the File to be written
1028 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1029 * @param _fp already open file pointer
1031 void gdcmParser::WriteEntry(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
1042 std::vector<std::string> tokens;
1046 // TODO (?) tester les echecs en ecriture (apres chaque fwrite)
1048 itsTimeToWritePixels = false;
1050 // === Deal with the length
1051 // --------------------
1052 if((tag->GetLength())%2==1)
1054 tag->SetValue(tag->GetValue()+"\0");
1055 tag->SetLength(tag->GetReadLength()+1);
1058 gr = tag->GetGroup();
1059 el = tag->GetElement();
1060 lgr = tag->GetReadLength();
1061 val = tag->GetValue().c_str();
1063 voidArea = tag->GetVoidArea();
1067 if (gr < 0x0008) return; // ignore pure DICOM V3 groups
1068 if (gr %2) return; // ignore shadow groups
1069 if (vr == "SQ" ) return; // ignore Sequences
1070 // TODO : find a trick to *skip* the SeQuences !
1071 // Not only ignore the SQ element
1072 if (gr == 0xfffe ) return; // ignore delimiters
1075 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
1076 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
1078 if ( (type == ExplicitVR) || (type == DICOMDIR) ) {
1080 guint16 z=0, shortLgr;
1082 if (gr == 0xfffe) { // NO Value Representation for 'delimiters'
1083 // no length : write ffffffff
1084 fwrite (&ff,(size_t)4 ,(size_t)1 ,_fp);
1085 return; // NO value for 'delimiters'
1089 if (vr == "unkn") { // Unknown was 'written'
1090 // deal with Little Endian
1091 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
1092 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
1094 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
1095 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ){
1096 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
1097 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
1099 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
1105 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
1108 // === Deal with the value
1109 // -------------------
1110 if (vr == "SQ") return; // no "value" to write for the SEQuences
1111 if (gr == 0xfffe)return; // no "value" to write for the delimiters
1113 if (voidArea != NULL)
1114 { // there is a 'non string' LUT, overlay, etc
1115 fwrite ( voidArea,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
1119 if (vr == "US" || vr == "SS")
1121 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1122 Tokenize (tag->GetValue(), tokens, "\\");
1123 for (unsigned int i=0; i<tokens.size();i++)
1125 val_uint16 = atoi(tokens[i].c_str());
1127 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
1132 if (vr == "UL" || vr == "SL")
1134 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1135 Tokenize (tag->GetValue(), tokens, "\\");
1136 for (unsigned int i=0; i<tokens.size();i++)
1138 val_uint32 = atoi(tokens[i].c_str());
1140 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
1146 // Pixels are never loaded in the element !
1147 // we stop writting when Pixel are processed
1148 // FIX : we loose trailing elements (RAB, right now)
1150 if ((gr == GrPixel) && (el == NumPixel) ) {
1152 if (compte == countGrPixel) {// we passed *all* the GrPixel,NumPixel
1153 itsTimeToWritePixels = true;
1157 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
1161 * \ingroup gdcmParser
1162 * \brief writes on disc according to the requested format
1163 * (ACR-NEMA, ExplicitVR, ImplicitVR) the image
1164 * using the Chained List
1165 * \warning does NOT add the missing elements in the header :
1166 * it's up to the user doing it !
1167 * (function CheckHeaderCoherence to be written)
1168 * \warning DON'T try, right now, to write a DICOM image
1169 * from an ACR Header (meta elements will be missing!)
1170 * \sa WriteEntriesDeprecated (Special temporary method for Theralys)
1171 * @param type type of the File to be written
1172 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1173 * @param _fp already open file pointer
1176 void gdcmParser::WriteEntries(FILE *_fp,FileType type)
1178 // TODO (?) tester les echecs en ecriture (apres chaque fwrite)
1180 for (ListTag::iterator tag2=listEntries.begin();
1181 tag2 != listEntries.end();
1184 WriteEntry(*tag2,_fp,type);
1185 if (itsTimeToWritePixels)
1191 * \ingroup gdcmParser
1192 * \brief writes on disc according to the requested format
1193 * (ACR-NEMA, ExplicitVR, ImplicitVR) the image,
1194 * using only the last synonym of each mutimap H Table post.
1195 * \warning Uses the H Table, instead of the Chained List
1196 * in order to be compliant with the old way to proceed
1197 * (added elements taken in to account)
1198 * Only THERALYS, during a transitory phase is supposed
1199 * to use this method !!!
1200 * \warning DON'T try, right now, to write a DICOM image
1201 * from an ACR Header (meta elements will be missing!)
1203 * @param _fp already open file pointer
1204 * @param type type of the File to be written
1205 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1207 void gdcmParser::WriteEntriesDeprecated(FILE *_fp,FileType type) {
1209 // restent a tester les echecs en ecriture (apres chaque fwrite)
1211 for (TagHeaderEntryHT::iterator tag2=tagHT.begin();
1212 tag2 != tagHT.end();
1214 WriteEntry(tag2->second,_fp,type);
1215 if (itsTimeToWritePixels)
1221 * \ingroup gdcmParser
1222 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1224 * @return The properly swaped 32 bits integer.
1226 guint32 gdcmParser::SwapLong(guint32 a) {
1231 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1232 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1236 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1240 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1243 dbg.Error(" gdcmParser::SwapLong : unset swap code");
1250 * \ingroup gdcmParser
1251 * \brief Unswaps back the bytes of 4-byte long integer accordingly to
1253 * @return The properly unswaped 32 bits integer.
1255 guint32 gdcmParser::UnswapLong(guint32 a) {
1256 return (SwapLong(a));
1260 * \ingroup gdcmParser
1261 * \brief Swaps the bytes so they agree with the processor order
1262 * @return The properly swaped 16 bits integer.
1264 guint16 gdcmParser::SwapShort(guint16 a) {
1265 if ( (sw==4321) || (sw==2143) )
1266 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1271 * \ingroup gdcmParser
1272 * \brief Unswaps the bytes so they agree with the processor order
1273 * @return The properly unswaped 16 bits integer.
1275 guint16 gdcmParser::UnswapShort(guint16 a) {
1276 return (SwapShort(a));
1279 //-----------------------------------------------------------------------------
1282 * \ingroup gdcmParser
1283 * \brief Parses the header of the file but WITHOUT loading element values.
1284 * @return false if file is not ACR-NEMA / DICOM
1286 bool gdcmParser::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1292 gdcmHeaderEntry *newHeaderEntry = (gdcmHeaderEntry *)0;
1293 while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1294 SkipHeaderEntry(newHeaderEntry);
1295 if ( (ignoreShadow==0) || (newHeaderEntry->GetGroup()%2) == 0) {
1296 AddHeaderEntry(newHeaderEntry);
1303 * \ingroup gdcmParser
1304 * \brief Loads the element values of all the Header Entries pointed in the
1305 * public Chained List.
1307 void gdcmParser::LoadHeaderEntries(void) {
1309 for (ListTag::iterator i = GetListEntry().begin();
1310 i != GetListEntry().end();
1313 LoadHeaderEntry(*i);
1318 // Load 'non string' values
1319 std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
1320 if( PhotometricInterpretation == "PALETTE COLOR " ) {
1321 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
1322 LoadEntryVoidArea(0x0028,0x1201); // R LUT
1323 LoadEntryVoidArea(0x0028,0x1202); // G LUT
1324 LoadEntryVoidArea(0x0028,0x1203); // B LUT
1326 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1327 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1328 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1330 //FIXME : how to use it?
1331 LoadEntryVoidArea(0x0028,0x3006); //LUT Data (CTX dependent)
1333 // --------------------------------------------------------------
1334 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1336 // if recognition code tells us we deal with a LibIDO image
1337 // we switch lineNumber and columnNumber
1339 std::string RecCode;
1340 RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
1341 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1342 RecCode == "CANRME_AILIBOD1_1." )
1344 filetype = ACR_LIBIDO;
1345 std::string rows = GetEntryByNumber(0x0028, 0x0010);
1346 std::string columns = GetEntryByNumber(0x0028, 0x0011);
1347 SetEntryByNumber(columns, 0x0028, 0x0010);
1348 SetEntryByNumber(rows , 0x0028, 0x0011);
1350 // ----------------- End of Special Patch ----------------
1354 * \ingroup gdcmParser
1355 * \brief Loads the element content if its length doesn't exceed
1356 * the value specified with gdcmParser::SetMaxSizeLoadEntry()
1357 * @param Entry Header Entry (Dicom Element) to be dealt with
1359 void gdcmParser::LoadHeaderEntry(gdcmHeaderEntry *Entry) {
1361 guint16 group = Entry->GetGroup();
1362 std::string vr= Entry->GetVR();
1363 guint32 length = Entry->GetLength();
1364 bool SkipLoad = false;
1366 fseek(fp, (long)Entry->GetOffset(), SEEK_SET);
1368 // the test was commented out to 'go inside' the SeQuences
1369 // we don't any longer skip them !
1371 // if( vr == "SQ" ) // (DO NOT remove this comment)
1374 // A SeQuence "contains" a set of Elements.
1375 // (fffe e000) tells us an Element is beginning
1376 // (fffe e00d) tells us an Element just ended
1377 // (fffe e0dd) tells us the current SeQuence just ended
1378 if( group == 0xfffe )
1382 Entry->SetLength(0);
1383 Entry->SetValue("gdcm::Skipped");
1387 // When the length is zero things are easy:
1388 if ( length == 0 ) {
1389 Entry->SetValue("");
1393 // The elements whose length is bigger than the specified upper bound
1394 // are not loaded. Instead we leave a short notice of the offset of
1395 // the element content and it's length.
1396 if (length > MaxSizeLoadEntry) {
1397 std::ostringstream s;
1398 s << "gdcm::NotLoaded.";
1399 s << " Address:" << (long)Entry->GetOffset();
1400 s << " Length:" << Entry->GetLength();
1401 s << " x(" << std::hex << Entry->GetLength() << ")";
1402 Entry->SetValue(s.str());
1406 // When integer(s) are expected, read and convert the following
1407 // n *(two or four bytes)
1408 // properly i.e. as integers as opposed to strings.
1409 // Elements with Value Multiplicity > 1
1410 // contain a set of integers (not a single one)
1412 // Any compacter code suggested (?)
1413 if ( IsHeaderEntryAnInteger(Entry) ) {
1415 std::ostringstream s;
1417 if (vr == "US" || vr == "SS") {
1419 NewInt = ReadInt16();
1422 for (int i=1; i < nbInt; i++) {
1424 NewInt = ReadInt16();
1429 else if (vr == "UL" || vr == "SL") {
1431 NewInt = ReadInt32();
1434 for (int i=1; i < nbInt; i++) {
1436 NewInt = ReadInt32();
1441 #ifdef GDCM_NO_ANSI_STRING_STREAM
1442 s << std::ends; // to avoid oddities on Solaris
1443 #endif //GDCM_NO_ANSI_STRING_STREAM
1445 Entry->SetValue(s.str());
1449 // We need an additional byte for storing \0 that is not on disk
1450 std::string NewValue(length,0);
1451 item_read = fread(&(NewValue[0]), (size_t)length, (size_t)1, fp);
1452 if ( item_read != 1 ) {
1453 dbg.Verbose(1, "gdcmParser::LoadElementValue","unread element value");
1454 Entry->SetValue("gdcm::UnRead");
1458 if( (vr == "UI") ) // Because of correspondance with the VR dic
1459 Entry->SetValue(NewValue.c_str()); // ??? JPR ???
1461 Entry->SetValue(NewValue);
1465 * \ingroup gdcmParser
1466 * \brief add a new Dicom Element pointer to
1467 * the H Table and at the end of the chained List
1468 * \warning push_bash in listEntries ONLY during ParseHeader
1469 * \todo something to allow further Elements addition,
1470 * (at their right place in the chained list)
1471 * when position to be taken care of
1472 * @param newHeaderEntry
1474 void gdcmParser::AddHeaderEntry(gdcmHeaderEntry *newHeaderEntry) {
1475 tagHT.insert( PairHT( newHeaderEntry->GetKey(),newHeaderEntry) );
1476 listEntries.push_back(newHeaderEntry);
1481 * \ingroup gdcmParser
1482 * \brief Find the value Length of the passed Header Entry
1483 * @param Entry Header Entry whose length of the value shall be loaded.
1485 void gdcmParser::FindHeaderEntryLength (gdcmHeaderEntry *Entry) {
1486 guint16 element = Entry->GetElement();
1487 guint16 group = Entry->GetGroup();
1488 std::string vr = Entry->GetVR();
1491 if( (element == NumPixel) && (group == GrPixel) )
1493 dbg.SetDebug(GDCM_DEBUG);
1494 dbg.Verbose(2, "gdcmParser::FindLength: ",
1495 "we reached (GrPixel,NumPixel)");
1498 if ( (filetype == ExplicitVR) && (! Entry->IsImplicitVR()) )
1500 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") )
1502 // The following reserved two bytes (see PS 3.5-2001, section
1503 // 7.1.2 Data element structure with explicit vr p27) must be
1504 // skipped before proceeding on reading the length on 4 bytes.
1505 fseek(fp, 2L, SEEK_CUR);
1506 guint32 length32 = ReadInt32();
1508 if ( (vr == "OB") && (length32 == 0xffffffff) )
1510 Entry->SetLength(FindHeaderEntryLengthOB());
1513 FixHeaderEntryFoundLength(Entry, length32);
1517 // Length is encoded on 2 bytes.
1518 length16 = ReadInt16();
1520 // We can tell the current file is encoded in big endian (like
1521 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1522 // and it's value is the one of the encoding of a big endian file.
1523 // In order to deal with such big endian encoded files, we have
1524 // (at least) two strategies:
1525 // * when we load the "Transfer Syntax" tag with value of big endian
1526 // encoding, we raise the proper flags. Then we wait for the end
1527 // of the META group (0x0002) among which is "Transfer Syntax",
1528 // before switching the swap code to big endian. We have to postpone
1529 // the switching of the swap code since the META group is fully encoded
1530 // in little endian, and big endian coding only starts at the next
1531 // group. The corresponding code can be hard to analyse and adds
1532 // many additional unnecessary tests for regular tags.
1533 // * the second strategy consists in waiting for trouble, that shall
1534 // appear when we find the first group with big endian encoding. This
1535 // is easy to detect since the length of a "Group Length" tag (the
1536 // ones with zero as element number) has to be of 4 (0x0004). When we
1537 // encounter 1024 (0x0400) chances are the encoding changed and we
1538 // found a group with big endian encoding.
1539 // We shall use this second strategy. In order to make sure that we
1540 // can interpret the presence of an apparently big endian encoded
1541 // length of a "Group Length" without committing a big mistake, we
1542 // add an additional check: we look in the already parsed elements
1543 // for the presence of a "Transfer Syntax" whose value has to be "big
1544 // endian encoding". When this is the case, chances are we have got our
1545 // hands on a big endian encoded file: we switch the swap code to
1546 // big endian and proceed...
1547 if ( (element == 0x0000) && (length16 == 0x0400) )
1549 if ( ! IsExplicitVRBigEndianTransferSyntax() )
1551 dbg.Verbose(0, "gdcmParser::FindLength", "not explicit VR");
1556 SwitchSwapToBigEndian();
1557 // Restore the unproperly loaded values i.e. the group, the element
1558 // and the dictionary entry depending on them.
1559 guint16 CorrectGroup = SwapShort(Entry->GetGroup());
1560 guint16 CorrectElem = SwapShort(Entry->GetElement());
1561 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
1565 // This correct tag is not in the dictionary. Create a new one.
1566 NewTag = NewVirtualDictEntry(CorrectGroup, CorrectElem);
1568 // FIXME this can create a memory leaks on the old entry that be
1569 // left unreferenced.
1570 Entry->SetDictEntry(NewTag);
1573 // Heuristic: well some files are really ill-formed.
1574 if ( length16 == 0xffff)
1577 //dbg.Verbose(0, "gdcmParser::FindLength",
1578 // "Erroneous element length fixed.");
1579 // Actually, length= 0xffff means that we deal with
1580 // Unknown Sequence Length
1582 FixHeaderEntryFoundLength(Entry, (guint32)length16);
1587 // Either implicit VR or a non DICOM conformal (see note below) explicit
1588 // VR that ommited the VR of (at least) this element. Farts happen.
1589 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1590 // on Data elements "Implicit and Explicit VR Data Elements shall
1591 // not coexist in a Data Set and Data Sets nested within it".]
1592 // Length is on 4 bytes.
1594 FixHeaderEntryFoundLength(Entry, ReadInt32());
1600 * \ingroup gdcmParser
1601 * \brief Find the Value Representation of the current Dicom Element.
1604 void gdcmParser::FindHeaderEntryVR( gdcmHeaderEntry *Entry)
1606 if (filetype != ExplicitVR)
1611 long PositionOnEntry = ftell(fp);
1612 // Warning: we believe this is explicit VR (Value Representation) because
1613 // we used a heuristic that found "UL" in the first tag. Alas this
1614 // doesn't guarantee that all the tags will be in explicit VR. In some
1615 // cases (see e-film filtered files) one finds implicit VR tags mixed
1616 // within an explicit VR file. Hence we make sure the present tag
1617 // is in explicit VR and try to fix things if it happens not to be
1620 int lgrLue=fread (&VR, (size_t)2,(size_t)1, fp); // lgrLue not used
1622 if(!CheckHeaderEntryVR(Entry,VR))
1624 fseek(fp, PositionOnEntry, SEEK_SET);
1625 // When this element is known in the dictionary we shall use, e.g. for
1626 // the semantics (see the usage of IsAnInteger), the VR proposed by the
1627 // dictionary entry. Still we have to flag the element as implicit since
1628 // we know now our assumption on expliciteness is not furfilled.
1630 if ( Entry->IsVRUnknown() )
1631 Entry->SetVR("Implicit");
1632 Entry->SetImplicitVR();
1637 * \ingroup gdcmParser
1638 * \brief Check the correspondance between the VR of the header entry
1639 * and the taken VR. If they are different, the header entry is
1640 * updated with the new VR.
1641 * @param Entry Header Entry to check
1642 * @param vr Dicom Value Representation
1643 * @return false if the VR is incorrect of if the VR isn't referenced
1644 * otherwise, it returns true
1646 bool gdcmParser::CheckHeaderEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
1648 char msg[100]; // for sprintf
1649 bool RealExplicit = true;
1651 // Assume we are reading a falsely explicit VR file i.e. we reached
1652 // a tag where we expect reading a VR but are in fact we read the
1653 // first to bytes of the length. Then we will interogate (through find)
1654 // the dicom_vr dictionary with oddities like "\004\0" which crashes
1655 // both GCC and VC++ implementations of the STL map. Hence when the
1656 // expected VR read happens to be non-ascii characters we consider
1657 // we hit falsely explicit VR tag.
1659 if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
1660 RealExplicit = false;
1662 // CLEANME searching the dicom_vr at each occurence is expensive.
1663 // PostPone this test in an optional integrity check at the end
1664 // of parsing or only in debug mode.
1665 if ( RealExplicit && !gdcmGlobal::GetVR()->Count(vr) )
1666 RealExplicit= false;
1668 if ( !RealExplicit )
1670 // We thought this was explicit VR, but we end up with an
1671 // implicit VR tag. Let's backtrack.
1672 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
1673 Entry->GetGroup(),Entry->GetElement());
1674 dbg.Verbose(1, "gdcmParser::FindVR: ",msg);
1675 if (Entry->GetGroup()%2 && Entry->GetElement() == 0x0000) { // Group length is UL !
1676 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1677 Entry->GetGroup(),Entry->GetElement(),
1678 "UL","FIXME","Group Length");
1679 Entry->SetDictEntry(NewEntry);
1684 if ( Entry->IsVRUnknown() )
1686 // When not a dictionary entry, we can safely overwrite the VR.
1687 if (Entry->GetElement() == 0x0000) { // Group length is UL !
1693 else if ( Entry->GetVR() != vr )
1695 // The VR present in the file and the dictionary disagree. We assume
1696 // the file writer knew best and use the VR of the file. Since it would
1697 // be unwise to overwrite the VR of a dictionary (since it would
1698 // compromise it's next user), we need to clone the actual DictEntry
1699 // and change the VR for the read one.
1700 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1701 Entry->GetGroup(),Entry->GetElement(),
1702 vr,"FIXME",Entry->GetName());
1703 Entry->SetDictEntry(NewEntry);
1709 * \ingroup gdcmParser
1710 * \brief Get the transformed value of the header entry. The VR value
1711 * is used to define the transformation to operate on the value
1712 * \warning NOT end user intended method !
1714 * @return Transformed entry value
1716 std::string gdcmParser::GetHeaderEntryValue(gdcmHeaderEntry *Entry)
1718 if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1720 std::string val=Entry->GetValue();
1721 std::string vr=Entry->GetVR();
1722 guint32 length = Entry->GetLength();
1723 std::ostringstream s;
1726 if (vr == "US" || vr == "SS")
1731 for (int i=0; i < nbInt; i++)
1735 NewInt16 = (val[2*i+0]&0xFF)+((val[2*i+1]&0xFF)<<8);
1736 NewInt16 = SwapShort(NewInt16);
1741 else if (vr == "UL" || vr == "SL")
1746 for (int i=0; i < nbInt; i++)
1750 NewInt32= (val[4*i+0]&0xFF)+((val[4*i+1]&0xFF)<<8)+
1751 ((val[4*i+2]&0xFF)<<16)+((val[4*i+3]&0xFF)<<24);
1752 NewInt32=SwapLong(NewInt32);
1756 #ifdef GDCM_NO_ANSI_STRING_STREAM
1757 s << std::ends; // to avoid oddities on Solaris
1758 #endif //GDCM_NO_ANSI_STRING_STREAM
1762 return(Entry->GetValue());
1766 * \ingroup gdcmParser
1767 * \brief Get the reverse transformed value of the header entry. The VR
1768 * value is used to define the reverse transformation to operate on
1770 * \warning NOT end user intended method !
1772 * @return Reverse transformed entry value
1774 std::string gdcmParser::GetHeaderEntryUnvalue(gdcmHeaderEntry *Entry)
1776 if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1778 std::string vr=Entry->GetVR();
1779 std::ostringstream s;
1780 std::vector<std::string> tokens;
1782 if (vr == "US" || vr == "SS")
1786 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1787 Tokenize (Entry->GetValue(), tokens, "\\");
1788 for (unsigned int i=0; i<tokens.size();i++)
1790 NewInt16 = atoi(tokens[i].c_str());
1791 s<<(NewInt16&0xFF)<<((NewInt16>>8)&0xFF);
1795 if (vr == "UL" || vr == "SL")
1799 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1800 Tokenize (Entry->GetValue(), tokens, "\\");
1801 for (unsigned int i=0; i<tokens.size();i++)
1803 NewInt32 = atoi(tokens[i].c_str());
1804 s<<(char)(NewInt32&0xFF)<<(char)((NewInt32>>8)&0xFF)
1805 <<(char)((NewInt32>>16)&0xFF)<<(char)((NewInt32>>24)&0xFF);
1810 #ifdef GDCM_NO_ANSI_STRING_STREAM
1811 s << std::ends; // to avoid oddities on Solaris
1812 #endif //GDCM_NO_ANSI_STRING_STREAM
1816 return(Entry->GetValue());
1820 * \ingroup gdcmParser
1821 * \brief Skip a given Header Entry
1822 * \warning NOT end user intended method !
1825 void gdcmParser::SkipHeaderEntry(gdcmHeaderEntry *entry)
1827 SkipBytes(entry->GetLength());
1831 * \ingroup gdcmParser
1832 * \brief When the length of an element value is obviously wrong (because
1833 * the parser went Jabberwocky) one can hope improving things by
1834 * applying this heuristic.
1836 void gdcmParser::FixHeaderEntryFoundLength(gdcmHeaderEntry *Entry, guint32 FoundLength)
1838 Entry->SetReadLength(FoundLength); // will be updated only if a bug is found
1840 if ( FoundLength == 0xffffffff) {
1844 guint16 gr =Entry->GetGroup();
1845 guint16 el =Entry->GetElement();
1847 if (FoundLength%2) {
1848 std::ostringstream s;
1849 s << "Warning : Tag with uneven length " << FoundLength
1850 << " in x(" << std::hex << gr << "," << el <<")" << std::dec;
1851 dbg.Verbose(0,s.str().c_str());
1854 // Sorry for the patch!
1855 // XMedCom did the trick to read some nasty GE images ...
1856 if (FoundLength == 13) {
1857 // The following 'if' will be removed when there is no more
1858 // images on Creatis HDs with a 13 length for Manufacturer...
1859 if ( (Entry->GetGroup() != 0x0008) ||
1860 ( (Entry->GetElement() != 0x0070) && (Entry->GetElement() != 0x0080) ) ){
1861 // end of remove area
1863 Entry->SetReadLength(10); // a bug is to be fixed
1867 // to fix some garbage 'Leonardo' Siemens images
1868 // May be commented out to avoid overhead
1869 else if ( (Entry->GetGroup() == 0x0009) &&
1870 ( (Entry->GetElement() == 0x1113) || (Entry->GetElement() == 0x1114) ) ){
1872 Entry->SetReadLength(4); // a bug is to be fixed
1876 // to try to 'go inside' SeQuences (with length), and not to skip them
1877 else if ( Entry->GetVR() == "SQ")
1879 if (enableSequences) // only if the user does want to !
1880 FoundLength =0; // ReadLength is unchanged
1883 // a SeQuence Element is beginning
1884 // Let's forget it's length
1885 // (we want to 'go inside')
1887 // Pb : *normaly* fffe|e000 is just a marker, its length *should be* zero
1888 // in gdcm-MR-PHILIPS-16-Multi-Seq.dcm we find lengthes as big as 28800
1889 // if we set the length to zero IsHeaderEntryAnInteger() breaks...
1890 // if we don't, we lost 28800 characters from the Header :-(
1892 else if(Entry->GetGroup() == 0xfffe)
1894 // cout << "ReadLength " <<Entry->GetReadLength() << " UsableLength " << FoundLength << endl;
1896 // sometimes, length seems to be wrong
1897 FoundLength =0; // some more clever checking to be done !
1899 // only gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1900 // causes troubles :-(
1903 Entry->SetUsableLength(FoundLength);
1907 * \ingroup gdcmParser
1908 * \brief Apply some heuristics to predict whether the considered
1909 * element value contains/represents an integer or not.
1910 * @param Entry The element value on which to apply the predicate.
1911 * @return The result of the heuristical predicate.
1913 bool gdcmParser::IsHeaderEntryAnInteger(gdcmHeaderEntry *Entry) {
1914 guint16 element = Entry->GetElement();
1915 guint16 group = Entry->GetGroup();
1916 std::string vr = Entry->GetVR();
1917 guint32 length = Entry->GetLength();
1918 // When we have some semantics on the element we just read, and if we
1919 // a priori know we are dealing with an integer, then we shall be
1920 // able to swap it's element value properly.
1921 if ( element == 0 ) // This is the group length of the group
1927 std::ostringstream s;
1928 int filePosition = ftell(fp);
1929 s << "Erroneous Group Length element length on : (" \
1930 << std::hex << group << " , " << element
1931 << ") -before- position x(" << filePosition << ")"
1932 << "lgt : " << length;
1933 // These 2 lines commented out : a *very dirty* patch
1934 // to go on PrintHeader'ing gdcm-MR-PHILIPS-16-Multi-Seq.dcm.
1935 // have a glance at offset x(8336) ...
1936 // For *regular* headers, the test is useless..
1937 // lets's print a warning message and go on,
1938 // instead of giving up with an error message
1940 //std::cout << s.str().c_str() << std::endl;
1942 // dbg.Error("gdcmParser::IsHeaderEntryAnInteger",
1943 // s.str().c_str());
1946 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1952 * \ingroup gdcmParser
1953 * \brief Find the Length till the next sequence delimiter
1954 * \warning NOT end user intended method !
1958 guint32 gdcmParser::FindHeaderEntryLengthOB(void) {
1959 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1962 long PositionOnEntry = ftell(fp);
1963 bool FoundSequenceDelimiter = false;
1964 guint32 TotalLength = 0;
1967 while ( ! FoundSequenceDelimiter)
1973 TotalLength += 4; // We even have to decount the group and element
1975 if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */
1977 char msg[100]; // for sprintf. Sorry
1978 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
1979 dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
1983 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
1984 FoundSequenceDelimiter = true;
1985 else if ( n != 0xe000 )
1987 char msg[100]; // for sprintf. Sorry
1988 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
1990 dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
1994 ItemLength = ReadInt32();
1995 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
1996 // the ItemLength with ReadInt32
1997 SkipBytes(ItemLength);
1999 fseek(fp, PositionOnEntry, SEEK_SET);
2004 * \ingroup gdcmParser
2005 * \brief Reads a supposed to be 16 Bits integer
2006 * (swaps it depending on processor endianity)
2007 * @return read value
2009 guint16 gdcmParser::ReadInt16(void) {
2012 item_read = fread (&g, (size_t)2,(size_t)1, fp);
2013 if ( item_read != 1 ) {
2015 dbg.Verbose(0, "gdcmParser::ReadInt16", " File Error");
2025 * \ingroup gdcmParser
2026 * \brief Reads a supposed to be 32 Bits integer
2027 * (swaps it depending on processor endianity)
2028 * @return read value
2030 guint32 gdcmParser::ReadInt32(void) {
2033 item_read = fread (&g, (size_t)4,(size_t)1, fp);
2034 if ( item_read != 1 ) {
2036 dbg.Verbose(0, "gdcmParser::ReadInt32", " File Error");
2046 * \ingroup gdcmParser
2047 * \brief skips bytes inside the source file
2048 * \warning NOT end user intended method !
2051 void gdcmParser::SkipBytes(guint32 NBytes) {
2052 //FIXME don't dump the returned value
2053 (void)fseek(fp, (long)NBytes, SEEK_CUR);
2057 * \ingroup gdcmParser
2058 * \brief Loads all the needed Dictionaries
2059 * \warning NOT end user intended method !
2061 void gdcmParser::Initialise(void)
2063 RefPubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
2064 RefShaDict = (gdcmDict*)0;
2068 * \ingroup gdcmParser
2069 * \brief Discover what the swap code is (among little endian, big endian,
2070 * bad little endian, bad big endian).
2072 * @return false when we are absolutely sure
2073 * it's neither ACR-NEMA nor DICOM
2074 * true when we hope ours assuptions are OK
2076 bool gdcmParser::CheckSwap() {
2078 // The only guaranted way of finding the swap code is to find a
2079 // group tag since we know it's length has to be of four bytes i.e.
2080 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2081 // occurs when we can't find such group...
2083 guint32 x=4; // x : for ntohs
2084 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2090 char deb[HEADER_LENGTH_TO_READ];
2092 // First, compare HostByteOrder and NetworkByteOrder in order to
2093 // determine if we shall need to swap bytes (i.e. the Endian type).
2099 // The easiest case is the one of a DICOM header, since it possesses a
2100 // file preamble where it suffice to look for the string "DICM".
2101 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2104 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2105 dbg.Verbose(1, "gdcmParser::CheckSwap:", "looks like DICOM Version3");
2107 // Next, determine the value representation (VR). Let's skip to the
2108 // first element (0002, 0000) and check there if we find "UL"
2109 // - or "OB" if the 1st one is (0002,0001) -,
2110 // in which case we (almost) know it is explicit VR.
2111 // WARNING: if it happens to be implicit VR then what we will read
2112 // is the length of the group. If this ascii representation of this
2113 // length happens to be "UL" then we shall believe it is explicit VR.
2114 // FIXME: in order to fix the above warning, we could read the next
2115 // element value (or a couple of elements values) in order to make
2116 // sure we are not commiting a big mistake.
2117 // We need to skip :
2118 // * the 128 bytes of File Preamble (often padded with zeroes),
2119 // * the 4 bytes of "DICM" string,
2120 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2121 // i.e. a total of 136 bytes.
2125 // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2126 // but elem 0002,0010 (Transfert Syntax) tells us the file is *Implicit* VR.
2129 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2130 (memcmp(entCur, "OB", (size_t)2) == 0) ||
2131 (memcmp(entCur, "UI", (size_t)2) == 0) ||
2132 (memcmp(entCur, "CS", (size_t)2) == 0) ) // CS, to remove later
2133 // when Write DCM *adds*
2135 // Use gdcmParser::dicom_vr to test all the possibilities
2136 // instead of just checking for UL, OB and UI !? // group 0000
2139 filetype = ExplicitVR;
2140 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2141 "explicit Value Representation");
2145 filetype = ImplicitVR;
2146 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2147 "not an explicit Value Representation");
2153 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2154 "HostByteOrder != NetworkByteOrder");
2159 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2160 "HostByteOrder = NetworkByteOrder");
2163 // Position the file position indicator at first tag (i.e.
2164 // after the file preamble and the "DICM" string).
2166 fseek (fp, 132L, SEEK_SET);
2170 // Alas, this is not a DicomV3 file and whatever happens there is no file
2171 // preamble. We can reset the file position indicator to where the data
2172 // is (i.e. the beginning of the file).
2173 dbg.Verbose(1, "gdcmParser::CheckSwap:", "not a DICOM Version3 file");
2176 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2177 // By clean we mean that the length of the first tag is written down.
2178 // If this is the case and since the length of the first group HAS to be
2179 // four (bytes), then determining the proper swap code is straightforward.
2182 // We assume the array of char we are considering contains the binary
2183 // representation of a 32 bits integer. Hence the following dirty
2185 s32 = *((guint32 *)(entCur));
2206 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2207 // It is time for despaired wild guesses.
2208 // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2209 // i.e. the 'group length' element is not present :
2211 // check the supposed to be 'group number'
2212 // 0x0002 or 0x0004 or 0x0008
2213 // to determine ' sw' value .
2214 // Only 0 or 4321 will be possible
2215 // (no oportunity to check for the formerly well known
2216 // ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian'
2217 // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -4, 8-)
2218 // the file IS NOT ACR-NEMA nor DICOM V3
2219 // Find a trick to tell it the caller...
2221 s16 = *((guint16 *)(deb));
2237 dbg.Verbose(0, "gdcmParser::CheckSwap:",
2238 "ACR/NEMA unfound swap info (Really hopeless !)");
2243 // Then the only info we have is the net2host one.
2253 * \ingroup gdcmParser
2254 * \brief Restore the unproperly loaded values i.e. the group, the element
2255 * and the dictionary entry depending on them.
2257 void gdcmParser::SwitchSwapToBigEndian(void)
2259 dbg.Verbose(1, "gdcmParser::SwitchSwapToBigEndian",
2260 "Switching to BigEndian mode.");
2281 * \ingroup gdcmParser
2282 * \brief during parsing, Header Elements too long are not loaded in memory
2285 void gdcmParser::SetMaxSizeLoadEntry(long NewSize)
2289 if ((guint32)NewSize >= (guint32)0xffffffff)
2291 MaxSizeLoadEntry = 0xffffffff;
2294 MaxSizeLoadEntry = NewSize;
2299 * \ingroup gdcmParser
2300 * \brief Header Elements too long will not be printed
2302 * \todo : not yet usable
2303 * (see MAX_SIZE_PRINT_ELEMENT_VALUE
2304 * in gdcmHeaderEntry gdcmLoadEntry)
2308 void gdcmParser::SetMaxSizePrintEntry(long NewSize)
2312 if ((guint32)NewSize >= (guint32)0xffffffff)
2314 MaxSizePrintEntry = 0xffffffff;
2317 MaxSizePrintEntry = NewSize;
2321 * \ingroup gdcmParser
2322 * \brief Searches both the public and the shadow dictionary (when they
2323 * exist) for the presence of the DictEntry with given name.
2324 * The public dictionary has precedence on the shadow one.
2325 * @param Name name of the searched DictEntry
2326 * @return Corresponding DictEntry when it exists, NULL otherwise.
2328 gdcmDictEntry *gdcmParser::GetDictEntryByName(std::string Name)
2330 gdcmDictEntry *found = (gdcmDictEntry *)0;
2331 if (!RefPubDict && !RefShaDict)
2333 dbg.Verbose(0, "gdcmParser::GetDictEntry",
2334 "we SHOULD have a default dictionary");
2338 found = RefPubDict->GetDictEntryByName(Name);
2344 found = RefShaDict->GetDictEntryByName(Name);
2352 * \ingroup gdcmParser
2353 * \brief Searches both the public and the shadow dictionary (when they
2354 * exist) for the presence of the DictEntry with given
2355 * group and element. The public dictionary has precedence on the
2357 * @param group group of the searched DictEntry
2358 * @param element element of the searched DictEntry
2359 * @return Corresponding DictEntry when it exists, NULL otherwise.
2361 gdcmDictEntry *gdcmParser::GetDictEntryByNumber(guint16 group,guint16 element)
2363 gdcmDictEntry *found = (gdcmDictEntry *)0;
2364 if (!RefPubDict && !RefShaDict)
2366 dbg.Verbose(0, "gdcmParser::GetDictEntry",
2367 "we SHOULD have a default dictionary");
2371 found = RefPubDict->GetDictEntryByNumber(group, element);
2377 found = RefShaDict->GetDictEntryByNumber(group, element);
2385 * \ingroup gdcmParser
2386 * \brief Read the next tag but WITHOUT loading it's value
2387 * @return On succes the newly created HeaderEntry, NULL on failure.
2389 gdcmHeaderEntry *gdcmParser::ReadNextHeaderEntry(void) {
2391 gdcmHeaderEntry *NewEntry;
2396 // We reached the EOF (or an error occured) therefore
2397 // header parsing has to be considered as finished.
2398 return (gdcmHeaderEntry *)0;
2400 /* Pb : how to propagate the element length (used in SkipHeaderEntry)
2401 // direct call to SkipBytes ?
2403 if (ignoreShadow == 1 && g%2 ==1) //JPR
2404 // if user wants to skip shadow groups
2405 // and current element *is* a shadow element
2406 // we don't create anything
2407 return (gdcmHeaderEntry *)1; // to tell caller it's NOT finished
2409 NewEntry = NewHeaderEntryByNumber(g, n);
2410 FindHeaderEntryVR(NewEntry);
2411 FindHeaderEntryLength(NewEntry);
2417 NewEntry->SetOffset(ftell(fp));
2422 * \ingroup gdcmParser
2423 * \brief Build a new Element Value from all the low level arguments.
2424 * Check for existence of dictionary entry, and build
2425 * a default one when absent.
2426 * @param Name Name of the underlying DictEntry
2428 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByName(std::string Name)
2430 gdcmDictEntry *NewTag = GetDictEntryByName(Name);
2432 NewTag = NewVirtualDictEntry(0xffff, 0xffff, "LO", "unkn", Name);
2434 gdcmHeaderEntry* NewEntry = new gdcmHeaderEntry(NewTag);
2437 dbg.Verbose(1, "gdcmParser::ObtainHeaderEntryByName",
2438 "failed to allocate gdcmHeaderEntry");
2439 return (gdcmHeaderEntry *)0;
2445 * \ingroup gdcmParser
2446 * \brief Request a new virtual dict entry to the dict set
2447 * @param group group of the underlying DictEntry
2448 * @param element element of the underlying DictEntry
2449 * @param vr VR of the underlying DictEntry
2450 * @param fourth owner group
2451 * @param name english name
2453 gdcmDictEntry *gdcmParser::NewVirtualDictEntry(guint16 group, guint16 element,
2458 return gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,element,vr,fourth,name);
2462 * \ingroup gdcmParser
2463 * \brief Build a new Element Value from all the low level arguments.
2464 * Check for existence of dictionary entry, and build
2465 * a default one when absent.
2466 * @param Group group of the underlying DictEntry
2467 * @param Elem element of the underlying DictEntry
2469 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByNumber(guint16 Group, guint16 Elem)
2471 // Find out if the tag we encountered is in the dictionaries:
2472 gdcmDictEntry *DictEntry = GetDictEntryByNumber(Group, Elem);
2474 DictEntry = NewVirtualDictEntry(Group, Elem);
2476 gdcmHeaderEntry *NewEntry = new gdcmHeaderEntry(DictEntry);
2479 dbg.Verbose(1, "gdcmParser::NewHeaderEntryByNumber",
2480 "failed to allocate gdcmHeaderEntry");
2486 // Never used; commented out, waiting for removal.
2488 * \ingroup gdcmParser
2489 * \brief Small utility function that creates a new manually crafted
2490 * (as opposed as read from the file) gdcmHeaderEntry with user
2491 * specified name and adds it to the public tag hash table.
2492 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
2493 * @param NewTagName The name to be given to this new tag.
2494 * @param VR The Value Representation to be given to this new tag.
2495 * @return The newly hand crafted Element Value.
2497 //gdcmHeaderEntry *gdcmParser::NewManualHeaderEntryToPubDict(std::string NewTagName,
2500 // gdcmHeaderEntry *NewEntry = NULL;
2501 // guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
2502 // guint32 FreeElem = 0;
2503 // gdcmDictEntry *DictEntry = NULL;
2505 // FreeElem = GenerateFreeTagKeyInGroup(StuffGroup);
2506 // if (FreeElem == UINT32_MAX)
2508 // dbg.Verbose(1, "gdcmHeader::NewManualHeaderEntryToPubDict",
2509 // "Group 0xffff in Public Dict is full");
2513 // DictEntry = NewVirtualDictEntry(StuffGroup, FreeElem,
2514 // VR, "GDCM", NewTagName);
2515 // NewEntry = new gdcmHeaderEntry(DictEntry);
2516 // AddHeaderEntry(NewEntry);
2521 * \ingroup gdcmParser
2522 * \brief Generate a free TagKey i.e. a TagKey that is not present
2523 * in the TagHt dictionary.
2524 * @param group The generated tag must belong to this group.
2525 * @return The element of tag with given group which is fee.
2527 guint32 gdcmParser::GenerateFreeTagKeyInGroup(guint16 group)
2529 for (guint32 elem = 0; elem < UINT32_MAX; elem++)
2531 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
2532 if (tagHT.count(key) == 0)
2538 //-----------------------------------------------------------------------------