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 * \warning : adds the Header Entry to the HTable, NOT to the chained List
457 * @param Value passed as a std::string
458 * @param Group group of the Entry
459 * @param Elem element of the Entry
460 * \return pointer to the created Header Entry
461 * NULL if creation failed
463 gdcmHeaderEntry * gdcmParser::ReplaceOrCreateByNumber(
468 a = GetHeaderEntryByNumber( Group, Elem);
470 gdcmHeaderEntry *a =NewHeaderEntryByNumber(Group, Elem);
475 //SetEntryByNumber(Value, Group, Elem);
481 * \ingroup gdcmParser
482 * \brief Modifies the value of a given Header Entry (Dicom Element)
483 * if it exists; Creates it with the given value if it doesn't
484 * @param Value passed as a char*
485 * @param Group group of the Entry
486 * @param Elem element of the Entry
487 * \return pointer to the created Header Entry
488 * NULL if creation failed
491 gdcmHeaderEntry * gdcmParser::ReplaceOrCreateByNumber(
495 gdcmHeaderEntry* nvHeaderEntry=NewHeaderEntryByNumber(Group, Elem);
500 AddHeaderEntry(nvHeaderEntry);
502 std::string v = Value;
503 SetEntryByNumber(v, Group, Elem);
504 return(nvHeaderEntry);
508 * \ingroup gdcmParser
509 * \brief Set a new value if the invoked element exists
510 * Seems to be useless !!!
511 * @param Value new element value
512 * @param Group group of the Entry
513 * @param Elem element of the Entry
516 bool gdcmParser::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem )
518 std::string v = Value;
519 SetEntryByNumber(v, Group, Elem);
523 //-----------------------------------------------------------------------------
527 * \ingroup gdcmParser
528 * \brief Checks if a given Dicom Element exists
530 * @param group Group number of the searched Dicom Element
531 * @param element Element number of the searched Dicom Element
532 * @return number of occurences
534 int gdcmParser::CheckIfEntryExistByNumber(guint16 group, guint16 element ) {
535 std::string key = gdcmDictEntry::TranslateToKey(group, element );
536 return (tagHT.count(key));
540 * \ingroup gdcmParser
541 * \brief Searches within Header Entries (Dicom Elements) parsed with
542 * the public and private dictionaries
543 * for the element value of a given tag.
544 * \warning Don't use any longer : use GetPubEntryByName
545 * @param tagName name of the searched element.
546 * @return Corresponding element value when it exists,
547 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
549 std::string gdcmParser::GetEntryByName(std::string tagName) {
550 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
551 if( dictEntry == NULL)
554 return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
558 * \ingroup gdcmParser
559 * \brief Searches within Header Entries (Dicom Elements) parsed with
560 * the public and private dictionaries
561 * for the element value representation of a given tag.
563 * Obtaining the VR (Value Representation) might be needed by caller
564 * to convert the string typed content to caller's native type
565 * (think of C++ vs Python). The VR is actually of a higher level
566 * of semantics than just the native C++ type.
567 * @param tagName name of the searched element.
568 * @return Corresponding element value representation when it exists,
569 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
571 std::string gdcmParser::GetEntryVRByName(std::string tagName) {
572 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
573 if( dictEntry == NULL)
576 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(dictEntry->GetGroup(),
577 dictEntry->GetElement());
578 return elem->GetVR();
583 * \ingroup gdcmParser
584 * \brief Searches within Header Entries (Dicom Elements) parsed with
585 * the public and private dictionaries
586 * for the element value representation of a given tag.
587 * @param group Group of the searched tag.
588 * @param element Element of the searched tag.
589 * @return Corresponding element value representation when it exists,
590 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
592 std::string gdcmParser::GetEntryByNumber(guint16 group, guint16 element){
593 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
594 if ( ! tagHT.count(key))
596 return tagHT.find(key)->second->GetValue();
600 * \ingroup gdcmParser
601 * \brief Searches within Header Entries (Dicom Elements) parsed with
602 * the public and private dictionaries
603 * for the element value representation of a given tag..
605 * Obtaining the VR (Value Representation) might be needed by caller
606 * to convert the string typed content to caller's native type
607 * (think of C++ vs Python). The VR is actually of a higher level
608 * of semantics than just the native C++ type.
609 * @param group Group of the searched tag.
610 * @param element Element of the searched tag.
611 * @return Corresponding element value representation when it exists,
612 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
614 std::string gdcmParser::GetEntryVRByNumber(guint16 group, guint16 element) {
615 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
618 return elem->GetVR();
622 * \ingroup gdcmParser
623 * \brief Searches within Header Entries (Dicom Elements) parsed with
624 * the public and private dictionaries
625 * for the value length of a given tag..
626 * @param group Group of the searched tag.
627 * @param element Element of the searched tag.
628 * @return Corresponding element length; -2 if not found
630 int gdcmParser::GetEntryLengthByNumber(guint16 group, guint16 element) {
631 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
634 return elem->GetLength();
637 * \ingroup gdcmParser
638 * \brief Sets the value (string) of the Header Entry (Dicom Element)
639 * @param content string value of the Dicom Element
640 * @param tagName name of the searched Dicom Element.
641 * @return true when found
643 bool gdcmParser::SetEntryByName(std::string content,std::string tagName) {
644 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
645 if( dictEntry == NULL)
648 return(SetEntryByNumber(content,dictEntry->GetGroup(),
649 dictEntry->GetElement()));
653 * \ingroup gdcmParser
654 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
655 * through it's (group, element) and modifies it's content with
657 * \warning Don't use any longer : use SetPubEntryByNumber
658 * @param content new value to substitute with
659 * @param group group of the Dicom Element to modify
660 * @param element element of the Dicom Element to modify
662 bool gdcmParser::SetEntryByNumber(std::string content,
666 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
667 if ( ! tagHT.count(key))
669 int l = content.length();
670 if(l%2) // Non even length are padded with a space (020H).
673 content = content + '\0';
678 TagHeaderEntryHT::iterator p2;
679 // DO NOT remove the following lines : they explain the stuff
680 //p= tagHT.equal_range(key); // get a pair of iterators first-last synonym
681 //p2=p.first; // iterator on the first synonym
682 //a=p2->second; // H Table target column (2-nd col)
685 a = ((tagHT.equal_range(key)).first)->second;
687 a-> SetValue(content);
689 std::string vr = a->GetVR();
692 if( (vr == "US") || (vr == "SS") )
694 else if( (vr == "UL") || (vr == "SL") )
704 * \ingroup gdcmParser
705 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
706 * in the PubHeaderEntrySet of this instance
707 * through it's (group, element) and modifies it's length with
709 * \warning Use with extreme caution.
710 * @param l new length to substitute with
711 * @param group group of the Entry to modify
712 * @param element element of the Entry to modify
713 * @return true on success, false otherwise.
715 bool gdcmParser::SetEntryLengthByNumber(guint32 l,
719 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
720 if ( ! tagHT.count(key))
722 if (l%2) l++; // length must be even
723 ( ((tagHT.equal_range(key)).first)->second )->SetLength(l);
729 * \ingroup gdcmParser
730 * \brief Gets (from Header) the offset of a 'non string' element value
731 * (LoadElementValues has already be executed)
732 * @param Group group of the Entry
733 * @param Elem element of the Entry
734 * @return File Offset of the Element Value
736 size_t gdcmParser::GetEntryOffsetByNumber(guint16 Group, guint16 Elem)
738 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
741 dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
742 "failed to Locate gdcmHeaderEntry");
745 return Entry->GetOffset();
749 * \ingroup gdcmParser
750 * \brief Gets (from Header) a 'non string' element value
751 * (LoadElementValues has already be executed)
752 * @param Group group of the Entry
753 * @param Elem element of the Entry
754 * @return Pointer to the 'non string' area
756 void * gdcmParser::GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem)
758 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
761 dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
762 "failed to Locate gdcmHeaderEntry");
765 return Entry->GetVoidArea();
769 * \ingroup gdcmParser
770 * \brief Loads (from disk) the element content
771 * when a string is not suitable
772 * @param Group group of the Entry
773 * @param Elem element of the Entry
775 void *gdcmParser::LoadEntryVoidArea(guint16 Group, guint16 Elem)
777 gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
780 size_t o =(size_t)Element->GetOffset();
781 fseek(fp, o, SEEK_SET);
782 int l=Element->GetLength();
783 void * a = malloc(l);
787 SetEntryVoidAreaByNumber(a, Group, Elem);
788 // TODO check the result
789 size_t l2 = fread(a, 1, l ,fp);
800 * \ingroup gdcmParser
801 * \brief Sets a 'non string' value to a given Dicom Element
803 * @param group Group number of the searched Dicom Element
804 * @param element Element number of the searched Dicom Element
807 bool gdcmParser::SetEntryVoidAreaByNumber(void * area,
811 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
812 if ( ! tagHT.count(key))
814 ( ((tagHT.equal_range(key)).first)->second )->SetVoidArea(area);
819 * \ingroup gdcmParser
820 * \brief Update the entries with the shadow dictionary.
821 * Only non even entries are analyzed
823 void gdcmParser::UpdateShaEntries(void) {
824 gdcmDictEntry *entry;
827 for(ListTag::iterator it=listEntries.begin();
828 it!=listEntries.end();
831 // Odd group => from public dictionary
832 if((*it)->GetGroup()%2==0)
835 // Peer group => search the corresponding dict entry
837 entry=RefShaDict->GetDictEntryByNumber((*it)->GetGroup(),(*it)->GetElement());
841 if((*it)->IsImplicitVR())
846 (*it)->SetValue(GetHeaderEntryUnvalue(*it));
848 // Set the new entry and the new value
849 (*it)->SetDictEntry(entry);
850 CheckHeaderEntryVR(*it,vr);
852 (*it)->SetValue(GetHeaderEntryValue(*it));
856 // Remove precedent value transformation
857 (*it)->SetDictEntry(NewVirtualDictEntry((*it)->GetGroup(),(*it)->GetElement(),vr));
863 * \ingroup gdcmParser
864 * \brief Searches within the Header Entries for a Dicom Element of
866 * @param tagName name of the searched Dicom Element.
867 * @return Corresponding Dicom Element when it exists, and NULL
870 gdcmHeaderEntry *gdcmParser::GetHeaderEntryByName(std::string tagName) {
871 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
872 if( dictEntry == NULL)
875 return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
879 * \ingroup gdcmParser
880 * \brief retrieves a Dicom Element (the first one) using (group, element)
881 * \warning (group, element) IS NOT an identifier inside the Dicom Header
882 * if you think it's NOT UNIQUE, check the count number
883 * and use iterators to retrieve ALL the Dicoms Elements within
884 * a given couple (group, element)
885 * @param group Group number of the searched Dicom Element
886 * @param element Element number of the searched Dicom Element
889 gdcmHeaderEntry* gdcmParser::GetHeaderEntryByNumber(guint16 group, guint16 element)
891 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
892 if ( ! tagHT.count(key))
894 return tagHT.find(key)->second;
898 * \ingroup gdcmParser
899 * \brief retrieves the Dicom Elements (all of them) using (group, element)
900 * @param group Group number of the searched Dicom Element.
901 * @param element Element number of the searched Dicom Element.
902 * @return a range (i.e.pair<,>) containing all elements whose key is group|element)
905 IterHT gdcmParser::GetHeaderEntrySameNumber(guint16 group, guint16 element){
906 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
907 return (tagHT.equal_range(key));
911 * \ingroup gdcmParser
912 * \brief Loads the element while preserving the current
913 * underlying file position indicator as opposed to
914 * to LoadHeaderEntry that modifies it.
915 * @param entry Header Entry whose value shall be loaded.
918 void gdcmParser::LoadHeaderEntrySafe(gdcmHeaderEntry * entry) {
919 long PositionOnEntry = ftell(fp);
920 LoadHeaderEntry(entry);
921 fseek(fp, PositionOnEntry, SEEK_SET);
925 * \ingroup gdcmParser
926 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
927 * \warning : to be re-written using the chained list instead of the H table.
928 * \warning : DO NOT use (doesn't work any longer because of the multimap)
929 * \todo : to be re-written using the chained list instead of the H table
930 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
931 * @param type Type of the File (ExplicitVR,ImplicitVR, ACR, ...)
933 void gdcmParser::UpdateGroupLength(bool SkipSequence, FileType type) {
937 gdcmHeaderEntry *elem;
939 std::string str_trash;
942 GroupHT groupHt; // to hold the length of each group
945 // typedef std::map<GroupKey, int> GroupHT;
947 gdcmHeaderEntry *elemZ;
949 // for each Tag in the DCM Header
951 for (TagHeaderEntryHT::iterator tag2 = tagHT.begin();
956 gr = elem->GetGroup();
957 el = elem->GetElement();
960 sprintf(trash, "%04x", gr);
961 key = trash; // generate 'group tag'
963 // if the caller decided not to take SEQUENCEs into account
964 // e.g : he wants to write an ACR-NEMA File
966 if (SkipSequence && vr == "SQ")
969 // Still unsolved problem :
970 // we cannot find the 'Sequence Delimitation Item'
971 // since it's at the end of the Hash Table
974 // pas SEQUENCE en ACR-NEMA
976 // --> la descente a l'interieur' des SQ
977 // devrait etre faite avec une liste chainee, pas avec une HTable...
979 if ( groupHt.count(key) == 0) // we just read the first elem of a given group
981 if (el == 0x0000) // the first elem is 0x0000
983 groupHt[key] = 0; // initialize group length
987 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
990 else // any elem but the first
992 if (type == ExplicitVR)
994 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
996 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
999 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
1003 unsigned short int gr_bid;
1005 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
1009 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
1010 // warning: unsigned int format, different type arg
1011 sscanf(g->first.c_str(),"%x",&gr_bid);
1012 tk = g->first + "|0000"; // generate the element full tag
1014 if ( tagHT.count(tk) == 0) // if element 0x0000 not found
1016 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
1017 elemZ = new gdcmHeaderEntry(tagZ);
1018 elemZ->SetLength(4);
1019 AddHeaderEntry(elemZ); // create it
1023 elemZ=GetHeaderEntryByNumber(gr_bid, 0x0000);
1025 sprintf(trash ,"%d",g->second);
1027 elemZ->SetValue(str_trash);
1032 * \ingroup gdcmParser
1033 * \brief writes on disc according to the requested format
1034 * (ACR-NEMA, ExplicitVR, ImplicitVR) ONE
1036 * @param tag pointer on the gdcmHeaderEntry to be written
1037 * @param type type of the File to be written
1038 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1039 * @param _fp already open file pointer
1041 void gdcmParser::WriteEntry(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
1052 std::vector<std::string> tokens;
1056 // TODO (?) tester les echecs en ecriture (apres chaque fwrite)
1058 itsTimeToWritePixels = false;
1060 gr = tag->GetGroup();
1061 el = tag->GetElement();
1062 lgr = tag->GetReadLength();
1063 val = tag->GetValue().c_str();
1065 voidArea = tag->GetVoidArea();
1067 // === Deal with the length
1068 // --------------------
1069 if((tag->GetLength())%2==1)
1071 tag->SetValue(tag->GetValue()+"\0");
1072 tag->SetLength(tag->GetReadLength()+1);
1077 if (gr < 0x0008) return; // ignore pure DICOM V3 groups
1078 if (gr %2) return; // ignore shadow groups
1079 if (vr == "SQ" ) return; // ignore Sequences
1080 // TODO : find a trick to *skip* the SeQuences !
1081 // Not only ignore the SQ element
1082 if (gr == 0xfffe ) return; // ignore delimiters
1085 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
1086 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
1088 if ( (type == ExplicitVR) || (type == DICOMDIR) ) {
1090 guint16 z=0, shortLgr;
1092 if (gr == 0xfffe) { // NO Value Representation for 'delimiters'
1093 // no length : write ffffffff
1095 // special patch to make some MR PHILIPS
1096 if (el == 0x0000) return; // images e-film readable // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1097 // from Hospital Guy de Chauliac,
1099 // we just ignore spurious fffe|0000 tag !
1101 fwrite (&ff,(size_t)4 ,(size_t)1 ,_fp);
1102 return; // NO value for 'delimiters'
1106 if (vr == "unkn") { // Unknown was 'written'
1107 // deal with Little Endian
1108 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
1109 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
1111 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
1112 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ){
1113 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
1114 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
1116 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
1122 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
1125 // === Deal with the value
1126 // -------------------
1127 if (vr == "SQ") return; // no "value" to write for the SEQuences
1128 if (gr == 0xfffe)return; // no "value" to write for the delimiters
1130 if (voidArea != NULL)
1131 { // there is a 'non string' LUT, overlay, etc
1132 fwrite ( voidArea,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
1136 if (vr == "US" || vr == "SS")
1138 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1139 Tokenize (tag->GetValue(), tokens, "\\");
1140 for (unsigned int i=0; i<tokens.size();i++)
1142 val_uint16 = atoi(tokens[i].c_str());
1144 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
1149 if (vr == "UL" || vr == "SL")
1151 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1152 Tokenize (tag->GetValue(), tokens, "\\");
1153 for (unsigned int i=0; i<tokens.size();i++)
1155 val_uint32 = atoi(tokens[i].c_str());
1157 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
1163 // Pixels are never loaded in the element !
1164 // we stop writting when Pixel are processed
1165 // FIX : we loose trailing elements (RAB, right now)
1167 if ((gr == GrPixel) && (el == NumPixel) ) {
1169 if (compte == countGrPixel) {// we passed *all* the GrPixel,NumPixel
1170 itsTimeToWritePixels = true;
1174 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
1178 * \ingroup gdcmParser
1179 * \brief writes on disc according to the requested format
1180 * (ACR-NEMA, ExplicitVR, ImplicitVR) the image
1181 * using the Chained List
1182 * \warning does NOT add the missing elements in the header :
1183 * it's up to the user doing it !
1184 * (function CheckHeaderCoherence to be written)
1185 * \warning DON'T try, right now, to write a DICOM image
1186 * from an ACR Header (meta elements will be missing!)
1187 * \sa WriteEntriesDeprecated (Special temporary method for Theralys)
1188 * @param type type of the File to be written
1189 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1190 * @param _fp already open file pointer
1193 void gdcmParser::WriteEntries(FILE *_fp,FileType type)
1195 // TODO (?) tester les echecs en ecriture (apres chaque fwrite)
1197 for (ListTag::iterator tag2=listEntries.begin();
1198 tag2 != listEntries.end();
1201 WriteEntry(*tag2,_fp,type);
1202 if (itsTimeToWritePixels)
1208 * \ingroup gdcmParser
1209 * \brief writes on disc according to the requested format
1210 * (ACR-NEMA, ExplicitVR, ImplicitVR) the image,
1211 * using only the last synonym of each mutimap H Table post.
1212 * \warning Uses the H Table, instead of the Chained List
1213 * in order to be compliant with the old way to proceed
1214 * (added elements taken in to account)
1215 * Only THERALYS, during a transitory phase is supposed
1216 * to use this method !!!
1217 * \warning DON'T try, right now, to write a DICOM image
1218 * from an ACR Header (meta elements will be missing!)
1220 * @param _fp already open file pointer
1221 * @param type type of the File to be written
1222 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1224 void gdcmParser::WriteEntriesDeprecated(FILE *_fp,FileType type) {
1226 // restent a tester les echecs en ecriture (apres chaque fwrite)
1228 for (TagHeaderEntryHT::iterator tag2=tagHT.begin();
1229 tag2 != tagHT.end();
1231 WriteEntry(tag2->second,_fp,type);
1232 if (itsTimeToWritePixels)
1238 * \ingroup gdcmParser
1239 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1241 * @return The properly swaped 32 bits integer.
1243 guint32 gdcmParser::SwapLong(guint32 a) {
1248 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1249 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1253 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1257 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1260 dbg.Error(" gdcmParser::SwapLong : unset swap code");
1267 * \ingroup gdcmParser
1268 * \brief Unswaps back the bytes of 4-byte long integer accordingly to
1270 * @return The properly unswaped 32 bits integer.
1272 guint32 gdcmParser::UnswapLong(guint32 a) {
1273 return (SwapLong(a));
1277 * \ingroup gdcmParser
1278 * \brief Swaps the bytes so they agree with the processor order
1279 * @return The properly swaped 16 bits integer.
1281 guint16 gdcmParser::SwapShort(guint16 a) {
1282 if ( (sw==4321) || (sw==2143) )
1283 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1288 * \ingroup gdcmParser
1289 * \brief Unswaps the bytes so they agree with the processor order
1290 * @return The properly unswaped 16 bits integer.
1292 guint16 gdcmParser::UnswapShort(guint16 a) {
1293 return (SwapShort(a));
1296 //-----------------------------------------------------------------------------
1299 * \ingroup gdcmParser
1300 * \brief Parses the header of the file but WITHOUT loading element values.
1301 * @return false if file is not ACR-NEMA / DICOM
1303 bool gdcmParser::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1309 gdcmHeaderEntry *newHeaderEntry = (gdcmHeaderEntry *)0;
1310 while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1311 SkipHeaderEntry(newHeaderEntry);
1312 if ( (ignoreShadow==0) || (newHeaderEntry->GetGroup()%2) == 0) {
1313 AddHeaderEntry(newHeaderEntry);
1320 * \ingroup gdcmParser
1321 * \brief Loads the element values of all the Header Entries pointed in the
1322 * public Chained List.
1324 void gdcmParser::LoadHeaderEntries(void) {
1326 for (ListTag::iterator i = GetListEntry().begin();
1327 i != GetListEntry().end();
1330 LoadHeaderEntry(*i);
1335 // Load 'non string' values
1336 std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
1337 if( PhotometricInterpretation == "PALETTE COLOR " ) {
1338 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
1339 LoadEntryVoidArea(0x0028,0x1201); // R LUT
1340 LoadEntryVoidArea(0x0028,0x1202); // G LUT
1341 LoadEntryVoidArea(0x0028,0x1203); // B LUT
1343 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1344 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1345 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1347 //FIXME : how to use it?
1348 LoadEntryVoidArea(0x0028,0x3006); //LUT Data (CTX dependent)
1350 // --------------------------------------------------------------
1351 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1353 // if recognition code tells us we deal with a LibIDO image
1354 // we switch lineNumber and columnNumber
1356 std::string RecCode;
1357 RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
1358 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1359 RecCode == "CANRME_AILIBOD1_1." )
1361 filetype = ACR_LIBIDO;
1362 std::string rows = GetEntryByNumber(0x0028, 0x0010);
1363 std::string columns = GetEntryByNumber(0x0028, 0x0011);
1364 SetEntryByNumber(columns, 0x0028, 0x0010);
1365 SetEntryByNumber(rows , 0x0028, 0x0011);
1367 // ----------------- End of Special Patch ----------------
1371 * \ingroup gdcmParser
1372 * \brief Loads the element content if its length doesn't exceed
1373 * the value specified with gdcmParser::SetMaxSizeLoadEntry()
1374 * @param Entry Header Entry (Dicom Element) to be dealt with
1376 void gdcmParser::LoadHeaderEntry(gdcmHeaderEntry *Entry) {
1378 guint16 group = Entry->GetGroup();
1379 std::string vr= Entry->GetVR();
1380 guint32 length = Entry->GetLength();
1381 bool SkipLoad = false;
1383 fseek(fp, (long)Entry->GetOffset(), SEEK_SET);
1385 // the test was commented out to 'go inside' the SeQuences
1386 // we don't any longer skip them !
1388 // if( vr == "SQ" ) // (DO NOT remove this comment)
1391 // A SeQuence "contains" a set of Elements.
1392 // (fffe e000) tells us an Element is beginning
1393 // (fffe e00d) tells us an Element just ended
1394 // (fffe e0dd) tells us the current SeQuence just ended
1395 if( group == 0xfffe )
1399 Entry->SetLength(0);
1400 Entry->SetValue("gdcm::Skipped");
1404 // When the length is zero things are easy:
1405 if ( length == 0 ) {
1406 Entry->SetValue("");
1410 // The elements whose length is bigger than the specified upper bound
1411 // are not loaded. Instead we leave a short notice of the offset of
1412 // the element content and it's length.
1413 if (length > MaxSizeLoadEntry) {
1414 std::ostringstream s;
1415 s << "gdcm::NotLoaded.";
1416 s << " Address:" << (long)Entry->GetOffset();
1417 s << " Length:" << Entry->GetLength();
1418 s << " x(" << std::hex << Entry->GetLength() << ")";
1419 Entry->SetValue(s.str());
1423 // When integer(s) are expected, read and convert the following
1424 // n *(two or four bytes)
1425 // properly i.e. as integers as opposed to strings.
1426 // Elements with Value Multiplicity > 1
1427 // contain a set of integers (not a single one)
1429 // Any compacter code suggested (?)
1430 if ( IsHeaderEntryAnInteger(Entry) ) {
1432 std::ostringstream s;
1434 if (vr == "US" || vr == "SS") {
1436 NewInt = ReadInt16();
1439 for (int i=1; i < nbInt; i++) {
1441 NewInt = ReadInt16();
1446 else if (vr == "UL" || vr == "SL") {
1448 NewInt = ReadInt32();
1451 for (int i=1; i < nbInt; i++) {
1453 NewInt = ReadInt32();
1458 #ifdef GDCM_NO_ANSI_STRING_STREAM
1459 s << std::ends; // to avoid oddities on Solaris
1460 #endif //GDCM_NO_ANSI_STRING_STREAM
1462 Entry->SetValue(s.str());
1466 // We need an additional byte for storing \0 that is not on disk
1467 std::string NewValue(length,0);
1468 item_read = fread(&(NewValue[0]), (size_t)length, (size_t)1, fp);
1469 if ( item_read != 1 ) {
1470 dbg.Verbose(1, "gdcmParser::LoadElementValue","unread element value");
1471 Entry->SetValue("gdcm::UnRead");
1475 if( (vr == "UI") ) // Because of correspondance with the VR dic
1476 Entry->SetValue(NewValue.c_str()); // ??? JPR ???
1478 Entry->SetValue(NewValue);
1482 * \ingroup gdcmParser
1483 * \brief add a new Dicom Element pointer to
1484 * the H Table and at the end of the chained List
1485 * \warning push_bash in listEntries ONLY during ParseHeader
1486 * \todo something to allow further Elements addition,
1487 * (at their right place in the chained list)
1488 * when position to be taken care of
1489 * @param newHeaderEntry
1491 void gdcmParser::AddHeaderEntry(gdcmHeaderEntry *newHeaderEntry) {
1492 tagHT.insert( PairHT( newHeaderEntry->GetKey(),newHeaderEntry) );
1493 listEntries.push_back(newHeaderEntry);
1498 * \ingroup gdcmParser
1499 * \brief Find the value Length of the passed Header Entry
1500 * @param Entry Header Entry whose length of the value shall be loaded.
1502 void gdcmParser::FindHeaderEntryLength (gdcmHeaderEntry *Entry) {
1503 guint16 element = Entry->GetElement();
1504 guint16 group = Entry->GetGroup();
1505 std::string vr = Entry->GetVR();
1508 if( (element == NumPixel) && (group == GrPixel) )
1510 dbg.SetDebug(GDCM_DEBUG);
1511 dbg.Verbose(2, "gdcmParser::FindLength: ",
1512 "we reached (GrPixel,NumPixel)");
1515 if ( (filetype == ExplicitVR) && (! Entry->IsImplicitVR()) )
1517 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") )
1519 // The following reserved two bytes (see PS 3.5-2001, section
1520 // 7.1.2 Data element structure with explicit vr p27) must be
1521 // skipped before proceeding on reading the length on 4 bytes.
1522 fseek(fp, 2L, SEEK_CUR);
1523 guint32 length32 = ReadInt32();
1525 if ( (vr == "OB") && (length32 == 0xffffffff) )
1527 Entry->SetLength(FindHeaderEntryLengthOB());
1530 FixHeaderEntryFoundLength(Entry, length32);
1534 // Length is encoded on 2 bytes.
1535 length16 = ReadInt16();
1537 // We can tell the current file is encoded in big endian (like
1538 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1539 // and it's value is the one of the encoding of a big endian file.
1540 // In order to deal with such big endian encoded files, we have
1541 // (at least) two strategies:
1542 // * when we load the "Transfer Syntax" tag with value of big endian
1543 // encoding, we raise the proper flags. Then we wait for the end
1544 // of the META group (0x0002) among which is "Transfer Syntax",
1545 // before switching the swap code to big endian. We have to postpone
1546 // the switching of the swap code since the META group is fully encoded
1547 // in little endian, and big endian coding only starts at the next
1548 // group. The corresponding code can be hard to analyse and adds
1549 // many additional unnecessary tests for regular tags.
1550 // * the second strategy consists in waiting for trouble, that shall
1551 // appear when we find the first group with big endian encoding. This
1552 // is easy to detect since the length of a "Group Length" tag (the
1553 // ones with zero as element number) has to be of 4 (0x0004). When we
1554 // encounter 1024 (0x0400) chances are the encoding changed and we
1555 // found a group with big endian encoding.
1556 // We shall use this second strategy. In order to make sure that we
1557 // can interpret the presence of an apparently big endian encoded
1558 // length of a "Group Length" without committing a big mistake, we
1559 // add an additional check: we look in the already parsed elements
1560 // for the presence of a "Transfer Syntax" whose value has to be "big
1561 // endian encoding". When this is the case, chances are we have got our
1562 // hands on a big endian encoded file: we switch the swap code to
1563 // big endian and proceed...
1564 if ( (element == 0x0000) && (length16 == 0x0400) )
1566 if ( ! IsExplicitVRBigEndianTransferSyntax() )
1568 dbg.Verbose(0, "gdcmParser::FindLength", "not explicit VR");
1573 SwitchSwapToBigEndian();
1574 // Restore the unproperly loaded values i.e. the group, the element
1575 // and the dictionary entry depending on them.
1576 guint16 CorrectGroup = SwapShort(Entry->GetGroup());
1577 guint16 CorrectElem = SwapShort(Entry->GetElement());
1578 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
1582 // This correct tag is not in the dictionary. Create a new one.
1583 NewTag = NewVirtualDictEntry(CorrectGroup, CorrectElem);
1585 // FIXME this can create a memory leaks on the old entry that be
1586 // left unreferenced.
1587 Entry->SetDictEntry(NewTag);
1590 // Heuristic: well some files are really ill-formed.
1591 if ( length16 == 0xffff)
1594 //dbg.Verbose(0, "gdcmParser::FindLength",
1595 // "Erroneous element length fixed.");
1596 // Actually, length= 0xffff means that we deal with
1597 // Unknown Sequence Length
1599 FixHeaderEntryFoundLength(Entry, (guint32)length16);
1604 // Either implicit VR or a non DICOM conformal (see note below) explicit
1605 // VR that ommited the VR of (at least) this element. Farts happen.
1606 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1607 // on Data elements "Implicit and Explicit VR Data Elements shall
1608 // not coexist in a Data Set and Data Sets nested within it".]
1609 // Length is on 4 bytes.
1611 FixHeaderEntryFoundLength(Entry, ReadInt32());
1617 * \ingroup gdcmParser
1618 * \brief Find the Value Representation of the current Dicom Element.
1621 void gdcmParser::FindHeaderEntryVR( gdcmHeaderEntry *Entry)
1623 if (filetype != ExplicitVR)
1628 long PositionOnEntry = ftell(fp);
1629 // Warning: we believe this is explicit VR (Value Representation) because
1630 // we used a heuristic that found "UL" in the first tag. Alas this
1631 // doesn't guarantee that all the tags will be in explicit VR. In some
1632 // cases (see e-film filtered files) one finds implicit VR tags mixed
1633 // within an explicit VR file. Hence we make sure the present tag
1634 // is in explicit VR and try to fix things if it happens not to be
1637 int lgrLue=fread (&VR, (size_t)2,(size_t)1, fp); // lgrLue not used
1639 if(!CheckHeaderEntryVR(Entry,VR))
1641 fseek(fp, PositionOnEntry, SEEK_SET);
1642 // When this element is known in the dictionary we shall use, e.g. for
1643 // the semantics (see the usage of IsAnInteger), the VR proposed by the
1644 // dictionary entry. Still we have to flag the element as implicit since
1645 // we know now our assumption on expliciteness is not furfilled.
1647 if ( Entry->IsVRUnknown() )
1648 Entry->SetVR("Implicit");
1649 Entry->SetImplicitVR();
1654 * \ingroup gdcmParser
1655 * \brief Check the correspondance between the VR of the header entry
1656 * and the taken VR. If they are different, the header entry is
1657 * updated with the new VR.
1658 * @param Entry Header Entry to check
1659 * @param vr Dicom Value Representation
1660 * @return false if the VR is incorrect of if the VR isn't referenced
1661 * otherwise, it returns true
1663 bool gdcmParser::CheckHeaderEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
1665 char msg[100]; // for sprintf
1666 bool RealExplicit = true;
1668 // Assume we are reading a falsely explicit VR file i.e. we reached
1669 // a tag where we expect reading a VR but are in fact we read the
1670 // first to bytes of the length. Then we will interogate (through find)
1671 // the dicom_vr dictionary with oddities like "\004\0" which crashes
1672 // both GCC and VC++ implementations of the STL map. Hence when the
1673 // expected VR read happens to be non-ascii characters we consider
1674 // we hit falsely explicit VR tag.
1676 if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
1677 RealExplicit = false;
1679 // CLEANME searching the dicom_vr at each occurence is expensive.
1680 // PostPone this test in an optional integrity check at the end
1681 // of parsing or only in debug mode.
1682 if ( RealExplicit && !gdcmGlobal::GetVR()->Count(vr) )
1683 RealExplicit= false;
1685 if ( !RealExplicit )
1687 // We thought this was explicit VR, but we end up with an
1688 // implicit VR tag. Let's backtrack.
1689 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
1690 Entry->GetGroup(),Entry->GetElement());
1691 dbg.Verbose(1, "gdcmParser::FindVR: ",msg);
1692 if (Entry->GetGroup()%2 && Entry->GetElement() == 0x0000) { // Group length is UL !
1693 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1694 Entry->GetGroup(),Entry->GetElement(),
1695 "UL","FIXME","Group Length");
1696 Entry->SetDictEntry(NewEntry);
1701 if ( Entry->IsVRUnknown() )
1703 // When not a dictionary entry, we can safely overwrite the VR.
1704 if (Entry->GetElement() == 0x0000) { // Group length is UL !
1710 else if ( Entry->GetVR() != vr )
1712 // The VR present in the file and the dictionary disagree. We assume
1713 // the file writer knew best and use the VR of the file. Since it would
1714 // be unwise to overwrite the VR of a dictionary (since it would
1715 // compromise it's next user), we need to clone the actual DictEntry
1716 // and change the VR for the read one.
1717 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1718 Entry->GetGroup(),Entry->GetElement(),
1719 vr,"FIXME",Entry->GetName());
1720 Entry->SetDictEntry(NewEntry);
1726 * \ingroup gdcmParser
1727 * \brief Get the transformed value of the header entry. The VR value
1728 * is used to define the transformation to operate on the value
1729 * \warning NOT end user intended method !
1731 * @return Transformed entry value
1733 std::string gdcmParser::GetHeaderEntryValue(gdcmHeaderEntry *Entry)
1735 if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1737 std::string val=Entry->GetValue();
1738 std::string vr=Entry->GetVR();
1739 guint32 length = Entry->GetLength();
1740 std::ostringstream s;
1743 if (vr == "US" || vr == "SS")
1748 for (int i=0; i < nbInt; i++)
1752 NewInt16 = (val[2*i+0]&0xFF)+((val[2*i+1]&0xFF)<<8);
1753 NewInt16 = SwapShort(NewInt16);
1758 else if (vr == "UL" || vr == "SL")
1763 for (int i=0; i < nbInt; i++)
1767 NewInt32= (val[4*i+0]&0xFF)+((val[4*i+1]&0xFF)<<8)+
1768 ((val[4*i+2]&0xFF)<<16)+((val[4*i+3]&0xFF)<<24);
1769 NewInt32=SwapLong(NewInt32);
1773 #ifdef GDCM_NO_ANSI_STRING_STREAM
1774 s << std::ends; // to avoid oddities on Solaris
1775 #endif //GDCM_NO_ANSI_STRING_STREAM
1779 return(Entry->GetValue());
1783 * \ingroup gdcmParser
1784 * \brief Get the reverse transformed value of the header entry. The VR
1785 * value is used to define the reverse transformation to operate on
1787 * \warning NOT end user intended method !
1789 * @return Reverse transformed entry value
1791 std::string gdcmParser::GetHeaderEntryUnvalue(gdcmHeaderEntry *Entry)
1793 if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1795 std::string vr=Entry->GetVR();
1796 std::ostringstream s;
1797 std::vector<std::string> tokens;
1799 if (vr == "US" || vr == "SS")
1803 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1804 Tokenize (Entry->GetValue(), tokens, "\\");
1805 for (unsigned int i=0; i<tokens.size();i++)
1807 NewInt16 = atoi(tokens[i].c_str());
1808 s<<(NewInt16&0xFF)<<((NewInt16>>8)&0xFF);
1812 if (vr == "UL" || vr == "SL")
1816 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1817 Tokenize (Entry->GetValue(), tokens, "\\");
1818 for (unsigned int i=0; i<tokens.size();i++)
1820 NewInt32 = atoi(tokens[i].c_str());
1821 s<<(char)(NewInt32&0xFF)<<(char)((NewInt32>>8)&0xFF)
1822 <<(char)((NewInt32>>16)&0xFF)<<(char)((NewInt32>>24)&0xFF);
1827 #ifdef GDCM_NO_ANSI_STRING_STREAM
1828 s << std::ends; // to avoid oddities on Solaris
1829 #endif //GDCM_NO_ANSI_STRING_STREAM
1833 return(Entry->GetValue());
1837 * \ingroup gdcmParser
1838 * \brief Skip a given Header Entry
1839 * \warning NOT end user intended method !
1842 void gdcmParser::SkipHeaderEntry(gdcmHeaderEntry *entry)
1844 SkipBytes(entry->GetLength());
1848 * \ingroup gdcmParser
1849 * \brief When the length of an element value is obviously wrong (because
1850 * the parser went Jabberwocky) one can hope improving things by
1851 * applying this heuristic.
1853 void gdcmParser::FixHeaderEntryFoundLength(gdcmHeaderEntry *Entry, guint32 FoundLength)
1855 Entry->SetReadLength(FoundLength); // will be updated only if a bug is found
1857 if ( FoundLength == 0xffffffff) {
1861 guint16 gr =Entry->GetGroup();
1862 guint16 el =Entry->GetElement();
1864 if (FoundLength%2) {
1865 std::ostringstream s;
1866 s << "Warning : Tag with uneven length " << FoundLength
1867 << " in x(" << std::hex << gr << "," << el <<")" << std::dec;
1868 dbg.Verbose(0,s.str().c_str());
1871 // Sorry for the patch!
1872 // XMedCom did the trick to read some nasty GE images ...
1873 if (FoundLength == 13) {
1874 // The following 'if' will be removed when there is no more
1875 // images on Creatis HDs with a 13 length for Manufacturer...
1876 if ( (Entry->GetGroup() != 0x0008) ||
1877 ( (Entry->GetElement() != 0x0070) && (Entry->GetElement() != 0x0080) ) ){
1878 // end of remove area
1880 Entry->SetReadLength(10); // a bug is to be fixed
1884 // to fix some garbage 'Leonardo' Siemens images
1885 // May be commented out to avoid overhead
1886 else if ( (Entry->GetGroup() == 0x0009) &&
1887 ( (Entry->GetElement() == 0x1113) || (Entry->GetElement() == 0x1114) ) ){
1889 Entry->SetReadLength(4); // a bug is to be fixed
1893 // to try to 'go inside' SeQuences (with length), and not to skip them
1894 else if ( Entry->GetVR() == "SQ")
1896 if (enableSequences) // only if the user does want to !
1897 FoundLength =0; // ReadLength is unchanged
1900 // a SeQuence Element is beginning
1901 // fffe|e000 is just a marker, its length *should be* zero
1902 else if(Entry->GetGroup() == 0xfffe)
1904 // *normally, fffe|0000 doesn't exist !
1905 if( Entry->GetElement() != 0x0000 ) // gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1906 // causes extra troubles :-(
1909 Entry->SetUsableLength(FoundLength);
1913 * \ingroup gdcmParser
1914 * \brief Apply some heuristics to predict whether the considered
1915 * element value contains/represents an integer or not.
1916 * @param Entry The element value on which to apply the predicate.
1917 * @return The result of the heuristical predicate.
1919 bool gdcmParser::IsHeaderEntryAnInteger(gdcmHeaderEntry *Entry) {
1920 guint16 element = Entry->GetElement();
1921 guint16 group = Entry->GetGroup();
1922 std::string vr = Entry->GetVR();
1923 guint32 length = Entry->GetLength();
1924 // When we have some semantics on the element we just read, and if we
1925 // a priori know we are dealing with an integer, then we shall be
1926 // able to swap it's element value properly.
1927 if ( element == 0 ) // This is the group length of the group
1933 std::ostringstream s;
1934 int filePosition = ftell(fp);
1935 s << "Erroneous Group Length element length on : (" \
1936 << std::hex << group << " , " << element
1937 << ") -before- position x(" << filePosition << ")"
1938 << "lgt : " << length;
1939 // These 2 lines commented out : a *very dirty* patch
1940 // to go on PrintHeader'ing gdcm-MR-PHILIPS-16-Multi-Seq.dcm.
1941 // have a glance at offset x(8336) ...
1942 // For *regular* headers, the test is useless..
1943 // lets's print a warning message and go on,
1944 // instead of giving up with an error message
1946 //std::cout << s.str().c_str() << std::endl;
1948 // dbg.Error("gdcmParser::IsHeaderEntryAnInteger",
1949 // s.str().c_str());
1952 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1958 * \ingroup gdcmParser
1959 * \brief Find the Length till the next sequence delimiter
1960 * \warning NOT end user intended method !
1964 guint32 gdcmParser::FindHeaderEntryLengthOB(void) {
1965 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1968 long PositionOnEntry = ftell(fp);
1969 bool FoundSequenceDelimiter = false;
1970 guint32 TotalLength = 0;
1973 while ( ! FoundSequenceDelimiter)
1979 TotalLength += 4; // We even have to decount the group and element
1981 if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */
1983 char msg[100]; // for sprintf. Sorry
1984 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
1985 dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
1989 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
1990 FoundSequenceDelimiter = true;
1991 else if ( n != 0xe000 )
1993 char msg[100]; // for sprintf. Sorry
1994 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
1996 dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
2000 ItemLength = ReadInt32();
2001 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
2002 // the ItemLength with ReadInt32
2003 SkipBytes(ItemLength);
2005 fseek(fp, PositionOnEntry, SEEK_SET);
2010 * \ingroup gdcmParser
2011 * \brief Reads a supposed to be 16 Bits integer
2012 * (swaps it depending on processor endianity)
2013 * @return read value
2015 guint16 gdcmParser::ReadInt16(void) {
2018 item_read = fread (&g, (size_t)2,(size_t)1, fp);
2019 if ( item_read != 1 ) {
2021 dbg.Verbose(0, "gdcmParser::ReadInt16", " File Error");
2031 * \ingroup gdcmParser
2032 * \brief Reads a supposed to be 32 Bits integer
2033 * (swaps it depending on processor endianity)
2034 * @return read value
2036 guint32 gdcmParser::ReadInt32(void) {
2039 item_read = fread (&g, (size_t)4,(size_t)1, fp);
2040 if ( item_read != 1 ) {
2042 dbg.Verbose(0, "gdcmParser::ReadInt32", " File Error");
2052 * \ingroup gdcmParser
2053 * \brief skips bytes inside the source file
2054 * \warning NOT end user intended method !
2057 void gdcmParser::SkipBytes(guint32 NBytes) {
2058 //FIXME don't dump the returned value
2059 (void)fseek(fp, (long)NBytes, SEEK_CUR);
2063 * \ingroup gdcmParser
2064 * \brief Loads all the needed Dictionaries
2065 * \warning NOT end user intended method !
2067 void gdcmParser::Initialise(void)
2069 RefPubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
2070 RefShaDict = (gdcmDict*)0;
2074 * \ingroup gdcmParser
2075 * \brief Discover what the swap code is (among little endian, big endian,
2076 * bad little endian, bad big endian).
2078 * @return false when we are absolutely sure
2079 * it's neither ACR-NEMA nor DICOM
2080 * true when we hope ours assuptions are OK
2082 bool gdcmParser::CheckSwap() {
2084 // The only guaranted way of finding the swap code is to find a
2085 // group tag since we know it's length has to be of four bytes i.e.
2086 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2087 // occurs when we can't find such group...
2089 guint32 x=4; // x : for ntohs
2090 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2096 char deb[HEADER_LENGTH_TO_READ];
2098 // First, compare HostByteOrder and NetworkByteOrder in order to
2099 // determine if we shall need to swap bytes (i.e. the Endian type).
2105 // The easiest case is the one of a DICOM header, since it possesses a
2106 // file preamble where it suffice to look for the string "DICM".
2107 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2110 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2111 dbg.Verbose(1, "gdcmParser::CheckSwap:", "looks like DICOM Version3");
2113 // Next, determine the value representation (VR). Let's skip to the
2114 // first element (0002, 0000) and check there if we find "UL"
2115 // - or "OB" if the 1st one is (0002,0001) -,
2116 // in which case we (almost) know it is explicit VR.
2117 // WARNING: if it happens to be implicit VR then what we will read
2118 // is the length of the group. If this ascii representation of this
2119 // length happens to be "UL" then we shall believe it is explicit VR.
2120 // FIXME: in order to fix the above warning, we could read the next
2121 // element value (or a couple of elements values) in order to make
2122 // sure we are not commiting a big mistake.
2123 // We need to skip :
2124 // * the 128 bytes of File Preamble (often padded with zeroes),
2125 // * the 4 bytes of "DICM" string,
2126 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2127 // i.e. a total of 136 bytes.
2131 // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2132 // but elem 0002,0010 (Transfert Syntax) tells us the file is *Implicit* VR.
2135 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2136 (memcmp(entCur, "OB", (size_t)2) == 0) ||
2137 (memcmp(entCur, "UI", (size_t)2) == 0) ||
2138 (memcmp(entCur, "CS", (size_t)2) == 0) ) // CS, to remove later
2139 // when Write DCM *adds*
2141 // Use gdcmParser::dicom_vr to test all the possibilities
2142 // instead of just checking for UL, OB and UI !? // group 0000
2145 filetype = ExplicitVR;
2146 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2147 "explicit Value Representation");
2151 filetype = ImplicitVR;
2152 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2153 "not an explicit Value Representation");
2159 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2160 "HostByteOrder != NetworkByteOrder");
2165 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2166 "HostByteOrder = NetworkByteOrder");
2169 // Position the file position indicator at first tag (i.e.
2170 // after the file preamble and the "DICM" string).
2172 fseek (fp, 132L, SEEK_SET);
2176 // Alas, this is not a DicomV3 file and whatever happens there is no file
2177 // preamble. We can reset the file position indicator to where the data
2178 // is (i.e. the beginning of the file).
2179 dbg.Verbose(1, "gdcmParser::CheckSwap:", "not a DICOM Version3 file");
2182 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2183 // By clean we mean that the length of the first tag is written down.
2184 // If this is the case and since the length of the first group HAS to be
2185 // four (bytes), then determining the proper swap code is straightforward.
2188 // We assume the array of char we are considering contains the binary
2189 // representation of a 32 bits integer. Hence the following dirty
2191 s32 = *((guint32 *)(entCur));
2212 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2213 // It is time for despaired wild guesses.
2214 // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2215 // i.e. the 'group length' element is not present :
2217 // check the supposed to be 'group number'
2218 // 0x0002 or 0x0004 or 0x0008
2219 // to determine ' sw' value .
2220 // Only 0 or 4321 will be possible
2221 // (no oportunity to check for the formerly well known
2222 // ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian'
2223 // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -4, 8-)
2224 // the file IS NOT ACR-NEMA nor DICOM V3
2225 // Find a trick to tell it the caller...
2227 s16 = *((guint16 *)(deb));
2243 dbg.Verbose(0, "gdcmParser::CheckSwap:",
2244 "ACR/NEMA unfound swap info (Really hopeless !)");
2249 // Then the only info we have is the net2host one.
2259 * \ingroup gdcmParser
2260 * \brief Restore the unproperly loaded values i.e. the group, the element
2261 * and the dictionary entry depending on them.
2263 void gdcmParser::SwitchSwapToBigEndian(void)
2265 dbg.Verbose(1, "gdcmParser::SwitchSwapToBigEndian",
2266 "Switching to BigEndian mode.");
2287 * \ingroup gdcmParser
2288 * \brief during parsing, Header Elements too long are not loaded in memory
2291 void gdcmParser::SetMaxSizeLoadEntry(long NewSize)
2295 if ((guint32)NewSize >= (guint32)0xffffffff)
2297 MaxSizeLoadEntry = 0xffffffff;
2300 MaxSizeLoadEntry = NewSize;
2305 * \ingroup gdcmParser
2306 * \brief Header Elements too long will not be printed
2308 * \todo : not yet usable
2309 * (see MAX_SIZE_PRINT_ELEMENT_VALUE
2310 * in gdcmHeaderEntry gdcmLoadEntry)
2314 void gdcmParser::SetMaxSizePrintEntry(long NewSize)
2318 if ((guint32)NewSize >= (guint32)0xffffffff)
2320 MaxSizePrintEntry = 0xffffffff;
2323 MaxSizePrintEntry = NewSize;
2327 * \ingroup gdcmParser
2328 * \brief Searches both the public and the shadow dictionary (when they
2329 * exist) for the presence of the DictEntry with given name.
2330 * The public dictionary has precedence on the shadow one.
2331 * @param Name name of the searched DictEntry
2332 * @return Corresponding DictEntry when it exists, NULL otherwise.
2334 gdcmDictEntry *gdcmParser::GetDictEntryByName(std::string Name)
2336 gdcmDictEntry *found = (gdcmDictEntry *)0;
2337 if (!RefPubDict && !RefShaDict)
2339 dbg.Verbose(0, "gdcmParser::GetDictEntry",
2340 "we SHOULD have a default dictionary");
2344 found = RefPubDict->GetDictEntryByName(Name);
2350 found = RefShaDict->GetDictEntryByName(Name);
2358 * \ingroup gdcmParser
2359 * \brief Searches both the public and the shadow dictionary (when they
2360 * exist) for the presence of the DictEntry with given
2361 * group and element. The public dictionary has precedence on the
2363 * @param group group of the searched DictEntry
2364 * @param element element of the searched DictEntry
2365 * @return Corresponding DictEntry when it exists, NULL otherwise.
2367 gdcmDictEntry *gdcmParser::GetDictEntryByNumber(guint16 group,guint16 element)
2369 gdcmDictEntry *found = (gdcmDictEntry *)0;
2370 if (!RefPubDict && !RefShaDict)
2372 dbg.Verbose(0, "gdcmParser::GetDictEntry",
2373 "we SHOULD have a default dictionary");
2377 found = RefPubDict->GetDictEntryByNumber(group, element);
2383 found = RefShaDict->GetDictEntryByNumber(group, element);
2391 * \ingroup gdcmParser
2392 * \brief Read the next tag but WITHOUT loading it's value
2393 * @return On succes the newly created HeaderEntry, NULL on failure.
2395 gdcmHeaderEntry *gdcmParser::ReadNextHeaderEntry(void) {
2397 gdcmHeaderEntry *NewEntry;
2402 // We reached the EOF (or an error occured) therefore
2403 // header parsing has to be considered as finished.
2404 return (gdcmHeaderEntry *)0;
2406 /* Pb : how to propagate the element length (used in SkipHeaderEntry)
2407 // direct call to SkipBytes ?
2409 if (ignoreShadow == 1 && g%2 ==1) //JPR
2410 // if user wants to skip shadow groups
2411 // and current element *is* a shadow element
2412 // we don't create anything
2413 return (gdcmHeaderEntry *)1; // to tell caller it's NOT finished
2415 NewEntry = NewHeaderEntryByNumber(g, n);
2416 FindHeaderEntryVR(NewEntry);
2417 FindHeaderEntryLength(NewEntry);
2423 NewEntry->SetOffset(ftell(fp));
2428 * \ingroup gdcmParser
2429 * \brief Build a new Element Value from all the low level arguments.
2430 * Check for existence of dictionary entry, and build
2431 * a default one when absent.
2432 * @param Name Name of the underlying DictEntry
2434 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByName(std::string Name)
2436 gdcmDictEntry *NewTag = GetDictEntryByName(Name);
2438 NewTag = NewVirtualDictEntry(0xffff, 0xffff, "LO", "unkn", Name);
2440 gdcmHeaderEntry* NewEntry = new gdcmHeaderEntry(NewTag);
2443 dbg.Verbose(1, "gdcmParser::ObtainHeaderEntryByName",
2444 "failed to allocate gdcmHeaderEntry");
2445 return (gdcmHeaderEntry *)0;
2451 * \ingroup gdcmParser
2452 * \brief Request a new virtual dict entry to the dict set
2453 * @param group group of the underlying DictEntry
2454 * @param element element of the underlying DictEntry
2455 * @param vr VR of the underlying DictEntry
2456 * @param fourth owner group
2457 * @param name english name
2459 gdcmDictEntry *gdcmParser::NewVirtualDictEntry(guint16 group, guint16 element,
2464 return gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,element,vr,fourth,name);
2468 * \ingroup gdcmParser
2469 * \brief Build a new Element Value from all the low level arguments.
2470 * Check for existence of dictionary entry, and build
2471 * a default one when absent.
2472 * @param Group group of the underlying DictEntry
2473 * @param Elem element of the underlying DictEntry
2475 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByNumber(guint16 Group, guint16 Elem)
2477 // Find out if the tag we encountered is in the dictionaries:
2478 gdcmDictEntry *DictEntry = GetDictEntryByNumber(Group, Elem);
2480 DictEntry = NewVirtualDictEntry(Group, Elem);
2482 gdcmHeaderEntry *NewEntry = new gdcmHeaderEntry(DictEntry);
2485 dbg.Verbose(1, "gdcmParser::NewHeaderEntryByNumber",
2486 "failed to allocate gdcmHeaderEntry");
2492 // Never used; commented out, waiting for removal.
2494 * \ingroup gdcmParser
2495 * \brief Small utility function that creates a new manually crafted
2496 * (as opposed as read from the file) gdcmHeaderEntry with user
2497 * specified name and adds it to the public tag hash table.
2498 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
2499 * @param NewTagName The name to be given to this new tag.
2500 * @param VR The Value Representation to be given to this new tag.
2501 * @return The newly hand crafted Element Value.
2503 //gdcmHeaderEntry *gdcmParser::NewManualHeaderEntryToPubDict(std::string NewTagName,
2506 // gdcmHeaderEntry *NewEntry = NULL;
2507 // guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
2508 // guint32 FreeElem = 0;
2509 // gdcmDictEntry *DictEntry = NULL;
2511 // FreeElem = GenerateFreeTagKeyInGroup(StuffGroup);
2512 // if (FreeElem == UINT32_MAX)
2514 // dbg.Verbose(1, "gdcmHeader::NewManualHeaderEntryToPubDict",
2515 // "Group 0xffff in Public Dict is full");
2519 // DictEntry = NewVirtualDictEntry(StuffGroup, FreeElem,
2520 // VR, "GDCM", NewTagName);
2521 // NewEntry = new gdcmHeaderEntry(DictEntry);
2522 // AddHeaderEntry(NewEntry);
2527 * \ingroup gdcmParser
2528 * \brief Generate a free TagKey i.e. a TagKey that is not present
2529 * in the TagHt dictionary.
2530 * @param group The generated tag must belong to this group.
2531 * @return The element of tag with given group which is fee.
2533 guint32 gdcmParser::GenerateFreeTagKeyInGroup(guint16 group)
2535 for (guint32 elem = 0; elem < UINT32_MAX; elem++)
2537 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
2538 if (tagHT.count(key) == 0)
2544 //-----------------------------------------------------------------------------