2 //-----------------------------------------------------------------------------
10 #include <netinet/in.h>
13 #ifdef GDCM_NO_ANSI_STRING_STREAM
15 # define ostringstream ostrstream
21 #include "gdcmParser.h"
22 #include "gdcmGlobal.h"
24 #include "gdcmDebug.h"
26 #define UI1_2_840_10008_1_2 "1.2.840.10008.1.2"
27 #define UI1_2_840_10008_1_2_1 "1.2.840.10008.1.2.1"
28 #define UI1_2_840_10008_1_2_2 "1.2.840.10008.1.2.2"
29 #define UI1_2_840_10008_1_2_1_99 "1.2.840.10008.1.2.1.99"
31 //-----------------------------------------------------------------------------
32 // Refer to gdcmParser::CheckSwap()
33 const unsigned int gdcmParser::HEADER_LENGTH_TO_READ = 256;
35 // Refer to gdcmParser::SetMaxSizeLoadEntry()
36 const unsigned int gdcmParser::MAX_SIZE_LOAD_ELEMENT_VALUE = 4096;
38 // Refer to gdcmParser::SetMaxSizePrintEntry()
39 // TODO : Right now, better see "define, in gdcmHederEntry.cxx
40 const unsigned int gdcmParser::MAX_SIZE_PRINT_ELEMENT_VALUE = 64;
42 //-----------------------------------------------------------------------------
43 // Constructor / Destructor
48 * @param exception_on_error whether we throw an exception or not
49 * @param enable_sequences = true to allow the header
50 * to be parsed *inside* the SeQuences,
51 * when they have an actual length
52 * @param ignore_shadow to allow skipping the shadow elements,
53 * to save memory space.
54 * \warning The TRUE value for this param has to be used
55 * with a FALSE value for the 'enable_sequence' param.
56 * ('public elements' may be embedded in 'shadow Sequences')
58 gdcmParser::gdcmParser(const char *inFilename,
59 bool exception_on_error,
60 bool enable_sequences,
62 enableSequences=enable_sequences;
63 ignoreShadow =ignore_shadow;
65 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
66 filename = inFilename;
69 if ( !OpenFile(exception_on_error))
76 wasUpdated = 0; // will be set to 1 if user adds an entry
77 printLevel = 1; // 'Medium' print level by default
83 * @param exception_on_error
85 gdcmParser::gdcmParser(bool exception_on_error) {
88 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
91 wasUpdated = 0; // will be set to 1 if user adds an entry
92 printLevel = 1; // 'Medium' print level by default
97 * \brief Canonical destructor.
99 gdcmParser::~gdcmParser (void) {
104 //-----------------------------------------------------------------------------
107 * \ingroup gdcmParser
108 * \brief Prints the Header Entries (Dicom Elements)
109 * both from the H Table and the chained list
112 void gdcmParser::PrintEntry(std::ostream & os) {
113 std::ostringstream s;
115 for (ListTag::iterator i = listEntries.begin();
116 i != listEntries.end();
119 (*i)->SetPrintLevel(printLevel);
126 * \ingroup gdcmParser
127 * \brief Prints The Dict Entries of THE public Dicom Dictionnry
130 void gdcmParser::PrintPubDict(std::ostream & os) {
131 RefPubDict->Print(os);
135 * \ingroup gdcmParser
136 * \brief Prints The Dict Entries of THE shadow Dicom Dictionnary
139 void gdcmParser::PrintShaDict(std::ostream & os) {
140 RefShaDict->Print(os);
143 //-----------------------------------------------------------------------------
146 * \ingroup gdcmParser
147 * \brief Get the public dictionary used
149 gdcmDict *gdcmParser::GetPubDict(void) {
154 * \ingroup gdcmParser
155 * \brief Get the shadow dictionary used
157 gdcmDict *gdcmParser::GetShaDict(void) {
162 * \ingroup gdcmParser
163 * \brief Set the shadow dictionary used
164 * \param dict dictionary to use in shadow
166 bool gdcmParser::SetShaDict(gdcmDict *dict){
172 * \ingroup gdcmParser
173 * \brief Set the shadow dictionary used
174 * \param dictName name of the dictionary to use in shadow
176 bool gdcmParser::SetShaDict(DictKey dictName){
177 RefShaDict=gdcmGlobal::GetDicts()->GetDict(dictName);
182 * \ingroup gdcmParser
183 * \brief This predicate, based on hopefully reasonable heuristics,
184 * decides whether or not the current gdcmParser was properly parsed
185 * and contains the mandatory information for being considered as
186 * a well formed and usable Dicom/Acr File.
187 * @return true when gdcmParser is the one of a reasonable Dicom/Acr file,
190 bool gdcmParser::IsReadable(void) {
191 if(filetype==Unknown) {
194 if(listEntries.size()<=0) {
202 * \ingroup gdcmParser
203 * \brief Determines if the Transfer Syntax was already encountered
204 * and if it corresponds to a ImplicitVRLittleEndian one.
205 * @return True when ImplicitVRLittleEndian found. False in all other cases.
207 bool gdcmParser::IsImplicitVRLittleEndianTransferSyntax(void) {
208 gdcmHeaderEntry *Element = GetHeaderEntryByNumber(0x0002, 0x0010);
211 LoadHeaderEntrySafe(Element);
213 std::string Transfer = Element->GetValue();
214 if ( Transfer == UI1_2_840_10008_1_2 )
220 * \ingroup gdcmParser
221 * \brief Determines if the Transfer Syntax was already encountered
222 * and if it corresponds to a ExplicitVRLittleEndian one.
223 * @return True when ExplicitVRLittleEndian found. False in all other cases.
225 bool gdcmParser::IsExplicitVRLittleEndianTransferSyntax(void) {
226 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
229 LoadHeaderEntrySafe(Element);
231 std::string Transfer = Element->GetValue();
232 if ( Transfer == UI1_2_840_10008_1_2_1 )
238 * \ingroup gdcmParser
239 * \brief Determines if the Transfer Syntax was already encountered
240 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
241 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
243 bool gdcmParser::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
244 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
247 LoadHeaderEntrySafe(Element);
249 std::string Transfer = Element->GetValue();
250 if ( Transfer == UI1_2_840_10008_1_2_1_99 )
256 * \ingroup gdcmParser
257 * \brief Determines if the Transfer Syntax was already encountered
258 * and if it corresponds to a Explicit VR Big Endian one.
259 * @return True when big endian found. False in all other cases.
261 bool gdcmParser::IsExplicitVRBigEndianTransferSyntax(void) {
262 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
265 LoadHeaderEntrySafe(Element);
267 std::string Transfer = Element->GetValue();
268 if ( Transfer == UI1_2_840_10008_1_2_2 ) //1.2.2 ??? A verifier !
274 * \ingroup gdcmParser
275 * \brief returns the File Type
276 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
277 * @return the FileType code
279 FileType gdcmParser::GetFileType(void) {
284 * \ingroup gdcmParser
285 * \brief opens the file
286 * @param exception_on_error
289 FILE *gdcmParser::OpenFile(bool exception_on_error)
292 fp=fopen(filename.c_str(),"rb");
293 if(exception_on_error)
296 throw gdcmFileError("gdcmParser::gdcmParser(const char *, bool)");
302 fread(&zero, (size_t)2, (size_t)1, fp);
304 //ACR -- or DICOM with no Preamble --
305 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
309 fseek(fp, 126L, SEEK_CUR);
311 fread(dicm, (size_t)4, (size_t)1, fp);
312 if( memcmp(dicm, "DICM", 4) == 0 )
316 dbg.Verbose(0, "gdcmParser::OpenFile not DICOM/ACR", filename.c_str());
319 dbg.Verbose(0, "gdcmParser::OpenFile cannot open file", filename.c_str());
325 * \ingroup gdcmParser
326 * \brief closes the file
327 * @return TRUE if the close was successfull
329 bool gdcmParser::CloseFile(void) {
330 int closed = fclose(fp);
338 * \ingroup gdcmParser
339 * \brief writes on disc all the Header Entries (Dicom Elements)
340 * of the Chained List
341 * @param fp file pointer on an already open file
342 * @param type type of the File to be written
343 * (ACR-NEMA, ExplicitVR, ImplicitVR)
344 * @return always "True" ?!
346 bool gdcmParser::Write(FILE *fp, FileType type) {
348 // TODO The stuff was rewritten using the chained list instead
350 // so we could remove the GroupHT from the gdcmParser
354 // TODO : move the following lines (and a lot of others, to be written)
355 // to a future function CheckAndCorrectHeader
358 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non
359 // (FileType est un champ de gdcmParser ...)
360 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
362 // a moins de se livrer a un tres complique ajout des champs manquants.
363 // faire un CheckAndCorrectHeader (?)
366 if (type == ImplicitVR)
368 std::string implicitVRTransfertSyntax = UI1_2_840_10008_1_2;
369 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
371 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
372 // values with a VR of UI shall be padded with a single trailing null
373 // Dans le cas suivant on doit pader manuellement avec un 0
375 SetEntryLengthByNumber(18, 0x0002, 0x0010);
378 if (type == ExplicitVR)
380 std::string explicitVRTransfertSyntax = UI1_2_840_10008_1_2_1;
381 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
383 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
384 // values with a VR of UI shall be padded with a single trailing null
385 // Dans le cas suivant on doit pader manuellement avec un 0
387 SetEntryLengthByNumber(20, 0x0002, 0x0010);
390 /* TODO : rewrite later, if really usefull
392 if ( (type == ImplicitVR) || (type == ExplicitVR) )
393 UpdateGroupLength(false,type);
395 UpdateGroupLength(true,ACR);
398 WriteEntries(fp,type);
403 * \ingroup gdcmParser
404 * \brief Modifies the value of a given Header Entry (Dicom Element)
405 * if it exists; Creates it with the given value if it doesn't
406 * \warning : adds the Header Entry to the HTable, NOT to the chained List
407 * @param Value passed as a std::string
408 * @param Group group of the Entry
409 * @param Elem element of the Entry
410 * \return pointer to the created Header Entry
411 * NULL if creation failed
413 gdcmHeaderEntry * gdcmParser::ReplaceOrCreateByNumber(
418 a = GetHeaderEntryByNumber( Group, Elem);
420 gdcmHeaderEntry *a =NewHeaderEntryByNumber(Group, Elem);
425 //SetEntryByNumber(Value, Group, Elem);
431 * \ingroup gdcmParser
432 * \brief Modifies the value of a given Header Entry (Dicom Element)
433 * if it exists; Creates it with the given value if it doesn't
434 * @param Value passed as a char*
435 * @param Group group of the Entry
436 * @param Elem element of the Entry
437 * \return pointer to the created Header Entry
438 * NULL if creation failed
441 gdcmHeaderEntry * gdcmParser::ReplaceOrCreateByNumber(
445 gdcmHeaderEntry* nvHeaderEntry=NewHeaderEntryByNumber(Group, Elem);
450 AddHeaderEntry(nvHeaderEntry);
452 std::string v = Value;
453 SetEntryByNumber(v, Group, Elem);
454 return(nvHeaderEntry);
458 * \ingroup gdcmParser
459 * \brief Set a new value if the invoked element exists
460 * Seems to be useless !!!
461 * @param Value new element value
462 * @param Group group of the Entry
463 * @param Elem element of the Entry
466 bool gdcmParser::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem )
468 std::string v = Value;
469 SetEntryByNumber(v, Group, Elem);
473 //-----------------------------------------------------------------------------
477 * \ingroup gdcmParser
478 * \brief Checks if a given Dicom Element exists
480 * @param group Group number of the searched Dicom Element
481 * @param element Element number of the searched Dicom Element
482 * @return number of occurences
484 int gdcmParser::CheckIfEntryExistByNumber(guint16 group, guint16 element ) {
485 std::string key = gdcmDictEntry::TranslateToKey(group, element );
486 return (tagHT.count(key));
490 * \ingroup gdcmParser
491 * \brief Searches within Header Entries (Dicom Elements) parsed with
492 * the public and private dictionaries
493 * for the element value of a given tag.
494 * \warning Don't use any longer : use GetPubEntryByName
495 * @param tagName name of the searched element.
496 * @return Corresponding element value when it exists,
497 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
499 std::string gdcmParser::GetEntryByName(std::string tagName) {
500 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
501 if( dictEntry == NULL)
504 return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
508 * \ingroup gdcmParser
509 * \brief Searches within Header Entries (Dicom Elements) parsed with
510 * the public and private dictionaries
511 * for the element value representation of a given tag.
513 * Obtaining the VR (Value Representation) might be needed by caller
514 * to convert the string typed content to caller's native type
515 * (think of C++ vs Python). The VR is actually of a higher level
516 * of semantics than just the native C++ type.
517 * @param tagName name of the searched element.
518 * @return Corresponding element value representation when it exists,
519 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
521 std::string gdcmParser::GetEntryVRByName(std::string tagName) {
522 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
523 if( dictEntry == NULL)
526 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(dictEntry->GetGroup(),
527 dictEntry->GetElement());
528 return elem->GetVR();
533 * \ingroup gdcmParser
534 * \brief Searches within Header Entries (Dicom Elements) parsed with
535 * the public and private dictionaries
536 * for the element value representation of a given tag.
537 * @param group Group of the searched tag.
538 * @param element Element of the searched tag.
539 * @return Corresponding element value representation when it exists,
540 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
542 std::string gdcmParser::GetEntryByNumber(guint16 group, guint16 element){
543 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
544 if ( ! tagHT.count(key))
546 return tagHT.find(key)->second->GetValue();
550 * \ingroup gdcmParser
551 * \brief Searches within Header Entries (Dicom Elements) parsed with
552 * the public and private dictionaries
553 * for the element value representation of a given tag..
555 * Obtaining the VR (Value Representation) might be needed by caller
556 * to convert the string typed content to caller's native type
557 * (think of C++ vs Python). The VR is actually of a higher level
558 * of semantics than just the native C++ type.
559 * @param group Group of the searched tag.
560 * @param element Element of the searched tag.
561 * @return Corresponding element value representation when it exists,
562 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
564 std::string gdcmParser::GetEntryVRByNumber(guint16 group, guint16 element) {
565 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
568 return elem->GetVR();
572 * \ingroup gdcmParser
573 * \brief Searches within Header Entries (Dicom Elements) parsed with
574 * the public and private dictionaries
575 * for the value length of a given tag..
576 * @param group Group of the searched tag.
577 * @param element Element of the searched tag.
578 * @return Corresponding element length; -2 if not found
580 int gdcmParser::GetEntryLengthByNumber(guint16 group, guint16 element) {
581 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
584 return elem->GetLength();
587 * \ingroup gdcmParser
588 * \brief Sets the value (string) of the Header Entry (Dicom Element)
589 * @param content string value of the Dicom Element
590 * @param tagName name of the searched Dicom Element.
591 * @return true when found
593 bool gdcmParser::SetEntryByName(std::string content,std::string tagName) {
594 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
595 if( dictEntry == NULL)
598 return(SetEntryByNumber(content,dictEntry->GetGroup(),
599 dictEntry->GetElement()));
603 * \ingroup gdcmParser
604 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
605 * through it's (group, element) and modifies it's content with
607 * \warning Don't use any longer : use SetPubEntryByNumber
608 * @param content new value to substitute with
609 * @param group group of the Dicom Element to modify
610 * @param element element of the Dicom Element to modify
612 bool gdcmParser::SetEntryByNumber(std::string content,
616 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
617 if ( ! tagHT.count(key))
619 int l = content.length();
620 if(l%2) // Non even length are padded with a space (020H).
623 content = content + '\0';
628 TagHeaderEntryHT::iterator p2;
629 // DO NOT remove the following lines : they explain the stuff
630 //p= tagHT.equal_range(key); // get a pair of iterators first-last synonym
631 //p2=p.first; // iterator on the first synonym
632 //a=p2->second; // H Table target column (2-nd col)
635 a = ((tagHT.equal_range(key)).first)->second;
637 a-> SetValue(content);
639 std::string vr = a->GetVR();
642 if( (vr == "US") || (vr == "SS") )
644 else if( (vr == "UL") || (vr == "SL") )
654 * \ingroup gdcmParser
655 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
656 * in the PubHeaderEntrySet of this instance
657 * through it's (group, element) and modifies it's length with
659 * \warning Use with extreme caution.
660 * @param l new length to substitute with
661 * @param group group of the Entry to modify
662 * @param element element of the Entry to modify
663 * @return true on success, false otherwise.
665 bool gdcmParser::SetEntryLengthByNumber(guint32 l,
669 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
670 if ( ! tagHT.count(key))
672 if (l%2) l++; // length must be even
673 ( ((tagHT.equal_range(key)).first)->second )->SetLength(l);
679 * \ingroup gdcmParser
680 * \brief Gets (from Header) the offset of a 'non string' element value
681 * (LoadElementValues has already be executed)
682 * @param Group group of the Entry
683 * @param Elem element of the Entry
684 * @return File Offset of the Element Value
686 size_t gdcmParser::GetEntryOffsetByNumber(guint16 Group, guint16 Elem)
688 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
691 dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
692 "failed to Locate gdcmHeaderEntry");
695 return Entry->GetOffset();
699 * \ingroup gdcmParser
700 * \brief Gets (from Header) a 'non string' element value
701 * (LoadElementValues has already be executed)
702 * @param Group group of the Entry
703 * @param Elem element of the Entry
704 * @return Pointer to the 'non string' area
706 void * gdcmParser::GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem)
708 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
711 dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
712 "failed to Locate gdcmHeaderEntry");
715 return Entry->GetVoidArea();
719 * \ingroup gdcmParser
720 * \brief Loads (from disk) the element content
721 * when a string is not suitable
722 * @param Group group of the Entry
723 * @param Elem element of the Entry
725 void *gdcmParser::LoadEntryVoidArea(guint16 Group, guint16 Elem)
727 gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
730 size_t o =(size_t)Element->GetOffset();
731 fseek(fp, o, SEEK_SET);
732 int l=Element->GetLength();
733 void * a = malloc(l);
737 SetEntryVoidAreaByNumber(a, Group, Elem);
738 // TODO check the result
739 size_t l2 = fread(a, 1, l ,fp);
750 * \ingroup gdcmParser
751 * \brief Sets a 'non string' value to a given Dicom Element
753 * @param group Group number of the searched Dicom Element
754 * @param element Element number of the searched Dicom Element
757 bool gdcmParser::SetEntryVoidAreaByNumber(void * area,
761 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
762 if ( ! tagHT.count(key))
764 ( ((tagHT.equal_range(key)).first)->second )->SetVoidArea(area);
769 * \ingroup gdcmParser
770 * \brief Update the entries with the shadow dictionary.
771 * Only non even entries are analyzed
773 void gdcmParser::UpdateShaEntries(void) {
774 gdcmDictEntry *entry;
777 for(ListTag::iterator it=listEntries.begin();
778 it!=listEntries.end();
781 // Odd group => from public dictionary
782 if((*it)->GetGroup()%2==0)
785 // Peer group => search the corresponding dict entry
787 entry=RefShaDict->GetDictEntryByNumber((*it)->GetGroup(),(*it)->GetElement());
791 if((*it)->IsImplicitVR())
796 (*it)->SetValue(GetHeaderEntryUnvalue(*it));
798 // Set the new entry and the new value
799 (*it)->SetDictEntry(entry);
800 CheckHeaderEntryVR(*it,vr);
802 (*it)->SetValue(GetHeaderEntryValue(*it));
806 // Remove precedent value transformation
807 (*it)->SetDictEntry(NewVirtualDictEntry((*it)->GetGroup(),(*it)->GetElement(),vr));
813 * \ingroup gdcmParser
814 * \brief Searches within the Header Entries for a Dicom Element of
816 * @param tagName name of the searched Dicom Element.
817 * @return Corresponding Dicom Element when it exists, and NULL
820 gdcmHeaderEntry *gdcmParser::GetHeaderEntryByName(std::string tagName) {
821 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
822 if( dictEntry == NULL)
825 return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
829 * \ingroup gdcmParser
830 * \brief retrieves a Dicom Element (the first one) using (group, element)
831 * \warning (group, element) IS NOT an identifier inside the Dicom Header
832 * if you think it's NOT UNIQUE, check the count number
833 * and use iterators to retrieve ALL the Dicoms Elements within
834 * a given couple (group, element)
835 * @param group Group number of the searched Dicom Element
836 * @param element Element number of the searched Dicom Element
839 gdcmHeaderEntry* gdcmParser::GetHeaderEntryByNumber(guint16 group, guint16 element)
841 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
842 if ( ! tagHT.count(key))
844 return tagHT.find(key)->second;
848 * \ingroup gdcmParser
849 * \brief retrieves the Dicom Elements (all of them) using (group, element)
850 * @param group Group number of the searched Dicom Element.
851 * @param element Element number of the searched Dicom Element.
852 * @return a range (i.e.pair<,>) containing all elements whose key is group|element)
855 IterHT gdcmParser::GetHeaderEntrySameNumber(guint16 group, guint16 element){
856 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
857 return (tagHT.equal_range(key));
861 * \ingroup gdcmParser
862 * \brief Loads the element while preserving the current
863 * underlying file position indicator as opposed to
864 * to LoadHeaderEntry that modifies it.
865 * @param entry Header Entry whose value shall be loaded.
868 void gdcmParser::LoadHeaderEntrySafe(gdcmHeaderEntry * entry) {
869 long PositionOnEntry = ftell(fp);
870 LoadHeaderEntry(entry);
871 fseek(fp, PositionOnEntry, SEEK_SET);
875 * \ingroup gdcmParser
876 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
877 * \warning : to be re-written using the chained list instead of the H table.
878 * \warning : DO NOT use (doesn't work any longer because of the multimap)
879 * \todo : to be re-written using the chained list instead of the H table
880 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
881 * @param type Type of the File (ExplicitVR,ImplicitVR, ACR, ...)
883 void gdcmParser::UpdateGroupLength(bool SkipSequence, FileType type) {
887 gdcmHeaderEntry *elem;
889 std::string str_trash;
892 GroupHT groupHt; // to hold the length of each group
895 // typedef std::map<GroupKey, int> GroupHT;
897 gdcmHeaderEntry *elemZ;
899 // for each Tag in the DCM Header
901 for (TagHeaderEntryHT::iterator tag2 = tagHT.begin();
906 gr = elem->GetGroup();
907 el = elem->GetElement();
910 sprintf(trash, "%04x", gr);
911 key = trash; // generate 'group tag'
913 // if the caller decided not to take SEQUENCEs into account
914 // e.g : he wants to write an ACR-NEMA File
916 if (SkipSequence && vr == "SQ")
919 // Still unsolved problem :
920 // we cannot find the 'Sequence Delimitation Item'
921 // since it's at the end of the Hash Table
924 // pas SEQUENCE en ACR-NEMA
926 // --> la descente a l'interieur' des SQ
927 // devrait etre faite avec une liste chainee, pas avec une HTable...
929 if ( groupHt.count(key) == 0) // we just read the first elem of a given group
931 if (el == 0x0000) // the first elem is 0x0000
933 groupHt[key] = 0; // initialize group length
937 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
940 else // any elem but the first
942 if (type == ExplicitVR)
944 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
946 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
949 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
953 unsigned short int gr_bid;
955 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
959 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
960 // warning: unsigned int format, different type arg
961 sscanf(g->first.c_str(),"%x",&gr_bid);
962 tk = g->first + "|0000"; // generate the element full tag
964 if ( tagHT.count(tk) == 0) // if element 0x0000 not found
966 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
967 elemZ = new gdcmHeaderEntry(tagZ);
969 AddHeaderEntry(elemZ); // create it
973 elemZ=GetHeaderEntryByNumber(gr_bid, 0x0000);
975 sprintf(trash ,"%d",g->second);
977 elemZ->SetValue(str_trash);
982 * \ingroup gdcmParser
983 * \brief writes on disc according to the requested format
984 * (ACR-NEMA, ExplicitVR, ImplicitVR) ONE
986 * @param tag pointer on the gdcmHeaderEntry to be written
987 * @param type type of the File to be written
988 * (ACR-NEMA, ExplicitVR, ImplicitVR)
989 * @param _fp already open file pointer
991 void gdcmParser::WriteEntry(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
1002 std::vector<std::string> tokens;
1006 // TODO (?) tester les echecs en ecriture (apres chaque fwrite)
1008 itsTimeToWritePixels = false;
1010 gr = tag->GetGroup();
1011 el = tag->GetElement();
1012 lgr = tag->GetReadLength();
1013 val = tag->GetValue().c_str();
1015 voidArea = tag->GetVoidArea();
1017 // === Deal with the length
1018 // --------------------
1019 if((tag->GetLength())%2==1)
1021 tag->SetValue(tag->GetValue()+"\0");
1022 tag->SetLength(tag->GetReadLength()+1);
1027 if (gr < 0x0008) return; // ignore pure DICOM V3 groups
1028 if (gr %2) return; // ignore shadow groups
1029 if (vr == "SQ" ) return; // ignore Sequences
1030 // TODO : find a trick to *skip* the SeQuences !
1031 // Not only ignore the SQ element
1032 if (gr == 0xfffe ) return; // ignore delimiters
1035 fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
1036 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
1038 if ( (type == ExplicitVR) || (type == DICOMDIR) ) {
1040 guint16 z=0, shortLgr;
1042 if (gr == 0xfffe) { // NO Value Representation for 'delimiters'
1043 // no length : write ffffffff
1045 // special patch to make some MR PHILIPS
1046 if (el == 0x0000) return; // images e-film readable // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1047 // from Hospital Guy de Chauliac,
1049 // we just ignore spurious fffe|0000 tag !
1051 fwrite (&ff,(size_t)4 ,(size_t)1 ,_fp);
1052 return; // NO value for 'delimiters'
1056 if (vr == "unkn") { // Unknown was 'written'
1057 // deal with Little Endian
1058 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
1059 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
1061 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
1062 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ){
1063 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
1064 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
1066 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
1072 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
1075 // === Deal with the value
1076 // -------------------
1077 if (vr == "SQ") return; // no "value" to write for the SEQuences
1078 if (gr == 0xfffe)return; // no "value" to write for the delimiters
1080 if (voidArea != NULL)
1081 { // there is a 'non string' LUT, overlay, etc
1082 fwrite ( voidArea,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
1086 if (vr == "US" || vr == "SS")
1088 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1089 Tokenize (tag->GetValue(), tokens, "\\");
1090 for (unsigned int i=0; i<tokens.size();i++)
1092 val_uint16 = atoi(tokens[i].c_str());
1094 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
1099 if (vr == "UL" || vr == "SL")
1101 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1102 Tokenize (tag->GetValue(), tokens, "\\");
1103 for (unsigned int i=0; i<tokens.size();i++)
1105 val_uint32 = atoi(tokens[i].c_str());
1107 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
1113 // Pixels are never loaded in the element !
1114 // we stop writting when Pixel are processed
1115 // FIX : we loose trailing elements (RAB, right now)
1117 if ((gr == GrPixel) && (el == NumPixel) ) {
1119 if (compte == countGrPixel) {// we passed *all* the GrPixel,NumPixel
1120 itsTimeToWritePixels = true;
1124 fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
1128 * \ingroup gdcmParser
1129 * \brief writes on disc according to the requested format
1130 * (ACR-NEMA, ExplicitVR, ImplicitVR) the image
1131 * using the Chained List
1132 * \warning does NOT add the missing elements in the header :
1133 * it's up to the user doing it !
1134 * (function CheckHeaderCoherence to be written)
1135 * \warning DON'T try, right now, to write a DICOM image
1136 * from an ACR Header (meta elements will be missing!)
1137 * \sa WriteEntriesDeprecated (Special temporary method for Theralys)
1138 * @param type type of the File to be written
1139 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1140 * @param _fp already open file pointer
1143 void gdcmParser::WriteEntries(FILE *_fp,FileType type)
1145 // TODO (?) tester les echecs en ecriture (apres chaque fwrite)
1147 for (ListTag::iterator tag2=listEntries.begin();
1148 tag2 != listEntries.end();
1151 WriteEntry(*tag2,_fp,type);
1152 if (itsTimeToWritePixels)
1158 * \ingroup gdcmParser
1159 * \brief writes on disc according to the requested format
1160 * (ACR-NEMA, ExplicitVR, ImplicitVR) the image,
1161 * using only the last synonym of each mutimap H Table post.
1162 * \warning Uses the H Table, instead of the Chained List
1163 * in order to be compliant with the old way to proceed
1164 * (added elements taken in to account)
1165 * Only THERALYS, during a transitory phase is supposed
1166 * to use this method !!!
1167 * \warning DON'T try, right now, to write a DICOM image
1168 * from an ACR Header (meta elements will be missing!)
1170 * @param _fp already open file pointer
1171 * @param type type of the File to be written
1172 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1174 void gdcmParser::WriteEntriesDeprecated(FILE *_fp,FileType type) {
1176 // restent a tester les echecs en ecriture (apres chaque fwrite)
1178 for (TagHeaderEntryHT::iterator tag2=tagHT.begin();
1179 tag2 != tagHT.end();
1181 WriteEntry(tag2->second,_fp,type);
1182 if (itsTimeToWritePixels)
1188 * \ingroup gdcmParser
1189 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1191 * @return The properly swaped 32 bits integer.
1193 guint32 gdcmParser::SwapLong(guint32 a) {
1198 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1199 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1203 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1207 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1210 dbg.Error(" gdcmParser::SwapLong : unset swap code");
1217 * \ingroup gdcmParser
1218 * \brief Unswaps back the bytes of 4-byte long integer accordingly to
1220 * @return The properly unswaped 32 bits integer.
1222 guint32 gdcmParser::UnswapLong(guint32 a) {
1223 return (SwapLong(a));
1227 * \ingroup gdcmParser
1228 * \brief Swaps the bytes so they agree with the processor order
1229 * @return The properly swaped 16 bits integer.
1231 guint16 gdcmParser::SwapShort(guint16 a) {
1232 if ( (sw==4321) || (sw==2143) )
1233 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1238 * \ingroup gdcmParser
1239 * \brief Unswaps the bytes so they agree with the processor order
1240 * @return The properly unswaped 16 bits integer.
1242 guint16 gdcmParser::UnswapShort(guint16 a) {
1243 return (SwapShort(a));
1246 //-----------------------------------------------------------------------------
1249 * \ingroup gdcmParser
1250 * \brief Parses the header of the file but WITHOUT loading element values.
1251 * @return false if file is not ACR-NEMA / DICOM
1253 bool gdcmParser::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1259 gdcmHeaderEntry *newHeaderEntry = (gdcmHeaderEntry *)0;
1260 while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1261 SkipHeaderEntry(newHeaderEntry);
1262 if ( (ignoreShadow==0) || (newHeaderEntry->GetGroup()%2) == 0) {
1263 AddHeaderEntry(newHeaderEntry);
1270 * \ingroup gdcmParser
1271 * \brief Loads the element values of all the Header Entries pointed in the
1272 * public Chained List.
1274 void gdcmParser::LoadHeaderEntries(void) {
1276 for (ListTag::iterator i = GetListEntry().begin();
1277 i != GetListEntry().end();
1280 LoadHeaderEntry(*i);
1285 // Load 'non string' values
1286 std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
1287 if( PhotometricInterpretation == "PALETTE COLOR " ) {
1288 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
1289 LoadEntryVoidArea(0x0028,0x1201); // R LUT
1290 LoadEntryVoidArea(0x0028,0x1202); // G LUT
1291 LoadEntryVoidArea(0x0028,0x1203); // B LUT
1293 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1294 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1295 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1297 //FIXME : how to use it?
1298 LoadEntryVoidArea(0x0028,0x3006); //LUT Data (CTX dependent)
1300 // --------------------------------------------------------------
1301 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1303 // if recognition code tells us we deal with a LibIDO image
1304 // we switch lineNumber and columnNumber
1306 std::string RecCode;
1307 RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
1308 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1309 RecCode == "CANRME_AILIBOD1_1." )
1311 filetype = ACR_LIBIDO;
1312 std::string rows = GetEntryByNumber(0x0028, 0x0010);
1313 std::string columns = GetEntryByNumber(0x0028, 0x0011);
1314 SetEntryByNumber(columns, 0x0028, 0x0010);
1315 SetEntryByNumber(rows , 0x0028, 0x0011);
1317 // ----------------- End of Special Patch ----------------
1321 * \ingroup gdcmParser
1322 * \brief Loads the element content if its length doesn't exceed
1323 * the value specified with gdcmParser::SetMaxSizeLoadEntry()
1324 * @param Entry Header Entry (Dicom Element) to be dealt with
1326 void gdcmParser::LoadHeaderEntry(gdcmHeaderEntry *Entry) {
1328 guint16 group = Entry->GetGroup();
1329 std::string vr= Entry->GetVR();
1330 guint32 length = Entry->GetLength();
1331 bool SkipLoad = false;
1333 fseek(fp, (long)Entry->GetOffset(), SEEK_SET);
1335 // the test was commented out to 'go inside' the SeQuences
1336 // we don't any longer skip them !
1338 // if( vr == "SQ" ) // (DO NOT remove this comment)
1341 // A SeQuence "contains" a set of Elements.
1342 // (fffe e000) tells us an Element is beginning
1343 // (fffe e00d) tells us an Element just ended
1344 // (fffe e0dd) tells us the current SeQuence just ended
1345 if( group == 0xfffe )
1349 Entry->SetLength(0);
1350 Entry->SetValue("gdcm::Skipped");
1354 // When the length is zero things are easy:
1355 if ( length == 0 ) {
1356 Entry->SetValue("");
1360 // The elements whose length is bigger than the specified upper bound
1361 // are not loaded. Instead we leave a short notice of the offset of
1362 // the element content and it's length.
1363 if (length > MaxSizeLoadEntry) {
1364 std::ostringstream s;
1365 s << "gdcm::NotLoaded.";
1366 s << " Address:" << (long)Entry->GetOffset();
1367 s << " Length:" << Entry->GetLength();
1368 s << " x(" << std::hex << Entry->GetLength() << ")";
1369 Entry->SetValue(s.str());
1373 // When integer(s) are expected, read and convert the following
1374 // n *(two or four bytes)
1375 // properly i.e. as integers as opposed to strings.
1376 // Elements with Value Multiplicity > 1
1377 // contain a set of integers (not a single one)
1379 // Any compacter code suggested (?)
1380 if ( IsHeaderEntryAnInteger(Entry) ) {
1382 std::ostringstream s;
1384 if (vr == "US" || vr == "SS") {
1386 NewInt = ReadInt16();
1389 for (int i=1; i < nbInt; i++) {
1391 NewInt = ReadInt16();
1396 else if (vr == "UL" || vr == "SL") {
1398 NewInt = ReadInt32();
1401 for (int i=1; i < nbInt; i++) {
1403 NewInt = ReadInt32();
1408 #ifdef GDCM_NO_ANSI_STRING_STREAM
1409 s << std::ends; // to avoid oddities on Solaris
1410 #endif //GDCM_NO_ANSI_STRING_STREAM
1412 Entry->SetValue(s.str());
1416 // We need an additional byte for storing \0 that is not on disk
1417 std::string NewValue(length,0);
1418 item_read = fread(&(NewValue[0]), (size_t)length, (size_t)1, fp);
1419 if ( item_read != 1 ) {
1420 dbg.Verbose(1, "gdcmParser::LoadElementValue","unread element value");
1421 Entry->SetValue("gdcm::UnRead");
1425 if( (vr == "UI") ) // Because of correspondance with the VR dic
1426 Entry->SetValue(NewValue.c_str());
1428 Entry->SetValue(NewValue);
1432 * \ingroup gdcmParser
1433 * \brief add a new Dicom Element pointer to
1434 * the H Table and at the end of the chained List
1435 * \warning push_bash in listEntries ONLY during ParseHeader
1436 * \todo something to allow further Elements addition,
1437 * (at their right place in the chained list)
1438 * when position to be taken care of
1439 * @param newHeaderEntry
1441 void gdcmParser::AddHeaderEntry(gdcmHeaderEntry *newHeaderEntry) {
1442 tagHT.insert( PairHT( newHeaderEntry->GetKey(),newHeaderEntry) );
1443 listEntries.push_back(newHeaderEntry);
1448 * \ingroup gdcmParser
1449 * \brief Find the value Length of the passed Header Entry
1450 * @param Entry Header Entry whose length of the value shall be loaded.
1452 void gdcmParser::FindHeaderEntryLength (gdcmHeaderEntry *Entry) {
1453 guint16 element = Entry->GetElement();
1454 guint16 group = Entry->GetGroup();
1455 std::string vr = Entry->GetVR();
1458 if( (element == NumPixel) && (group == GrPixel) )
1460 dbg.SetDebug(GDCM_DEBUG);
1461 dbg.Verbose(2, "gdcmParser::FindLength: ",
1462 "we reached (GrPixel,NumPixel)");
1465 if ( (filetype == ExplicitVR) && (! Entry->IsImplicitVR()) )
1467 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") )
1469 // The following reserved two bytes (see PS 3.5-2001, section
1470 // 7.1.2 Data element structure with explicit vr p27) must be
1471 // skipped before proceeding on reading the length on 4 bytes.
1472 fseek(fp, 2L, SEEK_CUR);
1473 guint32 length32 = ReadInt32();
1475 if ( (vr == "OB") && (length32 == 0xffffffff) )
1477 Entry->SetLength(FindHeaderEntryLengthOB());
1480 FixHeaderEntryFoundLength(Entry, length32);
1484 // Length is encoded on 2 bytes.
1485 length16 = ReadInt16();
1487 // We can tell the current file is encoded in big endian (like
1488 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1489 // and it's value is the one of the encoding of a big endian file.
1490 // In order to deal with such big endian encoded files, we have
1491 // (at least) two strategies:
1492 // * when we load the "Transfer Syntax" tag with value of big endian
1493 // encoding, we raise the proper flags. Then we wait for the end
1494 // of the META group (0x0002) among which is "Transfer Syntax",
1495 // before switching the swap code to big endian. We have to postpone
1496 // the switching of the swap code since the META group is fully encoded
1497 // in little endian, and big endian coding only starts at the next
1498 // group. The corresponding code can be hard to analyse and adds
1499 // many additional unnecessary tests for regular tags.
1500 // * the second strategy consists in waiting for trouble, that shall
1501 // appear when we find the first group with big endian encoding. This
1502 // is easy to detect since the length of a "Group Length" tag (the
1503 // ones with zero as element number) has to be of 4 (0x0004). When we
1504 // encounter 1024 (0x0400) chances are the encoding changed and we
1505 // found a group with big endian encoding.
1506 // We shall use this second strategy. In order to make sure that we
1507 // can interpret the presence of an apparently big endian encoded
1508 // length of a "Group Length" without committing a big mistake, we
1509 // add an additional check: we look in the already parsed elements
1510 // for the presence of a "Transfer Syntax" whose value has to be "big
1511 // endian encoding". When this is the case, chances are we have got our
1512 // hands on a big endian encoded file: we switch the swap code to
1513 // big endian and proceed...
1514 if ( (element == 0x0000) && (length16 == 0x0400) )
1516 if ( ! IsExplicitVRBigEndianTransferSyntax() )
1518 dbg.Verbose(0, "gdcmParser::FindLength", "not explicit VR");
1523 SwitchSwapToBigEndian();
1524 // Restore the unproperly loaded values i.e. the group, the element
1525 // and the dictionary entry depending on them.
1526 guint16 CorrectGroup = SwapShort(Entry->GetGroup());
1527 guint16 CorrectElem = SwapShort(Entry->GetElement());
1528 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
1532 // This correct tag is not in the dictionary. Create a new one.
1533 NewTag = NewVirtualDictEntry(CorrectGroup, CorrectElem);
1535 // FIXME this can create a memory leaks on the old entry that be
1536 // left unreferenced.
1537 Entry->SetDictEntry(NewTag);
1540 // Heuristic: well some files are really ill-formed.
1541 if ( length16 == 0xffff)
1544 //dbg.Verbose(0, "gdcmParser::FindLength",
1545 // "Erroneous element length fixed.");
1546 // Actually, length= 0xffff means that we deal with
1547 // Unknown Sequence Length
1549 FixHeaderEntryFoundLength(Entry, (guint32)length16);
1554 // Either implicit VR or a non DICOM conformal (see note below) explicit
1555 // VR that ommited the VR of (at least) this element. Farts happen.
1556 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1557 // on Data elements "Implicit and Explicit VR Data Elements shall
1558 // not coexist in a Data Set and Data Sets nested within it".]
1559 // Length is on 4 bytes.
1561 FixHeaderEntryFoundLength(Entry, ReadInt32());
1567 * \ingroup gdcmParser
1568 * \brief Find the Value Representation of the current Dicom Element.
1571 void gdcmParser::FindHeaderEntryVR( gdcmHeaderEntry *Entry)
1573 if (filetype != ExplicitVR)
1578 long PositionOnEntry = ftell(fp);
1579 // Warning: we believe this is explicit VR (Value Representation) because
1580 // we used a heuristic that found "UL" in the first tag. Alas this
1581 // doesn't guarantee that all the tags will be in explicit VR. In some
1582 // cases (see e-film filtered files) one finds implicit VR tags mixed
1583 // within an explicit VR file. Hence we make sure the present tag
1584 // is in explicit VR and try to fix things if it happens not to be
1587 int lgrLue=fread (&VR, (size_t)2,(size_t)1, fp); // lgrLue not used
1589 if(!CheckHeaderEntryVR(Entry,VR))
1591 fseek(fp, PositionOnEntry, SEEK_SET);
1592 // When this element is known in the dictionary we shall use, e.g. for
1593 // the semantics (see the usage of IsAnInteger), the VR proposed by the
1594 // dictionary entry. Still we have to flag the element as implicit since
1595 // we know now our assumption on expliciteness is not furfilled.
1597 if ( Entry->IsVRUnknown() )
1598 Entry->SetVR("Implicit");
1599 Entry->SetImplicitVR();
1604 * \ingroup gdcmParser
1605 * \brief Check the correspondance between the VR of the header entry
1606 * and the taken VR. If they are different, the header entry is
1607 * updated with the new VR.
1608 * @param Entry Header Entry to check
1609 * @param vr Dicom Value Representation
1610 * @return false if the VR is incorrect of if the VR isn't referenced
1611 * otherwise, it returns true
1613 bool gdcmParser::CheckHeaderEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
1615 char msg[100]; // for sprintf
1616 bool RealExplicit = true;
1618 // Assume we are reading a falsely explicit VR file i.e. we reached
1619 // a tag where we expect reading a VR but are in fact we read the
1620 // first to bytes of the length. Then we will interogate (through find)
1621 // the dicom_vr dictionary with oddities like "\004\0" which crashes
1622 // both GCC and VC++ implementations of the STL map. Hence when the
1623 // expected VR read happens to be non-ascii characters we consider
1624 // we hit falsely explicit VR tag.
1626 if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
1627 RealExplicit = false;
1629 // CLEANME searching the dicom_vr at each occurence is expensive.
1630 // PostPone this test in an optional integrity check at the end
1631 // of parsing or only in debug mode.
1632 if ( RealExplicit && !gdcmGlobal::GetVR()->Count(vr) )
1633 RealExplicit= false;
1635 if ( !RealExplicit )
1637 // We thought this was explicit VR, but we end up with an
1638 // implicit VR tag. Let's backtrack.
1639 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
1640 Entry->GetGroup(),Entry->GetElement());
1641 dbg.Verbose(1, "gdcmParser::FindVR: ",msg);
1642 if (Entry->GetGroup()%2 && Entry->GetElement() == 0x0000) { // Group length is UL !
1643 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1644 Entry->GetGroup(),Entry->GetElement(),
1645 "UL","FIXME","Group Length");
1646 Entry->SetDictEntry(NewEntry);
1651 if ( Entry->IsVRUnknown() )
1653 // When not a dictionary entry, we can safely overwrite the VR.
1654 if (Entry->GetElement() == 0x0000) { // Group length is UL !
1660 else if ( Entry->GetVR() != vr )
1662 // The VR present in the file and the dictionary disagree. We assume
1663 // the file writer knew best and use the VR of the file. Since it would
1664 // be unwise to overwrite the VR of a dictionary (since it would
1665 // compromise it's next user), we need to clone the actual DictEntry
1666 // and change the VR for the read one.
1667 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1668 Entry->GetGroup(),Entry->GetElement(),
1669 vr,"FIXME",Entry->GetName());
1670 Entry->SetDictEntry(NewEntry);
1676 * \ingroup gdcmParser
1677 * \brief Get the transformed value of the header entry. The VR value
1678 * is used to define the transformation to operate on the value
1679 * \warning NOT end user intended method !
1681 * @return Transformed entry value
1683 std::string gdcmParser::GetHeaderEntryValue(gdcmHeaderEntry *Entry)
1685 if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1687 std::string val=Entry->GetValue();
1688 std::string vr=Entry->GetVR();
1689 guint32 length = Entry->GetLength();
1690 std::ostringstream s;
1693 if (vr == "US" || vr == "SS")
1698 for (int i=0; i < nbInt; i++)
1702 NewInt16 = (val[2*i+0]&0xFF)+((val[2*i+1]&0xFF)<<8);
1703 NewInt16 = SwapShort(NewInt16);
1708 else if (vr == "UL" || vr == "SL")
1713 for (int i=0; i < nbInt; i++)
1717 NewInt32= (val[4*i+0]&0xFF)+((val[4*i+1]&0xFF)<<8)+
1718 ((val[4*i+2]&0xFF)<<16)+((val[4*i+3]&0xFF)<<24);
1719 NewInt32=SwapLong(NewInt32);
1723 #ifdef GDCM_NO_ANSI_STRING_STREAM
1724 s << std::ends; // to avoid oddities on Solaris
1725 #endif //GDCM_NO_ANSI_STRING_STREAM
1729 return(Entry->GetValue());
1733 * \ingroup gdcmParser
1734 * \brief Get the reverse transformed value of the header entry. The VR
1735 * value is used to define the reverse transformation to operate on
1737 * \warning NOT end user intended method !
1739 * @return Reverse transformed entry value
1741 std::string gdcmParser::GetHeaderEntryUnvalue(gdcmHeaderEntry *Entry)
1743 if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1745 std::string vr=Entry->GetVR();
1746 std::ostringstream s;
1747 std::vector<std::string> tokens;
1749 if (vr == "US" || vr == "SS")
1753 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1754 Tokenize (Entry->GetValue(), tokens, "\\");
1755 for (unsigned int i=0; i<tokens.size();i++)
1757 NewInt16 = atoi(tokens[i].c_str());
1758 s<<(NewInt16&0xFF)<<((NewInt16>>8)&0xFF);
1762 if (vr == "UL" || vr == "SL")
1766 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1767 Tokenize (Entry->GetValue(), tokens, "\\");
1768 for (unsigned int i=0; i<tokens.size();i++)
1770 NewInt32 = atoi(tokens[i].c_str());
1771 s<<(char)(NewInt32&0xFF)<<(char)((NewInt32>>8)&0xFF)
1772 <<(char)((NewInt32>>16)&0xFF)<<(char)((NewInt32>>24)&0xFF);
1777 #ifdef GDCM_NO_ANSI_STRING_STREAM
1778 s << std::ends; // to avoid oddities on Solaris
1779 #endif //GDCM_NO_ANSI_STRING_STREAM
1783 return(Entry->GetValue());
1787 * \ingroup gdcmParser
1788 * \brief Skip a given Header Entry
1789 * \warning NOT end user intended method !
1792 void gdcmParser::SkipHeaderEntry(gdcmHeaderEntry *entry)
1794 SkipBytes(entry->GetLength());
1798 * \ingroup gdcmParser
1799 * \brief When the length of an element value is obviously wrong (because
1800 * the parser went Jabberwocky) one can hope improving things by
1801 * applying this heuristic.
1803 void gdcmParser::FixHeaderEntryFoundLength(gdcmHeaderEntry *Entry, guint32 FoundLength)
1805 Entry->SetReadLength(FoundLength); // will be updated only if a bug is found
1807 if ( FoundLength == 0xffffffff) {
1811 guint16 gr =Entry->GetGroup();
1812 guint16 el =Entry->GetElement();
1814 if (FoundLength%2) {
1815 std::ostringstream s;
1816 s << "Warning : Tag with uneven length " << FoundLength
1817 << " in x(" << std::hex << gr << "," << el <<")" << std::dec;
1818 dbg.Verbose(0,s.str().c_str());
1821 // Sorry for the patch!
1822 // XMedCom did the trick to read some nasty GE images ...
1823 if (FoundLength == 13) {
1824 // The following 'if' will be removed when there is no more
1825 // images on Creatis HDs with a 13 length for Manufacturer...
1826 if ( (Entry->GetGroup() != 0x0008) ||
1827 ( (Entry->GetElement() != 0x0070) && (Entry->GetElement() != 0x0080) ) ){
1828 // end of remove area
1830 Entry->SetReadLength(10); // a bug is to be fixed
1834 // to fix some garbage 'Leonardo' Siemens images
1835 // May be commented out to avoid overhead
1836 else if ( (Entry->GetGroup() == 0x0009) &&
1837 ( (Entry->GetElement() == 0x1113) || (Entry->GetElement() == 0x1114) ) ){
1839 Entry->SetReadLength(4); // a bug is to be fixed
1843 // to try to 'go inside' SeQuences (with length), and not to skip them
1844 else if ( Entry->GetVR() == "SQ")
1846 if (enableSequences) // only if the user does want to !
1847 FoundLength =0; // ReadLength is unchanged
1850 // a SeQuence Element is beginning
1851 // fffe|e000 is just a marker, its length *should be* zero
1852 else if(Entry->GetGroup() == 0xfffe)
1854 // *normally, fffe|0000 doesn't exist !
1855 if( Entry->GetElement() != 0x0000 ) // gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1856 // causes extra troubles :-(
1859 Entry->SetUsableLength(FoundLength);
1863 * \ingroup gdcmParser
1864 * \brief Apply some heuristics to predict whether the considered
1865 * element value contains/represents an integer or not.
1866 * @param Entry The element value on which to apply the predicate.
1867 * @return The result of the heuristical predicate.
1869 bool gdcmParser::IsHeaderEntryAnInteger(gdcmHeaderEntry *Entry) {
1870 guint16 element = Entry->GetElement();
1871 guint16 group = Entry->GetGroup();
1872 std::string vr = Entry->GetVR();
1873 guint32 length = Entry->GetLength();
1874 // When we have some semantics on the element we just read, and if we
1875 // a priori know we are dealing with an integer, then we shall be
1876 // able to swap it's element value properly.
1877 if ( element == 0 ) // This is the group length of the group
1883 std::ostringstream s;
1884 int filePosition = ftell(fp);
1885 s << "Erroneous Group Length element length on : (" \
1886 << std::hex << group << " , " << element
1887 << ") -before- position x(" << filePosition << ")"
1888 << "lgt : " << length;
1889 // These 2 lines commented out : a *very dirty* patch
1890 // to go on PrintHeader'ing gdcm-MR-PHILIPS-16-Multi-Seq.dcm.
1891 // have a glance at offset x(8336) ...
1892 // For *regular* headers, the test is useless..
1893 // lets's print a warning message and go on,
1894 // instead of giving up with an error message
1896 //std::cout << s.str().c_str() << std::endl;
1898 // dbg.Error("gdcmParser::IsHeaderEntryAnInteger",
1899 // s.str().c_str());
1902 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1908 * \ingroup gdcmParser
1909 * \brief Find the Length till the next sequence delimiter
1910 * \warning NOT end user intended method !
1914 guint32 gdcmParser::FindHeaderEntryLengthOB(void) {
1915 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1918 long PositionOnEntry = ftell(fp);
1919 bool FoundSequenceDelimiter = false;
1920 guint32 TotalLength = 0;
1923 while ( ! FoundSequenceDelimiter)
1929 TotalLength += 4; // We even have to decount the group and element
1931 if ( g != 0xfffe && g!=0xb00c ) //for bogus header
1933 char msg[100]; // for sprintf. Sorry
1934 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
1935 dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
1939 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) // for bogus header
1940 FoundSequenceDelimiter = true;
1941 else if ( n != 0xe000 )
1943 char msg[100]; // for sprintf. Sorry
1944 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
1946 dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
1950 ItemLength = ReadInt32();
1951 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
1952 // the ItemLength with ReadInt32
1953 SkipBytes(ItemLength);
1955 fseek(fp, PositionOnEntry, SEEK_SET);
1960 * \ingroup gdcmParser
1961 * \brief Reads a supposed to be 16 Bits integer
1962 * (swaps it depending on processor endianity)
1963 * @return read value
1965 guint16 gdcmParser::ReadInt16(void) {
1968 item_read = fread (&g, (size_t)2,(size_t)1, fp);
1969 if ( item_read != 1 ) {
1971 dbg.Verbose(0, "gdcmParser::ReadInt16", " File Error");
1981 * \ingroup gdcmParser
1982 * \brief Reads a supposed to be 32 Bits integer
1983 * (swaps it depending on processor endianity)
1984 * @return read value
1986 guint32 gdcmParser::ReadInt32(void) {
1989 item_read = fread (&g, (size_t)4,(size_t)1, fp);
1990 if ( item_read != 1 ) {
1992 dbg.Verbose(0, "gdcmParser::ReadInt32", " File Error");
2002 * \ingroup gdcmParser
2003 * \brief skips bytes inside the source file
2004 * \warning NOT end user intended method !
2007 void gdcmParser::SkipBytes(guint32 NBytes) {
2008 //FIXME don't dump the returned value
2009 (void)fseek(fp, (long)NBytes, SEEK_CUR);
2013 * \ingroup gdcmParser
2014 * \brief Loads all the needed Dictionaries
2015 * \warning NOT end user intended method !
2017 void gdcmParser::Initialise(void)
2019 RefPubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
2020 RefShaDict = (gdcmDict*)0;
2024 * \ingroup gdcmParser
2025 * \brief Discover what the swap code is (among little endian, big endian,
2026 * bad little endian, bad big endian).
2028 * @return false when we are absolutely sure
2029 * it's neither ACR-NEMA nor DICOM
2030 * true when we hope ours assuptions are OK
2032 bool gdcmParser::CheckSwap() {
2034 // The only guaranted way of finding the swap code is to find a
2035 // group tag since we know it's length has to be of four bytes i.e.
2036 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2037 // occurs when we can't find such group...
2039 guint32 x=4; // x : for ntohs
2040 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2046 char deb[HEADER_LENGTH_TO_READ];
2048 // First, compare HostByteOrder and NetworkByteOrder in order to
2049 // determine if we shall need to swap bytes (i.e. the Endian type).
2055 // The easiest case is the one of a DICOM header, since it possesses a
2056 // file preamble where it suffice to look for the string "DICM".
2057 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2060 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2061 dbg.Verbose(1, "gdcmParser::CheckSwap:", "looks like DICOM Version3");
2063 // Next, determine the value representation (VR). Let's skip to the
2064 // first element (0002, 0000) and check there if we find "UL"
2065 // - or "OB" if the 1st one is (0002,0001) -,
2066 // in which case we (almost) know it is explicit VR.
2067 // WARNING: if it happens to be implicit VR then what we will read
2068 // is the length of the group. If this ascii representation of this
2069 // length happens to be "UL" then we shall believe it is explicit VR.
2070 // FIXME: in order to fix the above warning, we could read the next
2071 // element value (or a couple of elements values) in order to make
2072 // sure we are not commiting a big mistake.
2073 // We need to skip :
2074 // * the 128 bytes of File Preamble (often padded with zeroes),
2075 // * the 4 bytes of "DICM" string,
2076 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2077 // i.e. a total of 136 bytes.
2081 // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2082 // but elem 0002,0010 (Transfert Syntax) tells us the file is *Implicit* VR.
2085 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2086 (memcmp(entCur, "OB", (size_t)2) == 0) ||
2087 (memcmp(entCur, "UI", (size_t)2) == 0) ||
2088 (memcmp(entCur, "CS", (size_t)2) == 0) ) // CS, to remove later
2089 // when Write DCM *adds*
2091 // Use gdcmParser::dicom_vr to test all the possibilities
2092 // instead of just checking for UL, OB and UI !? // group 0000
2095 filetype = ExplicitVR;
2096 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2097 "explicit Value Representation");
2101 filetype = ImplicitVR;
2102 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2103 "not an explicit Value Representation");
2109 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2110 "HostByteOrder != NetworkByteOrder");
2115 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2116 "HostByteOrder = NetworkByteOrder");
2119 // Position the file position indicator at first tag (i.e.
2120 // after the file preamble and the "DICM" string).
2122 fseek (fp, 132L, SEEK_SET);
2126 // Alas, this is not a DicomV3 file and whatever happens there is no file
2127 // preamble. We can reset the file position indicator to where the data
2128 // is (i.e. the beginning of the file).
2129 dbg.Verbose(1, "gdcmParser::CheckSwap:", "not a DICOM Version3 file");
2132 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2133 // By clean we mean that the length of the first tag is written down.
2134 // If this is the case and since the length of the first group HAS to be
2135 // four (bytes), then determining the proper swap code is straightforward.
2138 // We assume the array of char we are considering contains the binary
2139 // representation of a 32 bits integer. Hence the following dirty
2141 s32 = *((guint32 *)(entCur));
2162 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2163 // It is time for despaired wild guesses.
2164 // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2165 // i.e. the 'group length' element is not present :
2167 // check the supposed to be 'group number'
2168 // 0x0002 or 0x0004 or 0x0008
2169 // to determine ' sw' value .
2170 // Only 0 or 4321 will be possible
2171 // (no oportunity to check for the formerly well known
2172 // ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian'
2173 // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -4, 8-)
2174 // the file IS NOT ACR-NEMA nor DICOM V3
2175 // Find a trick to tell it the caller...
2177 s16 = *((guint16 *)(deb));
2193 dbg.Verbose(0, "gdcmParser::CheckSwap:",
2194 "ACR/NEMA unfound swap info (Really hopeless !)");
2199 // Then the only info we have is the net2host one.
2209 * \ingroup gdcmParser
2210 * \brief Restore the unproperly loaded values i.e. the group, the element
2211 * and the dictionary entry depending on them.
2213 void gdcmParser::SwitchSwapToBigEndian(void)
2215 dbg.Verbose(1, "gdcmParser::SwitchSwapToBigEndian",
2216 "Switching to BigEndian mode.");
2237 * \ingroup gdcmParser
2238 * \brief during parsing, Header Elements too long are not loaded in memory
2241 void gdcmParser::SetMaxSizeLoadEntry(long NewSize)
2245 if ((guint32)NewSize >= (guint32)0xffffffff)
2247 MaxSizeLoadEntry = 0xffffffff;
2250 MaxSizeLoadEntry = NewSize;
2255 * \ingroup gdcmParser
2256 * \brief Header Elements too long will not be printed
2258 * \todo : not yet usable
2259 * (see MAX_SIZE_PRINT_ELEMENT_VALUE
2260 * in gdcmHeaderEntry gdcmLoadEntry)
2264 void gdcmParser::SetMaxSizePrintEntry(long NewSize)
2268 if ((guint32)NewSize >= (guint32)0xffffffff)
2270 MaxSizePrintEntry = 0xffffffff;
2273 MaxSizePrintEntry = NewSize;
2277 * \ingroup gdcmParser
2278 * \brief Searches both the public and the shadow dictionary (when they
2279 * exist) for the presence of the DictEntry with given name.
2280 * The public dictionary has precedence on the shadow one.
2281 * @param Name name of the searched DictEntry
2282 * @return Corresponding DictEntry when it exists, NULL otherwise.
2284 gdcmDictEntry *gdcmParser::GetDictEntryByName(std::string Name)
2286 gdcmDictEntry *found = (gdcmDictEntry *)0;
2287 if (!RefPubDict && !RefShaDict)
2289 dbg.Verbose(0, "gdcmParser::GetDictEntry",
2290 "we SHOULD have a default dictionary");
2294 found = RefPubDict->GetDictEntryByName(Name);
2300 found = RefShaDict->GetDictEntryByName(Name);
2308 * \ingroup gdcmParser
2309 * \brief Searches both the public and the shadow dictionary (when they
2310 * exist) for the presence of the DictEntry with given
2311 * group and element. The public dictionary has precedence on the
2313 * @param group group of the searched DictEntry
2314 * @param element element of the searched DictEntry
2315 * @return Corresponding DictEntry when it exists, NULL otherwise.
2317 gdcmDictEntry *gdcmParser::GetDictEntryByNumber(guint16 group,guint16 element)
2319 gdcmDictEntry *found = (gdcmDictEntry *)0;
2320 if (!RefPubDict && !RefShaDict)
2322 dbg.Verbose(0, "gdcmParser::GetDictEntry",
2323 "we SHOULD have a default dictionary");
2327 found = RefPubDict->GetDictEntryByNumber(group, element);
2333 found = RefShaDict->GetDictEntryByNumber(group, element);
2341 * \ingroup gdcmParser
2342 * \brief Read the next tag but WITHOUT loading it's value
2343 * @return On succes the newly created HeaderEntry, NULL on failure.
2345 gdcmHeaderEntry *gdcmParser::ReadNextHeaderEntry(void) {
2347 gdcmHeaderEntry *NewEntry;
2352 // We reached the EOF (or an error occured) therefore
2353 // header parsing has to be considered as finished.
2354 return (gdcmHeaderEntry *)0;
2356 // Pb : how to propagate the element length (used in SkipHeaderEntry)
2357 // direct call to SkipBytes ?
2359 // if (ignoreShadow == 1 && g%2 ==1)
2360 // if user wants to skip shadow groups
2361 // and current element *is* a shadow element
2362 // we don't create anything
2363 // return (gdcmHeaderEntry *)1; // to tell caller it's NOT finished
2365 NewEntry = NewHeaderEntryByNumber(g, n);
2366 FindHeaderEntryVR(NewEntry);
2367 FindHeaderEntryLength(NewEntry);
2373 NewEntry->SetOffset(ftell(fp));
2378 * \ingroup gdcmParser
2379 * \brief Build a new Element Value from all the low level arguments.
2380 * Check for existence of dictionary entry, and build
2381 * a default one when absent.
2382 * @param Name Name of the underlying DictEntry
2384 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByName(std::string Name)
2386 gdcmDictEntry *NewTag = GetDictEntryByName(Name);
2388 NewTag = NewVirtualDictEntry(0xffff, 0xffff, "LO", "unkn", Name);
2390 gdcmHeaderEntry* NewEntry = new gdcmHeaderEntry(NewTag);
2393 dbg.Verbose(1, "gdcmParser::ObtainHeaderEntryByName",
2394 "failed to allocate gdcmHeaderEntry");
2395 return (gdcmHeaderEntry *)0;
2401 * \ingroup gdcmParser
2402 * \brief Request a new virtual dict entry to the dict set
2403 * @param group group of the underlying DictEntry
2404 * @param element element of the underlying DictEntry
2405 * @param vr VR of the underlying DictEntry
2406 * @param fourth owner group
2407 * @param name english name
2409 gdcmDictEntry *gdcmParser::NewVirtualDictEntry(guint16 group, guint16 element,
2414 return gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,element,vr,fourth,name);
2418 * \ingroup gdcmParser
2419 * \brief Build a new Element Value from all the low level arguments.
2420 * Check for existence of dictionary entry, and build
2421 * a default one when absent.
2422 * @param Group group of the underlying DictEntry
2423 * @param Elem element of the underlying DictEntry
2425 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByNumber(guint16 Group, guint16 Elem)
2427 // Find out if the tag we encountered is in the dictionaries:
2428 gdcmDictEntry *DictEntry = GetDictEntryByNumber(Group, Elem);
2430 DictEntry = NewVirtualDictEntry(Group, Elem);
2432 gdcmHeaderEntry *NewEntry = new gdcmHeaderEntry(DictEntry);
2435 dbg.Verbose(1, "gdcmParser::NewHeaderEntryByNumber",
2436 "failed to allocate gdcmHeaderEntry");
2442 // Never used; commented out, waiting for removal.
2444 * \ingroup gdcmParser
2445 * \brief Small utility function that creates a new manually crafted
2446 * (as opposed as read from the file) gdcmHeaderEntry with user
2447 * specified name and adds it to the public tag hash table.
2448 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
2449 * @param NewTagName The name to be given to this new tag.
2450 * @param VR The Value Representation to be given to this new tag.
2451 * @return The newly hand crafted Element Value.
2453 //gdcmHeaderEntry *gdcmParser::NewManualHeaderEntryToPubDict(std::string NewTagName,
2456 // gdcmHeaderEntry *NewEntry = NULL;
2457 // guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
2458 // guint32 FreeElem = 0;
2459 // gdcmDictEntry *DictEntry = NULL;
2461 // FreeElem = GenerateFreeTagKeyInGroup(StuffGroup);
2462 // if (FreeElem == UINT32_MAX)
2464 // dbg.Verbose(1, "gdcmHeader::NewManualHeaderEntryToPubDict",
2465 // "Group 0xffff in Public Dict is full");
2469 // DictEntry = NewVirtualDictEntry(StuffGroup, FreeElem,
2470 // VR, "GDCM", NewTagName);
2471 // NewEntry = new gdcmHeaderEntry(DictEntry);
2472 // AddHeaderEntry(NewEntry);
2477 * \ingroup gdcmParser
2478 * \brief Generate a free TagKey i.e. a TagKey that is not present
2479 * in the TagHt dictionary.
2480 * @param group The generated tag must belong to this group.
2481 * @return The element of tag with given group which is fee.
2483 guint32 gdcmParser::GenerateFreeTagKeyInGroup(guint16 group)
2485 for (guint32 elem = 0; elem < UINT32_MAX; elem++)
2487 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
2488 if (tagHT.count(key) == 0)
2494 //-----------------------------------------------------------------------------