1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.cxx,v $
6 Date: $Date: 2005/01/11 17:19:39 $
7 Version: $Revision: 1.175 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmDocument.h"
20 #include "gdcmValEntry.h"
21 #include "gdcmBinEntry.h"
22 #include "gdcmSeqEntry.h"
23 #include "gdcmGlobal.h"
25 #include "gdcmDebug.h"
27 #include "gdcmException.h"
28 #include "gdcmDictSet.h"
29 #include "gdcmRLEFramesInfo.h"
30 #include "gdcmJPEGFragmentsInfo.h"
31 #include "gdcmDocEntrySet.h"
32 #include "gdcmSQItem.h"
38 #if defined(_MSC_VER) || defined(__BORLANDC__)
41 #include <netinet/in.h>
47 //-----------------------------------------------------------------------------
48 // Refer to Document::CheckSwap()
49 //const unsigned int Document::HEADER_LENGTH_TO_READ = 256;
51 // Refer to Document::SetMaxSizeLoadEntry()
52 const unsigned int Document::MAX_SIZE_LOAD_ELEMENT_VALUE = 0xfff; // 4096
53 const unsigned int Document::MAX_SIZE_PRINT_ELEMENT_VALUE = 0x7fffffff;
55 //-----------------------------------------------------------------------------
56 // Constructor / Destructor
60 * @param filename file to be opened for parsing
62 Document::Document( std::string const &filename ) : ElementSet(-1)
64 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
74 Group0002Parsed = false;
76 gdcmVerboseMacro( "Starting parsing of file: " << Filename.c_str());
77 // Fp->seekg( 0, std::ios::beg);
79 Fp->seekg(0, std::ios::end);
80 long lgt = Fp->tellg();
82 Fp->seekg( 0, std::ios::beg);
84 long beg = Fp->tellg();
87 ParseDES( this, beg, lgt, false); // le Load sera fait a la volee
89 Fp->seekg( 0, std::ios::beg);
91 // Load 'non string' values
93 std::string PhotometricInterpretation = GetEntry(0x0028,0x0004);
94 if( PhotometricInterpretation == "PALETTE COLOR " )
96 LoadEntryBinArea(0x0028,0x1200); // gray LUT
98 /// The tags refered by the three following lines used to be CORRECTLY
99 /// defined as having an US Value Representation in the public
100 /// dictionnary. BUT the semantics implied by the three following
101 /// lines state that the corresponding tag contents are in fact
102 /// the ones of a BinEntry.
103 /// In order to fix things "Quick and Dirty" the dictionnary was
104 /// altered on PURPOUS but now contains a WRONG value.
105 /// In order to fix things and restore the dictionary to its
106 /// correct value, one needs to decided of the semantics by deciding
107 /// wether the following tags are either:
108 /// - multivaluated US, and hence loaded as ValEntry, but afterwards
109 /// also used as BinEntry, which requires the proper conversion,
110 /// - OW, and hence loaded as BinEntry, but afterwards also used
111 /// as ValEntry, which requires the proper conversion.
112 LoadEntryBinArea(0x0028,0x1201); // R LUT
113 LoadEntryBinArea(0x0028,0x1202); // G LUT
114 LoadEntryBinArea(0x0028,0x1203); // B LUT
116 // Segmented Red Palette Color LUT Data
117 LoadEntryBinArea(0x0028,0x1221);
118 // Segmented Green Palette Color LUT Data
119 LoadEntryBinArea(0x0028,0x1222);
120 // Segmented Blue Palette Color LUT Data
121 LoadEntryBinArea(0x0028,0x1223);
123 //FIXME later : how to use it?
124 LoadEntryBinArea(0x0028,0x3006); //LUT Data (CTX dependent)
128 // --------------------------------------------------------------
129 // Specific code to allow gdcm to read ACR-LibIDO formated images
130 // Note: ACR-LibIDO is an extension of the ACR standard that was
131 // used at CREATIS. For the time being (say a couple years)
132 // we keep this kludge to allow a smooth move to gdcm for
133 // CREATIS developpers (sorry folks).
135 // if recognition code tells us we deal with a LibIDO image
136 // we switch lineNumber and columnNumber
139 RecCode = GetEntry(0x0008, 0x0010); // recognition code
140 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
141 RecCode == "CANRME_AILIBOD1_1." ) // for brain-damaged softwares
142 // with "little-endian strings"
144 Filetype = ACR_LIBIDO;
145 std::string rows = GetEntry(0x0028, 0x0010);
146 std::string columns = GetEntry(0x0028, 0x0011);
147 SetEntry(columns, 0x0028, 0x0010);
148 SetEntry(rows , 0x0028, 0x0011);
150 // ----------------- End of ACR-LibIDO kludge ------------------
154 * \brief This default constructor doesn't parse the file. You should
155 * then invoke \ref Document::SetFileName and then the parsing.
157 Document::Document() : ElementSet(-1)
161 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
164 Filetype = ExplicitVR;
165 Group0002Parsed = false;
169 * \brief Canonical destructor.
171 Document::~Document ()
180 //-----------------------------------------------------------------------------
184 * \brief Prints The Dict Entries of THE public Dicom Dictionary
187 void Document::PrintPubDict(std::ostream &os)
189 RefPubDict->SetPrintLevel(PrintLevel);
190 RefPubDict->Print(os);
194 * \brief Prints The Dict Entries of THE shadow Dicom Dictionary
197 void Document::PrintShaDict(std::ostream &os)
199 RefShaDict->SetPrintLevel(PrintLevel);
200 RefShaDict->Print(os);
203 //-----------------------------------------------------------------------------
206 * \brief Get the public dictionary used
208 Dict *Document::GetPubDict()
214 * \brief Get the shadow dictionary used
216 Dict *Document::GetShaDict()
222 * \brief Set the shadow dictionary used
223 * @param dict dictionary to use in shadow
225 bool Document::SetShaDict(Dict *dict)
232 * \brief Set the shadow dictionary used
233 * @param dictName name of the dictionary to use in shadow
235 bool Document::SetShaDict(DictKey const &dictName)
237 RefShaDict = Global::GetDicts()->GetDict(dictName);
242 * \brief This predicate, based on hopefully reasonable heuristics,
243 * decides whether or not the current Document was properly parsed
244 * and contains the mandatory information for being considered as
245 * a well formed and usable Dicom/Acr File.
246 * @return true when Document is the one of a reasonable Dicom/Acr file,
249 bool Document::IsReadable()
251 if( Filetype == Unknown)
253 gdcmVerboseMacro( "Wrong filetype");
259 gdcmVerboseMacro( "No tags in internal hash table.");
267 * \brief Accessor to the Transfer Syntax (when present) of the
268 * current document (it internally handles reading the
269 * value from disk when only parsing occured).
270 * @return The encountered Transfer Syntax of the current document.
272 std::string Document::GetTransferSyntax()
274 DocEntry *entry = GetDocEntry(0x0002, 0x0010);
280 // The entry might be present but not loaded (parsing and loading
281 // happen at different stages): try loading and proceed with check...
282 LoadDocEntrySafe(entry);
283 if (ValEntry *valEntry = dynamic_cast< ValEntry* >(entry) )
285 std::string transfer = valEntry->GetValue();
286 // The actual transfer (as read from disk) might be padded. We
287 // first need to remove the potential padding. We can make the
288 // weak assumption that padding was not executed with digits...
289 if ( transfer.length() == 0 )
291 // for brain damaged headers
294 while ( !isdigit((unsigned char)transfer[transfer.length()-1]) )
296 transfer.erase(transfer.length()-1, 1);
304 * \brief Predicate for dicom version 3 file.
305 * @return True when the file is a dicom version 3.
307 bool Document::IsDicomV3()
309 // Checking if Transfer Syntax exists is enough
310 // Anyway, it's to late check if the 'Preamble' was found ...
311 // And ... would it be a rich idea to check ?
312 // (some 'no Preamble' DICOM images exist !)
313 return GetDocEntry(0x0002, 0x0010) != NULL;
317 * \brief returns the File Type
318 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
319 * @return the FileType code
321 FileType Document::GetFileType()
327 * \brief Tries to open the file \ref Document::Filename and
328 * checks the preamble when existing.
329 * @return The FILE pointer on success.
331 std::ifstream *Document::OpenFile()
334 HasDCMPreamble = false;
335 if (Filename.length() == 0)
342 gdcmVerboseMacro( "File already open: " << Filename.c_str());
345 Fp = new std::ifstream(Filename.c_str(), std::ios::in | std::ios::binary);
348 gdcmVerboseMacro( "Cannot open file: " << Filename.c_str());
355 Fp->read((char*)&zero, (size_t)2);
362 //ACR -- or DICOM with no Preamble; may start with a Shadow Group --
364 zero == 0x0001 || zero == 0x0100 || zero == 0x0002 || zero == 0x0200 ||
365 zero == 0x0003 || zero == 0x0300 || zero == 0x0004 || zero == 0x0400 ||
366 zero == 0x0005 || zero == 0x0500 || zero == 0x0006 || zero == 0x0600 ||
367 zero == 0x0007 || zero == 0x0700 || zero == 0x0008 || zero == 0x0800 )
370 = Util::Format("ACR/DICOM with no preamble: (%04x)\n", zero);
371 gdcmVerboseMacro( msg.c_str() );
376 Fp->seekg(126L, std::ios::cur);
378 Fp->read(dicm, (size_t)4);
384 if( memcmp(dicm, "DICM", 4) == 0 )
386 HasDCMPreamble = true;
391 gdcmVerboseMacro( "Not DICOM/ACR (missing preamble)" << Filename.c_str());
397 * \brief closes the file
398 * @return TRUE if the close was successfull
400 bool Document::CloseFile()
409 return true; //FIXME how do we detect a non-close ifstream ?
413 * \brief Writes in a file all the Header Entries (Dicom Elements)
414 * @param fp file pointer on an already open file
415 * @param filetype Type of the File to be written
416 * (ACR-NEMA, ExplicitVR, ImplicitVR)
417 * \return Always true.
419 void Document::WriteContent(std::ofstream *fp, FileType filetype)
421 /// \todo move the following lines (and a lot of others, to be written)
422 /// to a future function CheckAndCorrectHeader
423 /// (necessary if user wants to write a DICOM V3 file
424 /// starting from an ACR-NEMA (V2) Header
426 if ( filetype == ImplicitVR || filetype == ExplicitVR )
428 // writing Dicom File Preamble
429 char filePreamble[128];
430 memset(filePreamble, 0, 128);
431 fp->write(filePreamble, 128);
432 fp->write("DICM", 4);
436 * \todo rewrite later, if really usefull
437 * - 'Group Length' element is optional in DICOM
438 * - but un-updated odd groups lengthes can causes pb
441 * if ( (filetype == ImplicitVR) || (filetype == ExplicitVR) )
442 * UpdateGroupLength(false,filetype);
443 * if ( filetype == ACR)
444 * UpdateGroupLength(true,ACR);
447 ElementSet::WriteContent(fp, filetype); // This one is recursive
451 * \brief Modifies the value of a given Doc Entry (Dicom Element)
452 * when it exists. Create it with the given value when unexistant.
453 * @param value (string) Value to be set
454 * @param group Group number of the Entry
455 * @param elem Element number of the Entry
456 * @param vr V(alue) R(epresentation) of the Entry -if private Entry-
457 * \return pointer to the modified/created Header Entry (NULL when creation
460 ValEntry *Document::ReplaceOrCreate(std::string const &value,
465 ValEntry *valEntry = 0;
466 DocEntry *currentEntry = GetDocEntry( group, elem);
470 valEntry = dynamic_cast< ValEntry* >(currentEntry);
474 if( valEntry->GetVR()!=vr )
477 // if currentEntry doesn't correspond to the requested valEntry
480 if (!RemoveEntry(currentEntry))
482 gdcmVerboseMacro( "Removal of previous DocEntry failed.");
489 // Create a new valEntry if necessary
492 valEntry = NewValEntry(group, elem, vr);
494 if ( !AddEntry(valEntry))
496 gdcmVerboseMacro("AddEntry failed although this is a creation.");
503 // Set the binEntry value
504 SetEntry(value, valEntry);
509 * \brief Modifies the value of a given Header Entry (Dicom Element)
510 * when it exists. Create it with the given value when unexistant.
511 * A copy of the binArea is made to be kept in the Document.
512 * @param binArea (binary) value to be set
513 * @param Group Group number of the Entry
514 * @param Elem Element number of the Entry
515 * @param vr V(alue) R(epresentation) of the Entry -if private Entry-
516 * \return pointer to the modified/created Header Entry (NULL when creation
519 BinEntry *Document::ReplaceOrCreate(uint8_t *binArea,
525 BinEntry *binEntry = 0;
526 DocEntry *currentEntry = GetDocEntry( group, elem);
528 // Verify the currentEntry
531 binEntry = dynamic_cast< BinEntry* >(currentEntry);
535 if( binEntry->GetVR()!=vr )
538 // if currentEntry doesn't correspond to the requested valEntry
541 if (!RemoveEntry(currentEntry))
543 gdcmVerboseMacro( "Removal of previous DocEntry failed.");
550 // Create a new binEntry if necessary
553 binEntry = NewBinEntry(group, elem, vr);
555 if ( !AddEntry(binEntry))
557 gdcmVerboseMacro( "AddEntry failed allthough this is a creation.");
564 // Set the binEntry value
566 if (lgth>0 && binArea)
568 tmpArea = new uint8_t[lgth];
569 memcpy(tmpArea,binArea,lgth);
575 if (!SetEntry(tmpArea,lgth,binEntry))
587 * \brief Modifies the value of a given Header Entry (Dicom Element)
588 * when it exists. Create it when unexistant.
589 * @param Group Group number of the Entry
590 * @param Elem Element number of the Entry
591 * \return pointer to the modified/created SeqEntry (NULL when creation
594 SeqEntry *Document::ReplaceOrCreate( uint16_t group, uint16_t elem)
596 SeqEntry *seqEntry = 0;
597 DocEntry *currentEntry = GetDocEntry( group, elem);
599 // Verify the currentEntry
602 seqEntry = dynamic_cast< SeqEntry* >(currentEntry);
606 if( seqEntry->GetVR()!="SQ" )
609 // if currentEntry doesn't correspond to the requested valEntry
612 if (!RemoveEntry(currentEntry))
614 gdcmVerboseMacro( "Removal of previous DocEntry failed.");
621 // Create a new seqEntry if necessary
624 seqEntry = NewSeqEntry(group, elem);
626 if ( !AddEntry(seqEntry))
628 gdcmVerboseMacro( "AddEntry failed allthough this is a creation.");
639 * \brief Set a new value if the invoked element exists
640 * Seems to be useless !!!
641 * @param value new element value
642 * @param group group number of the Entry
643 * @param elem element number of the Entry
646 bool Document::ReplaceIfExist(std::string const &value,
647 uint16_t group, uint16_t elem )
649 SetEntry(value, group, elem);
654 //-----------------------------------------------------------------------------
658 * \brief Checks if a given Dicom Element exists within the H table
659 * @param group Group number of the searched Dicom Element
660 * @param element Element number of the searched Dicom Element
661 * @return true is found
663 bool Document::CheckIfEntryExist(uint16_t group, uint16_t element )
665 const std::string &key = DictEntry::TranslateToKey(group, element );
666 return TagHT.count(key) != 0;
671 * \brief Searches within Header Entries (Dicom Elements) parsed with
672 * the public and private dictionaries
673 * for the element value representation of a given tag.
674 * @param group Group number of the searched tag.
675 * @param element Element number of the searched tag.
676 * @return Corresponding element value representation when it exists,
677 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
679 std::string Document::GetEntry(uint16_t group, uint16_t element)
681 TagKey key = DictEntry::TranslateToKey(group, element);
682 if ( !TagHT.count(key))
687 return ((ValEntry *)TagHT.find(key)->second)->GetValue();
691 * \brief Searches within Header Entries (Dicom Elements) parsed with
692 * the public and private dictionaries
693 * for the element value representation of a given tag..
695 * Obtaining the VR (Value Representation) might be needed by caller
696 * to convert the string typed content to caller's native type
697 * (think of C++ vs Python). The VR is actually of a higher level
698 * of semantics than just the native C++ type.
699 * @param group Group number of the searched tag.
700 * @param element Element number of the searched tag.
701 * @return Corresponding element value representation when it exists,
702 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
704 std::string Document::GetEntryVR(uint16_t group, uint16_t element)
706 DocEntry *elem = GetDocEntry(group, element);
711 return elem->GetVR();
715 * \brief Searches within Header Entries (Dicom Elements) parsed with
716 * the public and private dictionaries
717 * for the value length of a given tag..
718 * @param group Group number of the searched tag.
719 * @param element Element number of the searched tag.
720 * @return Corresponding element length; -2 if not found
722 int Document::GetEntryLength(uint16_t group, uint16_t element)
724 DocEntry *elem = GetDocEntry(group, element);
727 return -2; //magic number
729 return elem->GetLength();
733 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
734 * through it's (group, element) and modifies it's content with
736 * @param content new value (string) to substitute with
737 * @param group group number of the Dicom Element to modify
738 * @param element element number of the Dicom Element to modify
740 bool Document::SetEntry(std::string const& content,
741 uint16_t group, uint16_t element)
743 ValEntry *entry = GetValEntry(group, element);
746 gdcmVerboseMacro( "No corresponding ValEntry (try promotion first).");
749 return SetEntry(content,entry);
753 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
754 * through it's (group, element) and modifies it's content with
756 * @param content new value (void* -> uint8_t*) to substitute with
757 * @param lgth new value length
758 * @param group group number of the Dicom Element to modify
759 * @param element element number of the Dicom Element to modify
761 bool Document::SetEntry(uint8_t*content, int lgth,
762 uint16_t group, uint16_t element)
764 BinEntry *entry = GetBinEntry(group, element);
767 gdcmVerboseMacro( "No corresponding ValEntry (try promotion first).");
771 return SetEntry(content,lgth,entry);
775 * \brief Accesses an existing DocEntry (i.e. a Dicom Element)
776 * and modifies it's content with the given value.
777 * @param content new value (string) to substitute with
778 * @param entry Entry to be modified
780 bool Document::SetEntry(std::string const &content,ValEntry *entry)
784 entry->SetValue(content);
791 * \brief Accesses an existing BinEntry (i.e. a Dicom Element)
792 * and modifies it's content with the given value.
793 * @param content new value (void* -> uint8_t*) to substitute with
794 * @param entry Entry to be modified
795 * @param lgth new value length
797 bool Document::SetEntry(uint8_t *content, int lgth, BinEntry *entry)
801 // Hope Binary field length is *never* wrong
802 /*if(lgth%2) // Non even length are padded with a space (020H).
805 //content = content + '\0'; // fing a trick to enlarge a binary field?
808 entry->SetBinArea(content);
809 entry->SetLength(lgth);
810 entry->SetValue(GDCM_BINLOADED);
817 * \brief Gets (from Header) a 'non string' element value
818 * (LoadElementValues has already be executed)
819 * @param group group number of the Entry
820 * @param elem element number of the Entry
821 * @return Pointer to the 'non string' area
823 void *Document::GetEntryBinArea(uint16_t group, uint16_t elem)
825 DocEntry *entry = GetDocEntry(group, elem);
828 gdcmVerboseMacro( "No entry");
831 if ( BinEntry *binEntry = dynamic_cast<BinEntry*>(entry) )
833 return binEntry->GetBinArea();
840 * \brief Loads (from disk) the element content
841 * when a string is not suitable
842 * @param group group number of the Entry
843 * @param elem element number of the Entry
845 void Document::LoadEntryBinArea(uint16_t group, uint16_t elem)
847 // Search the corresponding DocEntry
848 DocEntry *docElement = GetDocEntry(group, elem);
852 BinEntry *binElement = dynamic_cast<BinEntry *>(docElement);
856 LoadEntryBinArea(binElement);
860 * \brief Loads (from disk) the element content
861 * when a string is not suitable
862 * @param element Entry whose binArea is going to be loaded
864 void Document::LoadEntryBinArea(BinEntry *element)
866 if(element->GetBinArea())
873 size_t o =(size_t)element->GetOffset();
874 Fp->seekg(o, std::ios::beg);
876 size_t l = element->GetLength();
877 uint8_t *a = new uint8_t[l];
880 gdcmVerboseMacro( "Cannot allocate a");
884 /// \todo check the result
885 Fp->read((char*)a, l);
886 if( Fp->fail() || Fp->eof())
892 element->SetBinArea(a);
899 * \brief Sets a 'non string' value to a given Dicom Element
900 * @param area area containing the 'non string' value
901 * @param group Group number of the searched Dicom Element
902 * @param element Element number of the searched Dicom Element
905 /*bool Document::SetEntryBinArea(uint8_t *area,
906 uint16_t group, uint16_t element)
908 DocEntry *currentEntry = GetDocEntry(group, element);
914 if ( BinEntry *binEntry = dynamic_cast<BinEntry*>(currentEntry) )
916 binEntry->SetBinArea( area );
924 * \brief retrieves a Dicom Element (the first one) using (group, element)
925 * \warning (group, element) IS NOT an identifier inside the Dicom Header
926 * if you think it's NOT UNIQUE, check the count number
927 * and use iterators to retrieve ALL the Dicoms Elements within
928 * a given couple (group, element)
929 * @param group Group number of the searched Dicom Element
930 * @param element Element number of the searched Dicom Element
933 DocEntry *Document::GetDocEntry(uint16_t group, uint16_t element)
935 TagKey key = DictEntry::TranslateToKey(group, element);
936 if ( !TagHT.count(key))
940 return TagHT.find(key)->second;
944 * \brief Same as \ref Document::GetDocEntry except it only
945 * returns a result when the corresponding entry is of type
947 * @return When present, the corresponding ValEntry.
949 ValEntry *Document::GetValEntry(uint16_t group, uint16_t element)
951 DocEntry *currentEntry = GetDocEntry(group, element);
956 if ( ValEntry *entry = dynamic_cast<ValEntry*>(currentEntry) )
960 gdcmVerboseMacro( "Unfound ValEntry.");
966 * \brief Same as \ref Document::GetDocEntry except it only
967 * returns a result when the corresponding entry is of type
969 * @return When present, the corresponding BinEntry.
971 BinEntry *Document::GetBinEntry(uint16_t group, uint16_t element)
973 DocEntry *currentEntry = GetDocEntry(group, element);
978 if ( BinEntry *entry = dynamic_cast<BinEntry*>(currentEntry) )
982 gdcmVerboseMacro( "Unfound BinEntry.");
988 * \brief Loads the element while preserving the current
989 * underlying file position indicator as opposed to
990 * to LoadDocEntry that modifies it.
991 * @param entry Header Entry whose value shall be loaded.
994 void Document::LoadDocEntrySafe(DocEntry *entry)
998 long PositionOnEntry = Fp->tellg();
1000 Fp->seekg(PositionOnEntry, std::ios::beg);
1005 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1007 * @return The properly swaped 32 bits integer.
1009 uint32_t Document::SwapLong(uint32_t a)
1016 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1017 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1021 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1025 a=( ((a<< 8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1028 gdcmErrorMacro( "Unset swap code:" << SwapCode );
1035 * \brief Unswaps back the bytes of 4-byte long integer accordingly to
1037 * @return The properly unswaped 32 bits integer.
1039 uint32_t Document::UnswapLong(uint32_t a)
1045 * \brief Swaps the bytes so they agree with the processor order
1046 * @return The properly swaped 16 bits integer.
1048 uint16_t Document::SwapShort(uint16_t a)
1050 if ( SwapCode == 4321 || SwapCode == 2143 )
1052 a = ((( a << 8 ) & 0x0ff00 ) | (( a >> 8 ) & 0x00ff ) );
1058 * \brief Unswaps the bytes so they agree with the processor order
1059 * @return The properly unswaped 16 bits integer.
1061 uint16_t Document::UnswapShort(uint16_t a)
1063 return SwapShort(a);
1066 //-----------------------------------------------------------------------------
1070 * \brief Parses a DocEntrySet (Zero-level DocEntries or SQ Item DocEntries)
1071 * @return length of the parsed set.
1073 void Document::ParseDES(DocEntrySet *set, long offset,
1074 long l_max, bool delim_mode)
1076 DocEntry *newDocEntry = 0;
1077 ValEntry *newValEntry;
1078 BinEntry *newBinEntry;
1079 SeqEntry *newSeqEntry;
1085 if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1091 newDocEntry = ReadNextDocEntry( );
1097 vr = newDocEntry->GetVR();
1098 newValEntry = dynamic_cast<ValEntry*>(newDocEntry);
1099 newBinEntry = dynamic_cast<BinEntry*>(newDocEntry);
1100 newSeqEntry = dynamic_cast<SeqEntry*>(newDocEntry);
1102 if ( newValEntry || newBinEntry )
1106 if ( ! Global::GetVR()->IsVROfBinaryRepresentable(vr) )
1108 ////// Neither ValEntry NOR BinEntry: should mean UNKOWN VR
1109 gdcmVerboseMacro( "Neither Valentry, nor BinEntry."
1110 "Probably unknown VR.");
1113 //////////////////// BinEntry or UNKOWN VR:
1114 // When "this" is a Document the Key is simply of the
1115 // form ( group, elem )...
1116 if (Document *dummy = dynamic_cast< Document* > ( set ) )
1119 newBinEntry->SetKey( newBinEntry->GetKey() );
1121 // but when "this" is a SQItem, we are inserting this new
1122 // valEntry in a sequence item, and the kay has the
1123 // generalized form (refer to \ref BaseTagKey):
1124 if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
1126 newBinEntry->SetKey( parentSQItem->GetBaseTagKey()
1127 + newBinEntry->GetKey() );
1130 LoadDocEntry( newBinEntry );
1131 if( !set->AddEntry( newBinEntry ) )
1133 //Expect big troubles if here
1134 //delete newBinEntry;
1140 /////////////////////// ValEntry
1141 // When "set" is a Document, then we are at the top of the
1142 // hierarchy and the Key is simply of the form ( group, elem )...
1143 if (Document *dummy = dynamic_cast< Document* > ( set ) )
1146 newValEntry->SetKey( newValEntry->GetKey() );
1148 // ...but when "set" is a SQItem, we are inserting this new
1149 // valEntry in a sequence item. Hence the key has the
1150 // generalized form (refer to \ref BaseTagKey):
1151 if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
1153 newValEntry->SetKey( parentSQItem->GetBaseTagKey()
1154 + newValEntry->GetKey() );
1157 LoadDocEntry( newValEntry );
1158 bool delimitor=newValEntry->IsItemDelimitor();
1159 if( !set->AddEntry( newValEntry ) )
1161 // If here expect big troubles
1162 //delete newValEntry; //otherwise mem leak
1172 if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1180 if ( ( newDocEntry->GetGroup() == 0x7fe0 )
1181 && ( newDocEntry->GetElement() == 0x0010 ) )
1183 std::string ts = GetTransferSyntax();
1184 if ( Global::GetTS()->IsRLELossless(ts) )
1186 long positionOnEntry = Fp->tellg();
1187 Fp->seekg( newDocEntry->GetOffset(), std::ios::beg );
1189 Fp->seekg( positionOnEntry, std::ios::beg );
1191 else if ( Global::GetTS()->IsJPEG(ts) )
1193 long positionOnEntry = Fp->tellg();
1194 Fp->seekg( newDocEntry->GetOffset(), std::ios::beg );
1195 ComputeJPEGFragmentInfo();
1196 Fp->seekg( positionOnEntry, std::ios::beg );
1200 // Just to make sure we are at the beginning of next entry.
1201 SkipToNextDocEntry(newDocEntry);
1206 unsigned long l = newDocEntry->GetReadLength();
1207 if ( l != 0 ) // don't mess the delim_mode for zero-length sequence
1209 if ( l == 0xffffffff )
1218 // no other way to create it ...
1219 newSeqEntry->SetDelimitorMode( delim_mode );
1221 // At the top of the hierarchy, stands a Document. When "set"
1222 // is a Document, then we are building the first depth level.
1223 // Hence the SeqEntry we are building simply has a depth
1225 if (Document *dummy = dynamic_cast< Document* > ( set ) )
1228 newSeqEntry->SetDepthLevel( 1 );
1229 newSeqEntry->SetKey( newSeqEntry->GetKey() );
1231 // But when "set" is allready a SQItem, we are building a nested
1232 // sequence, and hence the depth level of the new SeqEntry
1233 // we are building, is one level deeper:
1234 if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
1236 newSeqEntry->SetDepthLevel( parentSQItem->GetDepthLevel() + 1 );
1237 newSeqEntry->SetKey( parentSQItem->GetBaseTagKey()
1238 + newSeqEntry->GetKey() );
1242 { // Don't try to parse zero-length sequences
1243 ParseSQ( newSeqEntry,
1244 newDocEntry->GetOffset(),
1247 set->AddEntry( newSeqEntry );
1248 if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1260 * \brief Parses a Sequence ( SeqEntry after SeqEntry)
1261 * @return parsed length for this level
1263 void Document::ParseSQ( SeqEntry *seqEntry,
1264 long offset, long l_max, bool delim_mode)
1266 int SQItemNumber = 0;
1271 DocEntry *newDocEntry = ReadNextDocEntry();
1274 // FIXME Should warn user
1279 if ( newDocEntry->IsSequenceDelimitor() )
1281 seqEntry->SetSequenceDelimitationItem( newDocEntry );
1285 if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1291 SQItem *itemSQ = new SQItem( seqEntry->GetDepthLevel() );
1292 std::ostringstream newBase;
1293 newBase << seqEntry->GetKey()
1297 itemSQ->SetBaseTagKey( newBase.str() );
1298 unsigned int l = newDocEntry->GetReadLength();
1300 if ( l == 0xffffffff )
1309 ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod);
1312 seqEntry->AddEntry( itemSQ, SQItemNumber );
1314 if ( !delim_mode && ((long)(Fp->tellg())-offset ) >= l_max )
1322 * \brief Loads the element content if its length doesn't exceed
1323 * the value specified with Document::SetMaxSizeLoadEntry()
1324 * @param entry Header Entry (Dicom Element) to be dealt with
1326 void Document::LoadDocEntry(DocEntry *entry)
1328 uint16_t group = entry->GetGroup();
1329 std::string vr = entry->GetVR();
1330 uint32_t length = entry->GetLength();
1332 Fp->seekg((long)entry->GetOffset(), std::ios::beg);
1334 // A SeQuence "contains" a set of Elements.
1335 // (fffe e000) tells us an Element is beginning
1336 // (fffe e00d) tells us an Element just ended
1337 // (fffe e0dd) tells us the current SeQuence just ended
1338 if( group == 0xfffe )
1340 // NO more value field for SQ !
1344 // When the length is zero things are easy:
1347 ((ValEntry *)entry)->SetValue("");
1351 // The elements whose length is bigger than the specified upper bound
1352 // are not loaded. Instead we leave a short notice of the offset of
1353 // the element content and it's length.
1355 std::ostringstream s;
1356 if (length > MaxSizeLoadEntry)
1358 if (BinEntry *binEntryPtr = dynamic_cast< BinEntry* >(entry) )
1360 //s << "gdcm::NotLoaded (BinEntry)";
1361 s << GDCM_NOTLOADED;
1362 s << " Address:" << (long)entry->GetOffset();
1363 s << " Length:" << entry->GetLength();
1364 s << " x(" << std::hex << entry->GetLength() << ")";
1365 binEntryPtr->SetValue(s.str());
1367 // Be carefull : a BinEntry IS_A ValEntry ...
1368 else if (ValEntry *valEntryPtr = dynamic_cast< ValEntry* >(entry) )
1370 // s << "gdcm::NotLoaded. (ValEntry)";
1371 s << GDCM_NOTLOADED;
1372 s << " Address:" << (long)entry->GetOffset();
1373 s << " Length:" << entry->GetLength();
1374 s << " x(" << std::hex << entry->GetLength() << ")";
1375 valEntryPtr->SetValue(s.str());
1380 gdcmErrorMacro( "MaxSizeLoadEntry exceeded, neither a BinEntry "
1381 << "nor a ValEntry ?! Should never print that !" );
1384 // to be sure we are at the end of the value ...
1385 Fp->seekg((long)entry->GetOffset()+(long)entry->GetLength(),
1390 // When we find a BinEntry not very much can be done :
1391 if (BinEntry *binEntryPtr = dynamic_cast< BinEntry* >(entry) )
1393 s << GDCM_BINLOADED;
1394 binEntryPtr->SetValue(s.str());
1395 LoadEntryBinArea(binEntryPtr); // last one, not to erase length !
1399 /// \todo Any compacter code suggested (?)
1400 if ( IsDocEntryAnInteger(entry) )
1404 // When short integer(s) are expected, read and convert the following
1405 // n *two characters properly i.e. consider them as short integers as
1406 // opposed to strings.
1407 // Elements with Value Multiplicity > 1
1408 // contain a set of integers (not a single one)
1409 if (vr == "US" || vr == "SS")
1412 NewInt = ReadInt16();
1416 for (int i=1; i < nbInt; i++)
1419 NewInt = ReadInt16();
1424 // See above comment on multiple integers (mutatis mutandis).
1425 else if (vr == "UL" || vr == "SL")
1428 NewInt = ReadInt32();
1432 for (int i=1; i < nbInt; i++)
1435 NewInt = ReadInt32();
1440 #ifdef GDCM_NO_ANSI_STRING_STREAM
1441 s << std::ends; // to avoid oddities on Solaris
1442 #endif //GDCM_NO_ANSI_STRING_STREAM
1444 ((ValEntry *)entry)->SetValue(s.str());
1448 // FIXME: We need an additional byte for storing \0 that is not on disk
1449 char *str = new char[length+1];
1450 Fp->read(str, (size_t)length);
1451 str[length] = '\0'; //this is only useful when length is odd
1452 // Special DicomString call to properly handle \0 and even length
1453 std::string newValue;
1456 newValue = Util::DicomString(str, length+1);
1457 gdcmVerboseMacro("Warning: bad length: " << length <<
1458 ",For string :" << newValue.c_str());
1459 // Since we change the length of string update it length
1460 //entry->SetReadLength(length+1);
1464 newValue = Util::DicomString(str, length);
1468 if ( ValEntry *valEntry = dynamic_cast<ValEntry* >(entry) )
1470 if ( Fp->fail() || Fp->eof())
1472 gdcmVerboseMacro("Unread element value");
1473 valEntry->SetValue(GDCM_UNREAD);
1479 // Because of correspondance with the VR dic
1480 valEntry->SetValue(newValue);
1484 valEntry->SetValue(newValue);
1489 gdcmErrorMacro( "Should have a ValEntry, here !");
1495 * \brief Find the value Length of the passed Header Entry
1496 * @param entry Header Entry whose length of the value shall be loaded.
1498 void Document::FindDocEntryLength( DocEntry *entry )
1499 throw ( FormatError )
1501 uint16_t element = entry->GetElement();
1502 std::string vr = entry->GetVR();
1505 if ( Filetype == ExplicitVR && !entry->IsImplicitVR() )
1507 if ( vr == "OB" || vr == "OW" || vr == "SQ" || vr == "UN" )
1509 // The following reserved two bytes (see PS 3.5-2003, section
1510 // "7.1.2 Data element structure with explicit vr", p 27) must be
1511 // skipped before proceeding on reading the length on 4 bytes.
1512 Fp->seekg( 2L, std::ios::cur);
1513 uint32_t length32 = ReadInt32();
1515 if ( (vr == "OB" || vr == "OW") && length32 == 0xffffffff )
1520 lengthOB = FindDocEntryLengthOBOrOW();
1522 catch ( FormatUnexpected )
1524 // Computing the length failed (this happens with broken
1525 // files like gdcm-JPEG-LossLess3a.dcm). We still have a
1526 // chance to get the pixels by deciding the element goes
1527 // until the end of the file. Hence we artificially fix the
1528 // the length and proceed.
1529 long currentPosition = Fp->tellg();
1530 Fp->seekg(0L,std::ios::end);
1532 long lengthUntilEOF = (long)(Fp->tellg())-currentPosition;
1533 Fp->seekg(currentPosition, std::ios::beg);
1535 entry->SetReadLength(lengthUntilEOF);
1536 entry->SetLength(lengthUntilEOF);
1539 entry->SetReadLength(lengthOB);
1540 entry->SetLength(lengthOB);
1543 FixDocEntryFoundLength(entry, length32);
1547 // Length is encoded on 2 bytes.
1548 length16 = ReadInt16();
1550 // We can tell the current file is encoded in big endian (like
1551 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1552 // and it's value is the one of the encoding of a big endian file.
1553 // In order to deal with such big endian encoded files, we have
1554 // (at least) two strategies:
1555 // * when we load the "Transfer Syntax" tag with value of big endian
1556 // encoding, we raise the proper flags. Then we wait for the end
1557 // of the META group (0x0002) among which is "Transfer Syntax",
1558 // before switching the swap code to big endian. We have to postpone
1559 // the switching of the swap code since the META group is fully encoded
1560 // in little endian, and big endian coding only starts at the next
1561 // group. The corresponding code can be hard to analyse and adds
1562 // many additional unnecessary tests for regular tags.
1563 // * the second strategy consists in waiting for trouble, that shall
1564 // appear when we find the first group with big endian encoding. This
1565 // is easy to detect since the length of a "Group Length" tag (the
1566 // ones with zero as element number) has to be of 4 (0x0004). When we
1567 // encounter 1024 (0x0400) chances are the encoding changed and we
1568 // found a group with big endian encoding.
1569 // We shall use this second strategy. In order to make sure that we
1570 // can interpret the presence of an apparently big endian encoded
1571 // length of a "Group Length" without committing a big mistake, we
1572 // add an additional check: we look in the already parsed elements
1573 // for the presence of a "Transfer Syntax" whose value has to be "big
1574 // endian encoding". When this is the case, chances are we have got our
1575 // hands on a big endian encoded file: we switch the swap code to
1576 // big endian and proceed...
1577 if ( element == 0x0000 && length16 == 0x0400 )
1579 std::string ts = GetTransferSyntax();
1580 if ( Global::GetTS()->GetSpecialTransferSyntax(ts)
1581 != TS::ExplicitVRBigEndian )
1583 throw FormatError( "Document::FindDocEntryLength()",
1584 " not explicit VR." );
1588 SwitchByteSwapCode();
1590 // Restore the unproperly loaded values i.e. the group, the element
1591 // and the dictionary entry depending on them.
1592 uint16_t correctGroup = SwapShort( entry->GetGroup() );
1593 uint16_t correctElem = SwapShort( entry->GetElement() );
1594 DictEntry *newTag = GetDictEntry( correctGroup, correctElem );
1597 // This correct tag is not in the dictionary. Create a new one.
1598 newTag = NewVirtualDictEntry(correctGroup, correctElem);
1600 // FIXME this can create a memory leaks on the old entry that be
1601 // left unreferenced.
1602 entry->SetDictEntry( newTag );
1605 // Heuristic: well, some files are really ill-formed.
1606 if ( length16 == 0xffff)
1608 // 0xffff means that we deal with 'Unknown Length' Sequence
1611 FixDocEntryFoundLength( entry, (uint32_t)length16 );
1616 // Either implicit VR or a non DICOM conformal (see note below) explicit
1617 // VR that ommited the VR of (at least) this element. Farts happen.
1618 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1619 // on Data elements "Implicit and Explicit VR Data Elements shall
1620 // not coexist in a Data Set and Data Sets nested within it".]
1621 // Length is on 4 bytes.
1623 FixDocEntryFoundLength( entry, ReadInt32() );
1629 * \brief Find the Value Representation of the current Dicom Element.
1630 * @return Value Representation of the current Entry
1632 std::string Document::FindDocEntryVR()
1634 if ( Filetype != ExplicitVR )
1635 return GDCM_UNKNOWN;
1637 long positionOnEntry = Fp->tellg();
1638 // Warning: we believe this is explicit VR (Value Representation) because
1639 // we used a heuristic that found "UL" in the first tag. Alas this
1640 // doesn't guarantee that all the tags will be in explicit VR. In some
1641 // cases (see e-film filtered files) one finds implicit VR tags mixed
1642 // within an explicit VR file. Hence we make sure the present tag
1643 // is in explicit VR and try to fix things if it happens not to be
1647 Fp->read (vr, (size_t)2);
1650 if( !CheckDocEntryVR(vr) )
1652 Fp->seekg(positionOnEntry, std::ios::beg);
1653 return GDCM_UNKNOWN;
1659 * \brief Check the correspondance between the VR of the header entry
1660 * and the taken VR. If they are different, the header entry is
1661 * updated with the new VR.
1662 * @param vr Dicom Value Representation
1663 * @return false if the VR is incorrect of if the VR isn't referenced
1664 * otherwise, it returns true
1666 bool Document::CheckDocEntryVR(VRKey vr)
1668 // CLEANME searching the dicom_vr at each occurence is expensive.
1669 // PostPone this test in an optional integrity check at the end
1670 // of parsing or only in debug mode.
1671 if ( !Global::GetVR()->IsValidVR(vr) )
1678 * \brief Get the transformed value of the header entry. The VR value
1679 * is used to define the transformation to operate on the value
1680 * \warning NOT end user intended method !
1681 * @param entry entry to tranform
1682 * @return Transformed entry value
1684 std::string Document::GetDocEntryValue(DocEntry *entry)
1686 if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1688 std::string val = ((ValEntry *)entry)->GetValue();
1689 std::string vr = entry->GetVR();
1690 uint32_t length = entry->GetLength();
1691 std::ostringstream s;
1694 // When short integer(s) are expected, read and convert the following
1695 // n * 2 bytes properly i.e. as a multivaluated strings
1696 // (each single value is separated fromthe next one by '\'
1697 // as usual for standard multivaluated filels
1698 // Elements with Value Multiplicity > 1
1699 // contain a set of short integers (not a single one)
1701 if( vr == "US" || vr == "SS" )
1706 for (int i=0; i < nbInt; i++)
1712 newInt16 = ( val[2*i+0] & 0xFF ) + ( ( val[2*i+1] & 0xFF ) << 8);
1713 newInt16 = SwapShort( newInt16 );
1718 // When integer(s) are expected, read and convert the following
1719 // n * 4 bytes properly i.e. as a multivaluated strings
1720 // (each single value is separated fromthe next one by '\'
1721 // as usual for standard multivaluated filels
1722 // Elements with Value Multiplicity > 1
1723 // contain a set of integers (not a single one)
1724 else if( vr == "UL" || vr == "SL" )
1729 for (int i=0; i < nbInt; i++)
1735 newInt32 = ( val[4*i+0] & 0xFF )
1736 + (( val[4*i+1] & 0xFF ) << 8 )
1737 + (( val[4*i+2] & 0xFF ) << 16 )
1738 + (( val[4*i+3] & 0xFF ) << 24 );
1739 newInt32 = SwapLong( newInt32 );
1743 #ifdef GDCM_NO_ANSI_STRING_STREAM
1744 s << std::ends; // to avoid oddities on Solaris
1745 #endif //GDCM_NO_ANSI_STRING_STREAM
1749 return ((ValEntry *)entry)->GetValue();
1753 * \brief Get the reverse transformed value of the header entry. The VR
1754 * value is used to define the reverse transformation to operate on
1756 * \warning NOT end user intended method !
1757 * @param entry Entry to reverse transform
1758 * @return Reverse transformed entry value
1760 std::string Document::GetDocEntryUnvalue(DocEntry *entry)
1762 if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1764 std::string vr = entry->GetVR();
1765 std::vector<std::string> tokens;
1766 std::ostringstream s;
1768 if ( vr == "US" || vr == "SS" )
1772 tokens.erase( tokens.begin(), tokens.end()); // clean any previous value
1773 Util::Tokenize (((ValEntry *)entry)->GetValue(), tokens, "\\");
1774 for (unsigned int i=0; i<tokens.size(); i++)
1776 newInt16 = atoi(tokens[i].c_str());
1777 s << ( newInt16 & 0xFF )
1778 << (( newInt16 >> 8 ) & 0xFF );
1782 if ( vr == "UL" || vr == "SL")
1786 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1787 Util::Tokenize (((ValEntry *)entry)->GetValue(), tokens, "\\");
1788 for (unsigned int i=0; i<tokens.size();i++)
1790 newInt32 = atoi(tokens[i].c_str());
1791 s << (char)( newInt32 & 0xFF )
1792 << (char)(( newInt32 >> 8 ) & 0xFF )
1793 << (char)(( newInt32 >> 16 ) & 0xFF )
1794 << (char)(( newInt32 >> 24 ) & 0xFF );
1799 #ifdef GDCM_NO_ANSI_STRING_STREAM
1800 s << std::ends; // to avoid oddities on Solaris
1801 #endif //GDCM_NO_ANSI_STRING_STREAM
1805 return ((ValEntry *)entry)->GetValue();
1809 * \brief Skip a given Header Entry
1810 * \warning NOT end user intended method !
1811 * @param entry entry to skip
1813 void Document::SkipDocEntry(DocEntry *entry)
1815 SkipBytes(entry->GetLength());
1819 * \brief Skips to the begining of the next Header Entry
1820 * \warning NOT end user intended method !
1821 * @param currentDocEntry entry to skip
1823 void Document::SkipToNextDocEntry(DocEntry *currentDocEntry)
1825 Fp->seekg((long)(currentDocEntry->GetOffset()), std::ios::beg);
1826 Fp->seekg( (long)(currentDocEntry->GetReadLength()),std::ios::cur);
1830 * \brief When the length of an element value is obviously wrong (because
1831 * the parser went Jabberwocky) one can hope improving things by
1832 * applying some heuristics.
1833 * @param entry entry to check
1834 * @param foundLength first assumption about length
1836 void Document::FixDocEntryFoundLength(DocEntry *entry,
1837 uint32_t foundLength)
1839 entry->SetReadLength( foundLength ); // will be updated only if a bug is found
1840 if ( foundLength == 0xffffffff)
1845 uint16_t gr = entry->GetGroup();
1846 uint16_t elem = entry->GetElement();
1848 if ( foundLength % 2)
1850 gdcmVerboseMacro( "Warning : Tag with uneven length " << foundLength
1851 << " in x(" << std::hex << gr << "," << elem <<")");
1854 //////// Fix for some naughty General Electric images.
1855 // Allthough not recent many such GE corrupted images are still present
1856 // on Creatis hard disks. Hence this fix shall remain when such images
1857 // are no longer in use (we are talking a few years, here)...
1858 // Note: XMedCom probably uses such a trick since it is able to read
1859 // those pesky GE images ...
1860 if ( foundLength == 13)
1862 // Only happens for this length !
1863 if ( gr != 0x0008 || ( elem != 0x0070 && elem != 0x0080 ) )
1866 entry->SetReadLength(10); /// \todo a bug is to be fixed !?
1870 //////// Fix for some brain-dead 'Leonardo' Siemens images.
1871 // Occurence of such images is quite low (unless one leaves close to a
1872 // 'Leonardo' source. Hence, one might consider commenting out the
1873 // following fix on efficiency reasons.
1874 else if ( gr == 0x0009 && ( elem == 0x1113 || elem == 0x1114 ) )
1877 entry->SetReadLength(4); /// \todo a bug is to be fixed !?
1880 else if ( entry->GetVR() == "SQ" )
1882 foundLength = 0; // ReadLength is unchanged
1885 //////// We encountered a 'delimiter' element i.e. a tag of the form
1886 // "fffe|xxxx" which is just a marker. Delimiters length should not be
1887 // taken into account.
1888 else if( gr == 0xfffe )
1890 // According to the norm, fffe|0000 shouldn't exist. BUT the Philips
1891 // image gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm happens to
1892 // causes extra troubles...
1893 if( entry->GetElement() != 0x0000 )
1899 entry->SetLength(foundLength);
1903 * \brief Apply some heuristics to predict whether the considered
1904 * element value contains/represents an integer or not.
1905 * @param entry The element value on which to apply the predicate.
1906 * @return The result of the heuristical predicate.
1908 bool Document::IsDocEntryAnInteger(DocEntry *entry)
1910 uint16_t elem = entry->GetElement();
1911 uint16_t group = entry->GetGroup();
1912 const std::string &vr = entry->GetVR();
1913 uint32_t length = entry->GetLength();
1915 // When we have some semantics on the element we just read, and if we
1916 // a priori know we are dealing with an integer, then we shall be
1917 // able to swap it's element value properly.
1918 if ( elem == 0 ) // This is the group length of the group
1926 // Allthough this should never happen, still some images have a
1927 // corrupted group length [e.g. have a glance at offset x(8336) of
1928 // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm].
1929 // Since for dicom compliant and well behaved headers, the present
1930 // test is useless (and might even look a bit paranoid), when we
1931 // encounter such an ill-formed image, we simply display a warning
1932 // message and proceed on parsing (while crossing fingers).
1933 long filePosition = Fp->tellg();
1934 gdcmVerboseMacro( "Erroneous Group Length element length on : ("
1935 << std::hex << group << " , " << elem
1936 << ") -before- position x(" << filePosition << ")"
1937 << "lgt : " << length );
1941 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
1949 * \brief Find the Length till the next sequence delimiter
1950 * \warning NOT end user intended method !
1954 uint32_t Document::FindDocEntryLengthOBOrOW()
1955 throw( FormatUnexpected )
1957 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1958 long positionOnEntry = Fp->tellg();
1959 bool foundSequenceDelimiter = false;
1960 uint32_t totalLength = 0;
1962 while ( !foundSequenceDelimiter )
1968 group = ReadInt16();
1971 catch ( FormatError )
1973 throw FormatError("Unexpected end of file encountered during ",
1974 "Document::FindDocEntryLengthOBOrOW()");
1977 // We have to decount the group and element we just read
1980 if ( group != 0xfffe || ( ( elem != 0xe0dd ) && ( elem != 0xe000 ) ) )
1982 long filePosition = Fp->tellg();
1983 gdcmVerboseMacro( "Neither an Item tag nor a Sequence delimiter tag on :"
1984 << std::hex << group << " , " << elem
1985 << ") -before- position x(" << filePosition << ")" );
1987 Fp->seekg(positionOnEntry, std::ios::beg);
1988 throw FormatUnexpected( "Neither an Item tag nor a Sequence delimiter tag.");
1991 if ( elem == 0xe0dd )
1993 foundSequenceDelimiter = true;
1996 uint32_t itemLength = ReadInt32();
1997 // We add 4 bytes since we just read the ItemLength with ReadInt32
1998 totalLength += itemLength + 4;
1999 SkipBytes(itemLength);
2001 if ( foundSequenceDelimiter )
2006 Fp->seekg( positionOnEntry, std::ios::beg);
2011 * \brief Reads a supposed to be 16 Bits integer
2012 * (swaps it depending on processor endianity)
2013 * @return read value
2015 uint16_t Document::ReadInt16()
2016 throw( FormatError )
2019 Fp->read ((char*)&g, (size_t)2);
2022 throw FormatError( "Document::ReadInt16()", " file error." );
2026 throw FormatError( "Document::ReadInt16()", "EOF." );
2033 * \brief Reads a supposed to be 32 Bits integer
2034 * (swaps it depending on processor endianity)
2035 * @return read value
2037 uint32_t Document::ReadInt32()
2038 throw( FormatError )
2041 Fp->read ((char*)&g, (size_t)4);
2044 throw FormatError( "Document::ReadInt32()", " file error." );
2048 throw FormatError( "Document::ReadInt32()", "EOF." );
2055 * \brief skips bytes inside the source file
2056 * \warning NOT end user intended method !
2059 void Document::SkipBytes(uint32_t nBytes)
2061 //FIXME don't dump the returned value
2062 Fp->seekg((long)nBytes, std::ios::cur);
2066 * \brief Loads all the needed Dictionaries
2067 * \warning NOT end user intended method !
2069 void Document::Initialise()
2071 RefPubDict = Global::GetDicts()->GetDefaultPubDict();
2073 RLEInfo = new RLEFramesInfo;
2074 JPEGInfo = new JPEGFragmentsInfo;
2079 * \brief Discover what the swap code is (among little endian, big endian,
2080 * bad little endian, bad big endian).
2082 * @return false when we are absolutely sure
2083 * it's neither ACR-NEMA nor DICOM
2084 * true when we hope ours assuptions are OK
2086 bool Document::CheckSwap()
2088 // The only guaranted way of finding the swap code is to find a
2089 // group tag since we know it's length has to be of four bytes i.e.
2090 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2091 // occurs when we can't find such group...
2093 uint32_t x = 4; // x : for ntohs
2094 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2100 // First, compare HostByteOrder and NetworkByteOrder in order to
2101 // determine if we shall need to swap bytes (i.e. the Endian type).
2102 if ( x == ntohs(x) )
2111 // The easiest case is the one of a DICOM header, since it possesses a
2112 // file preamble where it suffice to look for the string "DICM".
2115 char *entCur = deb + 128;
2116 if( memcmp(entCur, "DICM", (size_t)4) == 0 )
2118 gdcmVerboseMacro( "Looks like DICOM Version3" );
2120 // Next, determine the value representation (VR). Let's skip to the
2121 // first element (0002, 0000) and check there if we find "UL"
2122 // - or "OB" if the 1st one is (0002,0001) -,
2123 // in which case we (almost) know it is explicit VR.
2124 // WARNING: if it happens to be implicit VR then what we will read
2125 // is the length of the group. If this ascii representation of this
2126 // length happens to be "UL" then we shall believe it is explicit VR.
2127 // FIXME: in order to fix the above warning, we could read the next
2128 // element value (or a couple of elements values) in order to make
2129 // sure we are not commiting a big mistake.
2130 // We need to skip :
2131 // * the 128 bytes of File Preamble (often padded with zeroes),
2132 // * the 4 bytes of "DICM" string,
2133 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2134 // i.e. a total of 136 bytes.
2138 // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2139 // but elem 0002,0010 (Transfer Syntax) tells us the file is
2140 // *Implicit* VR. -and it is !-
2142 if( memcmp(entCur, "UL", (size_t)2) == 0 ||
2143 memcmp(entCur, "OB", (size_t)2) == 0 ||
2144 memcmp(entCur, "UI", (size_t)2) == 0 ||
2145 memcmp(entCur, "CS", (size_t)2) == 0 ) // CS, to remove later
2146 // when Write DCM *adds*
2148 // Use Document::dicom_vr to test all the possibilities
2149 // instead of just checking for UL, OB and UI !? group 0000
2151 Filetype = ExplicitVR;
2152 gdcmVerboseMacro( "Explicit Value Representation");
2156 Filetype = ImplicitVR;
2157 gdcmVerboseMacro( "Not an explicit Value Representation");
2163 gdcmVerboseMacro( "HostByteOrder != NetworkByteOrder");
2168 gdcmVerboseMacro( "HostByteOrder = NetworkByteOrder");
2171 // Position the file position indicator at first tag (i.e.
2172 // after the file preamble and the "DICM" string).
2173 Fp->seekg(0, std::ios::beg);
2174 Fp->seekg ( 132L, std::ios::beg);
2178 // Alas, this is not a DicomV3 file and whatever happens there is no file
2179 // preamble. We can reset the file position indicator to where the data
2180 // is (i.e. the beginning of the file).
2181 gdcmVerboseMacro( "Not a DICOM Version3 file");
2182 Fp->seekg(0, std::ios::beg);
2184 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2185 // By clean we mean that the length of the first tag is written down.
2186 // If this is the case and since the length of the first group HAS to be
2187 // four (bytes), then determining the proper swap code is straightforward.
2190 // We assume the array of char we are considering contains the binary
2191 // representation of a 32 bits integer. Hence the following dirty
2193 s32 = *((uint32_t *)(entCur));
2214 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2215 // It is time for despaired wild guesses.
2216 // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2217 // i.e. the 'group length' element is not present :
2219 // check the supposed-to-be 'group number'
2220 // in ( 0x0001 .. 0x0008 )
2221 // to determine ' SwapCode' value .
2222 // Only 0 or 4321 will be possible
2223 // (no oportunity to check for the formerly well known
2224 // ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian'
2225 // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -3, 4, ..., 8-)
2226 // the file IS NOT ACR-NEMA nor DICOM V3
2227 // Find a trick to tell it the caller...
2229 s16 = *((uint16_t *)(deb));
2256 gdcmVerboseMacro( "ACR/NEMA unfound swap info (Really hopeless !)");
2260 // Then the only info we have is the net2host one.
2272 * \brief Change the Byte Swap code.
2274 void Document::SwitchByteSwapCode()
2276 gdcmVerboseMacro( "Switching Byte Swap code.");
2277 if ( SwapCode == 0 )
2281 else if ( SwapCode == 4321 )
2285 else if ( SwapCode == 3412 )
2289 else if ( SwapCode == 2143 )
2296 * \brief during parsing, Header Elements too long are not loaded in memory
2299 void Document::SetMaxSizeLoadEntry(long newSize)
2305 if ((uint32_t)newSize >= (uint32_t)0xffffffff )
2307 MaxSizeLoadEntry = 0xffffffff;
2310 MaxSizeLoadEntry = newSize;
2315 * \brief Header Elements too long will not be printed
2316 * \todo See comments of \ref Document::MAX_SIZE_PRINT_ELEMENT_VALUE
2319 void Document::SetMaxSizePrintEntry(long newSize)
2321 //DOH !! This is exactly SetMaxSizeLoadEntry FIXME FIXME
2326 if ((uint32_t)newSize >= (uint32_t)0xffffffff )
2328 MaxSizePrintEntry = 0xffffffff;
2331 MaxSizePrintEntry = newSize;
2337 * \brief Handle broken private tag from Philips NTSCAN
2338 * where the endianess is being switch to BigEndian for no
2342 void Document::HandleBrokenEndian(uint16_t group, uint16_t elem)
2344 // Endian reversion. Some files contain groups of tags with reversed endianess.
2345 static int reversedEndian = 0;
2346 // try to fix endian switching in the middle of headers
2347 if ((group == 0xfeff) && (elem == 0x00e0))
2349 // start endian swap mark for group found
2351 SwitchByteSwapCode();
2356 else if (group == 0xfffe && elem == 0xe00d && reversedEndian)
2358 // end of reversed endian group
2360 SwitchByteSwapCode();
2365 * \brief Group 0002 is always coded Little Endian
2366 * whatever Transfer Syntax is
2369 void Document::HandleOutOfGroup0002(uint16_t group)
2371 // Endian reversion. Some files contain groups of tags with reversed endianess.
2372 if ( !Group0002Parsed && group != 0x0002)
2374 Group0002Parsed = true;
2375 // we just came out of group 0002
2376 // if Transfer syntax is Big Endian we have to change CheckSwap
2378 TagKey key = DictEntry::TranslateToKey(0x0002, 0x0010);
2379 if ( !TagHT.count(key))
2381 gdcmVerboseMacro("True DICOM File, with NO Tansfer Syntax ?!?");
2385 // FIXME Strangely, this works with
2386 //'Implicit VR Transfer Syntax (GE Private)
2388 if ( ((ValEntry *)TagHT.find(key)->second)->GetValue()
2389 == "Explicit VR - Big Endian" )
2391 gdcmVerboseMacro("Tansfer Syntax = Explicit VR - Big Endian");
2392 SwitchByteSwapCode();
2398 * \brief Read the next tag but WITHOUT loading it's value
2399 * (read the 'Group Number', the 'Element Number',
2400 * gets the Dict Entry
2401 * gets the VR, gets the length, gets the offset value)
2402 * @return On succes the newly created DocEntry, NULL on failure.
2404 DocEntry *Document::ReadNextDocEntry()
2411 group = ReadInt16();
2414 catch ( FormatError e )
2416 // We reached the EOF (or an error occured) therefore
2417 // header parsing has to be considered as finished.
2422 // Sometimes file contains groups of tags with reversed endianess.
2423 HandleBrokenEndian(group, elem);
2425 // In 'true DICOM' files Group 0002 is allways little endian
2426 if ( HasDCMPreamble )
2427 HandleOutOfGroup0002(group);
2429 std::string vr = FindDocEntryVR();
2430 std::string realVR = vr;
2432 if( vr == GDCM_UNKNOWN)
2434 DictEntry *dictEntry = GetDictEntry(group,elem);
2436 realVR = dictEntry->GetVR();
2440 if( Global::GetVR()->IsVROfSequence(realVR) )
2441 newEntry = NewSeqEntry(group, elem);
2442 else if( Global::GetVR()->IsVROfStringRepresentable(realVR) )
2443 newEntry = NewValEntry(group, elem,vr);
2445 newEntry = NewBinEntry(group, elem,vr);
2447 if( vr == GDCM_UNKNOWN )
2449 if( Filetype == ExplicitVR )
2451 // We thought this was explicit VR, but we end up with an
2452 // implicit VR tag. Let's backtrack.
2454 msg = Util::Format("Falsely explicit vr file (%04x,%04x)\n",
2455 newEntry->GetGroup(), newEntry->GetElement());
2456 gdcmVerboseMacro( msg.c_str() );
2458 newEntry->SetImplicitVR();
2463 FindDocEntryLength(newEntry);
2465 catch ( FormatError e )
2473 newEntry->SetOffset(Fp->tellg());
2480 * \brief Generate a free TagKey i.e. a TagKey that is not present
2481 * in the TagHt dictionary.
2482 * @param group The generated tag must belong to this group.
2483 * @return The element of tag with given group which is fee.
2485 uint32_t Document::GenerateFreeTagKeyInGroup(uint16_t group)
2487 for (uint32_t elem = 0; elem < UINT32_MAX; elem++)
2489 TagKey key = DictEntry::TranslateToKey(group, elem);
2490 if (TagHT.count(key) == 0)
2499 * \brief Assuming the internal file pointer \ref Document::Fp
2500 * is placed at the beginning of a tag check whether this
2501 * tag is (TestGroup, TestElement).
2502 * \warning On success the internal file pointer \ref Document::Fp
2503 * is modified to point after the tag.
2504 * On failure (i.e. when the tag wasn't the expected tag
2505 * (TestGroup, TestElement) the internal file pointer
2506 * \ref Document::Fp is restored to it's original position.
2507 * @param testGroup The expected group of the tag.
2508 * @param testElement The expected Element of the tag.
2509 * @return True on success, false otherwise.
2511 bool Document::ReadTag(uint16_t testGroup, uint16_t testElement)
2513 long positionOnEntry = Fp->tellg();
2514 long currentPosition = Fp->tellg(); // On debugging purposes
2516 //// Read the Item Tag group and element, and make
2517 // sure they are what we expected:
2518 uint16_t itemTagGroup;
2519 uint16_t itemTagElement;
2522 itemTagGroup = ReadInt16();
2523 itemTagElement = ReadInt16();
2525 catch ( FormatError e )
2527 //std::cerr << e << std::endl;
2530 if ( itemTagGroup != testGroup || itemTagElement != testElement )
2532 gdcmVerboseMacro( "Wrong Item Tag found:"
2533 << " We should have found tag ("
2534 << std::hex << testGroup << "," << testElement << ")" << std::endl
2535 << " but instead we encountered tag ("
2536 << std::hex << itemTagGroup << "," << itemTagElement << ")"
2538 << " at address: " << (unsigned int)currentPosition
2540 << " 0x(" << (unsigned int)currentPosition << ")"
2542 Fp->seekg(positionOnEntry, std::ios::beg);
2550 * \brief Assuming the internal file pointer \ref Document::Fp
2551 * is placed at the beginning of a tag (TestGroup, TestElement),
2552 * read the length associated to the Tag.
2553 * \warning On success the internal file pointer \ref Document::Fp
2554 * is modified to point after the tag and it's length.
2555 * On failure (i.e. when the tag wasn't the expected tag
2556 * (TestGroup, TestElement) the internal file pointer
2557 * \ref Document::Fp is restored to it's original position.
2558 * @param testGroup The expected group of the tag.
2559 * @param testElement The expected Element of the tag.
2560 * @return On success returns the length associated to the tag. On failure
2563 uint32_t Document::ReadTagLength(uint16_t testGroup, uint16_t testElement)
2565 long positionOnEntry = Fp->tellg();
2566 (void)positionOnEntry;
2568 if ( !ReadTag(testGroup, testElement) )
2573 //// Then read the associated Item Length
2574 long currentPosition = Fp->tellg();
2575 uint32_t itemLength = ReadInt32();
2577 gdcmVerboseMacro( "Basic Item Length is: "
2578 << itemLength << std::endl
2579 << " at address: " << (unsigned int)currentPosition);
2585 * \brief When parsing the Pixel Data of an encapsulated file, read
2586 * the basic offset table (when present, and BTW dump it).
2588 void Document::ReadAndSkipEncapsulatedBasicOffsetTable()
2590 //// Read the Basic Offset Table Item Tag length...
2591 uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
2593 // When present, read the basic offset table itself.
2594 // Notes: - since the presence of this basic offset table is optional
2595 // we can't rely on it for the implementation, and we will simply
2596 // trash it's content (when present).
2597 // - still, when present, we could add some further checks on the
2598 // lengths, but we won't bother with such fuses for the time being.
2599 if ( itemLength != 0 )
2601 char *basicOffsetTableItemValue = new char[itemLength + 1];
2602 Fp->read(basicOffsetTableItemValue, itemLength);
2605 for (unsigned int i=0; i < itemLength; i += 4 )
2607 uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
2609 gdcmVerboseMacro( "Read one length: " <<
2610 std::hex << individualLength );
2614 delete[] basicOffsetTableItemValue;
2619 * \brief Parse pixel data from disk of [multi-]fragment RLE encoding.
2620 * Compute the RLE extra information and store it in \ref RLEInfo
2621 * for later pixel retrieval usage.
2623 void Document::ComputeRLEInfo()
2625 std::string ts = GetTransferSyntax();
2626 if ( Global::GetTS()->IsRLELossless(ts) )
2631 // Encoded pixel data: for the time being we are only concerned with
2632 // Jpeg or RLE Pixel data encodings.
2633 // As stated in PS 3.5-2003, section 8.2 p44:
2634 // "If sent in Encapsulated Format (i.e. other than the Native Format) the
2635 // value representation OB is used".
2636 // Hence we expect an OB value representation. Concerning OB VR,
2637 // the section PS 3.5-2003, section A.4.c p 58-59, states:
2638 // "For the Value Representations OB and OW, the encoding shall meet the
2639 // following specifications depending on the Data element tag:"
2641 // - the first item in the sequence of items before the encoded pixel
2642 // data stream shall be basic offset table item. The basic offset table
2643 // item value, however, is not required to be present"
2645 ReadAndSkipEncapsulatedBasicOffsetTable();
2647 // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G)
2648 // Loop on the individual frame[s] and store the information
2649 // on the RLE fragments in a RLEFramesInfo.
2650 // Note: - when only a single frame is present, this is a
2652 // - when more than one frame are present, then we are in
2653 // the case of a multi-frame image.
2655 while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) )
2657 // Parse the RLE Header and store the corresponding RLE Segment
2658 // Offset Table information on fragments of this current Frame.
2659 // Note that the fragment pixels themselves are not loaded
2660 // (but just skipped).
2661 long frameOffset = Fp->tellg();
2663 uint32_t nbRleSegments = ReadInt32();
2664 if ( nbRleSegments > 16 )
2666 // There should be at most 15 segments (refer to RLEFrame class)
2667 gdcmVerboseMacro( "Too many segments.");
2670 uint32_t rleSegmentOffsetTable[16];
2671 for( int k = 1; k <= 15; k++ )
2673 rleSegmentOffsetTable[k] = ReadInt32();
2676 // Deduce from both the RLE Header and the frameLength the
2677 // fragment length, and again store this info in a
2679 long rleSegmentLength[15];
2680 // skipping (not reading) RLE Segments
2681 if ( nbRleSegments > 1)
2683 for(unsigned int k = 1; k <= nbRleSegments-1; k++)
2685 rleSegmentLength[k] = rleSegmentOffsetTable[k+1]
2686 - rleSegmentOffsetTable[k];
2687 SkipBytes(rleSegmentLength[k]);
2691 rleSegmentLength[nbRleSegments] = frameLength
2692 - rleSegmentOffsetTable[nbRleSegments];
2693 SkipBytes(rleSegmentLength[nbRleSegments]);
2695 // Store the collected info
2696 RLEFrame *newFrameInfo = new RLEFrame;
2697 newFrameInfo->NumberFragments = nbRleSegments;
2698 for( unsigned int uk = 1; uk <= nbRleSegments; uk++ )
2700 newFrameInfo->Offset[uk] = frameOffset + rleSegmentOffsetTable[uk];
2701 newFrameInfo->Length[uk] = rleSegmentLength[uk];
2703 RLEInfo->Frames.push_back( newFrameInfo );
2706 // Make sure that at the end of the item we encounter a 'Sequence
2708 if ( !ReadTag(0xfffe, 0xe0dd) )
2710 gdcmVerboseMacro( "No sequence delimiter item at end of RLE item sequence");
2715 * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
2716 * Compute the jpeg extra information (fragment[s] offset[s] and
2717 * length) and store it[them] in \ref JPEGInfo for later pixel
2720 void Document::ComputeJPEGFragmentInfo()
2722 // If you need to, look for comments of ComputeRLEInfo().
2723 std::string ts = GetTransferSyntax();
2724 if ( ! Global::GetTS()->IsJPEG(ts) )
2729 ReadAndSkipEncapsulatedBasicOffsetTable();
2731 // Loop on the fragments[s] and store the parsed information in a
2733 long fragmentLength;
2734 while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) )
2736 long fragmentOffset = Fp->tellg();
2738 // Store the collected info
2739 JPEGFragment *newFragment = new JPEGFragment;
2740 newFragment->Offset = fragmentOffset;
2741 newFragment->Length = fragmentLength;
2742 JPEGInfo->Fragments.push_back( newFragment );
2744 SkipBytes( fragmentLength );
2747 // Make sure that at the end of the item we encounter a 'Sequence
2749 if ( !ReadTag(0xfffe, 0xe0dd) )
2751 gdcmVerboseMacro( "No sequence delimiter item at end of JPEG item sequence");
2756 * \brief Walk recursively the given \ref DocEntrySet, and feed
2757 * the given hash table (\ref TagDocEntryHT) with all the
2758 * \ref DocEntry (Dicom entries) encountered.
2759 * This method does the job for \ref BuildFlatHashTable.
2760 * @param builtHT Where to collect all the \ref DocEntry encountered
2761 * when recursively walking the given set.
2762 * @param set The structure to be traversed (recursively).
2764 void Document::BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
2767 if (ElementSet *elementSet = dynamic_cast< ElementSet* > ( set ) )
2769 TagDocEntryHT const ¤tHT = elementSet->GetTagHT();
2770 for( TagDocEntryHT::const_iterator i = currentHT.begin();
2771 i != currentHT.end();
2774 DocEntry *entry = i->second;
2775 if ( SeqEntry *seqEntry = dynamic_cast<SeqEntry*>(entry) )
2777 const ListSQItem& items = seqEntry->GetSQItems();
2778 for( ListSQItem::const_iterator item = items.begin();
2779 item != items.end();
2782 BuildFlatHashTableRecurse( builtHT, *item );
2786 builtHT[entry->GetKey()] = entry;
2791 if (SQItem *SQItemSet = dynamic_cast< SQItem* > ( set ) )
2793 const ListDocEntry& currentList = SQItemSet->GetDocEntries();
2794 for (ListDocEntry::const_iterator i = currentList.begin();
2795 i != currentList.end();
2798 DocEntry *entry = *i;
2799 if ( SeqEntry *seqEntry = dynamic_cast<SeqEntry*>(entry) )
2801 const ListSQItem& items = seqEntry->GetSQItems();
2802 for( ListSQItem::const_iterator item = items.begin();
2803 item != items.end();
2806 BuildFlatHashTableRecurse( builtHT, *item );
2810 builtHT[entry->GetKey()] = entry;
2817 * \brief Build a \ref TagDocEntryHT (i.e. a std::map<>) from the current
2820 * The structure used by a Document (through \ref ElementSet),
2821 * in order to hold the parsed entries of a Dicom header, is a recursive
2822 * one. This is due to the fact that the sequences (when present)
2823 * can be nested. Additionaly, the sequence items (represented in
2824 * gdcm as \ref SQItem) add an extra complexity to the data
2825 * structure. Hence, a gdcm user whishing to visit all the entries of
2826 * a Dicom header will need to dig in the gdcm internals (which
2827 * implies exposing all the internal data structures to the API).
2828 * In order to avoid this burden to the user, \ref BuildFlatHashTable
2829 * recursively builds a temporary hash table, which holds all the
2830 * Dicom entries in a flat structure (a \ref TagDocEntryHT i.e. a
2832 * \warning Of course there is NO integrity constrain between the
2833 * returned \ref TagDocEntryHT and the \ref ElementSet used
2834 * to build it. Hence if the underlying \ref ElementSet is
2835 * altered, then it is the caller responsability to invoke
2836 * \ref BuildFlatHashTable again...
2837 * @return The flat std::map<> we juste build.
2839 TagDocEntryHT *Document::BuildFlatHashTable()
2841 TagDocEntryHT *FlatHT = new TagDocEntryHT;
2842 BuildFlatHashTableRecurse( *FlatHT, this );
2849 * \brief Compares two documents, according to \ref DicomDir rules
2850 * \warning Does NOT work with ACR-NEMA files
2851 * \todo Find a trick to solve the pb (use RET fields ?)
2853 * @return true if 'smaller'
2855 bool Document::operator<(Document &document)
2858 std::string s1 = GetEntry(0x0010,0x0010);
2859 std::string s2 = document.GetEntry(0x0010,0x0010);
2871 s1 = GetEntry(0x0010,0x0020);
2872 s2 = document.GetEntry(0x0010,0x0020);
2883 // Study Instance UID
2884 s1 = GetEntry(0x0020,0x000d);
2885 s2 = document.GetEntry(0x0020,0x000d);
2896 // Serie Instance UID
2897 s1 = GetEntry(0x0020,0x000e);
2898 s2 = document.GetEntry(0x0020,0x000e);
2915 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
2916 * @param filetype Type of the File to be written
2918 int Document::ComputeGroup0002Length( FileType filetype )
2923 int groupLength = 0;
2924 bool found0002 = false;
2926 // for each zero-level Tag in the DCM Header
2930 entry = GetNextEntry();
2933 gr = entry->GetGroup();
2939 el = entry->GetElement();
2940 vr = entry->GetVR();
2942 if (filetype == ExplicitVR)
2944 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
2946 groupLength += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
2949 groupLength += 2 + 2 + 4 + entry->GetLength();
2951 else if (found0002 )
2954 entry = GetNextEntry();
2959 } // end namespace gdcm
2961 //-----------------------------------------------------------------------------