1 /*=========================================================================
4 Module: $RCSfile: gdcmDocument.cxx,v $
6 Date: $Date: 2004/08/01 00:59:21 $
7 Version: $Revision: 1.62 $
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.htm 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"
24 #include "gdcmGlobal.h"
26 #include "gdcmDebug.h"
35 #include <netinet/in.h>
40 // Implicit VR Little Endian
41 #define UI1_2_840_10008_1_2 "1.2.840.10008.1.2"
42 // Explicit VR Little Endian
43 #define UI1_2_840_10008_1_2_1 "1.2.840.10008.1.2.1"
44 // Deflated Explicit VR Little Endian
45 #define UI1_2_840_10008_1_2_1_99 "1.2.840.10008.1.2.1.99"
46 // Explicit VR Big Endian
47 #define UI1_2_840_10008_1_2_2 "1.2.840.10008.1.2.2"
48 // JPEG Baseline (Process 1)
49 #define UI1_2_840_10008_1_2_4_50 "1.2.840.10008.1.2.4.50"
50 // JPEG Extended (Process 2 & 4)
51 #define UI1_2_840_10008_1_2_4_51 "1.2.840.10008.1.2.4.51"
52 // JPEG Extended (Process 3 & 5)
53 #define UI1_2_840_10008_1_2_4_52 "1.2.840.10008.1.2.4.52"
54 // JPEG Spectral Selection, Non-Hierarchical (Process 6 & 8)
55 #define UI1_2_840_10008_1_2_4_53 "1.2.840.10008.1.2.4.53"
56 // JPEG Full Progression, Non-Hierarchical (Process 10 & 12)
57 #define UI1_2_840_10008_1_2_4_55 "1.2.840.10008.1.2.4.55"
58 // JPEG Lossless, Non-Hierarchical (Process 14)
59 #define UI1_2_840_10008_1_2_4_57 "1.2.840.10008.1.2.4.57"
60 // JPEG Lossless, Hierarchical, First-Order Prediction (Process 14,
61 // [Selection Value 1])
62 #define UI1_2_840_10008_1_2_4_70 "1.2.840.10008.1.2.4.70"
64 #define UI1_2_840_10008_1_2_4_90 "1.2.840.10008.1.2.4.90"
66 #define UI1_2_840_10008_1_2_4_91 "1.2.840.10008.1.2.4.91"
68 #define UI1_2_840_10008_1_2_5 "1.2.840.10008.1.2.5"
69 // UI1_1_2_840_10008_1_2_5
70 #define str2num(str, typeNum) *((typeNum *)(str))
72 //-----------------------------------------------------------------------------
73 // Refer to gdcmDocument::CheckSwap()
74 const unsigned int gdcmDocument::HEADER_LENGTH_TO_READ = 256;
76 // Refer to gdcmDocument::SetMaxSizeLoadEntry()
77 const unsigned int gdcmDocument::MAX_SIZE_LOAD_ELEMENT_VALUE = 0xfff; // 4096
78 const unsigned int gdcmDocument::MAX_SIZE_PRINT_ELEMENT_VALUE = 0x7fffffff;
80 //-----------------------------------------------------------------------------
81 // Constructor / Destructor
85 * @param inFilename file to be opened for parsing
86 * @param exception_on_error whether we throw an exception or not
88 gdcmDocument::gdcmDocument( std::string const & filename,
89 bool exception_on_error)
92 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
96 if ( !OpenFile(exception_on_error))
101 dbg.Verbose(0, "gdcmDocument::gdcmDocument: starting parsing of file: ",
105 fseek(Fp,0L,SEEK_END);
106 long lgt = ftell(Fp);
110 long beg = ftell(Fp);
115 long l = ParseDES( this, beg, lgt, false); // le Load sera fait a la volee
116 (void)l; //is l used anywhere ?
120 // Load 'non string' values
122 std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
123 if( PhotometricInterpretation == "PALETTE COLOR " )
125 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
126 LoadEntryVoidArea(0x0028,0x1201); // R LUT
127 LoadEntryVoidArea(0x0028,0x1202); // G LUT
128 LoadEntryVoidArea(0x0028,0x1203); // B LUT
130 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
131 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
132 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
134 //FIXME later : how to use it?
135 LoadEntryVoidArea(0x0028,0x3006); //LUT Data (CTX dependent)
139 // --------------------------------------------------------------
140 // Special Patch to allow gdcm to read ACR-LibIDO formated images
142 // if recognition code tells us we deal with a LibIDO image
143 // we switch lineNumber and columnNumber
146 RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
147 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
148 RecCode == "CANRME_AILIBOD1_1." ) // for brain-damaged softwares
149 // with "little-endian strings"
151 Filetype = gdcmACR_LIBIDO;
152 std::string rows = GetEntryByNumber(0x0028, 0x0010);
153 std::string columns = GetEntryByNumber(0x0028, 0x0011);
154 SetEntryByNumber(columns, 0x0028, 0x0010);
155 SetEntryByNumber(rows , 0x0028, 0x0011);
157 // ----------------- End of Special Patch ----------------
159 PrintLevel = 1; // 'Medium' print level by default
164 * @param exception_on_error
166 gdcmDocument::gdcmDocument(bool exception_on_error)
169 (void)exception_on_error;
171 SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
174 PrintLevel = 1; // 'Medium' print level by default
178 * \brief Canonical destructor.
180 gdcmDocument::~gdcmDocument ()
185 // Recursive clean up of sequences
186 for (TagDocEntryHT::const_iterator it = TagHT.begin();
187 it != TagHT.end(); ++it )
194 //-----------------------------------------------------------------------------
198 * \brief Prints The Dict Entries of THE public Dicom Dictionary
201 void gdcmDocument::PrintPubDict(std::ostream & os)
203 RefPubDict->Print(os);
207 * \brief Prints The Dict Entries of THE shadow Dicom Dictionary
210 void gdcmDocument::PrintShaDict(std::ostream & os)
212 RefShaDict->Print(os);
215 //-----------------------------------------------------------------------------
218 * \brief Get the public dictionary used
220 gdcmDict *gdcmDocument::GetPubDict()
226 * \brief Get the shadow dictionary used
228 gdcmDict *gdcmDocument::GetShaDict()
234 * \brief Set the shadow dictionary used
235 * \param dict dictionary to use in shadow
237 bool gdcmDocument::SetShaDict(gdcmDict *dict)
244 * \brief Set the shadow dictionary used
245 * \param dictName name of the dictionary to use in shadow
247 bool gdcmDocument::SetShaDict(DictKey dictName)
249 RefShaDict = gdcmGlobal::GetDicts()->GetDict(dictName);
254 * \brief This predicate, based on hopefully reasonable heuristics,
255 * decides whether or not the current gdcmDocument was properly parsed
256 * and contains the mandatory information for being considered as
257 * a well formed and usable Dicom/Acr File.
258 * @return true when gdcmDocument is the one of a reasonable Dicom/Acr file,
261 bool gdcmDocument::IsReadable()
263 if( Filetype == gdcmUnknown)
265 std::cout << " gdcmDocument::IsReadable: Filetype " << Filetype
266 << " " << "gdcmUnknown " << gdcmUnknown << std::endl; //JPR
267 dbg.Verbose(0, "gdcmDocument::IsReadable: wrong filetype");
273 dbg.Verbose(0, "gdcmDocument::IsReadable: no tags in internal"
283 * \brief Internal function that checks whether the Transfer Syntax given
284 * as argument is the one present in the current document.
285 * @param SyntaxToCheck The transfert syntax we need to check against.
286 * @return True when SyntaxToCheck corresponds to the Transfer Syntax of
287 * the current document. False either when the document contains
288 * no Transfer Syntax, or when the Tranfer Syntaxes doesn't match.
290 bool gdcmDocument::IsGivenTransferSyntax(std::string const & syntaxToCheck)
292 gdcmDocEntry *entry = GetDocEntryByNumber(0x0002, 0x0010);
298 // The entry might be present but not loaded (parsing and loading
299 // happen at different stages): try loading and proceed with check...
300 LoadDocEntrySafe(entry);
301 if (gdcmValEntry* valEntry = dynamic_cast< gdcmValEntry* >(entry) )
303 std::string transfer = valEntry->GetValue();
304 // The actual transfer (as read from disk) might be padded. We
305 // first need to remove the potential padding. We can make the
306 // weak assumption that padding was not executed with digits...
307 if ( transfer.length() == 0 ) { // for brain damaged headers
310 while ( ! isdigit(transfer[transfer.length()-1]) )
312 transfer.erase(transfer.length()-1, 1);
314 if ( transfer == syntaxToCheck )
323 * \brief Determines if the Transfer Syntax of the present document
324 * corresponds to a Implicit Value Representation of
326 * \sa \ref gdcmDocument::IsGivenTransferSyntax.
327 * @return True when ImplicitVRLittleEndian found. False in all other cases.
329 bool gdcmDocument::IsImplicitVRLittleEndianTransferSyntax()
331 return IsGivenTransferSyntax(UI1_2_840_10008_1_2);
335 * \brief Determines if the Transfer Syntax was already encountered
336 * and if it corresponds to a ExplicitVRLittleEndian one.
337 * @return True when ExplicitVRLittleEndian found. False in all other cases.
339 bool gdcmDocument::IsExplicitVRLittleEndianTransferSyntax()
341 return IsGivenTransferSyntax(UI1_2_840_10008_1_2_1);
345 * \brief Determines if the Transfer Syntax was already encountered
346 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
347 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
349 bool gdcmDocument::IsDeflatedExplicitVRLittleEndianTransferSyntax()
351 return IsGivenTransferSyntax(UI1_2_840_10008_1_2_1_99);
355 * \brief Determines if the Transfer Syntax was already encountered
356 * and if it corresponds to a Explicit VR Big Endian one.
357 * @return True when big endian found. False in all other cases.
359 bool gdcmDocument::IsExplicitVRBigEndianTransferSyntax()
361 return IsGivenTransferSyntax(UI1_2_840_10008_1_2_2);
365 * \brief Determines if the Transfer Syntax was already encountered
366 * and if it corresponds to a JPEGBaseLineProcess1 one.
367 * @return True when JPEGBaseLineProcess1found. False in all other cases.
369 bool gdcmDocument::IsJPEGBaseLineProcess1TransferSyntax()
371 return IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_50);
375 * \brief Determines if the Transfer Syntax was already encountered
376 * and if it corresponds to a JPEGExtendedProcess2-4 one.
377 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
379 bool gdcmDocument::IsJPEGExtendedProcess2_4TransferSyntax()
381 return IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_51);
385 * \brief Determines if the Transfer Syntax was already encountered
386 * and if it corresponds to a JPEGExtendeProcess3-5 one.
387 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
389 bool gdcmDocument::IsJPEGExtendedProcess3_5TransferSyntax()
391 return IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_52);
395 * \brief Determines if the Transfer Syntax was already encountered
396 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
397 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
400 bool gdcmDocument::IsJPEGSpectralSelectionProcess6_8TransferSyntax()
402 return IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_53);
406 * \brief Determines if the Transfer Syntax was already encountered
407 * and if it corresponds to a RLE Lossless one.
408 * @return True when RLE Lossless found. False in all
411 bool gdcmDocument::IsRLELossLessTransferSyntax()
413 return IsGivenTransferSyntax(UI1_2_840_10008_1_2_5);
417 * \brief Determines if Transfer Syntax was already encountered
418 * and if it corresponds to a JPEG Lossless one.
419 * @return True when RLE Lossless found. False in all
423 bool gdcmDocument::IsJPEGLossless()
425 return ( IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_55)
426 || IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_57)
427 || IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_70) );
431 * \brief Determines if the Transfer Syntax was already encountered
432 * and if it corresponds to a JPEG2000 one
433 * @return True when JPEG2000 (Lossly or LossLess) found. False in all
436 bool gdcmDocument::IsJPEG2000()
438 return ( IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_90)
439 || IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_91) );
443 * \brief Predicate for dicom version 3 file.
444 * @return True when the file is a dicom version 3.
446 bool gdcmDocument::IsDicomV3()
448 // Checking if Transfert Syntax exists is enough
449 // Anyway, it's to late check if the 'Preamble' was found ...
450 // And ... would it be a rich idea to check ?
451 // (some 'no Preamble' DICOM images exist !)
452 return GetDocEntryByNumber(0x0002, 0x0010) != NULL;
456 * \brief returns the File Type
457 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
458 * @return the FileType code
460 FileType gdcmDocument::GetFileType()
466 * \brief opens the file
467 * @param exception_on_error
470 FILE *gdcmDocument::OpenFile(bool exception_on_error)
473 Fp = fopen(Filename.c_str(),"rb");
477 if(exception_on_error)
479 throw gdcmFileError("gdcmDocument::gdcmDocument(const char *, bool)");
483 dbg.Verbose(0, "gdcmDocument::OpenFile cannot open file: ",
492 fread(&zero, (size_t)2, (size_t)1, Fp);
494 //ACR -- or DICOM with no Preamble --
495 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200 )
501 fseek(Fp, 126L, SEEK_CUR);
503 fread(dicm, (size_t)4, (size_t)1, Fp);
504 if( memcmp(dicm, "DICM", 4) == 0 )
510 dbg.Verbose(0, "gdcmDocument::OpenFile not DICOM/ACR", Filename.c_str());
514 dbg.Verbose(0, "gdcmDocument::OpenFile cannot open file", Filename.c_str());
521 * \brief closes the file
522 * @return TRUE if the close was successfull
524 bool gdcmDocument::CloseFile()
526 int closed = fclose(Fp);
533 * \brief Writes in a file all the Header Entries (Dicom Elements)
534 * @param fp file pointer on an already open file
535 * @param filetype Type of the File to be written
536 * (ACR-NEMA, ExplicitVR, ImplicitVR)
537 * \return Always true.
539 void gdcmDocument::Write(FILE* fp,FileType filetype)
541 /// \todo move the following lines (and a lot of others, to be written)
542 /// to a future function CheckAndCorrectHeader
544 /// WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
545 /// no way (check : FileType est un champ de gdcmDocument ...)
546 /// a moins de se livrer a un tres complique ajout des champs manquants.
547 /// faire un CheckAndCorrectHeader (?)
549 if (filetype == gdcmImplicitVR)
551 std::string implicitVRTransfertSyntax = UI1_2_840_10008_1_2;
552 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
554 /// \todo Refer to standards on page 21, chapter 6.2
555 /// "Value representation": values with a VR of UI shall be
556 /// padded with a single trailing null
557 /// in the following case we have to padd manually with a 0
559 SetEntryLengthByNumber(18, 0x0002, 0x0010);
562 if (filetype == gdcmExplicitVR)
564 std::string explicitVRTransfertSyntax = UI1_2_840_10008_1_2_1;
565 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
567 /// \todo Refer to standards on page 21, chapter 6.2
568 /// "Value representation": values with a VR of UI shall be
569 /// padded with a single trailing null
570 /// Dans le cas suivant on doit pader manuellement avec un 0
572 SetEntryLengthByNumber(20, 0x0002, 0x0010);
576 * \todo rewrite later, if really usefull
577 * - 'Group Length' element is optional in DICOM
578 * - but un-updated odd groups lengthes can causes pb
581 * if ( (filetype == ImplicitVR) || (filetype == ExplicitVR) )
582 * UpdateGroupLength(false,filetype);
583 * if ( filetype == ACR)
584 * UpdateGroupLength(true,ACR);
587 gdcmElementSet::Write(fp, filetype); // This one is recursive
592 * \brief Modifies the value of a given Header Entry (Dicom Element)
593 * when it exists. Create it with the given value when unexistant.
594 * @param Value (string) Value to be set
595 * @param Group Group number of the Entry
596 * @param Elem Element number of the Entry
597 * \return pointer to the modified/created Header Entry (NULL when creation
601 gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
602 std::string const & value,
606 gdcmValEntry* valEntry = 0;
608 gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem);
611 // The entry wasn't present and we simply create the required ValEntry:
612 currentEntry = NewDocEntryByNumber(group, elem);
615 dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: call to"
616 " NewDocEntryByNumber failed.");
619 valEntry = new gdcmValEntry(currentEntry);
620 if ( !AddEntry(valEntry))
622 dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: AddEntry"
623 " failed allthough this is a creation.");
628 valEntry = dynamic_cast< gdcmValEntry* >(currentEntry);
631 // We need to promote the gdcmDocEntry to a gdcmValEntry:
632 valEntry = new gdcmValEntry(currentEntry);
633 if (!RemoveEntry(currentEntry))
635 dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: removal"
636 " of previous DocEntry failed.");
639 if ( !AddEntry(valEntry))
641 dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: adding"
642 " promoted ValEntry failed.");
648 SetEntryByNumber(value, group, elem);
654 * \brief Modifies the value of a given Header Entry (Dicom Element)
655 * when it exists. Create it with the given value when unexistant.
656 * @param voidArea (binary) value to be set
657 * @param Group Group number of the Entry
658 * @param Elem Element number of the Entry
659 * \return pointer to the modified/created Header Entry (NULL when creation
662 gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber(
669 gdcmDocEntry* a = GetDocEntryByNumber( group, elem);
672 a = NewBinEntryByNumber(group, elem);
678 b = new gdcmBinEntry(a);
680 b->SetVoidArea(voidArea);
682 SetEntryByNumber(voidArea, lgth, group, elem);
683 //b->SetVoidArea(voidArea); //what if b == 0 !!
689 * \brief Set a new value if the invoked element exists
690 * Seems to be useless !!!
691 * @param Value new element value
692 * @param Group group number of the Entry
693 * @param Elem element number of the Entry
696 bool gdcmDocument::ReplaceIfExistByNumber(std::string const & value,
697 uint16_t group, uint16_t elem )
699 SetEntryByNumber(value, group, elem);
704 //-----------------------------------------------------------------------------
708 * \brief Checks if a given Dicom Element exists within the H table
709 * @param group Group number of the searched Dicom Element
710 * @param element Element number of the searched Dicom Element
711 * @return number of occurences
713 int gdcmDocument::CheckIfEntryExistByNumber(uint16_t group, uint16_t element )
715 const std::string &key = gdcmDictEntry::TranslateToKey(group, element );
716 return TagHT.count(key);
720 * \brief Searches within Header Entries (Dicom Elements) parsed with
721 * the public and private dictionaries
722 * for the element value of a given tag.
723 * \warning Don't use any longer : use GetPubEntryByName
724 * @param tagName name of the searched element.
725 * @return Corresponding element value when it exists,
726 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
728 std::string gdcmDocument::GetEntryByName(TagName const & tagName)
730 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
736 return GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement());
740 * \brief Searches within Header Entries (Dicom Elements) parsed with
741 * the public and private dictionaries
742 * for the element value representation of a given tag.
744 * Obtaining the VR (Value Representation) might be needed by caller
745 * to convert the string typed content to caller's native type
746 * (think of C++ vs Python). The VR is actually of a higher level
747 * of semantics than just the native C++ type.
748 * @param tagName name of the searched element.
749 * @return Corresponding element value representation when it exists,
750 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
752 std::string gdcmDocument::GetEntryVRByName(TagName const & tagName)
754 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
755 if( dictEntry == NULL)
760 gdcmDocEntry* elem = GetDocEntryByNumber(dictEntry->GetGroup(),
761 dictEntry->GetElement());
762 return elem->GetVR();
767 * \brief Searches within Header Entries (Dicom Elements) parsed with
768 * the public and private dictionaries
769 * for the element value representation of a given tag.
770 * @param group Group number of the searched tag.
771 * @param element Element number of the searched tag.
772 * @return Corresponding element value representation when it exists,
773 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
775 std::string gdcmDocument::GetEntryByNumber(uint16_t group, uint16_t element)
777 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
778 /// \todo use map methods, instead of multimap JPR
779 if ( !TagHT.count(key))
784 return ((gdcmValEntry *)TagHT.find(key)->second)->GetValue();
788 * \brief Searches within Header Entries (Dicom Elements) parsed with
789 * the public and private dictionaries
790 * for the element value representation of a given tag..
792 * Obtaining the VR (Value Representation) might be needed by caller
793 * to convert the string typed content to caller's native type
794 * (think of C++ vs Python). The VR is actually of a higher level
795 * of semantics than just the native C++ type.
796 * @param group Group number of the searched tag.
797 * @param element Element number of the searched tag.
798 * @return Corresponding element value representation when it exists,
799 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
801 std::string gdcmDocument::GetEntryVRByNumber(uint16_t group, uint16_t element)
803 gdcmDocEntry* elem = GetDocEntryByNumber(group, element);
808 return elem->GetVR();
812 * \brief Searches within Header Entries (Dicom Elements) parsed with
813 * the public and private dictionaries
814 * for the value length of a given tag..
815 * @param group Group number of the searched tag.
816 * @param element Element number of the searched tag.
817 * @return Corresponding element length; -2 if not found
819 int gdcmDocument::GetEntryLengthByNumber(uint16_t group, uint16_t element)
821 gdcmDocEntry* elem = GetDocEntryByNumber(group, element);
824 return -2; //magic number
826 return elem->GetLength();
829 * \brief Sets the value (string) of the Header Entry (Dicom Element)
830 * @param content string value of the Dicom Element
831 * @param tagName name of the searched Dicom Element.
832 * @return true when found
834 bool gdcmDocument::SetEntryByName(std::string content,std::string tagName)
836 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
842 return SetEntryByNumber(content,dictEntry->GetGroup(),
843 dictEntry->GetElement());
847 * \brief Accesses an existing gdcmDocEntry (i.e. a Dicom Element)
848 * through it's (group, element) and modifies it's content with
850 * @param content new value (string) to substitute with
851 * @param group group number of the Dicom Element to modify
852 * @param element element number of the Dicom Element to modify
854 bool gdcmDocument::SetEntryByNumber(std::string content,
858 gdcmValEntry* valEntry = GetValEntryByNumber(group, element);
861 dbg.Verbose(0, "gdcmDocument::SetEntryByNumber: no corresponding",
862 " ValEntry (try promotion first).");
865 // Non even content must be padded with a space (020H)...
866 if( content.length() % 2 )
868 content += '\0'; // ... therefore we padd with (000H) .!?!
870 valEntry->SetValue(content);
872 // Integers have a special treatement for their length:
873 gdcmVRKey vr = valEntry->GetVR();
874 if( vr == "US" || vr == "SS" )
876 valEntry->SetLength(2);
878 else if( vr == "UL" || vr == "SL" )
880 valEntry->SetLength(4);
884 valEntry->SetLength(content.length());
891 * \brief Accesses an existing gdcmDocEntry (i.e. a Dicom Element)
892 * through it's (group, element) and modifies it's content with
894 * @param content new value (void *) to substitute with
895 * @param lgth new value length
896 * @param group group number of the Dicom Element to modify
897 * @param element element number of the Dicom Element to modify
899 bool gdcmDocument::SetEntryByNumber(void *content,
904 (void)lgth; //not used
905 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
906 if ( !TagHT.count(key))
911 /* Hope Binary field length is *never* wrong
912 if(lgth%2) // Non even length are padded with a space (020H).
915 //content = content + '\0'; // fing a trick to enlarge a binary field?
919 a = (gdcmBinEntry *)TagHT[key];
920 a->SetVoidArea(content);
921 //a->SetLength(lgth); // ???
926 * \brief Accesses an existing gdcmDocEntry (i.e. a Dicom Element)
927 * in the PubDocEntrySet of this instance
928 * through it's (group, element) and modifies it's length with
930 * \warning Use with extreme caution.
931 * @param l new length to substitute with
932 * @param group group number of the Entry to modify
933 * @param element element number of the Entry to modify
934 * @return true on success, false otherwise.
936 bool gdcmDocument::SetEntryLengthByNumber(uint32_t l,
940 /// \todo use map methods, instead of multimap JPR
941 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
942 if ( !TagHT.count(key) )
948 l++; // length must be even
950 ( ((TagHT.equal_range(key)).first)->second )->SetLength(l);
956 * \brief Gets (from Header) the offset of a 'non string' element value
957 * (LoadElementValues has already be executed)
958 * @param Group group number of the Entry
959 * @param Elem element number of the Entry
960 * @return File Offset of the Element Value
962 size_t gdcmDocument::GetEntryOffsetByNumber(uint16_t group, uint16_t elem)
964 gdcmDocEntry* entry = GetDocEntryByNumber(group, elem);
967 dbg.Verbose(1, "gdcmDocument::GetDocEntryByNumber: no entry present.");
970 return entry->GetOffset();
974 * \brief Gets (from Header) a 'non string' element value
975 * (LoadElementValues has already be executed)
976 * @param Group group number of the Entry
977 * @param Elem element number of the Entry
978 * @return Pointer to the 'non string' area
980 void * gdcmDocument::GetEntryVoidAreaByNumber(uint16_t group, uint16_t elem)
982 gdcmDocEntry* entry = GetDocEntryByNumber(group, elem);
985 dbg.Verbose(1, "gdcmDocument::GetDocEntryByNumber: no entry");
988 return ((gdcmBinEntry *)entry)->GetVoidArea();
992 * \brief Loads (from disk) the element content
993 * when a string is not suitable
994 * @param Group group number of the Entry
995 * @param Elem element number of the Entry
997 void *gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem)
999 gdcmDocEntry *docElement = GetDocEntryByNumber(group, elem);
1004 size_t o =(size_t)docElement->GetOffset();
1005 fseek(Fp, o, SEEK_SET);
1006 size_t l = docElement->GetLength();
1007 char* a = new char[l];
1010 dbg.Verbose(0, "gdcmDocument::LoadEntryVoidArea cannot allocate a");
1013 size_t l2 = fread(a, 1, l , Fp);
1019 /// \todo Drop any already existing void area! JPR
1020 SetEntryVoidAreaByNumber(a, group, elem);
1025 * \brief Loads (from disk) the element content
1026 * when a string is not suitable
1027 * @param Element Entry whose voidArea is going to be loaded
1029 void *gdcmDocument::LoadEntryVoidArea(gdcmBinEntry *element)
1031 size_t o =(size_t)element->GetOffset();
1032 fseek(Fp, o, SEEK_SET);
1033 size_t l = element->GetLength();
1034 char* a = new char[l];
1037 dbg.Verbose(0, "gdcmDocument::LoadEntryVoidArea cannot allocate a");
1040 element->SetVoidArea((void *)a);
1041 /// \todo check the result
1042 size_t l2 = fread(a, 1, l , Fp);
1053 * \brief Sets a 'non string' value to a given Dicom Element
1054 * @param area area containing the 'non string' value
1055 * @param group Group number of the searched Dicom Element
1056 * @param element Element number of the searched Dicom Element
1059 bool gdcmDocument::SetEntryVoidAreaByNumber(void * area,
1063 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
1064 if ( !TagHT.count(key))
1068 // This was for multimap ?
1069 (( gdcmBinEntry *)( ((TagHT.equal_range(key)).first)->second ))->SetVoidArea(area);
1075 * \brief Update the entries with the shadow dictionary.
1076 * Only non even entries are analyzed
1078 void gdcmDocument::UpdateShaEntries()
1080 //gdcmDictEntry *entry;
1083 /// \todo TODO : still any use to explore recursively the whole structure?
1085 for(ListTag::iterator it=listEntries.begin();
1086 it!=listEntries.end();
1089 // Odd group => from public dictionary
1090 if((*it)->GetGroup()%2==0)
1093 // Peer group => search the corresponding dict entry
1095 entry=RefShaDict->GetDictEntryByNumber((*it)->GetGroup(),(*it)->GetElement());
1099 if((*it)->IsImplicitVR())
1104 (*it)->SetValue(GetDocEntryUnvalue(*it)); // to go on compiling
1106 // Set the new entry and the new value
1107 (*it)->SetDictEntry(entry);
1108 CheckDocEntryVR(*it,vr);
1110 (*it)->SetValue(GetDocEntryValue(*it)); // to go on compiling
1115 // Remove precedent value transformation
1116 (*it)->SetDictEntry(NewVirtualDictEntry((*it)->GetGroup(),(*it)->GetElement(),vr));
1123 * \brief Searches within the Header Entries for a Dicom Element of
1125 * @param tagName name of the searched Dicom Element.
1126 * @return Corresponding Dicom Element when it exists, and NULL
1129 gdcmDocEntry* gdcmDocument::GetDocEntryByName(std::string const & tagName)
1131 gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName);
1137 return GetDocEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement());
1141 * \brief retrieves a Dicom Element (the first one) using (group, element)
1142 * \warning (group, element) IS NOT an identifier inside the Dicom Header
1143 * if you think it's NOT UNIQUE, check the count number
1144 * and use iterators to retrieve ALL the Dicoms Elements within
1145 * a given couple (group, element)
1146 * @param group Group number of the searched Dicom Element
1147 * @param element Element number of the searched Dicom Element
1150 gdcmDocEntry* gdcmDocument::GetDocEntryByNumber(uint16_t group,
1153 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
1154 if ( !TagHT.count(key))
1158 return TagHT.find(key)->second;
1162 * \brief Same as \ref gdcmDocument::GetDocEntryByNumber except it only
1163 * returns a result when the corresponding entry is of type
1165 * @return When present, the corresponding ValEntry.
1167 gdcmValEntry* gdcmDocument::GetValEntryByNumber(uint16_t group,
1170 gdcmDocEntry* currentEntry = GetDocEntryByNumber(group, element);
1171 if ( !currentEntry )
1175 if ( gdcmValEntry* valEntry = dynamic_cast<gdcmValEntry*>(currentEntry) )
1179 dbg.Verbose(0, "gdcmDocument::GetValEntryByNumber: unfound ValEntry.");
1185 * \brief Loads the element while preserving the current
1186 * underlying file position indicator as opposed to
1187 * to LoadDocEntry that modifies it.
1188 * @param entry Header Entry whose value shall be loaded.
1191 void gdcmDocument::LoadDocEntrySafe(gdcmDocEntry * entry)
1193 long PositionOnEntry = ftell(Fp);
1194 LoadDocEntry(entry);
1195 fseek(Fp, PositionOnEntry, SEEK_SET);
1199 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1201 * @return The properly swaped 32 bits integer.
1203 uint32_t gdcmDocument::SwapLong(uint32_t a)
1210 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1211 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1215 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1219 a=( ((a<< 8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1222 std::cout << "swapCode= " << SwapCode << std::endl;
1223 dbg.Error(" gdcmDocument::SwapLong : unset swap code");
1230 * \brief Unswaps back the bytes of 4-byte long integer accordingly to
1232 * @return The properly unswaped 32 bits integer.
1234 uint32_t gdcmDocument::UnswapLong(uint32_t a)
1240 * \brief Swaps the bytes so they agree with the processor order
1241 * @return The properly swaped 16 bits integer.
1243 uint16_t gdcmDocument::SwapShort(uint16_t a)
1245 if ( SwapCode == 4321 || SwapCode == 2143 )
1247 a =((( a << 8 ) & 0x0ff00 ) | (( a >> 8 ) & 0x00ff ) );
1253 * \brief Unswaps the bytes so they agree with the processor order
1254 * @return The properly unswaped 16 bits integer.
1256 uint16_t gdcmDocument::UnswapShort(uint16_t a)
1258 return SwapShort(a);
1261 //-----------------------------------------------------------------------------
1265 * \brief Parses a DocEntrySet (Zero-level DocEntries or SQ Item DocEntries)
1266 * @return length of the parsed set.
1269 long gdcmDocument::ParseDES(gdcmDocEntrySet *set,
1274 gdcmDocEntry *newDocEntry = 0;
1275 gdcmValEntry *newValEntry = 0;
1276 unsigned long l = 0;
1278 int depth = set->GetDepthLevel();
1281 if ( !delim_mode && (ftell(Fp)-offset) >= l_max)
1285 newDocEntry = ReadNextDocEntry( );
1291 gdcmVRKey vr = newDocEntry->GetVR();
1295 if ( gdcmGlobal::GetVR()->IsVROfGdcmStringRepresentable(vr) )
1298 newValEntry = new gdcmValEntry(newDocEntry->GetDictEntry());
1299 newValEntry->Copy(newDocEntry);
1300 newValEntry->SetDepthLevel(depth);
1301 set->AddEntry(newValEntry);
1302 LoadDocEntry(newValEntry);
1303 if (newValEntry->isItemDelimitor())
1307 if ( !delim_mode && (ftell(Fp)-offset) >= l_max)
1314 if ( ! gdcmGlobal::GetVR()->IsVROfGdcmBinaryRepresentable(vr) )
1316 ////// Neither ValEntry NOR BinEntry: should mean UNKOWN VR
1317 dbg.Verbose(0, "gdcmDocument::ParseDES: neither Valentry, "
1318 "nor BinEntry. Probably unknown VR.");
1321 ////// BinEntry or UNKOWN VR:
1322 gdcmBinEntry *bn = new gdcmBinEntry(newDocEntry->GetDictEntry());
1323 bn->Copy(newDocEntry);
1328 if (newDocEntry->GetGroup() == 0x7fe0 &&
1329 newDocEntry->GetElement() == 0x0010 )
1331 if (newDocEntry->GetReadLength()==0xffffffff)
1333 // Broken US.3405.1.dcm
1334 Parse7FE0(); // to skip the pixels
1335 // (multipart JPEG/RLE are trouble makers)
1339 SkipToNextDocEntry(newDocEntry);
1340 l = newDocEntry->GetFullLength();
1345 // to be sure we are at the beginning
1346 SkipToNextDocEntry(newDocEntry);
1347 l = newDocEntry->GetFullLength();
1353 l = newDocEntry->GetReadLength();
1354 if ( l != 0 ) // don't mess the delim_mode for zero-length sequence
1356 if ( l == 0xffffffff )
1365 // no other way to create it ...
1366 gdcmSeqEntry *sq = new gdcmSeqEntry(newDocEntry->GetDictEntry(),
1367 set->GetDepthLevel());
1368 sq->Copy(newDocEntry);
1369 sq->SetDelimitorMode(delim_mode);
1370 sq->SetDepthLevel(depth);
1373 { // Don't try to parse zero-length sequences
1374 long lgt = ParseSQ( sq,
1375 newDocEntry->GetOffset(),
1377 (void)lgt; //not used...
1380 if ( !delim_mode && (ftell(Fp)-offset) >= l_max)
1388 return l; // Probably useless
1392 * \brief Parses a Sequence ( SeqEntry after SeqEntry)
1393 * @return parsed length for this level
1395 long gdcmDocument::ParseSQ(gdcmSeqEntry *set,
1396 long offset, long l_max, bool delim_mode)
1398 int SQItemNumber = 0;
1400 //int depth = set->GetDepthLevel();
1401 //(void)depth; //not used
1405 gdcmDocEntry *newDocEntry = ReadNextDocEntry();
1408 // FIXME Should warn user
1413 if ( newDocEntry->isSequenceDelimitor() )
1415 set->SetSequenceDelimitationItem( newDocEntry );
1419 if ( !delim_mode && (ftell(Fp)-offset) >= l_max)
1424 gdcmSQItem *itemSQ = new gdcmSQItem(set->GetDepthLevel());
1425 itemSQ->AddEntry(newDocEntry);
1426 unsigned int l = newDocEntry->GetReadLength();
1428 if ( l == 0xffffffff )
1437 int lgr = ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod);
1438 (void)lgr; //FIXME not used
1440 set->AddEntry(itemSQ, SQItemNumber);
1442 if ( !delim_mode && (ftell(Fp)-offset) >= l_max)
1448 int lgth = ftell(Fp) - offset;
1453 * \brief Loads the element content if its length doesn't exceed
1454 * the value specified with gdcmDocument::SetMaxSizeLoadEntry()
1455 * @param Entry Header Entry (Dicom Element) to be dealt with
1457 void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry)
1460 uint16_t group = entry->GetGroup();
1461 std::string vr = entry->GetVR();
1462 uint32_t length = entry->GetLength();
1464 fseek(Fp, (long)entry->GetOffset(), SEEK_SET);
1466 // A SeQuence "contains" a set of Elements.
1467 // (fffe e000) tells us an Element is beginning
1468 // (fffe e00d) tells us an Element just ended
1469 // (fffe e0dd) tells us the current SeQuence just ended
1470 if( group == 0xfffe )
1472 // NO more value field for SQ !
1476 // When the length is zero things are easy:
1479 ((gdcmValEntry *)entry)->SetValue("");
1483 // The elements whose length is bigger than the specified upper bound
1484 // are not loaded. Instead we leave a short notice of the offset of
1485 // the element content and it's length.
1487 std::ostringstream s;
1488 if (length > MaxSizeLoadEntry)
1490 if (gdcmBinEntry* binEntryPtr = dynamic_cast< gdcmBinEntry* >(entry) )
1492 s << "gdcm::NotLoaded (BinEntry)";
1493 s << " Address:" << (long)entry->GetOffset();
1494 s << " Length:" << entry->GetLength();
1495 s << " x(" << std::hex << entry->GetLength() << ")";
1496 binEntryPtr->SetValue(s.str());
1498 // to be sure we are at the end of the value ...
1499 fseek(Fp, (long)entry->GetOffset()+(long)entry->GetLength(), SEEK_SET);
1501 return; //FIXME FIXME FIXME FIXME ????
1503 // Be carefull : a BinEntry IS_A ValEntry ...
1504 if (gdcmValEntry* valEntryPtr = dynamic_cast< gdcmValEntry* >(entry) )
1506 s << "gdcm::NotLoaded. (ValEntry)";
1507 s << " Address:" << (long)entry->GetOffset();
1508 s << " Length:" << entry->GetLength();
1509 s << " x(" << std::hex << entry->GetLength() << ")";
1510 valEntryPtr->SetValue(s.str());
1512 // to be sure we are at the end of the value ...
1513 fseek(Fp,(long)entry->GetOffset()+(long)entry->GetLength(),SEEK_SET);
1518 // When we find a BinEntry not very much can be done :
1519 if (gdcmBinEntry* binEntryPtr = dynamic_cast< gdcmBinEntry* >(entry) )
1522 LoadEntryVoidArea(binEntryPtr);
1523 s << "gdcm::Loaded (BinEntry)";
1524 binEntryPtr->SetValue(s.str());
1528 /// \todo Any compacter code suggested (?)
1529 if ( IsDocEntryAnInteger(entry) )
1532 std::ostringstream s;
1534 // When short integer(s) are expected, read and convert the following
1535 // n *two characters properly i.e. consider them as short integers as
1536 // opposed to strings.
1537 // Elements with Value Multiplicity > 1
1538 // contain a set of integers (not a single one)
1539 if (vr == "US" || vr == "SS")
1542 NewInt = ReadInt16();
1546 for (int i=1; i < nbInt; i++)
1549 NewInt = ReadInt16();
1554 // See above comment on multiple integers (mutatis mutandis).
1555 else if (vr == "UL" || vr == "SL")
1558 NewInt = ReadInt32();
1562 for (int i=1; i < nbInt; i++)
1565 NewInt = ReadInt32();
1570 #ifdef GDCM_NO_ANSI_STRING_STREAM
1571 s << std::ends; // to avoid oddities on Solaris
1572 #endif //GDCM_NO_ANSI_STRING_STREAM
1574 ((gdcmValEntry *)entry)->SetValue(s.str());
1578 // We need an additional byte for storing \0 that is not on disk
1579 //std::string newValue(length,0);
1580 //item_read = fread(&(newValue[0]), (size_t)length, (size_t)1, Fp);
1581 //rah !! I can't believe it could work, normally this is a const char* !!!
1582 char *str = new char[length+1];
1583 item_read = fread(str, (size_t)length, (size_t)1, Fp);
1585 std::string newValue = str;
1587 if ( gdcmValEntry* valEntry = dynamic_cast<gdcmValEntry* >(entry) )
1589 if ( item_read != 1 )
1591 dbg.Verbose(1, "gdcmDocument::LoadDocEntry",
1592 "unread element value");
1593 valEntry->SetValue("gdcm::UnRead");
1599 // Because of correspondance with the VR dic
1600 valEntry->SetValue(newValue);
1604 valEntry->SetValue(newValue);
1609 dbg.Error(true, "gdcmDocument::LoadDocEntry"
1610 "Should have a ValEntry, here !");
1616 * \brief Find the value Length of the passed Header Entry
1617 * @param Entry Header Entry whose length of the value shall be loaded.
1619 void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry)
1621 uint16_t element = entry->GetElement();
1622 std::string vr = entry->GetVR();
1626 if ( Filetype == gdcmExplicitVR && !entry->IsImplicitVR() )
1628 if ( vr == "OB" || vr == "OW" || vr == "SQ" || vr == "UN" )
1630 // The following reserved two bytes (see PS 3.5-2001, section
1631 // 7.1.2 Data element structure with explicit vr p27) must be
1632 // skipped before proceeding on reading the length on 4 bytes.
1633 fseek(Fp, 2L, SEEK_CUR);
1634 uint32_t length32 = ReadInt32();
1636 if ( vr == "OB" && length32 == 0xffffffff )
1638 uint32_t lengthOB = FindDocEntryLengthOB();
1641 // Computing the length failed (this happens with broken
1642 // files like gdcm-JPEG-LossLess3a.dcm). We still have a
1643 // chance to get the pixels by deciding the element goes
1644 // until the end of the file. Hence we artificially fix the
1645 // the length and proceed.
1646 long currentPosition = ftell(Fp);
1647 fseek(Fp,0L,SEEK_END);
1648 long lengthUntilEOF = ftell(Fp) - currentPosition;
1649 fseek(Fp, currentPosition, SEEK_SET);
1650 entry->SetLength(lengthUntilEOF);
1654 entry->SetLength(lengthOB);
1657 FixDocEntryFoundLength(entry, length32);
1661 // Length is encoded on 2 bytes.
1662 length16 = ReadInt16();
1664 // We can tell the current file is encoded in big endian (like
1665 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1666 // and it's value is the one of the encoding of a big endian file.
1667 // In order to deal with such big endian encoded files, we have
1668 // (at least) two strategies:
1669 // * when we load the "Transfer Syntax" tag with value of big endian
1670 // encoding, we raise the proper flags. Then we wait for the end
1671 // of the META group (0x0002) among which is "Transfer Syntax",
1672 // before switching the swap code to big endian. We have to postpone
1673 // the switching of the swap code since the META group is fully encoded
1674 // in little endian, and big endian coding only starts at the next
1675 // group. The corresponding code can be hard to analyse and adds
1676 // many additional unnecessary tests for regular tags.
1677 // * the second strategy consists in waiting for trouble, that shall
1678 // appear when we find the first group with big endian encoding. This
1679 // is easy to detect since the length of a "Group Length" tag (the
1680 // ones with zero as element number) has to be of 4 (0x0004). When we
1681 // encounter 1024 (0x0400) chances are the encoding changed and we
1682 // found a group with big endian encoding.
1683 // We shall use this second strategy. In order to make sure that we
1684 // can interpret the presence of an apparently big endian encoded
1685 // length of a "Group Length" without committing a big mistake, we
1686 // add an additional check: we look in the already parsed elements
1687 // for the presence of a "Transfer Syntax" whose value has to be "big
1688 // endian encoding". When this is the case, chances are we have got our
1689 // hands on a big endian encoded file: we switch the swap code to
1690 // big endian and proceed...
1691 if ( (element == 0x0000) && (length16 == 0x0400) )
1693 if ( ! IsExplicitVRBigEndianTransferSyntax() )
1695 dbg.Verbose(0, "gdcmDocument::FindLength", "not explicit VR");
1700 SwitchSwapToBigEndian();
1701 // Restore the unproperly loaded values i.e. the group, the element
1702 // and the dictionary entry depending on them.
1703 uint16_t correctGroup = SwapShort( entry->GetGroup() );
1704 uint16_t correctElem = SwapShort( entry->GetElement() );
1705 gdcmDictEntry* newTag = GetDictEntryByNumber( correctGroup,
1709 // This correct tag is not in the dictionary. Create a new one.
1710 newTag = NewVirtualDictEntry(correctGroup, correctElem);
1712 // FIXME this can create a memory leaks on the old entry that be
1713 // left unreferenced.
1714 entry->SetDictEntry( newTag );
1717 // Heuristic: well, some files are really ill-formed.
1718 if ( length16 == 0xffff)
1721 // Length16= 0xffff means that we deal with
1722 // 'Unknown Length' Sequence
1724 FixDocEntryFoundLength(entry, (uint32_t)length16);
1729 // Either implicit VR or a non DICOM conformal (see note below) explicit
1730 // VR that ommited the VR of (at least) this element. Farts happen.
1731 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1732 // on Data elements "Implicit and Explicit VR Data Elements shall
1733 // not coexist in a Data Set and Data Sets nested within it".]
1734 // Length is on 4 bytes.
1736 FixDocEntryFoundLength(entry, ReadInt32());
1742 * \brief Find the Value Representation of the current Dicom Element.
1745 void gdcmDocument::FindDocEntryVR( gdcmDocEntry *entry )
1747 if ( Filetype != gdcmExplicitVR )
1754 long positionOnEntry = ftell(Fp);
1755 // Warning: we believe this is explicit VR (Value Representation) because
1756 // we used a heuristic that found "UL" in the first tag. Alas this
1757 // doesn't guarantee that all the tags will be in explicit VR. In some
1758 // cases (see e-film filtered files) one finds implicit VR tags mixed
1759 // within an explicit VR file. Hence we make sure the present tag
1760 // is in explicit VR and try to fix things if it happens not to be
1763 fread (vr, (size_t)2,(size_t)1, Fp);
1766 if( !CheckDocEntryVR(entry, vr) )
1768 fseek(Fp, positionOnEntry, SEEK_SET);
1769 // When this element is known in the dictionary we shall use, e.g. for
1770 // the semantics (see the usage of IsAnInteger), the VR proposed by the
1771 // dictionary entry. Still we have to flag the element as implicit since
1772 // we know now our assumption on expliciteness is not furfilled.
1774 if ( entry->IsVRUnknown() )
1776 entry->SetVR("Implicit");
1778 entry->SetImplicitVR();
1783 * \brief Check the correspondance between the VR of the header entry
1784 * and the taken VR. If they are different, the header entry is
1785 * updated with the new VR.
1786 * @param Entry Header Entry to check
1787 * @param vr Dicom Value Representation
1788 * @return false if the VR is incorrect of if the VR isn't referenced
1789 * otherwise, it returns true
1791 bool gdcmDocument::CheckDocEntryVR(gdcmDocEntry *entry, gdcmVRKey vr)
1793 char msg[100]; // for sprintf
1794 bool realExplicit = true;
1796 // Assume we are reading a falsely explicit VR file i.e. we reached
1797 // a tag where we expect reading a VR but are in fact we read the
1798 // first to bytes of the length. Then we will interogate (through find)
1799 // the dicom_vr dictionary with oddities like "\004\0" which crashes
1800 // both GCC and VC++ implementations of the STL map. Hence when the
1801 // expected VR read happens to be non-ascii characters we consider
1802 // we hit falsely explicit VR tag.
1804 if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
1806 realExplicit = false;
1809 // CLEANME searching the dicom_vr at each occurence is expensive.
1810 // PostPone this test in an optional integrity check at the end
1811 // of parsing or only in debug mode.
1812 if ( realExplicit && !gdcmGlobal::GetVR()->Count(vr) )
1814 realExplicit = false;
1817 if ( !realExplicit )
1819 // We thought this was explicit VR, but we end up with an
1820 // implicit VR tag. Let's backtrack.
1821 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
1822 entry->GetGroup(), entry->GetElement());
1823 dbg.Verbose(1, "gdcmDocument::FindVR: ",msg);
1825 if( entry->GetGroup() % 2 && entry->GetElement() == 0x0000)
1827 // Group length is UL !
1828 gdcmDictEntry* newEntry = NewVirtualDictEntry(
1829 entry->GetGroup(), entry->GetElement(),
1830 "UL", "FIXME", "Group Length");
1831 entry->SetDictEntry( newEntry );
1836 if ( entry->IsVRUnknown() )
1838 // When not a dictionary entry, we can safely overwrite the VR.
1839 if( entry->GetElement() == 0x0000 )
1841 // Group length is UL !
1849 else if ( entry->GetVR() != vr )
1851 // The VR present in the file and the dictionary disagree. We assume
1852 // the file writer knew best and use the VR of the file. Since it would
1853 // be unwise to overwrite the VR of a dictionary (since it would
1854 // compromise it's next user), we need to clone the actual DictEntry
1855 // and change the VR for the read one.
1856 gdcmDictEntry* newEntry = NewVirtualDictEntry(
1857 entry->GetGroup(), entry->GetElement(),
1858 vr, "FIXME", entry->GetName());
1859 entry->SetDictEntry(newEntry);
1866 * \brief Get the transformed value of the header entry. The VR value
1867 * is used to define the transformation to operate on the value
1868 * \warning NOT end user intended method !
1870 * @return Transformed entry value
1872 std::string gdcmDocument::GetDocEntryValue(gdcmDocEntry *entry)
1874 if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1876 std::string val = ((gdcmValEntry *)entry)->GetValue();
1877 std::string vr = entry->GetVR();
1878 uint32_t length = entry->GetLength();
1879 std::ostringstream s;
1882 // When short integer(s) are expected, read and convert the following
1883 // n * 2 bytes properly i.e. as a multivaluated strings
1884 // (each single value is separated fromthe next one by '\'
1885 // as usual for standard multivaluated filels
1886 // Elements with Value Multiplicity > 1
1887 // contain a set of short integers (not a single one)
1889 if( vr == "US" || vr == "SS" )
1894 for (int i=0; i < nbInt; i++)
1900 newInt16 = ( val[2*i+0] & 0xFF ) + ( ( val[2*i+1] & 0xFF ) << 8);
1901 newInt16 = SwapShort( newInt16 );
1906 // When integer(s) are expected, read and convert the following
1907 // n * 4 bytes properly i.e. as a multivaluated strings
1908 // (each single value is separated fromthe next one by '\'
1909 // as usual for standard multivaluated filels
1910 // Elements with Value Multiplicity > 1
1911 // contain a set of integers (not a single one)
1912 else if( vr == "UL" || vr == "SL" )
1917 for (int i=0; i < nbInt; i++)
1923 newInt32 = ( val[4*i+0] & 0xFF )
1924 + (( val[4*i+1] & 0xFF ) << 8 )
1925 + (( val[4*i+2] & 0xFF ) << 16 )
1926 + (( val[4*i+3] & 0xFF ) << 24 );
1927 newInt32 = SwapLong( newInt32 );
1931 #ifdef GDCM_NO_ANSI_STRING_STREAM
1932 s << std::ends; // to avoid oddities on Solaris
1933 #endif //GDCM_NO_ANSI_STRING_STREAM
1937 return ((gdcmValEntry *)entry)->GetValue();
1941 * \brief Get the reverse transformed value of the header entry. The VR
1942 * value is used to define the reverse transformation to operate on
1944 * \warning NOT end user intended method !
1946 * @return Reverse transformed entry value
1948 std::string gdcmDocument::GetDocEntryUnvalue(gdcmDocEntry* entry)
1950 if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1952 std::string vr = entry->GetVR();
1953 std::vector<std::string> tokens;
1954 std::ostringstream s;
1956 if ( vr == "US" || vr == "SS" )
1960 tokens.erase( tokens.begin(), tokens.end()); // clean any previous value
1961 Tokenize (((gdcmValEntry *)entry)->GetValue(), tokens, "\\");
1962 for (unsigned int i=0; i<tokens.size(); i++)
1964 newInt16 = atoi(tokens[i].c_str());
1965 s << ( newInt16 & 0xFF )
1966 << (( newInt16 >> 8 ) & 0xFF );
1970 if ( vr == "UL" || vr == "SL")
1974 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1975 Tokenize (((gdcmValEntry *)entry)->GetValue(), tokens, "\\");
1976 for (unsigned int i=0; i<tokens.size();i++)
1978 newInt32 = atoi(tokens[i].c_str());
1979 s << (char)( newInt32 & 0xFF )
1980 << (char)(( newInt32 >> 8 ) & 0xFF )
1981 << (char)(( newInt32 >> 16 ) & 0xFF )
1982 << (char)(( newInt32 >> 24 ) & 0xFF );
1987 #ifdef GDCM_NO_ANSI_STRING_STREAM
1988 s << std::ends; // to avoid oddities on Solaris
1989 #endif //GDCM_NO_ANSI_STRING_STREAM
1993 return ((gdcmValEntry *)entry)->GetValue();
1997 * \brief Skip a given Header Entry
1998 * \warning NOT end user intended method !
2001 void gdcmDocument::SkipDocEntry(gdcmDocEntry *entry)
2003 SkipBytes(entry->GetLength());
2007 * \brief Skips to the begining of the next Header Entry
2008 * \warning NOT end user intended method !
2011 void gdcmDocument::SkipToNextDocEntry(gdcmDocEntry *entry)
2013 fseek(Fp, (long)(entry->GetOffset()), SEEK_SET);
2014 fseek(Fp, (long)(entry->GetReadLength()), SEEK_CUR);
2018 * \brief When the length of an element value is obviously wrong (because
2019 * the parser went Jabberwocky) one can hope improving things by
2020 * applying some heuristics.
2022 void gdcmDocument::FixDocEntryFoundLength(gdcmDocEntry *entry,
2023 uint32_t foundLength)
2025 entry->SetReadLength( foundLength ); // will be updated only if a bug is found
2026 if ( foundLength == 0xffffffff)
2031 uint16_t gr = entry->GetGroup();
2032 uint16_t el = entry->GetElement();
2034 if ( foundLength % 2)
2036 std::ostringstream s;
2037 s << "Warning : Tag with uneven length "
2039 << " in x(" << std::hex << gr << "," << el <<")" << std::dec;
2040 dbg.Verbose(0, s.str().c_str());
2043 //////// Fix for some naughty General Electric images.
2044 // Allthough not recent many such GE corrupted images are still present
2045 // on Creatis hard disks. Hence this fix shall remain when such images
2046 // are no longer in user (we are talking a few years, here)...
2047 // Note: XMedCom probably uses such a trick since it is able to read
2048 // those pesky GE images ...
2049 if ( foundLength == 13)
2051 // Only happens for this length !
2052 if ( entry->GetGroup() != 0x0008
2053 || ( entry->GetElement() != 0x0070
2054 && entry->GetElement() != 0x0080 ) )
2057 entry->SetReadLength(10); /// \todo a bug is to be fixed !?
2061 //////// Fix for some brain-dead 'Leonardo' Siemens images.
2062 // Occurence of such images is quite low (unless one leaves close to a
2063 // 'Leonardo' source. Hence, one might consider commenting out the
2064 // following fix on efficiency reasons.
2065 else if ( entry->GetGroup() == 0x0009
2066 && ( entry->GetElement() == 0x1113
2067 || entry->GetElement() == 0x1114 ) )
2070 entry->SetReadLength(4); /// \todo a bug is to be fixed !?
2073 else if ( entry->GetVR() == "SQ" )
2075 foundLength = 0; // ReadLength is unchanged
2078 //////// We encountered a 'delimiter' element i.e. a tag of the form
2079 // "fffe|xxxx" which is just a marker. Delimiters length should not be
2080 // taken into account.
2081 else if( entry->GetGroup() == 0xfffe )
2083 // According to the norm, fffe|0000 shouldn't exist. BUT the Philips
2084 // image gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm happens to
2085 // causes extra troubles...
2086 if( entry->GetElement() != 0x0000 )
2092 entry->SetUsableLength(foundLength);
2096 * \brief Apply some heuristics to predict whether the considered
2097 * element value contains/represents an integer or not.
2098 * @param Entry The element value on which to apply the predicate.
2099 * @return The result of the heuristical predicate.
2101 bool gdcmDocument::IsDocEntryAnInteger(gdcmDocEntry *entry)
2103 uint16_t element = entry->GetElement();
2104 uint16_t group = entry->GetGroup();
2105 std::string vr = entry->GetVR();
2106 uint32_t length = entry->GetLength();
2108 // When we have some semantics on the element we just read, and if we
2109 // a priori know we are dealing with an integer, then we shall be
2110 // able to swap it's element value properly.
2111 if ( element == 0 ) // This is the group length of the group
2119 // Allthough this should never happen, still some images have a
2120 // corrupted group length [e.g. have a glance at offset x(8336) of
2121 // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm].
2122 // Since for dicom compliant and well behaved headers, the present
2123 // test is useless (and might even look a bit paranoid), when we
2124 // encounter such an ill-formed image, we simply display a warning
2125 // message and proceed on parsing (while crossing fingers).
2126 std::ostringstream s;
2127 int filePosition = ftell(Fp);
2128 s << "Erroneous Group Length element length on : (" \
2129 << std::hex << group << " , " << element
2130 << ") -before- position x(" << filePosition << ")"
2131 << "lgt : " << length;
2132 dbg.Verbose(0, "gdcmDocument::IsDocEntryAnInteger", s.str().c_str() );
2136 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
2145 * \brief Find the Length till the next sequence delimiter
2146 * \warning NOT end user intended method !
2150 uint32_t gdcmDocument::FindDocEntryLengthOB()
2152 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
2153 long positionOnEntry = ftell(Fp);
2154 bool foundSequenceDelimiter = false;
2155 uint32_t totalLength = 0;
2157 while ( !foundSequenceDelimiter )
2159 uint16_t g = ReadInt16();
2160 uint16_t n = ReadInt16();
2166 // We have to decount the group and element we just read
2169 if ( g != 0xfffe || ( n != 0xe0dd && n != 0xe000 ) )
2171 dbg.Verbose(1, "gdcmDocument::FindLengthOB: neither an Item tag "
2172 "nor a Sequence delimiter tag.");
2173 fseek(Fp, positionOnEntry, SEEK_SET);
2180 foundSequenceDelimiter = true;
2183 uint32_t itemLength = ReadInt32();
2184 // We add 4 bytes since we just read the ItemLength with ReadInt32
2185 totalLength += itemLength + 4;
2186 SkipBytes(itemLength);
2188 if ( foundSequenceDelimiter )
2193 fseek(Fp, positionOnEntry, SEEK_SET);
2198 * \brief Reads a supposed to be 16 Bits integer
2199 * (swaps it depending on processor endianity)
2200 * @return read value
2202 uint16_t gdcmDocument::ReadInt16()
2205 size_t item_read = fread (&g, (size_t)2,(size_t)1, Fp);
2206 if ( item_read != 1 )
2210 dbg.Verbose(0, "gdcmDocument::ReadInt16", " File Error");
2221 * \brief Reads a supposed to be 32 Bits integer
2222 * (swaps it depending on processor endianity)
2223 * @return read value
2225 uint32_t gdcmDocument::ReadInt32()
2228 size_t item_read = fread (&g, (size_t)4,(size_t)1, Fp);
2229 if ( item_read != 1 )
2233 dbg.Verbose(0, "gdcmDocument::ReadInt32", " File Error");
2244 * \brief skips bytes inside the source file
2245 * \warning NOT end user intended method !
2248 void gdcmDocument::SkipBytes(uint32_t nBytes)
2250 //FIXME don't dump the returned value
2251 (void)fseek(Fp, (long)nBytes, SEEK_CUR);
2255 * \brief Loads all the needed Dictionaries
2256 * \warning NOT end user intended method !
2258 void gdcmDocument::Initialise()
2260 RefPubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
2265 * \brief Discover what the swap code is (among little endian, big endian,
2266 * bad little endian, bad big endian).
2268 * @return false when we are absolutely sure
2269 * it's neither ACR-NEMA nor DICOM
2270 * true when we hope ours assuptions are OK
2272 bool gdcmDocument::CheckSwap()
2274 // The only guaranted way of finding the swap code is to find a
2275 // group tag since we know it's length has to be of four bytes i.e.
2276 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2277 // occurs when we can't find such group...
2279 uint32_t x = 4; // x : for ntohs
2280 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2284 char deb[HEADER_LENGTH_TO_READ];
2286 // First, compare HostByteOrder and NetworkByteOrder in order to
2287 // determine if we shall need to swap bytes (i.e. the Endian type).
2288 if ( x == ntohs(x) )
2297 // The easiest case is the one of a DICOM header, since it possesses a
2298 // file preamble where it suffice to look for the string "DICM".
2299 int lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, Fp);
2300 (void)lgrLue; //FIXME not used
2302 char *entCur = deb + 128;
2303 if( memcmp(entCur, "DICM", (size_t)4) == 0 )
2305 dbg.Verbose(1, "gdcmDocument::CheckSwap:", "looks like DICOM Version3");
2307 // Next, determine the value representation (VR). Let's skip to the
2308 // first element (0002, 0000) and check there if we find "UL"
2309 // - or "OB" if the 1st one is (0002,0001) -,
2310 // in which case we (almost) know it is explicit VR.
2311 // WARNING: if it happens to be implicit VR then what we will read
2312 // is the length of the group. If this ascii representation of this
2313 // length happens to be "UL" then we shall believe it is explicit VR.
2314 // FIXME: in order to fix the above warning, we could read the next
2315 // element value (or a couple of elements values) in order to make
2316 // sure we are not commiting a big mistake.
2317 // We need to skip :
2318 // * the 128 bytes of File Preamble (often padded with zeroes),
2319 // * the 4 bytes of "DICM" string,
2320 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2321 // i.e. a total of 136 bytes.
2325 // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2326 // but elem 0002,0010 (Transfert Syntax) tells us the file is
2327 // *Implicit* VR. -and it is !-
2329 if( memcmp(entCur, "UL", (size_t)2) == 0 ||
2330 memcmp(entCur, "OB", (size_t)2) == 0 ||
2331 memcmp(entCur, "UI", (size_t)2) == 0 ||
2332 memcmp(entCur, "CS", (size_t)2) == 0 ) // CS, to remove later
2333 // when Write DCM *adds*
2335 // Use gdcmDocument::dicom_vr to test all the possibilities
2336 // instead of just checking for UL, OB and UI !? group 0000
2338 Filetype = gdcmExplicitVR;
2339 dbg.Verbose(1, "gdcmDocument::CheckSwap:",
2340 "explicit Value Representation");
2344 Filetype = gdcmImplicitVR;
2345 dbg.Verbose(1, "gdcmDocument::CheckSwap:",
2346 "not an explicit Value Representation");
2352 dbg.Verbose(1, "gdcmDocument::CheckSwap:",
2353 "HostByteOrder != NetworkByteOrder");
2358 dbg.Verbose(1, "gdcmDocument::CheckSwap:",
2359 "HostByteOrder = NetworkByteOrder");
2362 // Position the file position indicator at first tag (i.e.
2363 // after the file preamble and the "DICM" string).
2365 fseek (Fp, 132L, SEEK_SET);
2369 // Alas, this is not a DicomV3 file and whatever happens there is no file
2370 // preamble. We can reset the file position indicator to where the data
2371 // is (i.e. the beginning of the file).
2372 dbg.Verbose(1, "gdcmDocument::CheckSwap:", "not a DICOM Version3 file");
2375 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2376 // By clean we mean that the length of the first tag is written down.
2377 // If this is the case and since the length of the first group HAS to be
2378 // four (bytes), then determining the proper swap code is straightforward.
2381 // We assume the array of char we are considering contains the binary
2382 // representation of a 32 bits integer. Hence the following dirty
2384 s32 = *((uint32_t *)(entCur));
2405 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2406 // It is time for despaired wild guesses.
2407 // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2408 // i.e. the 'group length' element is not present :
2410 // check the supposed to be 'group number'
2411 // 0x0002 or 0x0004 or 0x0008
2412 // to determine ' SwapCode' value .
2413 // Only 0 or 4321 will be possible
2414 // (no oportunity to check for the formerly well known
2415 // ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian'
2416 // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -4, 8-)
2417 // the file IS NOT ACR-NEMA nor DICOM V3
2418 // Find a trick to tell it the caller...
2420 s16 = *((uint16_t *)(deb));
2437 dbg.Verbose(0, "gdcmDocument::CheckSwap:",
2438 "ACR/NEMA unfound swap info (Really hopeless !)");
2439 Filetype = gdcmUnknown;
2442 // Then the only info we have is the net2host one.
2452 * \brief Restore the unproperly loaded values i.e. the group, the element
2453 * and the dictionary entry depending on them.
2455 void gdcmDocument::SwitchSwapToBigEndian()
2457 dbg.Verbose(1, "gdcmDocument::SwitchSwapToBigEndian",
2458 "Switching to BigEndian mode.");
2459 if ( SwapCode == 0 )
2463 else if ( SwapCode == 4321 )
2467 else if ( SwapCode == 3412 )
2471 else if ( SwapCode == 2143 )
2478 * \brief during parsing, Header Elements too long are not loaded in memory
2481 void gdcmDocument::SetMaxSizeLoadEntry(long newSize)
2487 if ((uint32_t)newSize >= (uint32_t)0xffffffff )
2489 MaxSizeLoadEntry = 0xffffffff;
2492 MaxSizeLoadEntry = newSize;
2497 * \brief Header Elements too long will not be printed
2498 * \todo See comments of \ref gdcmDocument::MAX_SIZE_PRINT_ELEMENT_VALUE
2501 void gdcmDocument::SetMaxSizePrintEntry(long newSize)
2503 //DOH !! This is exactly SetMaxSizeLoadEntry FIXME FIXME
2508 if ((uint32_t)newSize >= (uint32_t)0xffffffff )
2510 MaxSizePrintEntry = 0xffffffff;
2513 MaxSizePrintEntry = newSize;
2519 * \brief Read the next tag but WITHOUT loading it's value
2520 * (read the 'Group Number', the 'Element Number',
2521 * gets the Dict Entry
2522 * gets the VR, gets the length, gets the offset value)
2523 * @return On succes the newly created DocEntry, NULL on failure.
2525 gdcmDocEntry *gdcmDocument::ReadNextDocEntry()
2527 uint16_t g = ReadInt16();
2528 uint16_t n = ReadInt16();
2532 // We reached the EOF (or an error occured) therefore
2533 // header parsing has to be considered as finished.
2536 gdcmDocEntry *newEntry = NewDocEntryByNumber(g, n);
2538 FindDocEntryVR(newEntry);
2539 FindDocEntryLength(newEntry);
2547 newEntry->SetOffset(ftell(Fp));
2554 * \brief Generate a free gdcmTagKey i.e. a gdcmTagKey that is not present
2555 * in the TagHt dictionary.
2556 * @param group The generated tag must belong to this group.
2557 * @return The element of tag with given group which is fee.
2559 uint32_t gdcmDocument::GenerateFreeTagKeyInGroup(uint16_t group)
2561 for (uint32_t elem = 0; elem < UINT32_MAX; elem++)
2563 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, elem);
2564 if (TagHT.count(key) == 0)
2573 * \brief Assuming the internal file pointer \ref gdcmDocument::fp
2574 * is placed at the beginning of a tag check whether this
2575 * tag is (TestGroup, TestElement).
2576 * \warning On success the internal file pointer \ref gdcmDocument::fp
2577 * is modified to point after the tag.
2578 * On failure (i.e. when the tag wasn't the expected tag
2579 * (TestGroup, TestElement) the internal file pointer
2580 * \ref gdcmDocument::fp is restored to it's original position.
2581 * @param TestGroup The expected group of the tag.
2582 * @param TestElement The expected Element of the tag.
2583 * @return True on success, false otherwise.
2585 bool gdcmDocument::ReadTag(uint16_t testGroup, uint16_t testElement)
2587 long positionOnEntry = ftell(Fp);
2588 long currentPosition = ftell(Fp); // On debugging purposes
2590 //// Read the Item Tag group and element, and make
2591 // sure they are what we expected:
2592 uint16_t itemTagGroup = ReadInt16();
2593 uint16_t itemTagElement = ReadInt16();
2594 if ( itemTagGroup != testGroup || itemTagElement != testElement )
2596 std::ostringstream s;
2597 s << " We should have found tag (";
2598 s << std::hex << testGroup << "," << testElement << ")" << std::endl;
2599 s << " but instead we encountered tag (";
2600 s << std::hex << itemTagGroup << "," << itemTagElement << ")"
2602 s << " at address: " << (unsigned)currentPosition << std::endl;
2603 dbg.Verbose(0, "gdcmDocument::ReadItemTagLength: wrong Item Tag found:");
2604 dbg.Verbose(0, s.str().c_str());
2605 fseek(Fp, positionOnEntry, SEEK_SET);
2613 * \brief Assuming the internal file pointer \ref gdcmDocument::fp
2614 * is placed at the beginning of a tag (TestGroup, TestElement),
2615 * read the length associated to the Tag.
2616 * \warning On success the internal file pointer \ref gdcmDocument::fp
2617 * is modified to point after the tag and it's length.
2618 * On failure (i.e. when the tag wasn't the expected tag
2619 * (TestGroup, TestElement) the internal file pointer
2620 * \ref gdcmDocument::fp is restored to it's original position.
2621 * @param TestGroup The expected group of the tag.
2622 * @param TestElement The expected Element of the tag.
2623 * @return On success returns the length associated to the tag. On failure
2626 uint32_t gdcmDocument::ReadTagLength(uint16_t testGroup, uint16_t testElement)
2628 long PositionOnEntry = ftell(Fp);
2629 (void)PositionOnEntry;
2631 if ( !ReadTag(testGroup, testElement) )
2636 //// Then read the associated Item Length
2637 long currentPosition = ftell(Fp);
2638 uint32_t itemLength = ReadInt32();
2640 std::ostringstream s;
2641 s << "Basic Item Length is: "
2642 << itemLength << std::endl;
2643 s << " at address: " << (unsigned)currentPosition << std::endl;
2644 dbg.Verbose(0, "gdcmDocument::ReadItemTagLength: ", s.str().c_str());
2650 * \brief Parse pixel data from disk for multi-fragment Jpeg/Rle files
2651 * No other way so 'skip' the Data
2654 void gdcmDocument::Parse7FE0 ()
2656 gdcmDocEntry* element = GetDocEntryByNumber(0x0002, 0x0010);
2659 // Should warn user FIXME
2663 if ( IsImplicitVRLittleEndianTransferSyntax()
2664 || IsExplicitVRLittleEndianTransferSyntax()
2665 || IsExplicitVRBigEndianTransferSyntax() /// \todo 1.2.2 ??? A verifier !
2666 || IsDeflatedExplicitVRLittleEndianTransferSyntax() )
2671 // ---------------- for Parsing : Position on begining of Jpeg/RLE Pixels
2673 //// Read the Basic Offset Table Item Tag length...
2674 uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
2676 //// ... and then read length[s] itself[themselves]. We don't use
2677 // the values read (BTW what is the purpous of those lengths ?)
2678 if ( itemLength != 0 )
2680 // BTW, what is the purpous of those length anyhow !?
2681 char* basicOffsetTableItemValue = new char[itemLength + 1];
2682 fread(basicOffsetTableItemValue, itemLength, 1, Fp);
2684 for (unsigned int i=0; i < itemLength; i += 4 )
2686 uint32_t individualLength = str2num(&basicOffsetTableItemValue[i],uint32_t);
2687 std::ostringstream s;
2688 s << " Read one length: ";
2689 s << std::hex << individualLength << std::endl;
2690 dbg.Verbose(0, "gdcmDocument::Parse7FE0: ", s.str().c_str());
2692 delete[] basicOffsetTableItemValue;
2695 if ( ! IsRLELossLessTransferSyntax() )
2699 //// We then skip (not reading them) all the fragments of images:
2700 while ( (itemLength = ReadTagLength(0xfffe, 0xe000)) )
2702 SkipBytes(itemLength);
2709 long rleSegmentLength[15], fragmentLength;
2711 // While we find some items:
2712 while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) )
2714 // Parse fragments of the current Fragment (Frame)
2715 //------------------ scanning (not reading) fragment pixels
2716 uint32_t nbRleSegments = ReadInt32();
2718 //// Reading RLE Segments Offset Table
2719 uint32_t rleSegmentOffsetTable[15];
2720 for(int k=1; k<=15; k++)
2722 ftellRes = ftell(Fp);
2723 rleSegmentOffsetTable[k] = ReadInt32();
2726 // skipping (not reading) RLE Segments
2727 if ( nbRleSegments > 1)
2729 for(unsigned int k = 1; k <= nbRleSegments-1; k++)
2731 rleSegmentLength[k] = rleSegmentOffsetTable[k+1]
2732 - rleSegmentOffsetTable[k];
2733 ftellRes = ftell(Fp);
2734 SkipBytes(rleSegmentLength[k]);
2738 rleSegmentLength[nbRleSegments] = fragmentLength
2739 - rleSegmentOffsetTable[nbRleSegments];
2740 ftellRes = ftell(Fp);
2741 SkipBytes(rleSegmentLength[nbRleSegments]);
2744 // Make sure that at the end of the item we encounter a 'Sequence
2746 if ( ! ReadTag(0xfffe, 0xe0dd) )
2748 dbg.Verbose(0, "gdcmDocument::Parse7FE0: no sequence delimiter item");
2749 dbg.Verbose(0, " at end of RLE item sequence");
2757 * \brief Compares two documents, according to \ref gdcmDicomDir rules
2758 * \warning Does NOT work with ACR-NEMA files
2759 * \todo Find a trick to solve the pb (use RET fields ?)
2761 * @return true if 'smaller'
2763 bool gdcmDocument::operator<(gdcmDocument &document)
2766 std::string s1 = GetEntryByNumber(0x0010,0x0010);
2767 std::string s2 = document.GetEntryByNumber(0x0010,0x0010);
2779 s1 = GetEntryByNumber(0x0010,0x0020);
2780 s2 = document.GetEntryByNumber(0x0010,0x0020);
2791 // Study Instance UID
2792 s1 = GetEntryByNumber(0x0020,0x000d);
2793 s2 = document.GetEntryByNumber(0x0020,0x000d);
2804 // Serie Instance UID
2805 s1 = GetEntryByNumber(0x0020,0x000e);
2806 s2 = document.GetEntryByNumber(0x0020,0x000e);
2822 //-----------------------------------------------------------------------------