1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.cxx,v $
6 Date: $Date: 2005/10/26 06:34:03 $
7 Version: $Revision: 1.309 $
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 "gdcmSeqEntry.h"
21 #include "gdcmGlobal.h"
23 #include "gdcmDebug.h"
25 #include "gdcmDictSet.h"
26 #include "gdcmDocEntrySet.h"
27 #include "gdcmSQItem.h"
28 #include "gdcmDataEntry.h"
33 #include <ctype.h> // for isdigit
34 #include <stdlib.h> // for atoi
38 //-----------------------------------------------------------------------------
40 // Refer to Document::SetMaxSizeLoadEntry()
41 const unsigned int Document::MAX_SIZE_LOAD_ELEMENT_VALUE = 0xfff; // 4096
43 //-----------------------------------------------------------------------------
44 // Constructor / Destructor
45 // Constructors and destructors are protected to avoid user to invoke directly
48 * \brief This default constructor neither loads nor parses the file.
49 * You should then invoke \ref Document::Load.
57 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
60 Filetype = ExplicitVR;
61 // Load will set it to true if sucessfull
62 Group0002Parsed = false;
63 IsDocumentAlreadyLoaded = false;
64 IsDocumentModified = true;
65 LoadMode = LD_ALL; // default : load everything, later
70 * \brief Canonical destructor.
72 Document::~Document ()
77 //-----------------------------------------------------------------------------
81 * \brief Loader. use SetLoadMode(), SetFileName() before !
82 * @return false if file cannot be open or no swap info was found,
83 * or no tag was found.
85 bool Document::Load( )
87 if ( GetFileName() == "" )
89 gdcmWarningMacro( "Use SetFileName, before !" );
92 return DoTheLoadingDocumentJob( );
95 * \brief Loader. (DEPRECATED : not to break the API)
96 * @param fileName 'Document' (File or DicomDir) to be open for parsing
97 * @return false if file cannot be open or no swap info was found,
98 * or no tag was found.
100 bool Document::Load( std::string const &fileName )
103 return DoTheLoadingDocumentJob( );
107 * \brief Performs the Loading Job (internal use only)
108 * @return false if file cannot be open or no swap info was found,
109 * or no tag was found.
111 bool Document::DoTheLoadingDocumentJob( )
113 if ( ! IsDocumentModified ) // Nothing to do !
121 // warning already performed in OpenFile()
122 //gdcmWarningMacro( "Unable to open as an ACR/DICOM file: "
123 // << Filename.c_str() );
128 Group0002Parsed = false;
130 gdcmDebugMacro( "Starting parsing of file: " << Filename.c_str());
132 Fp->seekg(0, std::ios::end);
133 long lgt = Fp->tellg(); // total length of the file
135 Fp->seekg(0, std::ios::beg);
137 // CheckSwap returns a boolean
138 // (false if no swap info of any kind was found)
141 gdcmWarningMacro( "Neither a DICOM V3 nor an ACR-NEMA file: "
142 << Filename.c_str());
147 long beg = Fp->tellg(); // just after DICOM preamble (if any)
149 lgt -= beg; // remaining length to parse
152 // Loading is done during parsing
153 ParseDES( this, beg, lgt, false); // delim_mode is first defaulted to false
157 gdcmWarningMacro( "No tag in internal hash table for: "
158 << Filename.c_str());
162 IsDocumentAlreadyLoaded = true;
164 Fp->seekg( 0, std::ios::beg);
166 // Load 'non string' values
168 std::string PhotometricInterpretation = GetEntryString(0x0028,0x0004);
169 if ( PhotometricInterpretation == "PALETTE COLOR " )
172 // Probabely this line should be outside the 'if'
173 // Try to find an image sample holding a 'gray LUT'
174 LoadEntryBinArea(0x0028,0x1200); // gray LUT
177 /// The tags refered by the three following lines used to be CORRECTLY
178 /// defined as having an US Value Representation in the public
179 /// dictionary. BUT the semantics implied by the three following
180 /// lines state that the corresponding tag contents are in fact
181 /// the ones of a DataEntry.
182 /// In order to fix things "Quick and Dirty" the dictionary was
183 /// altered on PURPOSE but now contains a WRONG value.
184 /// In order to fix things and restore the dictionary to its
185 /// correct value, one needs to decided of the semantics by deciding
186 /// whether the following tags are either :
187 /// - multivaluated US, and hence loaded as DataEntry, but afterwards
188 /// also used as DataEntry, which requires the proper conversion,
189 /// - OW, and hence loaded as DataEntry, but afterwards also used
190 /// as DataEntry, which requires the proper conversion.
191 LoadEntryBinArea(0x0028,0x1201); // R LUT
192 LoadEntryBinArea(0x0028,0x1202); // G LUT
193 LoadEntryBinArea(0x0028,0x1203); // B LUT
195 // Segmented Red Palette Color LUT Data
196 LoadEntryBinArea(0x0028,0x1221);
197 // Segmented Green Palette Color LUT Data
198 LoadEntryBinArea(0x0028,0x1222);
199 // Segmented Blue Palette Color LUT Data
200 LoadEntryBinArea(0x0028,0x1223);
203 //FIXME later : how to use it?
204 SeqEntry *modLutSeq = GetSeqEntry(0x0028,0x3000);
207 SQItem *sqi= modLutSeq->GetFirstSQItem();
210 DataEntry *dataEntry = sqi->GetDataEntry(0x0028,0x3006);
211 if ( dataEntry != 0 )
213 if ( dataEntry->GetLength() != 0 )
215 LoadEntryBinArea(dataEntry); //LUT Data (CTX dependent)
221 // Force Loading some more elements if user asked to.
224 for (ListElements::iterator it = UserForceLoadList.begin();
225 it != UserForceLoadList.end();
228 gdcmWarningMacro( "Force Load " << std::hex
229 << (*it).Group << "|" <<(*it).Elem );
231 d = GetDocEntry( (*it).Group, (*it).Elem);
235 gdcmWarningMacro( "You asked toForce Load " << std::hex
236 << (*it).Group <<"|"<< (*it).Elem
237 << " that doesn't exist" );
241 LoadDocEntry(d, true);
246 // ----------------------------
247 // Specific code to allow gdcm to read ACR-LibIDO formated images
248 // Note: ACR-LibIDO is an extension of the ACR standard that was
249 // used at CREATIS. For the time being (say a couple of years)
250 // we keep this kludge to allow CREATIS users
251 // reading their old images.
253 // if recognition code tells us we deal with a LibIDO image
254 // we switch lineNumber and columnNumber
257 RecCode = GetEntryString(0x0008, 0x0010); // recognition code (RET)
258 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
259 RecCode == "CANRME_AILIBOD1_1." ) // for brain-damaged softwares
260 // with "little-endian strings"
262 Filetype = ACR_LIBIDO;
263 std::string rows = GetEntryString(0x0028, 0x0010);
264 std::string columns = GetEntryString(0x0028, 0x0011);
265 SetEntryString(columns, 0x0028, 0x0010);
266 SetEntryString(rows , 0x0028, 0x0011);
268 // --- End of ACR-LibIDO kludge ---
274 * \brief Adds a new element we want to load anyway
275 * @param group Group number of the target tag.
276 * @param elem Element number of the target tag.
278 void Document::AddForceLoadElement (uint16_t group, uint16_t elem)
283 UserForceLoadList.push_back(el);
286 * \brief Get the public dictionary used
288 Dict *Document::GetPubDict()
294 * \brief Get the shadow dictionary used
296 Dict *Document::GetShaDict()
302 * \brief Set the shadow dictionary used
303 * @param dict dictionary to use in shadow
305 bool Document::SetShaDict(Dict *dict)
312 * \brief Set the shadow dictionary used
313 * @param dictName name of the dictionary to use in shadow
315 bool Document::SetShaDict(DictKey const &dictName)
317 RefShaDict = Global::GetDicts()->GetDict(dictName);
322 * \brief This predicate tells us whether or not the current Document
323 * was properly parsed and contains at least *one* Dicom Element
324 * (and nothing more, sorry).
325 * @return false when we're 150 % sure it's NOT a Dicom/Acr file,
328 bool Document::IsReadable()
330 if ( Filetype == Unknown )
332 gdcmWarningMacro( "Wrong filetype");
338 gdcmWarningMacro( "No tag in internal hash table.");
346 * \brief Predicate for dicom version 3 file.
347 * @return True when the file is a dicom version 3.
349 bool Document::IsDicomV3()
351 // Checking if Transfer Syntax exists is enough
352 // Anyway, it's too late check if the 'Preamble' was found ...
353 // And ... would it be a rich idea to check ?
354 // (some 'no Preamble' DICOM images exist !)
355 return GetDocEntry(0x0002, 0x0010) != NULL;
359 * \brief Predicate for Papyrus file
360 * Dedicated to whomsoever it may concern
361 * @return True when the file is a Papyrus file.
363 bool Document::IsPapyrus()
365 // check for Papyrus private Sequence
366 DocEntry *e = GetDocEntry(0x0041, 0x1050);
369 // check if it's actually a Sequence
370 if ( !dynamic_cast<SeqEntry*>(e) )
376 * \brief returns the File Type
377 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
378 * @return the FileType code
380 FileType Document::GetFileType()
386 * \brief Accessor to the Transfer Syntax (when present) of the
387 * current document (it internally handles reading the
388 * value from disk when only parsing occured).
389 * @return The encountered Transfer Syntax of the current document, if DICOM.
390 * GDCM_UNKNOWN for ACR-NEMA files (or broken headers ...)
392 std::string Document::GetTransferSyntax()
394 DocEntry *entry = GetDocEntry(0x0002, 0x0010);
400 // The entry might be present but not loaded (parsing and loading
401 // happen at different stages): try loading and proceed with check...
402 LoadDocEntrySafe(entry);
403 if (DataEntry *dataEntry = dynamic_cast<DataEntry *>(entry) )
405 std::string transfer = dataEntry->GetString();
406 // The actual transfer (as read from disk) might be padded. We
407 // first need to remove the potential padding. We can make the
408 // weak assumption that padding was not executed with digits...
409 if ( transfer.length() == 0 )
411 // for brain damaged headers
414 while ( !isdigit((unsigned char)transfer[transfer.length()-1]) )
416 transfer.erase(transfer.length()-1, 1);
417 if ( transfer.length() == 0 )
419 // for brain damaged headers
429 * \brief Accesses the info from 0002,0010 : Transfer Syntax and TS
430 * @return The full Transfer Syntax Name (as opposed to Transfer Syntax UID)
432 std::string Document::GetTransferSyntaxName()
434 // use the TS (TS : Transfer Syntax)
435 std::string transferSyntax = GetEntryString(0x0002,0x0010);
437 if ( (transferSyntax.find(GDCM_NOTLOADED) < transferSyntax.length()) )
439 gdcmErrorMacro( "Transfer Syntax not loaded. " << std::endl
440 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE" );
441 return "Uncompressed ACR-NEMA";
443 if ( transferSyntax == GDCM_UNFOUND )
445 gdcmWarningMacro( "Unfound Transfer Syntax (0002,0010)");
446 return "Uncompressed ACR-NEMA";
449 // we do it only when we need it
450 const TSKey &tsName = Global::GetTS()->GetValue( transferSyntax );
452 // Global::GetTS() is a global static you shall never try to delete it!
456 // --------------- Swap Code ------------------
458 * \brief Swaps the bytes so they agree with the processor order
459 * @return The properly swaped 16 bits integer.
461 uint16_t Document::SwapShort(uint16_t a)
463 if ( SwapCode == 4321 || SwapCode == 2143 )
465 //a = ((( a << 8 ) & 0xff00 ) | (( a >> 8 ) & 0x00ff ) );
467 a = ( a << 8 ) | ( a >> 8 );
473 * \brief Swaps back the bytes of 4-byte long integer accordingly to
475 * @return The properly swaped 32 bits integer.
477 uint32_t Document::SwapLong(uint32_t a)
484 // a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
485 // ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
487 a=( ( a<<24) | ((a<<8) & 0x00ff0000) |
488 ((a>>8) & 0x0000ff00) | (a>>24) );
491 // a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
492 a=( (a<<16) | (a>>16) );
495 a=( ((a<< 8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
498 gdcmErrorMacro( "Unset swap code:" << SwapCode );
505 // -----------------File I/O ---------------
507 * \brief Tries to open the file \ref Document::Filename and
508 * checks the preamble when existing.
509 * @return The FILE pointer on success.
511 std::ifstream *Document::OpenFile()
513 HasDCMPreamble = false;
514 if (Filename.length() == 0)
521 gdcmWarningMacro( "File already open: " << Filename.c_str());
525 Fp = new std::ifstream(Filename.c_str(), std::ios::in | std::ios::binary);
528 // Don't user gdcmErrorMacro :
529 // a spurious message will appear when you use, for instance
530 // gdcm::FileHelper *fh = new gdcm::FileHelper( outputFileName );
531 // to create outputFileName.
532 gdcmWarningMacro( "Cannot open file: " << Filename.c_str());
536 //exit(1); // No function is allowed to leave the application instead
537 // of warning the caller
541 Fp->read((char*)&zero, (size_t)2);
548 //-- ACR or DICOM with no Preamble; may start with a Shadow Group --
550 zero == 0x0001 || zero == 0x0100 || zero == 0x0002 || zero == 0x0200 ||
551 zero == 0x0003 || zero == 0x0300 || zero == 0x0004 || zero == 0x0400 ||
552 zero == 0x0005 || zero == 0x0500 || zero == 0x0006 || zero == 0x0600 ||
553 zero == 0x0007 || zero == 0x0700 || zero == 0x0008 || zero == 0x0800 )
555 std::string msg = Util::Format(
556 "ACR/DICOM starting at the beginning of the file:(%04x)\n", zero);
557 gdcmWarningMacro( msg.c_str() );
562 Fp->seekg(126L, std::ios::cur);
563 char dicm[4]; // = {' ',' ',' ',' '};
564 Fp->read(dicm, (size_t)4);
570 if ( memcmp(dicm, "DICM", 4) == 0 )
572 HasDCMPreamble = true;
576 // -- Neither ACR/No Preamble Dicom nor DICOMV3 file
578 gdcmWarningMacro( "Neither ACR/No Preamble Dicom nor DICOMV3 file: "
579 << Filename.c_str());
584 * \brief closes the file
585 * @return TRUE if the close was successfull
587 bool Document::CloseFile()
599 * \brief Writes in a file all the Entries (Dicom Elements)
600 * @param fp file pointer on an already open file (actually: Output File Stream)
601 * @param filetype Type of the File to be written
602 * (ACR-NEMA, ExplicitVR, ImplicitVR)
604 void Document::WriteContent(std::ofstream *fp, FileType filetype)
606 // Skip if user wants to write an ACR-NEMA file
608 if ( filetype == ImplicitVR || filetype == ExplicitVR ||
611 // writing Dicom File Preamble
612 char filePreamble[128];
613 memset(filePreamble, 0, 128);
614 fp->write(filePreamble, 128);
615 fp->write("DICM", 4);
619 * \todo rewrite later, if really usefull
620 * - 'Group Length' element is optional in DICOM
621 * - but un-updated odd groups lengthes can causes pb
624 * if ( (filetype == ImplicitVR) || (filetype == ExplicitVR) )
625 * UpdateGroupLength(false,filetype);
626 * if ( filetype == ACR)
627 * UpdateGroupLength(true,ACR);
630 ElementSet::WriteContent(fp, filetype); // This one is recursive
633 // -----------------------------------------
636 * \brief Loads (from disk) the element content
637 * when a string is not suitable
638 * @param group group number of the Entry
639 * @param elem element number of the Entry
641 void Document::LoadEntryBinArea(uint16_t group, uint16_t elem)
643 // Search the corresponding DocEntry
644 DocEntry *docEntry = GetDocEntry(group, elem);
647 gdcmWarningMacro(std::hex << group << "|" << elem
648 << "doesn't exist" );
651 DataEntry *dataEntry = dynamic_cast<DataEntry *>(docEntry);
654 gdcmWarningMacro(std::hex << group << "|" << elem
655 << "is NOT a DataEntry");
658 LoadEntryBinArea(dataEntry);
662 * \brief Loads (from disk) the element content
663 * when a string is not suitable
664 * @param entry Entry whose binArea is going to be loaded
666 void Document::LoadEntryBinArea(DataEntry *entry)
668 if( entry->GetBinArea() )
675 size_t o =(size_t)entry->GetOffset();
676 Fp->seekg(o, std::ios::beg);
678 size_t l = entry->GetLength();
679 uint8_t *data = new uint8_t[l];
682 gdcmWarningMacro( "Cannot allocate DataEntry content for : "
683 << std::hex << entry->GetGroup()
684 << "|" << entry->GetElement() );
689 Fp->read((char*)data, l);
690 if ( Fp->fail() || Fp->eof() )
693 entry->SetState(DataEntry::STATE_UNREAD);
697 // Swap the data content if necessary
699 unsigned short vrLgth =
700 Global::GetVR()->GetAtomicElementLength(entry->GetVR());
701 if( entry->GetVR() == "OW" )
708 uint16_t *data16 = (uint16_t *)data;
709 for(i=0;i<l/vrLgth;i++)
710 data16[i] = SwapShort(data16[i]);
715 uint32_t *data32 = (uint32_t *)data;
716 for(i=0;i<l/vrLgth;i++)
717 data32[i] = SwapLong(data32[i]);
722 gdcmWarningMacro("Can't swap 64 bits data");
723 /* uint64_t *data64 = (uint64_t *)data;
724 for(i=0;i<l/vrLgth;i++)
725 data64[i] = SwapLongLong(data64[i]);*/
730 entry->SetBinArea(data);
737 * \brief Loads the element while preserving the current
738 * underlying file position indicator as opposed to
739 * LoadDocEntry that modifies it.
740 * @param entry DocEntry whose value will be loaded.
742 void Document::LoadDocEntrySafe(DocEntry *entry)
746 long PositionOnEntry = Fp->tellg();
748 Fp->seekg(PositionOnEntry, std::ios::beg);
753 * \brief Compares two documents, according to \ref DicomDir rules
754 * \warning Does NOT work with ACR-NEMA files
755 * \todo Find a trick to solve the pb (use RET fields ?)
756 * @param document to compare with current one
757 * @return true if 'smaller'
759 bool Document::operator<(Document &document)
762 std::string s1 = GetEntryString(0x0010,0x0010);
763 std::string s2 = document.GetEntryString(0x0010,0x0010);
775 s1 = GetEntryString(0x0010,0x0020);
776 s2 = document.GetEntryString(0x0010,0x0020);
787 // Study Instance UID
788 s1 = GetEntryString(0x0020,0x000d);
789 s2 = document.GetEntryString(0x0020,0x000d);
800 // Serie Instance UID
801 s1 = GetEntryString(0x0020,0x000e);
802 s2 = document.GetEntryString(0x0020,0x000e);
817 //-----------------------------------------------------------------------------
820 * \brief Reads a supposed to be 16 Bits integer
821 * (swaps it depending on processor endianness)
824 uint16_t Document::ReadInt16()
828 Fp->read ((char*)&g, (size_t)2);
831 throw FormatError( "Document::ReadInt16()", " file error." );
835 throw FormatError( "Document::ReadInt16()", "EOF." );
842 * \brief Reads a supposed to be 32 Bits integer
843 * (swaps it depending on processor endianness)
846 uint32_t Document::ReadInt32()
850 Fp->read ((char*)&g, (size_t)4);
853 throw FormatError( "Document::ReadInt32()", " file error." );
857 throw FormatError( "Document::ReadInt32()", "EOF." );
864 * \brief skips bytes inside the source file
865 * \warning NOT end user intended method !
868 void Document::SkipBytes(uint32_t nBytes)
870 //FIXME don't dump the returned value
871 Fp->seekg((long)nBytes, std::ios::cur);
875 * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
877 int Document::ComputeGroup0002Length( )
883 bool found0002 = false;
885 // for each zero-level Tag in the DCM Header
886 DocEntry *entry = GetFirstEntry();
889 gr = entry->GetGroup();
895 if ( entry->GetElement() != 0x0000 )
899 // FIXME : group 0x0002 is *always* Explicit VR!
901 //if ( filetype == ExplicitVR )
903 //if ( (vr == "OB")||(vr == "OW")||(vr == "UT")||(vr == "SQ"))
904 // (no SQ, OW, UT in group 0x0002;)
907 // explicit VR AND (OB, OW, SQ, UT) : 4 more bytes
911 groupLength += 2 + 2 + 4 + entry->GetLength();
917 entry = GetNextEntry();
922 //-----------------------------------------------------------------------------
925 * \brief Loads all the needed Dictionaries
926 * \warning NOT end user intended method !
928 void Document::Initialize()
930 RefPubDict = Global::GetDicts()->GetDefaultPubDict();
936 * \brief Parses a DocEntrySet (Zero-level DocEntries or SQ Item DocEntries)
937 * @param set DocEntrySet we are going to parse ('zero level' or a SQItem)
938 * @param offset start of parsing
939 * @param l_max length to parse (meaningless when we are in 'delimitor mode')
940 * @param delim_mode : whether we are in 'delimitor mode' (l=0xffffff) or not
942 void Document::ParseDES(DocEntrySet *set, long offset,
943 long l_max, bool delim_mode)
945 DocEntry *newDocEntry;
946 DataEntry *newDataEntry;
947 SeqEntry *newSeqEntry;
949 bool used; // will be set to false when something wrong happens to an Entry.
950 // (Entry will then be deleted)
951 bool delim_mode_intern = delim_mode;
953 gdcmWarningMacro( "Enter in ParseDES, delim-mode " << delim_mode
954 << " at offset " << std::hex << offset );
957 if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
962 newDocEntry = ReadNextDocEntry( );
965 // Private tag, in IMplicit VR are defaulted as a DataEntry,
966 // Very often they are only composed of Printable characters,
967 // and could be defaulted as a DataEntry.
968 // It's too late to do the Job
969 // (we should check the value, but we know it after LoadDocEntry ...)
971 // Uncoment this printf line to be able to 'follow' the DocEntries
972 // when something *very* strange happens
974 //printf( "%04x|%04x %s\n",newDocEntry->GetGroup(),
975 // newDocEntry->GetElement(),
976 // newDocEntry->GetVR().c_str() );
983 // an Item Starter found elsewhere but the first position
984 // of a SeqEntry means previous entry was a Sequence
985 // but we didn't get it (private Sequence + Implicit VR)
986 // we have to backtrack.
987 if ( !first && newDocEntry->IsItemStarter() )
989 newDocEntry = Backtrack(newDocEntry);
993 PreviousDocEntry = newDocEntry;
997 newDataEntry = dynamic_cast<DataEntry*>(newDocEntry);
1001 //////////////////////////// DataEntry
1003 vr = newDocEntry->GetVR();
1005 // Useless checking, now !
1007 if ( Filetype == ExplicitVR &&
1008 !Global::GetVR()->IsVROfBinaryRepresentable(vr) )
1010 ////// No DataEntry: should mean UNKOWN VR
1011 gdcmWarningMacro( std::hex << newDocEntry->GetGroup()
1012 << "|" << newDocEntry->GetElement()
1014 " Probably 'Implicit VR' entry within "
1015 "an explicit VR 'document'.");
1019 if ( !set->AddEntry( newDataEntry ) )
1021 gdcmWarningMacro( "in ParseDES : cannot add a DataEntry "
1022 << newDataEntry->GetKey()
1024 << newDataEntry->GetOffset() << " )" );
1029 newDataEntry->Delete();
1030 // Load only if we can add (not a duplicate key)
1031 LoadDocEntry( newDataEntry );
1034 if ( newDataEntry->GetElement() == 0x0000 ) // if on group length
1036 if ( newDataEntry->GetGroup()%2 != 0 ) // if Shadow Group
1038 if ( LoadMode & LD_NOSHADOW ) // if user asked to skip shad.gr
1040 std::string strLgrGroup = newDataEntry->GetString();
1042 if ( newDataEntry->IsUnfound() )
1044 lgrGroup = atoi(strLgrGroup.c_str());
1045 Fp->seekg(lgrGroup, std::ios::cur);
1046 //used = false; // never used
1047 RemoveEntry( newDocEntry ); // Remove and delete
1048 // bcc 5.5 is right "assigned a value that's never used"
1056 bool delimitor = newDataEntry->IsItemDelimitor();
1059 (!delim_mode && ((long)(Fp->tellg())-offset) >= l_max) )
1062 newDocEntry->Delete();
1066 // Just to make sure we are at the beginning of next entry.
1067 SkipToNextDocEntry(newDocEntry);
1071 /////////////////////// SeqEntry : VR = "SQ"
1073 unsigned long l = newDocEntry->GetReadLength();
1074 if ( l != 0 ) // don't mess the delim_mode for 'zero-length sequence'
1076 if ( l == 0xffffffff )
1078 delim_mode_intern = true;
1082 delim_mode_intern = false;
1086 if ( (LoadMode & LD_NOSHADOWSEQ) && ! delim_mode_intern )
1088 // User asked to skip SeQuences *only* if they belong to Shadow Group
1089 if ( newDocEntry->GetGroup()%2 != 0 )
1091 Fp->seekg( l, std::ios::cur);
1092 newDocEntry->Delete(); // Delete, not in the set
1096 if ( (LoadMode & LD_NOSEQ) && ! delim_mode_intern )
1098 // User asked to skip *any* SeQuence
1099 Fp->seekg( l, std::ios::cur);
1100 newDocEntry->Delete(); // Delete, not in the set
1103 // delay the dynamic cast as late as possible
1104 newSeqEntry = dynamic_cast<SeqEntry*>(newDocEntry);
1106 // no other way to create the Delimitor ...
1107 newSeqEntry->SetDelimitorMode( delim_mode_intern );
1109 // At the top of the hierarchy, stands a Document. When "set"
1110 // is a Document, then we are building the first depth level.
1111 // Hence the SeqEntry we are building simply has a depth
1113 if ( set == this ) // ( dynamic_cast< Document* > ( set ) )
1115 newSeqEntry->SetDepthLevel( 1 );
1117 // But when "set" is already a SQItem, we are building a nested
1118 // sequence, and hence the depth level of the new SeqEntry
1119 // we are building, is one level deeper:
1121 // time waste hunting
1122 else if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
1124 newSeqEntry->SetDepthLevel( parentSQItem->GetDepthLevel() + 1 );
1128 { // Don't try to parse zero-length sequences
1130 gdcmWarningMacro( "Entry in ParseSQ, delim " << delim_mode_intern
1131 << " at offset " << std::hex
1132 << newDocEntry->GetOffset() );
1134 ParseSQ( newSeqEntry,
1135 newDocEntry->GetOffset(),
1136 l, delim_mode_intern);
1138 gdcmWarningMacro( "Exit from ParseSQ, delim " << delim_mode_intern);
1141 if ( !set->AddEntry( newSeqEntry ) )
1143 gdcmWarningMacro( "in ParseDES : cannot add a SeqEntry "
1144 << newSeqEntry->GetKey()
1146 << newSeqEntry->GetOffset() << " )" );
1151 newDocEntry->Delete();
1154 if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1157 newDocEntry->Delete();
1160 } // end SeqEntry : VR = "SQ"
1164 newDocEntry->Delete();
1168 gdcmDebugMacro( "Exit from ParseDES, delim-mode " << delim_mode );
1172 * \brief Parses a Sequence ( SeqEntry after SeqEntry)
1173 * @return parsed length for this level
1175 void Document::ParseSQ( SeqEntry *seqEntry,
1176 long offset, long l_max, bool delim_mode)
1178 int SQItemNumber = 0;
1180 long offsetStartCurrentSQItem = offset;
1184 // the first time, we read the fff0,e000 of the first SQItem
1185 DocEntry *newDocEntry = ReadNextDocEntry();
1189 // FIXME Should warn user
1190 gdcmWarningMacro("in ParseSQ : should never get here!");
1195 if ( newDocEntry->IsSequenceDelimitor() )
1197 seqEntry->SetDelimitationItem( newDocEntry );
1198 newDocEntry->Delete();
1202 if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1204 newDocEntry->Delete();
1207 // create the current SQItem
1208 SQItem *itemSQ = SQItem::New( seqEntry->GetDepthLevel() );
1209 unsigned int l = newDocEntry->GetReadLength();
1211 if ( l == 0xffffffff )
1220 // Let's try :------------
1221 // remove fff0,e000, created out of the SQItem
1222 Fp->seekg(offsetStartCurrentSQItem, std::ios::beg);
1223 // fill up the current SQItem, starting at the beginning of fff0,e000
1225 ParseDES(itemSQ, offsetStartCurrentSQItem, l+8, dlm_mod);
1227 offsetStartCurrentSQItem = Fp->tellg();
1228 // end try -----------------
1230 seqEntry->AddSQItem( itemSQ, SQItemNumber );
1232 newDocEntry->Delete();
1234 if ( !delim_mode && ((long)(Fp->tellg())-offset ) >= l_max )
1242 * \brief When a private Sequence + Implicit VR is encountered
1243 * we cannot guess it's a Sequence till we find the first
1244 * Item Starter. We then backtrack to do the job.
1245 * @param docEntry Item Starter that warned us
1247 DocEntry *Document::Backtrack(DocEntry *docEntry)
1249 // delete the Item Starter, built erroneously out of any Sequence
1250 // it's not yet in the HTable/chained list
1253 // Get all info we can from PreviousDocEntry
1254 uint16_t group = PreviousDocEntry->GetGroup();
1255 uint16_t elem = PreviousDocEntry->GetElement();
1256 uint32_t lgt = PreviousDocEntry->GetLength();
1257 long offset = PreviousDocEntry->GetOffset();
1259 gdcmWarningMacro( "Backtrack :" << std::hex << group
1261 << " at offset " << offset );
1262 RemoveEntry( PreviousDocEntry );
1264 // forge the Seq Entry
1265 DocEntry *newEntry = NewSeqEntry(group, elem);
1266 newEntry->SetLength(lgt);
1267 newEntry->SetOffset(offset);
1269 // Move back to the beginning of the Sequence
1270 Fp->seekg( 0, std::ios::beg);
1271 Fp->seekg(offset, std::ios::cur);
1277 * \brief Loads (or not) the element content depending if its length exceeds
1278 * or not the value specified with Document::SetMaxSizeLoadEntry()
1279 * @param entry Header Entry (Dicom Element) to be dealt with
1280 * @param forceLoad whether you want to force loading of 'long' elements
1282 void Document::LoadDocEntry(DocEntry *entry, bool forceLoad)
1284 uint16_t group = entry->GetGroup();
1285 uint16_t elem = entry->GetElement();
1286 const VRKey &vr = entry->GetVR();
1287 uint32_t length = entry->GetLength();
1289 Fp->seekg((long)entry->GetOffset(), std::ios::beg);
1291 // A SeQuence "contains" a set of Elements.
1292 // (fffe e000) tells us an Element is beginning
1293 // (fffe e00d) tells us an Element just ended
1294 // (fffe e0dd) tells us the current SeQuence just ended
1296 // (fffe 0000) is an 'impossible' tag value,
1297 // found in MR-PHILIPS-16-Multi-Seq.dcm
1299 if ( (group == 0xfffe && elem != 0x0000 ) || vr == "SQ" )
1301 // NO more value field for SQ !
1305 DataEntry *dataEntryPtr = dynamic_cast< DataEntry* >(entry);
1311 // When the length is zero things are easy:
1314 dataEntryPtr->SetBinArea(NULL,true);
1318 // The elements whose length is bigger than the specified upper bound
1319 // are not loaded. Instead we leave a short notice on the offset of
1320 // the element content and it's length.
1322 std::ostringstream s;
1326 if (length > MaxSizeLoadEntry)
1328 dataEntryPtr->SetBinArea(NULL,true);
1329 dataEntryPtr->SetState(DataEntry::STATE_NOTLOADED);
1331 // to be sure we are at the end of the value ...
1332 Fp->seekg((long)entry->GetOffset()+(long)entry->GetLength(),
1338 LoadEntryBinArea(dataEntryPtr); // last one, not to erase length !
1342 * \brief Find the value Length of the passed Doc Entry
1343 * @param entry Header Entry whose length of the value shall be loaded.
1345 void Document::FindDocEntryLength( DocEntry *entry )
1346 throw ( FormatError )
1348 const VRKey &vr = entry->GetVR();
1351 if ( Filetype == ExplicitVR && !entry->IsImplicitVR() )
1353 if ( vr == "OB" || vr == "OW" || vr == "SQ" || vr == "UT"
1356 // The following reserved two bytes (see PS 3.5-2003, section
1357 // "7.1.2 Data element structure with explicit vr", p 27) must be
1358 // skipped before proceeding on reading the length on 4 bytes.
1359 Fp->seekg( 2L, std::ios::cur);
1360 uint32_t length32 = ReadInt32();
1362 if ( (vr == "OB" || vr == "OW") && length32 == 0xffffffff )
1367 lengthOB = FindDocEntryLengthOBOrOW();
1369 catch ( FormatUnexpected )
1371 // Computing the length failed (this happens with broken
1372 // files like gdcm-JPEG-LossLess3a.dcm). We still have a
1373 // chance to get the pixels by deciding the element goes
1374 // until the end of the file. Hence we artificially fix the
1375 // the length and proceed.
1376 gdcmWarningMacro( " Computing the length failed for " <<
1377 entry->GetKey() <<" in " <<GetFileName());
1379 long currentPosition = Fp->tellg();
1380 Fp->seekg(0L,std::ios::end);
1382 long lengthUntilEOF = (long)(Fp->tellg())-currentPosition;
1383 Fp->seekg(currentPosition, std::ios::beg);
1385 entry->SetReadLength(lengthUntilEOF);
1386 entry->SetLength(lengthUntilEOF);
1389 entry->SetReadLength(lengthOB);
1390 entry->SetLength(lengthOB);
1393 FixDocEntryFoundLength(entry, length32);
1397 // Length is encoded on 2 bytes.
1398 length16 = ReadInt16();
1400 // 0xffff means that we deal with 'No Length' Sequence
1401 // or 'No Length' SQItem
1402 if ( length16 == 0xffff)
1406 FixDocEntryFoundLength( entry, (uint32_t)length16 );
1411 // Either implicit VR or a non DICOM conformal (see note below) explicit
1412 // VR that ommited the VR of (at least) this element. Farts happen.
1413 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1414 // on Data elements "Implicit and Explicit VR Data Elements shall
1415 // not coexist in a Data Set and Data Sets nested within it".]
1416 // Length is on 4 bytes.
1418 // Well ... group 0002 is always coded in 'Explicit VR Litle Endian'
1419 // even if Transfer Syntax is 'Implicit VR ...'
1421 FixDocEntryFoundLength( entry, ReadInt32() );
1427 * \brief Find the Length till the next sequence delimiter
1428 * \warning NOT end user intended method !
1431 uint32_t Document::FindDocEntryLengthOBOrOW()
1432 throw( FormatUnexpected )
1434 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1435 long positionOnEntry = Fp->tellg();
1436 bool foundSequenceDelimiter = false;
1437 uint32_t totalLength = 0;
1439 while ( !foundSequenceDelimiter )
1445 group = ReadInt16();
1448 catch ( FormatError )
1450 throw FormatError("Unexpected end of file encountered during ",
1451 "Document::FindDocEntryLengthOBOrOW()");
1453 // We have to decount the group and element we just read
1455 if ( group != 0xfffe || ( ( elem != 0xe0dd ) && ( elem != 0xe000 ) ) )
1457 long filePosition = Fp->tellg();
1459 "Neither an Item tag nor a Sequence delimiter tag on :"
1460 << std::hex << group << " , " << elem
1461 << ") -before- position x(" << filePosition << ")" );
1463 Fp->seekg(positionOnEntry, std::ios::beg);
1464 throw FormatUnexpected(
1465 "Neither an Item tag nor a Sequence delimiter tag.");
1467 if ( elem == 0xe0dd )
1469 foundSequenceDelimiter = true;
1471 uint32_t itemLength = ReadInt32();
1472 // We add 4 bytes since we just read the ItemLength with ReadInt32
1473 totalLength += itemLength + 4;
1474 SkipBytes(itemLength);
1476 if ( foundSequenceDelimiter )
1481 Fp->seekg( positionOnEntry, std::ios::beg);
1486 * \brief Find the Value Representation of the current Dicom Element.
1487 * @return Value Representation of the current Entry
1489 VRKey Document::FindDocEntryVR()
1491 if ( Filetype != ExplicitVR )
1492 return GDCM_VRUNKNOWN;
1494 long positionOnEntry = Fp->tellg();
1495 // Warning: we believe this is explicit VR (Value Representation) because
1496 // we used a heuristic that found "UL" in the first tag. Alas this
1497 // doesn't guarantee that all the tags will be in explicit VR. In some
1498 // cases (see e-film filtered files) one finds implicit VR tags mixed
1499 // within an explicit VR file. Hence we make sure the present tag
1500 // is in explicit VR and try to fix things if it happens not to be
1504 Fp->read(&(vr[0]),(size_t)2);
1506 //gdcmDebugMacro( "--> VR: " << vr )
1507 if ( !CheckDocEntryVR(vr) )
1509 gdcmWarningMacro( "Unknown VR '" << vr << "'" )
1510 Fp->seekg(positionOnEntry, std::ios::beg);
1511 return GDCM_VRUNKNOWN;
1517 * \brief Check the correspondance between the VR of the header entry
1518 * and the taken VR. If they are different, the header entry is
1519 * updated with the new VR.
1520 * @param vr Dicom Value Representation
1521 * @return false if the VR is incorrect or if the VR isn't referenced
1522 * otherwise, it returns true
1524 bool Document::CheckDocEntryVR(const VRKey &vr)
1526 return Global::GetVR()->IsValidVR(vr);
1530 * \brief Skip a given Header Entry
1531 * \warning NOT end user intended method !
1532 * @param entry entry to skip
1534 void Document::SkipDocEntry(DocEntry *entry)
1536 SkipBytes(entry->GetLength());
1540 * \brief Skips to the beginning of the next Header Entry
1541 * \warning NOT end user intended method !
1542 * @param currentDocEntry entry to skip
1544 void Document::SkipToNextDocEntry(DocEntry *currentDocEntry)
1546 int l = currentDocEntry->GetReadLength();
1547 if ( l == -1 ) // length = 0xffff shouldn't appear here ...
1548 // ... but PMS imagers happen !
1550 Fp->seekg((long)(currentDocEntry->GetOffset()), std::ios::beg);
1551 if (currentDocEntry->GetGroup() != 0xfffe) // for fffe pb
1553 Fp->seekg( (long)(currentDocEntry->GetReadLength()),std::ios::cur);
1558 * \brief When the length of an element value is obviously wrong (because
1559 * the parser went Jabberwocky) one can hope improving things by
1560 * applying some heuristics.
1561 * @param entry entry to check
1562 * @param foundLength first assumption about length
1564 void Document::FixDocEntryFoundLength(DocEntry *entry,
1565 uint32_t foundLength)
1567 entry->SetReadLength( foundLength );// will be updated only if a bug is found
1568 if ( foundLength == 0xffffffff)
1573 uint16_t gr = entry->GetGroup();
1574 uint16_t elem = entry->GetElement();
1576 if ( foundLength % 2)
1578 gdcmWarningMacro( "Warning : Tag with uneven length " << foundLength
1579 << " in x(" << std::hex << gr << "," << elem <<")");
1582 //////// Fix for some naughty General Electric images.
1583 // Allthough not recent many such GE corrupted images are still present
1584 // on Creatis hard disks. Hence this fix shall remain when such images
1585 // are no longer in use (we are talking a few years, here)...
1586 // Note: XMedCon probably uses such a trick since it is able to read
1587 // those pesky GE images ...
1588 if ( foundLength == 13)
1590 // Only happens for this length !
1591 if ( gr != 0x0008 || ( elem != 0x0070 && elem != 0x0080 ) )
1594 entry->SetReadLength(10); // a bug is to be fixed !?
1598 //////// Fix for some brain-dead 'Leonardo' Siemens images.
1599 // Occurence of such images is quite low (unless one leaves close to a
1600 // 'Leonardo' source. Hence, one might consider commenting out the
1601 // following fix on efficiency reasons.
1602 else if ( gr == 0x0009 && ( elem == 0x1113 || elem == 0x1114 ) )
1605 entry->SetReadLength(4); // a bug is to be fixed !
1608 else if ( entry->GetVR() == "SQ" )
1610 foundLength = 0; // ReadLength is unchanged
1613 //////// We encountered a 'delimiter' element i.e. a tag of the form
1614 // "fffe|xxxx" which is just a marker. Delimiters length should not be
1615 // taken into account.
1616 else if ( gr == 0xfffe )
1618 // According to the norm, fffe|0000 shouldn't exist. BUT the Philips
1619 // image gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm happens to
1620 // causes extra troubles...
1621 if ( entry->GetElement() != 0x0000 )
1627 foundLength=12; // to skip the mess that follows this bugged Tag !
1630 entry->SetLength(foundLength);
1634 * \brief Apply some heuristics to predict whether the considered
1635 * element value contains/represents an integer or not.
1636 * @param entry The element value on which to apply the predicate.
1637 * @return The result of the heuristical predicate.
1639 bool Document::IsDocEntryAnInteger(DocEntry *entry)
1641 uint16_t elem = entry->GetElement();
1642 uint16_t group = entry->GetGroup();
1643 const VRKey &vr = entry->GetVR();
1644 uint32_t length = entry->GetLength();
1646 // When we have some semantics on the element we just read, and if we
1647 // a priori know we are dealing with an integer, then we shall be
1648 // able to swap it's element value properly.
1649 if ( elem == 0 ) // This is the group length of the group
1657 // Although this should never happen, still some images have a
1658 // corrupted group length [e.g. have a glance at offset x(8336) of
1659 // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm.
1660 // Since for dicom compliant and well behaved headers, the present
1661 // test is useless (and might even look a bit paranoid), when we
1662 // encounter such an ill-formed image, we simply display a warning
1663 // message and proceed on parsing (while crossing fingers).
1664 long filePosition = Fp->tellg();
1665 gdcmWarningMacro( "Erroneous Group Length element length on : ("
1666 << std::hex << group << " , " << elem
1667 << ") -before- position x(" << filePosition << ")"
1668 << "lgt : " << length );
1672 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
1680 * \brief Discover what the swap code is (among little endian, big endian,
1681 * bad little endian, bad big endian).
1683 * @return false when we are absolutely sure
1684 * it's neither ACR-NEMA nor DICOM
1685 * true when we hope ours assuptions are OK
1687 bool Document::CheckSwap()
1694 // First, compare HostByteOrder and NetworkByteOrder in order to
1695 // determine if we shall need to swap bytes (i.e. the Endian type).
1696 bool net2host = Util::IsCurrentProcessorBigEndian();
1698 // The easiest case is the one of a 'true' DICOM header, we just have
1699 // to look for the string "DICM" inside the file preamble.
1702 char *entCur = deb + 128;
1703 if ( memcmp(entCur, "DICM", (size_t)4) == 0 )
1705 gdcmDebugMacro( "Looks like DICOM Version3 (preamble + DCM)" );
1707 // Group 0002 should always be VR, and the first element 0000
1708 // Let's be carefull (so many wrong headers ...)
1709 // and determine the value representation (VR) :
1710 // Let's skip to the first element (0002,0000) and check there if we find
1711 // "UL" - or "OB" if the 1st one is (0002,0001) -,
1712 // in which case we (almost) know it is explicit VR.
1713 // WARNING: if it happens to be implicit VR then what we will read
1714 // is the length of the group. If this ascii representation of this
1715 // length happens to be "UL" then we shall believe it is explicit VR.
1716 // We need to skip :
1717 // * the 128 bytes of File Preamble (often padded with zeroes),
1718 // * the 4 bytes of "DICM" string,
1719 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
1720 // i.e. a total of 136 bytes.
1723 // group 0x0002 *is always* Explicit VR Sometimes ,
1724 // even if elem 0002,0010 (Transfer Syntax) tells us the file is
1725 // *Implicit* VR (see former 'gdcmData/icone.dcm')
1727 if ( memcmp(entCur, "UL", (size_t)2) == 0 ||
1728 memcmp(entCur, "OB", (size_t)2) == 0 ||
1729 memcmp(entCur, "UI", (size_t)2) == 0 ||
1730 memcmp(entCur, "CS", (size_t)2) == 0 ) // CS, to remove later
1731 // when Write DCM *adds*
1733 // Use Document::dicom_vr to test all the possibilities
1734 // instead of just checking for UL, OB and UI !? group 0000
1736 Filetype = ExplicitVR;
1737 gdcmDebugMacro( "Group 0002 : Explicit Value Representation");
1741 Filetype = ImplicitVR;
1742 gdcmErrorMacro( "Group 0002 :Not an explicit Value Representation;"
1743 << "Looks like a bugged Header!");
1749 gdcmDebugMacro( "HostByteOrder != NetworkByteOrder");
1754 gdcmDebugMacro( "HostByteOrder = NetworkByteOrder");
1757 // Position the file position indicator at first tag
1758 // (i.e. after the file preamble and the "DICM" string).
1760 Fp->seekg(0, std::ios::beg); // FIXME : Is it usefull?
1762 Fp->seekg ( 132L, std::ios::beg);
1764 } // ------------------------------- End of DicomV3 ----------------
1766 // Alas, this is not a DicomV3 file and whatever happens there is no file
1767 // preamble. We can reset the file position indicator to where the data
1768 // is (i.e. the beginning of the file).
1770 gdcmWarningMacro( "Not a Kosher DICOM Version3 file (no preamble)");
1772 Fp->seekg(0, std::ios::beg);
1774 // Let's check 'No Preamble Dicom File' :
1775 // Should start with group 0x0002
1776 // and be Explicit Value Representation
1778 s16 = *((uint16_t *)(deb));
1791 if ( SwapCode != 0 )
1793 if ( memcmp(entCur, "UL", (size_t)2) == 0 ||
1794 memcmp(entCur, "OB", (size_t)2) == 0 ||
1795 memcmp(entCur, "UI", (size_t)2) == 0 ||
1796 memcmp(entCur, "SH", (size_t)2) == 0 ||
1797 memcmp(entCur, "AE", (size_t)2) == 0 ||
1798 memcmp(entCur, "OB", (size_t)2) == 0 )
1800 Filetype = ExplicitVR;
1801 gdcmDebugMacro( "Group 0002 : Explicit Value Representation");
1805 // ------------------------------- End of 'No Preamble' DicomV3 -------------
1807 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
1808 // By clean we mean that the length of the first group is written down.
1809 // If this is the case and since the length of the first group HAS to be
1810 // four (bytes), then determining the proper swap code is straightforward.
1813 // We assume the array of char we are considering contains the binary
1814 // representation of a 32 bits integer. Hence the following dirty
1816 s32 = *((uint32_t *)(entCur));
1836 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
1837 // It is time for despaired wild guesses.
1838 // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
1839 // i.e. the 'group length' element is not present :
1841 // check the supposed-to-be 'group number'
1842 // in ( 0x0001 .. 0x0008 )
1843 // to determine ' SwapCode' value .
1844 // Only 0 or 4321 will be possible
1845 // (no oportunity to check for the formerly well known
1846 // ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian'
1847 // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc-3, 4, ..., 8-)
1848 // the file IS NOT ACR-NEMA nor DICOM V3
1849 // Find a trick to tell it the caller...
1851 s16 = *((uint16_t *)(deb));
1878 gdcmWarningMacro("ACR/NEMA unfound swap info (Hopeless !)");
1886 * \brief Change the Byte Swap code.
1888 void Document::SwitchByteSwapCode()
1890 gdcmDebugMacro( "Switching Byte Swap code from "<< SwapCode
1891 << " at: 0x" << std::hex << Fp->tellg() );
1892 if ( SwapCode == 1234 )
1896 else if ( SwapCode == 4321 )
1900 else if ( SwapCode == 3412 )
1904 else if ( SwapCode == 2143 )
1908 gdcmDebugMacro( " Into: "<< SwapCode );
1912 * \brief during parsing, Header Elements too long are not loaded in memory
1913 * @param newSize new size
1915 void Document::SetMaxSizeLoadEntry(long newSize)
1921 if ((uint32_t)newSize >= (uint32_t)0xffffffff )
1923 MaxSizeLoadEntry = 0xffffffff;
1926 MaxSizeLoadEntry = newSize;
1930 * \brief Read the next tag WITHOUT loading it's value
1931 * (read the 'Group Number', the 'Element Number',
1932 * gets the Dict Entry
1933 * gets the VR, gets the length, gets the offset value)
1934 * @return On succes : the newly created DocEntry, NULL on failure.
1936 DocEntry *Document::ReadNextDocEntry()
1943 group = ReadInt16();
1946 catch ( FormatError )
1948 // We reached the EOF (or an error occured) therefore
1949 // header parsing has to be considered as finished.
1953 // Sometimes file contains groups of tags with reversed endianess.
1954 HandleBrokenEndian(group, elem);
1956 // In 'true DICOM' files Group 0002 is always little endian
1957 if ( HasDCMPreamble )
1958 HandleOutOfGroup0002(group, elem);
1960 VRKey vr = FindDocEntryVR();
1964 if ( vr == GDCM_VRUNKNOWN )
1966 if ( elem == 0x0000 ) // Group Length
1968 realVR = "UL"; // must be UL
1970 else if (group%2 == 1 && (elem >= 0x0010 && elem <=0x00ff ))
1972 // DICOM PS 3-5 7.8.1 a) states that those
1973 // (gggg-0010->00FF where gggg is odd) attributes have to be LO
1978 DictEntry *dictEntry = GetDictEntry(group,elem);
1981 realVR = dictEntry->GetVR();
1982 dictEntry->Unregister();
1986 // gdcmDebugMacro( "Found VR: " << vr << " / Real VR: " << realVR );
1989 if ( Global::GetVR()->IsVROfSequence(realVR) )
1990 newEntry = NewSeqEntry(group, elem);
1993 newEntry = NewDataEntry(group, elem, realVR);
1994 static_cast<DataEntry *>(newEntry)->SetState(DataEntry::STATE_NOTLOADED);
1997 if ( vr == GDCM_VRUNKNOWN )
1999 if ( Filetype == ExplicitVR )
2001 // We thought this was explicit VR, but we end up with an
2002 // implicit VR tag. Let's backtrack.
2003 if ( newEntry->GetGroup() != 0xfffe )
2006 int offset = Fp->tellg();
2008 "Entry (%04x,%04x) at x(%x) should be Explicit VR\n",
2009 newEntry->GetGroup(), newEntry->GetElement(), offset );
2010 gdcmWarningMacro( msg.c_str() );
2013 newEntry->SetImplicitVR();
2018 FindDocEntryLength(newEntry);
2020 catch ( FormatError )
2027 newEntry->SetOffset(Fp->tellg());
2033 * \brief Handle broken private tag from Philips NTSCAN
2034 * where the endianess is being switched to BigEndian
2035 * for no apparent reason
2038 void Document::HandleBrokenEndian(uint16_t &group, uint16_t &elem)
2040 // Endian reversion.
2041 // Some files contain groups of tags with reversed endianess.
2042 static int reversedEndian = 0;
2043 // try to fix endian switching in the middle of headers
2044 if ((group == 0xfeff) && (elem == 0x00e0))
2046 // start endian swap mark for group found
2048 SwitchByteSwapCode();
2053 else if (group == 0xfffe && elem == 0xe00d && reversedEndian)
2055 // end of reversed endian group
2057 SwitchByteSwapCode();
2059 else if (group == 0xfeff && elem == 0xdde0)
2061 // reversed Sequence Terminator found
2062 // probabely a bug in the header !
2063 // Do what you want, it breaks !
2065 //SwitchByteSwapCode();
2066 gdcmWarningMacro( "Should never get here! reversed Sequence Terminator!" );
2071 else if (group == 0xfffe && elem == 0xe0dd)
2073 gdcmWarningMacro( "Straight Sequence Terminator." );
2078 * \brief Group 0002 is always coded Little Endian
2079 * whatever Transfer Syntax is
2082 void Document::HandleOutOfGroup0002(uint16_t &group, uint16_t &elem)
2084 // Endian reversion.
2085 // Some files contain groups of tags with reversed endianess.
2086 if ( !Group0002Parsed && group != 0x0002)
2088 Group0002Parsed = true;
2089 // we just came out of group 0002
2090 // if Transfer syntax is Big Endian we have to change CheckSwap
2092 std::string ts = GetTransferSyntax();
2093 if ( !Global::GetTS()->IsTransferSyntax(ts) )
2095 gdcmWarningMacro("True DICOM File, with NO Tansfer Syntax: " << ts );
2099 // Group 0002 is always 'Explicit ...'
2100 // even when Transfer Syntax says 'Implicit ..."
2102 if ( Global::GetTS()->GetSpecialTransferSyntax(ts) ==
2103 TS::ImplicitVRLittleEndian )
2105 Filetype = ImplicitVR;
2108 // FIXME Strangely, this works with
2109 //'Implicit VR BigEndian Transfer Syntax (GE Private)
2111 // --> Probabely normal, since we considered we never have
2112 // to trust manufacturers.
2113 // (we find very often 'Implicit VR' tag,
2114 // even when Transfer Syntax tells us it's Explicit ...
2115 if ( Global::GetTS()->GetSpecialTransferSyntax(ts) ==
2116 TS::ExplicitVRBigEndian )
2118 gdcmWarningMacro("Transfer Syntax Name = ["
2119 << GetTransferSyntaxName() << "]" );
2120 SwitchByteSwapCode();
2121 group = SwapShort(group);
2122 elem = SwapShort(elem);
2127 //-----------------------------------------------------------------------------
2130 //-----------------------------------------------------------------------------
2131 } // end namespace gdcm