2 //-----------------------------------------------------------------------------
3 //#define UINT32_MAX (4294967295U)
6 #include "gdcmParser.h"
7 #include "gdcmGlobal.h"
18 #include <netinet/in.h>
23 #define UI1_2_840_10008_1_2 "1.2.840.10008.1.2"
24 #define UI1_2_840_10008_1_2_1 "1.2.840.10008.1.2.1"
25 #define UI1_2_840_10008_1_2_2 "1.2.840.10008.1.2.2"
26 #define UI1_2_840_10008_1_2_1_99 "1.2.840.10008.1.2.1.99"
29 guint32 totalSQlength;
30 guint32 alreadyParsedlength;
33 //-----------------------------------------------------------------------------
34 // Refer to gdcmParser::CheckSwap()
35 const unsigned int gdcmParser::HEADER_LENGTH_TO_READ = 256;
37 // Refer to gdcmParser::SetMaxSizeLoadEntry()
38 const unsigned int gdcmParser::MAX_SIZE_LOAD_ELEMENT_VALUE = 4096;
40 // Refer to gdcmParser::SetMaxSizePrintEntry()
41 // TODO : Right now, better see "define, in gdcmHederEntry.cxx
42 const unsigned int gdcmParser::MAX_SIZE_PRINT_ELEMENT_VALUE = 64;
44 //-----------------------------------------------------------------------------
45 // Constructor / Destructor
49 * @param inFilename file to be opened for parsing
50 * @param exception_on_error whether we throw an exception or not
51 * @param enable_sequences = true to allow the header
52 * to be parsed *inside* the SeQuences,
53 * when they have an actual length
54 * \warning enable_sequences *has to be* true for reading PAPYRUS 3.0 files
55 * @param ignore_shadow to allow skipping the shadow elements,
56 * to save memory space.
57 * \warning The TRUE value for this param has to be used
58 * with a FALSE value for the 'enable_sequence' param.
59 * ('public elements' may be embedded in 'shadow Sequences')
61 gdcmParser::gdcmParser(const char *inFilename,
62 bool exception_on_error,
63 bool enable_sequences,
65 enableSequences=enable_sequences;
66 ignoreShadow =ignore_shadow;
68 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
69 filename = inFilename;
72 if ( !OpenFile(exception_on_error))
78 wasUpdated = 0; // will be set to 1 if user adds an entry
79 printLevel = 1; // 'Medium' print level by default
85 * @param exception_on_error
87 gdcmParser::gdcmParser(bool exception_on_error) {
88 (void)exception_on_error;
91 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
94 wasUpdated = 0; // will be set to 1 if user adds an entry
95 printLevel = 1; // 'Medium' print level by default
100 * \brief Canonical destructor.
102 gdcmParser::~gdcmParser (void) {
107 //-----------------------------------------------------------------------------
110 * \ingroup gdcmParser
111 * \brief Prints the Header Entries (Dicom Elements)
112 * from the chained list
115 void gdcmParser::PrintEntry(std::ostream & os) {
117 for (ListTag::iterator i = listEntries.begin();
118 i != listEntries.end();
121 (*i)->SetPrintLevel(printLevel);
128 * \brief Prints The Dict Entries of THE public Dicom Dictionary
131 void gdcmParser::PrintPubDict(std::ostream & os) {
132 RefPubDict->Print(os);
136 * \brief Prints The Dict Entries of THE shadow Dicom Dictionary
139 void gdcmParser::PrintShaDict(std::ostream & os) {
140 RefShaDict->Print(os);
143 //-----------------------------------------------------------------------------
146 * \brief Get the public dictionary used
148 gdcmDict *gdcmParser::GetPubDict(void) {
153 * \brief Get the shadow dictionary used
155 gdcmDict *gdcmParser::GetShaDict(void) {
160 * \brief Set the shadow dictionary used
161 * \param dict dictionary to use in shadow
163 bool gdcmParser::SetShaDict(gdcmDict *dict){
169 * \brief Set the shadow dictionary used
170 * \param dictName name of the dictionary to use in shadow
172 bool gdcmParser::SetShaDict(DictKey dictName){
173 RefShaDict=gdcmGlobal::GetDicts()->GetDict(dictName);
178 * \brief This predicate, based on hopefully reasonable heuristics,
179 * decides whether or not the current gdcmParser was properly parsed
180 * and contains the mandatory information for being considered as
181 * a well formed and usable Dicom/Acr File.
182 * @return true when gdcmParser is the one of a reasonable Dicom/Acr file,
185 bool gdcmParser::IsReadable(void) {
186 if(filetype==Unknown) {
189 if(listEntries.size()<=0) {
197 * \brief Determines if the Transfer Syntax was already encountered
198 * and if it corresponds to a ImplicitVRLittleEndian one.
199 * @return True when ImplicitVRLittleEndian found. False in all other cases.
201 bool gdcmParser::IsImplicitVRLittleEndianTransferSyntax(void) {
202 gdcmHeaderEntry *Element = GetHeaderEntryByNumber(0x0002, 0x0010);
205 LoadHeaderEntrySafe(Element);
207 std::string Transfer = Element->GetValue();
208 if ( Transfer == UI1_2_840_10008_1_2 )
214 * \ingroup gdcmParser
215 * \brief Determines if the Transfer Syntax was already encountered
216 * and if it corresponds to a ExplicitVRLittleEndian one.
217 * @return True when ExplicitVRLittleEndian found. False in all other cases.
219 bool gdcmParser::IsExplicitVRLittleEndianTransferSyntax(void) {
220 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
223 LoadHeaderEntrySafe(Element);
225 std::string Transfer = Element->GetValue();
226 if ( Transfer == UI1_2_840_10008_1_2_1 )
232 * \brief Determines if the Transfer Syntax was already encountered
233 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
234 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
236 bool gdcmParser::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
237 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
240 LoadHeaderEntrySafe(Element);
242 std::string Transfer = Element->GetValue();
243 if ( Transfer == UI1_2_840_10008_1_2_1_99 )
249 * \brief Determines if the Transfer Syntax was already encountered
250 * and if it corresponds to a Explicit VR Big Endian one.
251 * @return True when big endian found. False in all other cases.
253 bool gdcmParser::IsExplicitVRBigEndianTransferSyntax(void) {
254 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
257 LoadHeaderEntrySafe(Element);
259 std::string Transfer = Element->GetValue();
260 if ( Transfer == UI1_2_840_10008_1_2_2 ) //1.2.2 ??? A verifier !
266 * \brief returns the File Type
267 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
268 * @return the FileType code
270 FileType gdcmParser::GetFileType(void) {
275 * \brief opens the file
276 * @param exception_on_error
279 FILE *gdcmParser::OpenFile(bool exception_on_error)
282 fp=fopen(filename.c_str(),"rb");
283 if(exception_on_error)
286 throw gdcmFileError("gdcmParser::gdcmParser(const char *, bool)");
292 fread(&zero, (size_t)2, (size_t)1, fp);
294 //ACR -- or DICOM with no Preamble --
295 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
299 fseek(fp, 126L, SEEK_CUR);
301 fread(dicm, (size_t)4, (size_t)1, fp);
302 if( memcmp(dicm, "DICM", 4) == 0 )
306 dbg.Verbose(0, "gdcmParser::OpenFile not DICOM/ACR", filename.c_str());
309 dbg.Verbose(0, "gdcmParser::OpenFile cannot open file", filename.c_str());
315 * \brief closes the file
316 * @return TRUE if the close was successfull
318 bool gdcmParser::CloseFile(void) {
319 int closed = fclose(fp);
327 * \brief Writes in a file all the Header Entries (Dicom Elements)
328 * of the Chained List
329 * @param fp file pointer on an already open file
330 * @param type Type of the File to be written
331 * (ACR-NEMA, ExplicitVR, ImplicitVR)
332 * \return Always true.
334 bool gdcmParser::Write(FILE *fp, FileType type) {
336 // TODO The stuff will have to be rewritten using the SeQuence based
337 // tree-like stucture instead of the chained list .
338 // (so we shall remove the GroupHT from the gdcmParser)
342 // TODO : move the following lines (and a lot of others, to be written)
343 // to a future function CheckAndCorrectHeader
346 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier
347 // DicomV3 ou non (FileType est un champ de gdcmParser ...)
348 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
350 // a moins de se livrer a un tres complique ajout des champs manquants.
351 // faire un CheckAndCorrectHeader (?)
353 if (type == ImplicitVR)
355 std::string implicitVRTransfertSyntax = UI1_2_840_10008_1_2;
356 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
358 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
359 // values with a VR of UI shall be padded with a single trailing null
360 // Dans le cas suivant on doit pader manuellement avec un 0
362 SetEntryLengthByNumber(18, 0x0002, 0x0010);
365 if (type == ExplicitVR)
367 std::string explicitVRTransfertSyntax = UI1_2_840_10008_1_2_1;
368 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
370 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
371 // values with a VR of UI shall be padded with a single trailing null
372 // Dans le cas suivant on doit pader manuellement avec un 0
374 SetEntryLengthByNumber(20, 0x0002, 0x0010);
377 /* TODO : rewrite later, if really usefull
379 --> Warning : un-updated odd groups lengths can causes pb
381 --> to be re- written with future org.
383 if ( (type == ImplicitVR) || (type == ExplicitVR) )
384 UpdateGroupLength(false,type);
386 UpdateGroupLength(true,ACR);
389 WriteEntries(fp,type);
394 * \brief Modifies the value of a given Header Entry (Dicom Element)
395 * when it exists. Create it with the given value when unexistant.
396 * \warning Adds the Header Entry to the HTable, NOT to the chained List
397 * @param Value Value to be set
398 * @param Group Group of the Entry
399 * @param Elem Element of the Entry
400 * \return pointer to the modified/created Header Entry (NULL when creation
403 gdcmHeaderEntry * gdcmParser::ReplaceOrCreateByNumber(
408 a = GetHeaderEntryByNumber( Group, Elem);
410 a =NewHeaderEntryByNumber(Group, Elem);
415 //CLEANME SetEntryByNumber(Value, Group, Elem);
421 * \brief Set a new value if the invoked element exists
422 * Seems to be useless !!!
423 * @param Value new element value
424 * @param Group group of the Entry
425 * @param Elem element of the Entry
428 bool gdcmParser::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem )
430 std::string v = Value;
431 SetEntryByNumber(v, Group, Elem);
435 //-----------------------------------------------------------------------------
439 * \brief Checks if a given Dicom Element exists
441 * @param group Group number of the searched Dicom Element
442 * @param element Element number of the searched Dicom Element
443 * @return number of occurences
445 int gdcmParser::CheckIfEntryExistByNumber(guint16 group, guint16 element ) {
446 std::string key = gdcmDictEntry::TranslateToKey(group, element );
447 return (tagHT.count(key));
451 * \ingroup gdcmParser
452 * \brief Searches within Header Entries (Dicom Elements) parsed with
453 * the public and private dictionaries
454 * for the element value of a given tag.
455 * \warning Don't use any longer : use GetPubEntryByName
456 * @param tagName name of the searched element.
457 * @return Corresponding element value when it exists,
458 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
460 std::string gdcmParser::GetEntryByName(std::string tagName) {
461 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
462 if( dictEntry == NULL)
465 return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
469 * \ingroup gdcmParser
470 * \brief Searches within Header Entries (Dicom Elements) parsed with
471 * the public and private dictionaries
472 * for the element value representation of a given tag.
474 * Obtaining the VR (Value Representation) might be needed by caller
475 * to convert the string typed content to caller's native type
476 * (think of C++ vs Python). The VR is actually of a higher level
477 * of semantics than just the native C++ type.
478 * @param tagName name of the searched element.
479 * @return Corresponding element value representation when it exists,
480 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
482 std::string gdcmParser::GetEntryVRByName(std::string tagName) {
483 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
484 if( dictEntry == NULL)
487 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(dictEntry->GetGroup(),
488 dictEntry->GetElement());
489 return elem->GetVR();
494 * \ingroup gdcmParser
495 * \brief Searches within Header Entries (Dicom Elements) parsed with
496 * the public and private dictionaries
497 * for the element value representation of a given tag.
498 * @param group Group of the searched tag.
499 * @param element Element of the searched tag.
500 * @return Corresponding element value representation when it exists,
501 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
503 std::string gdcmParser::GetEntryByNumber(guint16 group, guint16 element){
504 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
505 if ( ! tagHT.count(key))
507 return tagHT.find(key)->second->GetValue();
511 * \ingroup gdcmParser
512 * \brief Searches within Header Entries (Dicom Elements) parsed with
513 * the public and private dictionaries
514 * for the element value representation of a given tag..
516 * Obtaining the VR (Value Representation) might be needed by caller
517 * to convert the string typed content to caller's native type
518 * (think of C++ vs Python). The VR is actually of a higher level
519 * of semantics than just the native C++ type.
520 * @param group Group of the searched tag.
521 * @param element Element of the searched tag.
522 * @return Corresponding element value representation when it exists,
523 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
525 std::string gdcmParser::GetEntryVRByNumber(guint16 group, guint16 element) {
526 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
529 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 value length of a given tag..
537 * @param group Group of the searched tag.
538 * @param element Element of the searched tag.
539 * @return Corresponding element length; -2 if not found
541 int gdcmParser::GetEntryLengthByNumber(guint16 group, guint16 element) {
542 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
545 return elem->GetLength();
548 * \ingroup gdcmParser
549 * \brief Sets the value (string) of the Header Entry (Dicom Element)
550 * @param content string value of the Dicom Element
551 * @param tagName name of the searched Dicom Element.
552 * @return true when found
554 bool gdcmParser::SetEntryByName(std::string content,std::string tagName) {
555 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
556 if( dictEntry == NULL)
559 return(SetEntryByNumber(content,dictEntry->GetGroup(),
560 dictEntry->GetElement()));
564 * \ingroup gdcmParser
565 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
566 * through it's (group, element) and modifies it's content with
568 * \warning Don't use any longer : use SetPubEntryByNumber
569 * @param content new value to substitute with
570 * @param group group of the Dicom Element to modify
571 * @param element element of the Dicom Element to modify
573 bool gdcmParser::SetEntryByNumber(std::string content,
577 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
578 if ( ! tagHT.count(key))
580 int l = content.length();
581 if(l%2) // Non even length are padded with a space (020H).
584 content = content + '\0';
589 TagHeaderEntryHT::iterator p2;
590 // DO NOT remove the following lines : they explain the stuff
591 //p= tagHT.equal_range(key); // get a pair of iterators first-last synonym
592 //p2=p.first; // iterator on the first synonym
593 //a=p2->second; // H Table target column (2-nd col)
596 a = ((tagHT.equal_range(key)).first)->second;
598 a-> SetValue(content);
600 std::string vr = a->GetVR();
603 if( (vr == "US") || (vr == "SS") )
605 else if( (vr == "UL") || (vr == "SL") )
615 * \ingroup gdcmParser
616 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
617 * in the PubHeaderEntrySet of this instance
618 * through it's (group, element) and modifies it's length with
620 * \warning Use with extreme caution.
621 * @param l new length to substitute with
622 * @param group group of the Entry to modify
623 * @param element element of the Entry to modify
624 * @return true on success, false otherwise.
626 bool gdcmParser::SetEntryLengthByNumber(guint32 l,
630 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
631 if ( ! tagHT.count(key))
633 if (l%2) l++; // length must be even
634 ( ((tagHT.equal_range(key)).first)->second )->SetLength(l);
640 * \ingroup gdcmParser
641 * \brief Gets (from Header) the offset of a 'non string' element value
642 * (LoadElementValues has already be executed)
643 * @param Group group of the Entry
644 * @param Elem element of the Entry
645 * @return File Offset of the Element Value
647 size_t gdcmParser::GetEntryOffsetByNumber(guint16 Group, guint16 Elem)
649 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
652 dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
653 "failed to Locate gdcmHeaderEntry");
656 return Entry->GetOffset();
660 * \ingroup gdcmParser
661 * \brief Gets (from Header) a 'non string' element value
662 * (LoadElementValues has already be executed)
663 * @param Group group of the Entry
664 * @param Elem element of the Entry
665 * @return Pointer to the 'non string' area
667 void * gdcmParser::GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem)
669 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
672 dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
673 "failed to Locate gdcmHeaderEntry");
676 return Entry->GetVoidArea();
680 * \brief Loads (from disk) the element content
681 * when a string is not suitable
682 * @param Group group of the Entry
683 * @param Elem element of the Entry
685 void *gdcmParser::LoadEntryVoidArea(guint16 Group, guint16 Elem)
687 gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
690 size_t o =(size_t)Element->GetOffset();
691 fseek(fp, o, SEEK_SET);
692 size_t l=Element->GetLength();
693 char* a = new char[l];
697 SetEntryVoidAreaByNumber(a, Group, Elem);
698 // TODO check the result
699 size_t l2 = fread(a, 1, l ,fp);
710 * \ingroup gdcmParser
711 * \brief Sets a 'non string' value to a given Dicom Element
713 * @param group Group number of the searched Dicom Element
714 * @param element Element number of the searched Dicom Element
717 bool gdcmParser::SetEntryVoidAreaByNumber(void * area,
721 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
722 if ( ! tagHT.count(key))
724 ( ((tagHT.equal_range(key)).first)->second )->SetVoidArea(area);
729 * \ingroup gdcmParser
730 * \brief Update the entries with the shadow dictionary.
731 * Only non even entries are analyzed
733 void gdcmParser::UpdateShaEntries(void) {
734 gdcmDictEntry *entry;
737 for(ListTag::iterator it=listEntries.begin();
738 it!=listEntries.end();
741 // Odd group => from public dictionary
742 if((*it)->GetGroup()%2==0)
745 // Peer group => search the corresponding dict entry
747 entry=RefShaDict->GetDictEntryByNumber((*it)->GetGroup(),(*it)->GetElement());
751 if((*it)->IsImplicitVR())
756 (*it)->SetValue(GetHeaderEntryUnvalue(*it));
758 // Set the new entry and the new value
759 (*it)->SetDictEntry(entry);
760 CheckHeaderEntryVR(*it,vr);
762 (*it)->SetValue(GetHeaderEntryValue(*it));
766 // Remove precedent value transformation
767 (*it)->SetDictEntry(NewVirtualDictEntry((*it)->GetGroup(),(*it)->GetElement(),vr));
773 * \ingroup gdcmParser
774 * \brief Searches within the Header Entries for a Dicom Element of
776 * @param tagName name of the searched Dicom Element.
777 * @return Corresponding Dicom Element when it exists, and NULL
780 gdcmHeaderEntry *gdcmParser::GetHeaderEntryByName(std::string tagName) {
781 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
782 if( dictEntry == NULL)
785 return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
789 * \ingroup gdcmParser
790 * \brief retrieves a Dicom Element (the first one) using (group, element)
791 * \warning (group, element) IS NOT an identifier inside the Dicom Header
792 * if you think it's NOT UNIQUE, check the count number
793 * and use iterators to retrieve ALL the Dicoms Elements within
794 * a given couple (group, element)
795 * @param group Group number of the searched Dicom Element
796 * @param element Element number of the searched Dicom Element
799 gdcmHeaderEntry* gdcmParser::GetHeaderEntryByNumber(guint16 group, guint16 element)
801 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
802 if ( ! tagHT.count(key))
804 return tagHT.find(key)->second;
808 * \ingroup gdcmParser
809 * \brief retrieves the Dicom Elements (all of them) using (group, element)
810 * @param group Group number of the searched Dicom Element.
811 * @param element Element number of the searched Dicom Element.
812 * @return a range (i.e.pair<,>) containing all elements whose key is group|element)
815 IterHT gdcmParser::GetHeaderEntrySameNumber(guint16 group, guint16 element){
816 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
817 return (tagHT.equal_range(key));
821 * \ingroup gdcmParser
822 * \brief Loads the element while preserving the current
823 * underlying file position indicator as opposed to
824 * to LoadHeaderEntry that modifies it.
825 * @param entry Header Entry whose value shall be loaded.
828 void gdcmParser::LoadHeaderEntrySafe(gdcmHeaderEntry * entry) {
829 long PositionOnEntry = ftell(fp);
830 LoadHeaderEntry(entry);
831 fseek(fp, PositionOnEntry, SEEK_SET);
835 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
836 * \warning : to be re-written using the chained list instead of the H table.
837 * \warning : DO NOT use (doesn't work any longer because of the multimap)
838 * \todo : to be re-written using the chained list instead of the H table
839 * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
840 * @param type Type of the File (ExplicitVR,ImplicitVR, ACR, ...)
842 void gdcmParser::UpdateGroupLength(bool SkipSequence, FileType type) {
846 gdcmHeaderEntry *elem;
848 std::string str_trash;
851 GroupHT groupHt; // to hold the length of each group
854 // typedef std::map<GroupKey, int> GroupHT;
856 gdcmHeaderEntry *elemZ;
858 // for each Tag in the DCM Header
860 for (TagHeaderEntryHT::iterator tag2 = tagHT.begin();
865 gr = elem->GetGroup();
866 el = elem->GetElement();
869 sprintf(trash, "%04x", gr);
870 key = trash; // generate 'group tag'
872 // if the caller decided not to take SEQUENCEs into account
873 // e.g : he wants to write an ACR-NEMA File
875 if (SkipSequence && vr == "SQ")
878 // Still unsolved problem :
879 // we cannot find the 'Sequence Delimitation Item'
880 // since it's at the end of the Hash Table
883 // there is SEQUENCE in ACR-NEMA
885 // --> la descente a l'interieur' des SQ
886 // devrait etre faite avec une liste chainee, pas avec une HTable...
888 if ( groupHt.count(key) == 0) // we just read the first elem of a given group
890 if (el == 0x0000) // the first elem is 0x0000
892 groupHt[key] = 0; // initialize group length
896 groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
899 else // any elem but the first
901 if (type == ExplicitVR)
903 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
905 groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
908 groupHt[key] += 2 + 2 + 4 + elem->GetLength();
912 unsigned short int gr_bid;
914 for (GroupHT::iterator g = groupHt.begin(); // for each group we found
918 // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
919 // warning: unsigned int format, different type arg
920 sscanf(g->first.c_str(),"%x",&gr_bid); //FIXME
921 tk = g->first + "|0000"; // generate the element full tag
923 if ( tagHT.count(tk) == 0) // if element 0x0000 not found
925 gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
926 elemZ = new gdcmHeaderEntry(tagZ);
928 AddHeaderEntry(elemZ); // create it
932 elemZ=GetHeaderEntryByNumber(gr_bid, 0x0000);
934 sprintf(trash ,"%d",g->second);
936 elemZ->SetValue(str_trash);
941 * \brief Writes in a file (according to the requested format)
942 * the group, the element, the value representation and the length
943 * of a single gdcmHeaderEntry passed as argument.
944 * @param tag pointer on the gdcmHeaderEntry to be written
945 * @param _fp already open file pointer
946 * @param type type of the File to be written
948 void gdcmParser::WriteEntryTagVRLength(gdcmHeaderEntry *tag,
952 guint16 group = tag->GetGroup();
953 std::string vr = tag->GetVR();
954 guint16 el = tag->GetElement();
955 guint32 lgr = tag->GetReadLength();
957 if ( (group == 0xfffe) && (el == 0x0000) )
958 // Fix in order to make some MR PHILIPS images e-film readable
959 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
960 // we just *always* ignore spurious fffe|0000 tag !
963 fwrite ( &group,(size_t)2 ,(size_t)1 ,_fp); //group
964 fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
966 if ( type == ExplicitVR ) {
968 // Special case of delimiters:
969 if (group == 0xfffe) {
970 // Delimiters have NO Value Representation and have NO length.
971 // Hence we skip writing the VR and length and we pad by writing
975 fwrite (&ff,(size_t)4 ,(size_t)1 ,_fp);
980 guint16 shortLgr = lgr;
981 if (vr == "unkn") { // Unknown was 'written'
982 // deal with Little Endian
983 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
984 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
986 fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
987 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
989 fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
990 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
992 fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
998 fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
1003 * \brief Writes in a file (according to the requested format)
1004 * the value of a single gdcmHeaderEntry passed as argument.
1005 * @param tag Pointer on the gdcmHeaderEntry to be written
1006 * @param _fp Already open file pointer
1007 * @param type type of the File to be written
1009 void gdcmParser::WriteEntryValue(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
1012 guint16 group = tag->GetGroup();
1013 std::string vr = tag->GetVR();
1014 guint32 lgr = tag->GetReadLength();
1017 // SeQuences have no value:
1019 if (group == 0xfffe)
1020 // Delimiters have no associated value:
1024 voidArea = tag->GetVoidArea();
1025 if (voidArea != NULL)
1026 { // there is a 'non string' LUT, overlay, etc
1027 fwrite ( voidArea,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
1031 if (vr == "US" || vr == "SS")
1033 // some 'Short integer' fields may be mulivaluated
1034 // each single value is separated from the next one by '\'
1035 // we split the string and write each value as a short int
1036 std::vector<std::string> tokens;
1037 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1038 Tokenize (tag->GetValue(), tokens, "\\");
1039 for (unsigned int i=0; i<tokens.size();i++)
1041 guint16 val_uint16 = atoi(tokens[i].c_str());
1042 void *ptr = &val_uint16;
1043 fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
1048 // some 'Integer' fields may be mulivaluated
1049 // each single value is separated from the next one by '\'
1050 // we split the string and write each value as an int
1051 if (vr == "UL" || vr == "SL")
1053 std::vector<std::string> tokens;
1054 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1055 Tokenize (tag->GetValue(), tokens, "\\");
1056 for (unsigned int i=0; i<tokens.size();i++)
1058 guint32 val_uint32 = atoi(tokens[i].c_str());
1059 void *ptr = &val_uint32;
1060 fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
1065 fwrite (tag->GetValue().c_str(), (size_t)lgr ,(size_t)1, _fp); // Elem value
1069 * \brief Writes in a file (according to the requested format)
1070 * a single gdcmHeaderEntry passed as argument.
1071 * \sa WriteEntryValue, WriteEntryTagVRLength.
1072 * @param tag Pointer on the gdcmHeaderEntry to be written
1073 * @param _fp Already open file pointer
1074 * @param type type of the File to be written
1077 bool gdcmParser::WriteEntry(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
1079 guint32 length = tag->GetLength();
1081 // The value of a tag MUST (see the DICOM norm) be an odd number of
1082 // bytes. When this is not the case, pad with an additional byte:
1085 tag->SetValue(tag->GetValue()+"\0");
1086 tag->SetLength(tag->GetReadLength()+1);
1089 WriteEntryTagVRLength(tag, _fp, type);
1090 WriteEntryValue(tag, _fp, type);
1095 * \brief writes on disc according to the requested format
1096 * (ACR-NEMA, ExplicitVR, ImplicitVR) the image
1097 * using the Chained List
1098 * \warning does NOT add the missing elements in the header :
1099 * it's up to the user doing it !
1100 * (function CheckHeaderCoherence to be written)
1101 * \warning DON'T try, right now, to write a DICOM image
1102 * from an ACR Header (meta elements will be missing!)
1103 * \sa WriteEntriesDeprecated (Special temporary method for Theralys)
1104 * @param type type of the File to be written
1105 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1106 * @param _fp already open file pointer
1109 bool gdcmParser::WriteEntries(FILE *_fp,FileType type)
1111 // TODO (?) check write failures (after *each* fwrite)
1113 for (ListTag::iterator tag2=listEntries.begin();
1114 tag2 != listEntries.end();
1118 if ((*tag2)->GetGroup() < 0x0008)
1119 // Ignore pure DICOM V3 groups
1121 if ((*tag2)->GetElement() %2)
1122 // Ignore the "shadow" groups
1124 if ((*tag2)->GetVR() == "SQ" ) // ignore Sequences
1126 if ((*tag2)->GetSQDepthLevel() != 0) // Not only ignore the SQ element
1129 if (! WriteEntry(*tag2,_fp,type) )
1136 * \ingroup gdcmParser
1137 * \brief writes on disc according to the requested format
1138 * (ACR-NEMA, ExplicitVR, ImplicitVR) the image,
1139 * using only the last synonym of each mutimap H Table post.
1140 * \warning Uses the H Table, instead of the Chained List
1141 * in order to be compliant with the old way to proceed
1142 * (added elements taken in to account)
1143 * Only THERALYS, during a transitory phase is supposed
1144 * to use this method !!!
1145 * \warning DON'T try, right now, to write a DICOM image
1146 * from an ACR Header (meta elements will be missing!)
1148 * @param _fp already open file pointer
1149 * @param type type of the File to be written
1150 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1152 void gdcmParser::WriteEntriesDeprecated(FILE *_fp,FileType type) {
1154 // restent a tester les echecs en ecriture (apres chaque fwrite)
1156 for (TagHeaderEntryHT::iterator tag2=tagHT.begin();
1157 tag2 != tagHT.end();
1160 if ((*tag2->second).GetGroup() < 0x0008) continue; // ignore pure DICOM V3 groups
1161 if ((*tag2->second).GetElement() %2) continue; // ignore shadow groups
1162 if ((*tag2->second).GetVR() == "SQ" ) continue; // ignore Sequences
1163 if ((*tag2->second).GetSQDepthLevel() != 0) continue; // Not only ignore the SQ element
1165 if ( ! WriteEntry(tag2->second,_fp,type))
1171 * \ingroup gdcmParser
1172 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1174 * @return The properly swaped 32 bits integer.
1176 guint32 gdcmParser::SwapLong(guint32 a) {
1181 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1182 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1186 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1190 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1193 dbg.Error(" gdcmParser::SwapLong : unset swap code");
1200 * \ingroup gdcmParser
1201 * \brief Unswaps back the bytes of 4-byte long integer accordingly to
1203 * @return The properly unswaped 32 bits integer.
1205 guint32 gdcmParser::UnswapLong(guint32 a) {
1206 return (SwapLong(a));
1210 * \ingroup gdcmParser
1211 * \brief Swaps the bytes so they agree with the processor order
1212 * @return The properly swaped 16 bits integer.
1214 guint16 gdcmParser::SwapShort(guint16 a) {
1215 if ( (sw==4321) || (sw==2143) )
1216 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1221 * \ingroup gdcmParser
1222 * \brief Unswaps the bytes so they agree with the processor order
1223 * @return The properly unswaped 16 bits integer.
1225 guint16 gdcmParser::UnswapShort(guint16 a) {
1226 return (SwapShort(a));
1229 //-----------------------------------------------------------------------------
1232 * \ingroup gdcmParser
1233 * \brief Parses the header of the file and load element values.
1234 * @return false if file is not ACR-NEMA / PAPYRUS / DICOM
1236 bool gdcmParser::LoadHeaderEntries(bool exception_on_error) throw(gdcmFormatError) {
1237 (void)exception_on_error;
1242 gdcmHeaderEntry *newHeaderEntry = (gdcmHeaderEntry *)0;
1243 while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1244 SkipHeaderEntry(newHeaderEntry);
1245 if ( (ignoreShadow==0) || (newHeaderEntry->GetGroup()%2) == 0) {
1246 AddHeaderEntry(newHeaderEntry);
1250 // Be carefull : merging this two loops may cause troubles ...
1251 for (ListTag::iterator i = GetListEntry().begin();
1252 i != GetListEntry().end();
1255 LoadHeaderEntry(*i);
1259 // Load 'non string' values
1261 std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
1262 if( PhotometricInterpretation == "PALETTE COLOR " ) {
1263 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
1264 LoadEntryVoidArea(0x0028,0x1201); // R LUT
1265 LoadEntryVoidArea(0x0028,0x1202); // G LUT
1266 LoadEntryVoidArea(0x0028,0x1203); // B LUT
1268 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1269 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1270 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1272 //FIXME later : how to use it?
1273 LoadEntryVoidArea(0x0028,0x3006); //LUT Data (CTX dependent)
1275 // --------------------------------------------------------------
1276 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1278 // if recognition code tells us we deal with a LibIDO image
1279 // we switch lineNumber and columnNumber
1281 std::string RecCode;
1282 RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
1283 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1284 RecCode == "CANRME_AILIBOD1_1." ) // for brain-damaged softwares
1285 // with "little-endian strings"
1287 filetype = ACR_LIBIDO;
1288 std::string rows = GetEntryByNumber(0x0028, 0x0010);
1289 std::string columns = GetEntryByNumber(0x0028, 0x0011);
1290 SetEntryByNumber(columns, 0x0028, 0x0010);
1291 SetEntryByNumber(rows , 0x0028, 0x0011);
1293 // ----------------- End of Special Patch ----------------
1298 * \ingroup gdcmParser
1299 * \brief Loads the element content if its length doesn't exceed
1300 * the value specified with gdcmParser::SetMaxSizeLoadEntry()
1301 * @param Entry Header Entry (Dicom Element) to be dealt with
1303 void gdcmParser::LoadHeaderEntry(gdcmHeaderEntry *Entry) {
1305 guint16 group = Entry->GetGroup();
1306 std::string vr= Entry->GetVR();
1307 guint32 length = Entry->GetLength();
1309 fseek(fp, (long)Entry->GetOffset(), SEEK_SET);
1311 // A SeQuence "contains" a set of Elements.
1312 // (fffe e000) tells us an Element is beginning
1313 // (fffe e00d) tells us an Element just ended
1314 // (fffe e0dd) tells us the current SeQuence just ended
1315 if( group == 0xfffe ) {
1316 Entry->SetValue("gdcm::Skipped");
1320 // When the length is zero things are easy:
1321 if ( length == 0 ) {
1322 Entry->SetValue("");
1326 // The elements whose length is bigger than the specified upper bound
1327 // are not loaded. Instead we leave a short notice of the offset of
1328 // the element content and it's length.
1329 if (length > MaxSizeLoadEntry) {
1330 std::ostringstream s;
1331 s << "gdcm::NotLoaded.";
1332 s << " Address:" << (long)Entry->GetOffset();
1333 s << " Length:" << Entry->GetLength();
1334 s << " x(" << std::hex << Entry->GetLength() << ")";
1335 Entry->SetValue(s.str());
1339 // Any compacter code suggested (?)
1340 if ( IsHeaderEntryAnInteger(Entry) ) {
1342 std::ostringstream s;
1344 // When short integer(s) are expected, read and convert the following
1345 // n *two characters properly i.e. as short integers as opposed to strings.
1346 // Elements with Value Multiplicity > 1
1347 // contain a set of integers (not a single one)
1348 if (vr == "US" || vr == "SS") {
1350 NewInt = ReadInt16();
1353 for (int i=1; i < nbInt; i++) {
1355 NewInt = ReadInt16();
1360 // When integer(s) are expected, read and convert the following
1361 // n * four characters properly i.e. as integers as opposed to strings.
1362 // Elements with Value Multiplicity > 1
1363 // contain a set of integers (not a single one)
1364 else if (vr == "UL" || vr == "SL") {
1366 NewInt = ReadInt32();
1369 for (int i=1; i < nbInt; i++) {
1371 NewInt = ReadInt32();
1376 #ifdef GDCM_NO_ANSI_STRING_STREAM
1377 s << std::ends; // to avoid oddities on Solaris
1378 #endif //GDCM_NO_ANSI_STRING_STREAM
1380 Entry->SetValue(s.str());
1384 // We need an additional byte for storing \0 that is not on disk
1385 std::string NewValue(length,0);
1386 item_read = fread(&(NewValue[0]), (size_t)length, (size_t)1, fp);
1387 if ( item_read != 1 ) {
1388 dbg.Verbose(1, "gdcmParser::LoadElementValue","unread element value");
1389 Entry->SetValue("gdcm::UnRead");
1393 if( (vr == "UI") ) // Because of correspondance with the VR dic
1394 Entry->SetValue(NewValue.c_str());
1396 Entry->SetValue(NewValue);
1400 * \ingroup gdcmParser
1401 * \brief add a new Dicom Element pointer to
1402 * the H Table and at the end of the chained List
1403 * \warning push_bash in listEntries ONLY during ParseHeader
1404 * \todo something to allow further Elements addition,
1405 * (at their right place in the chained list)
1406 * when position to be taken care of
1407 * @param newHeaderEntry
1409 void gdcmParser::AddHeaderEntry(gdcmHeaderEntry *newHeaderEntry) {
1410 tagHT.insert( PairHT( newHeaderEntry->GetKey(),newHeaderEntry) );
1411 listEntries.push_back(newHeaderEntry);
1416 * \ingroup gdcmParser
1417 * \brief Find the value Length of the passed Header Entry
1418 * @param Entry Header Entry whose length of the value shall be loaded.
1420 void gdcmParser::FindHeaderEntryLength (gdcmHeaderEntry *Entry) {
1421 guint16 element = Entry->GetElement();
1422 //guint16 group = Entry->GetGroup(); //FIXME
1423 std::string vr = Entry->GetVR();
1427 if ( (filetype == ExplicitVR) && (! Entry->IsImplicitVR()) )
1429 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") )
1431 // The following reserved two bytes (see PS 3.5-2001, section
1432 // 7.1.2 Data element structure with explicit vr p27) must be
1433 // skipped before proceeding on reading the length on 4 bytes.
1434 fseek(fp, 2L, SEEK_CUR);
1435 guint32 length32 = ReadInt32();
1437 if ( (vr == "OB") && (length32 == 0xffffffff) )
1439 Entry->SetLength(FindHeaderEntryLengthOB());
1442 FixHeaderEntryFoundLength(Entry, length32);
1446 // Length is encoded on 2 bytes.
1447 length16 = ReadInt16();
1449 // We can tell the current file is encoded in big endian (like
1450 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1451 // and it's value is the one of the encoding of a big endian file.
1452 // In order to deal with such big endian encoded files, we have
1453 // (at least) two strategies:
1454 // * when we load the "Transfer Syntax" tag with value of big endian
1455 // encoding, we raise the proper flags. Then we wait for the end
1456 // of the META group (0x0002) among which is "Transfer Syntax",
1457 // before switching the swap code to big endian. We have to postpone
1458 // the switching of the swap code since the META group is fully encoded
1459 // in little endian, and big endian coding only starts at the next
1460 // group. The corresponding code can be hard to analyse and adds
1461 // many additional unnecessary tests for regular tags.
1462 // * the second strategy consists in waiting for trouble, that shall
1463 // appear when we find the first group with big endian encoding. This
1464 // is easy to detect since the length of a "Group Length" tag (the
1465 // ones with zero as element number) has to be of 4 (0x0004). When we
1466 // encounter 1024 (0x0400) chances are the encoding changed and we
1467 // found a group with big endian encoding.
1468 // We shall use this second strategy. In order to make sure that we
1469 // can interpret the presence of an apparently big endian encoded
1470 // length of a "Group Length" without committing a big mistake, we
1471 // add an additional check: we look in the already parsed elements
1472 // for the presence of a "Transfer Syntax" whose value has to be "big
1473 // endian encoding". When this is the case, chances are we have got our
1474 // hands on a big endian encoded file: we switch the swap code to
1475 // big endian and proceed...
1476 if ( (element == 0x0000) && (length16 == 0x0400) )
1478 if ( ! IsExplicitVRBigEndianTransferSyntax() )
1480 dbg.Verbose(0, "gdcmParser::FindLength", "not explicit VR");
1485 SwitchSwapToBigEndian();
1486 // Restore the unproperly loaded values i.e. the group, the element
1487 // and the dictionary entry depending on them.
1488 guint16 CorrectGroup = SwapShort(Entry->GetGroup());
1489 guint16 CorrectElem = SwapShort(Entry->GetElement());
1490 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
1494 // This correct tag is not in the dictionary. Create a new one.
1495 NewTag = NewVirtualDictEntry(CorrectGroup, CorrectElem);
1497 // FIXME this can create a memory leaks on the old entry that be
1498 // left unreferenced.
1499 Entry->SetDictEntry(NewTag);
1502 // Heuristic: well some files are really ill-formed.
1503 if ( length16 == 0xffff)
1506 //dbg.Verbose(0, "gdcmParser::FindLength",
1507 // "Erroneous element length fixed.");
1508 // Actually, length= 0xffff means that we deal with
1509 // Unknown Sequence Length
1511 FixHeaderEntryFoundLength(Entry, (guint32)length16);
1516 // Either implicit VR or a non DICOM conformal (see note below) explicit
1517 // VR that ommited the VR of (at least) this element. Farts happen.
1518 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1519 // on Data elements "Implicit and Explicit VR Data Elements shall
1520 // not coexist in a Data Set and Data Sets nested within it".]
1521 // Length is on 4 bytes.
1523 FixHeaderEntryFoundLength(Entry, ReadInt32());
1529 * \ingroup gdcmParser
1530 * \brief Find the Value Representation of the current Dicom Element.
1533 void gdcmParser::FindHeaderEntryVR( gdcmHeaderEntry *Entry)
1535 if (filetype != ExplicitVR)
1540 long PositionOnEntry = ftell(fp);
1541 // Warning: we believe this is explicit VR (Value Representation) because
1542 // we used a heuristic that found "UL" in the first tag. Alas this
1543 // doesn't guarantee that all the tags will be in explicit VR. In some
1544 // cases (see e-film filtered files) one finds implicit VR tags mixed
1545 // within an explicit VR file. Hence we make sure the present tag
1546 // is in explicit VR and try to fix things if it happens not to be
1549 (void)fread (&VR, (size_t)2,(size_t)1, fp);
1551 if(!CheckHeaderEntryVR(Entry,VR))
1553 fseek(fp, PositionOnEntry, SEEK_SET);
1554 // When this element is known in the dictionary we shall use, e.g. for
1555 // the semantics (see the usage of IsAnInteger), the VR proposed by the
1556 // dictionary entry. Still we have to flag the element as implicit since
1557 // we know now our assumption on expliciteness is not furfilled.
1559 if ( Entry->IsVRUnknown() )
1560 Entry->SetVR("Implicit");
1561 Entry->SetImplicitVR();
1566 * \ingroup gdcmParser
1567 * \brief Check the correspondance between the VR of the header entry
1568 * and the taken VR. If they are different, the header entry is
1569 * updated with the new VR.
1570 * @param Entry Header Entry to check
1571 * @param vr Dicom Value Representation
1572 * @return false if the VR is incorrect of if the VR isn't referenced
1573 * otherwise, it returns true
1575 bool gdcmParser::CheckHeaderEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
1577 char msg[100]; // for sprintf
1578 bool RealExplicit = true;
1580 // Assume we are reading a falsely explicit VR file i.e. we reached
1581 // a tag where we expect reading a VR but are in fact we read the
1582 // first to bytes of the length. Then we will interogate (through find)
1583 // the dicom_vr dictionary with oddities like "\004\0" which crashes
1584 // both GCC and VC++ implementations of the STL map. Hence when the
1585 // expected VR read happens to be non-ascii characters we consider
1586 // we hit falsely explicit VR tag.
1588 if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
1589 RealExplicit = false;
1591 // CLEANME searching the dicom_vr at each occurence is expensive.
1592 // PostPone this test in an optional integrity check at the end
1593 // of parsing or only in debug mode.
1594 if ( RealExplicit && !gdcmGlobal::GetVR()->Count(vr) )
1595 RealExplicit= false;
1597 if ( !RealExplicit )
1599 // We thought this was explicit VR, but we end up with an
1600 // implicit VR tag. Let's backtrack.
1601 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
1602 Entry->GetGroup(),Entry->GetElement());
1603 dbg.Verbose(1, "gdcmParser::FindVR: ",msg);
1604 if (Entry->GetGroup()%2 && Entry->GetElement() == 0x0000) { // Group length is UL !
1605 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1606 Entry->GetGroup(),Entry->GetElement(),
1607 "UL","FIXME","Group Length");
1608 Entry->SetDictEntry(NewEntry);
1613 if ( Entry->IsVRUnknown() )
1615 // When not a dictionary entry, we can safely overwrite the VR.
1616 if (Entry->GetElement() == 0x0000) { // Group length is UL !
1622 else if ( Entry->GetVR() != vr )
1624 // The VR present in the file and the dictionary disagree. We assume
1625 // the file writer knew best and use the VR of the file. Since it would
1626 // be unwise to overwrite the VR of a dictionary (since it would
1627 // compromise it's next user), we need to clone the actual DictEntry
1628 // and change the VR for the read one.
1629 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1630 Entry->GetGroup(),Entry->GetElement(),
1631 vr,"FIXME",Entry->GetName());
1632 Entry->SetDictEntry(NewEntry);
1638 * \ingroup gdcmParser
1639 * \brief Get the transformed value of the header entry. The VR value
1640 * is used to define the transformation to operate on the value
1641 * \warning NOT end user intended method !
1643 * @return Transformed entry value
1645 std::string gdcmParser::GetHeaderEntryValue(gdcmHeaderEntry *Entry)
1647 if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1649 std::string val=Entry->GetValue();
1650 std::string vr=Entry->GetVR();
1651 guint32 length = Entry->GetLength();
1652 std::ostringstream s;
1655 // When short integer(s) are expected, read and convert the following
1656 // n * 2 bytes properly i.e. as a multivaluated strings
1657 // (each single value is separated fromthe next one by '\'
1658 // as usual for standard multivaluated filels
1659 // Elements with Value Multiplicity > 1
1660 // contain a set of short integers (not a single one)
1662 if (vr == "US" || vr == "SS")
1667 for (int i=0; i < nbInt; i++)
1671 NewInt16 = (val[2*i+0]&0xFF)+((val[2*i+1]&0xFF)<<8);
1672 NewInt16 = SwapShort(NewInt16);
1677 // When integer(s) are expected, read and convert the following
1678 // n * 4 bytes properly i.e. as a multivaluated strings
1679 // (each single value is separated fromthe next one by '\'
1680 // as usual for standard multivaluated filels
1681 // Elements with Value Multiplicity > 1
1682 // contain a set of integers (not a single one)
1683 else if (vr == "UL" || vr == "SL")
1688 for (int i=0; i < nbInt; i++)
1692 NewInt32= (val[4*i+0]&0xFF)+((val[4*i+1]&0xFF)<<8)+
1693 ((val[4*i+2]&0xFF)<<16)+((val[4*i+3]&0xFF)<<24);
1694 NewInt32=SwapLong(NewInt32);
1698 #ifdef GDCM_NO_ANSI_STRING_STREAM
1699 s << std::ends; // to avoid oddities on Solaris
1700 #endif //GDCM_NO_ANSI_STRING_STREAM
1704 return(Entry->GetValue());
1708 * \ingroup gdcmParser
1709 * \brief Get the reverse transformed value of the header entry. The VR
1710 * value is used to define the reverse transformation to operate on
1712 * \warning NOT end user intended method !
1714 * @return Reverse transformed entry value
1716 std::string gdcmParser::GetHeaderEntryUnvalue(gdcmHeaderEntry *Entry)
1718 if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1720 std::string vr=Entry->GetVR();
1721 std::ostringstream s;
1722 std::vector<std::string> tokens;
1724 if (vr == "US" || vr == "SS")
1728 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1729 Tokenize (Entry->GetValue(), tokens, "\\");
1730 for (unsigned int i=0; i<tokens.size();i++)
1732 NewInt16 = atoi(tokens[i].c_str());
1733 s<<(NewInt16&0xFF)<<((NewInt16>>8)&0xFF);
1737 if (vr == "UL" || vr == "SL")
1741 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1742 Tokenize (Entry->GetValue(), tokens, "\\");
1743 for (unsigned int i=0; i<tokens.size();i++)
1745 NewInt32 = atoi(tokens[i].c_str());
1746 s<<(char)(NewInt32&0xFF)<<(char)((NewInt32>>8)&0xFF)
1747 <<(char)((NewInt32>>16)&0xFF)<<(char)((NewInt32>>24)&0xFF);
1752 #ifdef GDCM_NO_ANSI_STRING_STREAM
1753 s << std::ends; // to avoid oddities on Solaris
1754 #endif //GDCM_NO_ANSI_STRING_STREAM
1758 return(Entry->GetValue());
1762 * \ingroup gdcmParser
1763 * \brief Skip a given Header Entry
1764 * \warning NOT end user intended method !
1767 void gdcmParser::SkipHeaderEntry(gdcmHeaderEntry *entry)
1769 SkipBytes(entry->GetLength());
1773 * \ingroup gdcmParser
1774 * \brief When the length of an element value is obviously wrong (because
1775 * the parser went Jabberwocky) one can hope improving things by
1776 * applying this heuristic.
1778 void gdcmParser::FixHeaderEntryFoundLength(gdcmHeaderEntry *Entry, guint32 FoundLength)
1780 Entry->SetReadLength(FoundLength); // will be updated only if a bug is found
1781 if ( FoundLength == 0xffffffff) {
1785 guint16 gr =Entry->GetGroup();
1786 guint16 el =Entry->GetElement();
1788 if (FoundLength%2) {
1789 std::ostringstream s;
1790 s << "Warning : Tag with uneven length " << FoundLength
1791 << " in x(" << std::hex << gr << "," << el <<")" << std::dec;
1792 dbg.Verbose(0,s.str().c_str());
1795 // Sorry for the patch!
1796 // XMedCom did the trick to read some naughty GE images ...
1797 if (FoundLength == 13) {
1798 // The following 'if' will be removed when there is no more
1799 // images on Creatis HDs with a 13 length for Manufacturer...
1800 if ( (Entry->GetGroup() != 0x0008) ||
1801 ( (Entry->GetElement() != 0x0070) && (Entry->GetElement() != 0x0080) ) ){
1802 // end of remove area
1804 Entry->SetReadLength(10); // a bug is to be fixed
1808 // to fix some garbage 'Leonardo' Siemens images
1809 // May be commented out to avoid overhead
1810 else if ( (Entry->GetGroup() == 0x0009) &&
1811 ( (Entry->GetElement() == 0x1113) || (Entry->GetElement() == 0x1114) ) ){
1813 Entry->SetReadLength(4); // a bug is to be fixed
1817 // to try to 'go inside' SeQuences (with length), and not to skip them
1818 else if ( Entry->GetVR() == "SQ")
1820 if (enableSequences) // only if the user does want to !
1821 FoundLength =0; // ReadLength is unchanged
1824 // we found a 'delimiter' element
1825 // fffe|xxxx is just a marker, we don't take its length into account
1826 else if(Entry->GetGroup() == 0xfffe)
1828 // *normally, fffe|0000 doesn't exist !
1829 if( Entry->GetElement() != 0x0000 ) // gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1830 // causes extra troubles :-(
1834 Entry->SetUsableLength(FoundLength);
1838 * \ingroup gdcmParser
1839 * \brief Apply some heuristics to predict whether the considered
1840 * element value contains/represents an integer or not.
1841 * @param Entry The element value on which to apply the predicate.
1842 * @return The result of the heuristical predicate.
1844 bool gdcmParser::IsHeaderEntryAnInteger(gdcmHeaderEntry *Entry) {
1845 guint16 element = Entry->GetElement();
1846 guint16 group = Entry->GetGroup();
1847 std::string vr = Entry->GetVR();
1848 guint32 length = Entry->GetLength();
1849 // When we have some semantics on the element we just read, and if we
1850 // a priori know we are dealing with an integer, then we shall be
1851 // able to swap it's element value properly.
1852 if ( element == 0 ) // This is the group length of the group
1858 std::ostringstream s;
1859 int filePosition = ftell(fp);
1860 s << "Erroneous Group Length element length on : (" \
1861 << std::hex << group << " , " << element
1862 << ") -before- position x(" << filePosition << ")"
1863 << "lgt : " << length;
1864 // These 2 lines commented out : a *very dirty* patch
1865 // to go on PrintHeader'ing gdcm-MR-PHILIPS-16-Multi-Seq.dcm.
1866 // have a glance at offset x(8336) ...
1867 // For *regular* headers, the test is useless..
1868 // lets's print a warning message and go on,
1869 // instead of giving up with an error message
1871 //std::cout << s.str().c_str() << std::endl;
1872 // dbg.Error("gdcmParser::IsHeaderEntryAnInteger",
1873 // s.str().c_str());
1876 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1882 * \ingroup gdcmParser
1883 * \brief Find the Length till the next sequence delimiter
1884 * \warning NOT end user intended method !
1888 guint32 gdcmParser::FindHeaderEntryLengthOB(void) {
1889 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1892 long PositionOnEntry = ftell(fp);
1893 bool FoundSequenceDelimiter = false;
1894 guint32 TotalLength = 0;
1897 while ( ! FoundSequenceDelimiter)
1903 TotalLength += 4; // We even have to decount the group and element
1905 if ( g != 0xfffe && g!=0xb00c ) //for bogus header
1907 char msg[100]; // for sprintf. Sorry
1908 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
1909 dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
1913 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) // for bogus header
1914 FoundSequenceDelimiter = true;
1915 else if ( n != 0xe000 )
1917 char msg[100]; // for sprintf. Sorry
1918 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
1920 dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
1924 ItemLength = ReadInt32();
1925 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
1926 // the ItemLength with ReadInt32
1927 SkipBytes(ItemLength);
1929 fseek(fp, PositionOnEntry, SEEK_SET);
1934 * \ingroup gdcmParser
1935 * \brief Reads a supposed to be 16 Bits integer
1936 * (swaps it depending on processor endianity)
1937 * @return read value
1939 guint16 gdcmParser::ReadInt16(void) {
1942 item_read = fread (&g, (size_t)2,(size_t)1, fp);
1943 if ( item_read != 1 ) {
1945 dbg.Verbose(0, "gdcmParser::ReadInt16", " File Error");
1955 * \ingroup gdcmParser
1956 * \brief Reads a supposed to be 32 Bits integer
1957 * (swaps it depending on processor endianity)
1958 * @return read value
1960 guint32 gdcmParser::ReadInt32(void) {
1963 item_read = fread (&g, (size_t)4,(size_t)1, fp);
1964 if ( item_read != 1 ) {
1966 dbg.Verbose(0, "gdcmParser::ReadInt32", " File Error");
1976 * \ingroup gdcmParser
1977 * \brief skips bytes inside the source file
1978 * \warning NOT end user intended method !
1981 void gdcmParser::SkipBytes(guint32 NBytes) {
1982 //FIXME don't dump the returned value
1983 (void)fseek(fp, (long)NBytes, SEEK_CUR);
1987 * \ingroup gdcmParser
1988 * \brief Loads all the needed Dictionaries
1989 * \warning NOT end user intended method !
1991 void gdcmParser::Initialise(void)
1993 RefPubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
1994 RefShaDict = (gdcmDict*)0;
1998 * \ingroup gdcmParser
1999 * \brief Discover what the swap code is (among little endian, big endian,
2000 * bad little endian, bad big endian).
2002 * @return false when we are absolutely sure
2003 * it's neither ACR-NEMA nor DICOM
2004 * true when we hope ours assuptions are OK
2006 bool gdcmParser::CheckSwap() {
2008 // The only guaranted way of finding the swap code is to find a
2009 // group tag since we know it's length has to be of four bytes i.e.
2010 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2011 // occurs when we can't find such group...
2013 guint32 x=4; // x : for ntohs
2014 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2020 char deb[HEADER_LENGTH_TO_READ];
2022 // First, compare HostByteOrder and NetworkByteOrder in order to
2023 // determine if we shall need to swap bytes (i.e. the Endian type).
2029 // The easiest case is the one of a DICOM header, since it possesses a
2030 // file preamble where it suffice to look for the string "DICM".
2031 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2034 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2035 dbg.Verbose(1, "gdcmParser::CheckSwap:", "looks like DICOM Version3");
2037 // Next, determine the value representation (VR). Let's skip to the
2038 // first element (0002, 0000) and check there if we find "UL"
2039 // - or "OB" if the 1st one is (0002,0001) -,
2040 // in which case we (almost) know it is explicit VR.
2041 // WARNING: if it happens to be implicit VR then what we will read
2042 // is the length of the group. If this ascii representation of this
2043 // length happens to be "UL" then we shall believe it is explicit VR.
2044 // FIXME: in order to fix the above warning, we could read the next
2045 // element value (or a couple of elements values) in order to make
2046 // sure we are not commiting a big mistake.
2047 // We need to skip :
2048 // * the 128 bytes of File Preamble (often padded with zeroes),
2049 // * the 4 bytes of "DICM" string,
2050 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2051 // i.e. a total of 136 bytes.
2055 // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2056 // but elem 0002,0010 (Transfert Syntax) tells us the file is
2057 // *Implicit* VR. -and it is !-
2059 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2060 (memcmp(entCur, "OB", (size_t)2) == 0) ||
2061 (memcmp(entCur, "UI", (size_t)2) == 0) ||
2062 (memcmp(entCur, "CS", (size_t)2) == 0) ) // CS, to remove later
2063 // when Write DCM *adds*
2065 // Use gdcmParser::dicom_vr to test all the possibilities
2066 // instead of just checking for UL, OB and UI !? group 0000
2068 filetype = ExplicitVR;
2069 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2070 "explicit Value Representation");
2074 filetype = ImplicitVR;
2075 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2076 "not an explicit Value Representation");
2082 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2083 "HostByteOrder != NetworkByteOrder");
2088 dbg.Verbose(1, "gdcmParser::CheckSwap:",
2089 "HostByteOrder = NetworkByteOrder");
2092 // Position the file position indicator at first tag (i.e.
2093 // after the file preamble and the "DICM" string).
2095 fseek (fp, 132L, SEEK_SET);
2099 // Alas, this is not a DicomV3 file and whatever happens there is no file
2100 // preamble. We can reset the file position indicator to where the data
2101 // is (i.e. the beginning of the file).
2102 dbg.Verbose(1, "gdcmParser::CheckSwap:", "not a DICOM Version3 file");
2105 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2106 // By clean we mean that the length of the first tag is written down.
2107 // If this is the case and since the length of the first group HAS to be
2108 // four (bytes), then determining the proper swap code is straightforward.
2111 // We assume the array of char we are considering contains the binary
2112 // representation of a 32 bits integer. Hence the following dirty
2114 s32 = *((guint32 *)(entCur));
2135 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2136 // It is time for despaired wild guesses.
2137 // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2138 // i.e. the 'group length' element is not present :
2140 // check the supposed to be 'group number'
2141 // 0x0002 or 0x0004 or 0x0008
2142 // to determine ' sw' value .
2143 // Only 0 or 4321 will be possible
2144 // (no oportunity to check for the formerly well known
2145 // ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian'
2146 // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -4, 8-)
2147 // the file IS NOT ACR-NEMA nor DICOM V3
2148 // Find a trick to tell it the caller...
2150 s16 = *((guint16 *)(deb));
2166 dbg.Verbose(0, "gdcmParser::CheckSwap:",
2167 "ACR/NEMA unfound swap info (Really hopeless !)");
2172 // Then the only info we have is the net2host one.
2182 * \ingroup gdcmParser
2183 * \brief Restore the unproperly loaded values i.e. the group, the element
2184 * and the dictionary entry depending on them.
2186 void gdcmParser::SwitchSwapToBigEndian(void)
2188 dbg.Verbose(1, "gdcmParser::SwitchSwapToBigEndian",
2189 "Switching to BigEndian mode.");
2210 * \ingroup gdcmParser
2211 * \brief during parsing, Header Elements too long are not loaded in memory
2214 void gdcmParser::SetMaxSizeLoadEntry(long NewSize)
2218 if ((guint32)NewSize >= (guint32)0xffffffff)
2220 MaxSizeLoadEntry = 0xffffffff;
2223 MaxSizeLoadEntry = NewSize;
2228 * \ingroup gdcmParser
2229 * \brief Header Elements too long will not be printed
2231 * \todo : not yet usable
2232 * (see MAX_SIZE_PRINT_ELEMENT_VALUE
2233 * in gdcmHeaderEntry gdcmLoadEntry)
2237 void gdcmParser::SetMaxSizePrintEntry(long NewSize)
2241 if ((guint32)NewSize >= (guint32)0xffffffff)
2243 MaxSizePrintEntry = 0xffffffff;
2246 MaxSizePrintEntry = NewSize;
2250 * \ingroup gdcmParser
2251 * \brief Searches both the public and the shadow dictionary (when they
2252 * exist) for the presence of the DictEntry with given name.
2253 * The public dictionary has precedence on the shadow one.
2254 * @param Name name of the searched DictEntry
2255 * @return Corresponding DictEntry when it exists, NULL otherwise.
2257 gdcmDictEntry *gdcmParser::GetDictEntryByName(std::string Name)
2259 gdcmDictEntry *found = (gdcmDictEntry *)0;
2260 if (!RefPubDict && !RefShaDict)
2262 dbg.Verbose(0, "gdcmParser::GetDictEntry",
2263 "we SHOULD have a default dictionary");
2267 found = RefPubDict->GetDictEntryByName(Name);
2273 found = RefShaDict->GetDictEntryByName(Name);
2281 * \ingroup gdcmParser
2282 * \brief Searches both the public and the shadow dictionary (when they
2283 * exist) for the presence of the DictEntry with given
2284 * group and element. The public dictionary has precedence on the
2286 * @param group group of the searched DictEntry
2287 * @param element element of the searched DictEntry
2288 * @return Corresponding DictEntry when it exists, NULL otherwise.
2290 gdcmDictEntry *gdcmParser::GetDictEntryByNumber(guint16 group,guint16 element)
2292 gdcmDictEntry *found = (gdcmDictEntry *)0;
2293 if (!RefPubDict && !RefShaDict)
2295 dbg.Verbose(0, "gdcmParser::GetDictEntry",
2296 "we SHOULD have a default dictionary");
2300 found = RefPubDict->GetDictEntryByNumber(group, element);
2306 found = RefShaDict->GetDictEntryByNumber(group, element);
2314 * \ingroup gdcmParser
2315 * \brief Read the next tag but WITHOUT loading it's value
2316 * @return On succes the newly created HeaderEntry, NULL on failure.
2318 gdcmHeaderEntry *gdcmParser::ReadNextHeaderEntry(void) {
2320 gdcmHeaderEntry *NewEntry;
2325 // We reached the EOF (or an error occured) therefore
2326 // header parsing has to be considered as finished.
2327 return (gdcmHeaderEntry *)0;
2329 // Pb : how to propagate the element length (used in SkipHeaderEntry)
2330 // direct call to SkipBytes ?
2332 // if (ignoreShadow == 1 && g%2 ==1)
2333 // if user wants to skip shadow groups
2334 // and current element *is* a shadow element
2335 // we don't create anything
2336 // return (gdcmHeaderEntry *)1; // to tell caller it's NOT finished
2338 NewEntry = NewHeaderEntryByNumber(g, n);
2339 FindHeaderEntryVR(NewEntry);
2340 FindHeaderEntryLength(NewEntry);
2346 NewEntry->SetOffset(ftell(fp));
2351 * \ingroup gdcmParser
2352 * \brief Build a new Element Value from all the low level arguments.
2353 * Check for existence of dictionary entry, and build
2354 * a default one when absent.
2355 * @param Name Name of the underlying DictEntry
2357 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByName(std::string Name)
2359 gdcmDictEntry *NewTag = GetDictEntryByName(Name);
2361 NewTag = NewVirtualDictEntry(0xffff, 0xffff, "LO", "unkn", Name);
2363 gdcmHeaderEntry* NewEntry = new gdcmHeaderEntry(NewTag);
2366 dbg.Verbose(1, "gdcmParser::ObtainHeaderEntryByName",
2367 "failed to allocate gdcmHeaderEntry");
2368 return (gdcmHeaderEntry *)0;
2374 * \ingroup gdcmParser
2375 * \brief Request a new virtual dict entry to the dict set
2376 * @param group group of the underlying DictEntry
2377 * @param element element of the underlying DictEntry
2378 * @param vr VR of the underlying DictEntry
2379 * @param fourth owner group
2380 * @param name english name
2382 gdcmDictEntry *gdcmParser::NewVirtualDictEntry(guint16 group, guint16 element,
2387 return gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,element,vr,fourth,name);
2391 * \ingroup gdcmParser
2392 * \brief Build a new Element Value from all the low level arguments.
2393 * Check for existence of dictionary entry, and build
2394 * a default one when absent.
2395 * @param Group group of the underlying DictEntry
2396 * @param Elem element of the underlying DictEntry
2398 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByNumber(guint16 Group, guint16 Elem)
2400 // Find out if the tag we encountered is in the dictionaries:
2401 gdcmDictEntry *DictEntry = GetDictEntryByNumber(Group, Elem);
2403 DictEntry = NewVirtualDictEntry(Group, Elem);
2405 gdcmHeaderEntry *NewEntry = new gdcmHeaderEntry(DictEntry);
2408 dbg.Verbose(1, "gdcmParser::NewHeaderEntryByNumber",
2409 "failed to allocate gdcmHeaderEntry");
2415 // Never used; commented out, waiting for removal.
2417 * \ingroup gdcmParser
2418 * \brief Small utility function that creates a new manually crafted
2419 * (as opposed as read from the file) gdcmHeaderEntry with user
2420 * specified name and adds it to the public tag hash table.
2421 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
2422 * @param NewTagName The name to be given to this new tag.
2423 * @param VR The Value Representation to be given to this new tag.
2424 * @return The newly hand crafted Element Value.
2426 //gdcmHeaderEntry *gdcmParser::NewManualHeaderEntryToPubDict(std::string NewTagName,
2429 // gdcmHeaderEntry *NewEntry = NULL;
2430 // guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
2431 // guint32 FreeElem = 0;
2432 // gdcmDictEntry *DictEntry = NULL;
2434 // FreeElem = GenerateFreeTagKeyInGroup(StuffGroup);
2435 // if (FreeElem == UINT32_MAX)
2437 // dbg.Verbose(1, "gdcmHeader::NewManualHeaderEntryToPubDict",
2438 // "Group 0xffff in Public Dict is full");
2442 // DictEntry = NewVirtualDictEntry(StuffGroup, FreeElem,
2443 // VR, "GDCM", NewTagName);
2444 // NewEntry = new gdcmHeaderEntry(DictEntry);
2445 // AddHeaderEntry(NewEntry);
2450 * \ingroup gdcmParser
2451 * \brief Generate a free TagKey i.e. a TagKey that is not present
2452 * in the TagHt dictionary.
2453 * @param group The generated tag must belong to this group.
2454 * @return The element of tag with given group which is fee.
2456 guint32 gdcmParser::GenerateFreeTagKeyInGroup(guint16 group)
2458 for (guint32 elem = 0; elem < UINT32_MAX; elem++)
2460 TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
2461 if (tagHT.count(key) == 0)
2467 //-----------------------------------------------------------------------------