X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmDocument.cxx;h=71312ce5ce2b6bab3c66efceca65e65559e5e349;hb=eebb428694422967cab7559646df139d790afded;hp=499accac5410d45dee99ccb863dedd6eaa0adfab;hpb=49c0af19f5bf0f2402d37d8abf7fb139e7453245;p=gdcm.git diff --git a/src/gdcmDocument.cxx b/src/gdcmDocument.cxx index 499accac..71312ce5 100644 --- a/src/gdcmDocument.cxx +++ b/src/gdcmDocument.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocument.cxx,v $ Language: C++ - Date: $Date: 2004/08/01 00:59:21 $ - Version: $Revision: 1.62 $ + Date: $Date: 2004/09/07 13:57:04 $ + Version: $Revision: 1.71 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -25,7 +25,6 @@ #include "gdcmUtil.h" #include "gdcmDebug.h" -#include #include // For nthos: @@ -82,22 +81,20 @@ const unsigned int gdcmDocument::MAX_SIZE_PRINT_ELEMENT_VALUE = 0x7fffffff; /** * \brief constructor - * @param inFilename file to be opened for parsing - * @param exception_on_error whether we throw an exception or not + * @param filename file to be opened for parsing */ -gdcmDocument::gdcmDocument( std::string const & filename, - bool exception_on_error) +gdcmDocument::gdcmDocument( std::string const & filename ) : gdcmElementSet(-1) { SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE); Filename = filename; Initialise(); - if ( !OpenFile(exception_on_error)) + if ( !OpenFile() ) { return; } - + dbg.Verbose(0, "gdcmDocument::gdcmDocument: starting parsing of file: ", Filename.c_str()); rewind(Fp); @@ -137,7 +134,11 @@ gdcmDocument::gdcmDocument( std::string const & filename, CloseFile(); // -------------------------------------------------------------- - // Special Patch to allow gdcm to read ACR-LibIDO formated images + // Specific code to allow gdcm to read ACR-LibIDO formated images + // Note: ACR-LibIDO is an extension of the ACR standard that was + // used at CREATIS. For the time being (say a couple years) + // we keep this kludge to allow a smooth move to gdcm for + // CREATIS developpers (sorry folks). // // if recognition code tells us we deal with a LibIDO image // we switch lineNumber and columnNumber @@ -154,23 +155,20 @@ gdcmDocument::gdcmDocument( std::string const & filename, SetEntryByNumber(columns, 0x0028, 0x0010); SetEntryByNumber(rows , 0x0028, 0x0011); } - // ----------------- End of Special Patch ---------------- + // ----------------- End of ACR-LibIDO kludge ------------------ PrintLevel = 1; // 'Medium' print level by default } /** - * \brief constructor - * @param exception_on_error + * \brief This default constructor doesn't parse the file. You should + * then invoke \ref gdcmDocument::SetFileName and then the parsing. */ -gdcmDocument::gdcmDocument(bool exception_on_error) +gdcmDocument::gdcmDocument() :gdcmElementSet(-1) { - (void)exception_on_error; - SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE); Initialise(); - PrintLevel = 1; // 'Medium' print level by default } @@ -244,7 +242,7 @@ bool gdcmDocument::SetShaDict(gdcmDict *dict) * \brief Set the shadow dictionary used * \param dictName name of the dictionary to use in shadow */ -bool gdcmDocument::SetShaDict(DictKey dictName) +bool gdcmDocument::SetShaDict(DictKey const & dictName) { RefShaDict = gdcmGlobal::GetDicts()->GetDict(dictName); return !RefShaDict; @@ -262,8 +260,6 @@ bool gdcmDocument::IsReadable() { if( Filetype == gdcmUnknown) { - std::cout << " gdcmDocument::IsReadable: Filetype " << Filetype - << " " << "gdcmUnknown " << gdcmUnknown << std::endl; //JPR dbg.Verbose(0, "gdcmDocument::IsReadable: wrong filetype"); return false; } @@ -282,7 +278,7 @@ bool gdcmDocument::IsReadable() /** * \brief Internal function that checks whether the Transfer Syntax given * as argument is the one present in the current document. - * @param SyntaxToCheck The transfert syntax we need to check against. + * @param syntaxToCheck The transfert syntax we need to check against. * @return True when SyntaxToCheck corresponds to the Transfer Syntax of * the current document. False either when the document contains * no Transfer Syntax, or when the Tranfer Syntaxes doesn't match. @@ -463,58 +459,46 @@ FileType gdcmDocument::GetFileType() } /** - * \brief opens the file - * @param exception_on_error - * @return + * \brief Tries to open the file \ref gdcmDocument::Filename and + * checks the preamble when existing. + * @return The FILE pointer on success. */ -FILE *gdcmDocument::OpenFile(bool exception_on_error) - throw(gdcmFileError) +FILE* gdcmDocument::OpenFile() { - Fp = fopen(Filename.c_str(),"rb"); - - if(!Fp) - { - if(exception_on_error) - { - throw gdcmFileError("gdcmDocument::gdcmDocument(const char *, bool)"); - } - else - { - dbg.Verbose(0, "gdcmDocument::OpenFile cannot open file: ", - Filename.c_str()); - return NULL; - } - } + Fp = fopen(Filename.c_str(),"rb"); - if ( Fp ) - { - uint16_t zero; - fread(&zero, (size_t)2, (size_t)1, Fp); - - //ACR -- or DICOM with no Preamble -- - if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200 ) - { - return Fp; - } - - //DICOM - fseek(Fp, 126L, SEEK_CUR); - char dicm[4]; - fread(dicm, (size_t)4, (size_t)1, Fp); - if( memcmp(dicm, "DICM", 4) == 0 ) - { - return Fp; - } - - fclose(Fp); - dbg.Verbose(0, "gdcmDocument::OpenFile not DICOM/ACR", Filename.c_str()); - } - else - { - dbg.Verbose(0, "gdcmDocument::OpenFile cannot open file", Filename.c_str()); - } - - return 0; + if(!Fp) + { + dbg.Verbose( 0, + "gdcmDocument::OpenFile cannot open file: ", + Filename.c_str()); + return 0; + } + + uint16_t zero; + fread(&zero, (size_t)2, (size_t)1, Fp); + + //ACR -- or DICOM with no Preamble -- + if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200 ) + { + return Fp; + } + + //DICOM + fseek(Fp, 126L, SEEK_CUR); + char dicm[4]; + fread(dicm, (size_t)4, (size_t)1, Fp); + if( memcmp(dicm, "DICM", 4) == 0 ) + { + return Fp; + } + + fclose(Fp); + dbg.Verbose( 0, + "gdcmDocument::OpenFile not DICOM/ACR (missing preamble)", + Filename.c_str()); + + return 0; } /** @@ -591,9 +575,9 @@ void gdcmDocument::Write(FILE* fp,FileType filetype) /** * \brief Modifies the value of a given Header Entry (Dicom Element) * when it exists. Create it with the given value when unexistant. - * @param Value (string) Value to be set - * @param Group Group number of the Entry - * @param Elem Element number of the Entry + * @param value (string) Value to be set + * @param group Group number of the Entry + * @param elem Element number of the Entry * \return pointer to the modified/created Header Entry (NULL when creation * failed). */ @@ -604,8 +588,8 @@ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber( uint16_t elem ) { gdcmValEntry* valEntry = 0; - gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem); + if (!currentEntry) { // The entry wasn't present and we simply create the required ValEntry: @@ -685,12 +669,41 @@ gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber( return b; } + +/* + * \brief Modifies the value of a given Header Entry (Dicom Element) + * when it exists. Create it when unexistant. + * @param Group Group number of the Entry + * @param Elem Element number of the Entry + * \return pointer to the modified/created SeqEntry (NULL when creation + * failed). + */ +gdcmSeqEntry * gdcmDocument::ReplaceOrCreateByNumber( + uint16_t group, + uint16_t elem) +{ + gdcmSeqEntry* b = 0; + gdcmDocEntry* a = GetDocEntryByNumber( group, elem); + if (!a) + { + a = NewSeqEntryByNumber(group, elem); + if (!a) + { + return 0; + } + + b = new gdcmSeqEntry(a, 1); // FIXME : 1 (Depth) + AddEntry(b); + } + return b; +} + /** * \brief Set a new value if the invoked element exists * Seems to be useless !!! - * @param Value new element value - * @param Group group number of the Entry - * @param Elem element number of the Entry + * @param value new element value + * @param group group number of the Entry + * @param elem element number of the Entry * \return boolean */ bool gdcmDocument::ReplaceIfExistByNumber(std::string const & value, @@ -708,9 +721,9 @@ bool gdcmDocument::ReplaceIfExistByNumber(std::string const & value, * \brief Checks if a given Dicom Element exists within the H table * @param group Group number of the searched Dicom Element * @param element Element number of the searched Dicom Element - * @return number of occurences + * @return true is found */ -int gdcmDocument::CheckIfEntryExistByNumber(uint16_t group, uint16_t element ) +bool gdcmDocument::CheckIfEntryExistByNumber(uint16_t group, uint16_t element ) { const std::string &key = gdcmDictEntry::TranslateToKey(group, element ); return TagHT.count(key); @@ -831,7 +844,7 @@ int gdcmDocument::GetEntryLengthByNumber(uint16_t group, uint16_t element) * @param tagName name of the searched Dicom Element. * @return true when found */ -bool gdcmDocument::SetEntryByName(std::string content,std::string tagName) +bool gdcmDocument::SetEntryByName(std::string const & content,std::string const & tagName) { gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); if( !dictEntry ) @@ -851,7 +864,7 @@ bool gdcmDocument::SetEntryByName(std::string content,std::string tagName) * @param group group number of the Dicom Element to modify * @param element element number of the Dicom Element to modify */ -bool gdcmDocument::SetEntryByNumber(std::string content, +bool gdcmDocument::SetEntryByNumber(std::string const & content, uint16_t group, uint16_t element) { @@ -863,25 +876,28 @@ bool gdcmDocument::SetEntryByNumber(std::string content, return false; } // Non even content must be padded with a space (020H)... - if( content.length() % 2 ) + std::string evenContent = content; + if( evenContent.length() % 2 ) { - content += '\0'; // ... therefore we padd with (000H) .!?! + evenContent += '\0'; // ... therefore we padd with (000H) .!?! } - valEntry->SetValue(content); + valEntry->SetValue(evenContent); // Integers have a special treatement for their length: gdcmVRKey vr = valEntry->GetVR(); if( vr == "US" || vr == "SS" ) { - valEntry->SetLength(2); + int c = CountSubstring(content, "\\"); // for multivaluated items + valEntry->SetLength((c+1)*2); } else if( vr == "UL" || vr == "SL" ) { - valEntry->SetLength(4); + int c = CountSubstring(content, "\\"); // for multivaluated items + valEntry->SetLength((c+1)*4); } else { - valEntry->SetLength(content.length()); + valEntry->SetLength(evenContent.length()); } return true; @@ -915,10 +931,10 @@ bool gdcmDocument::SetEntryByNumber(void *content, //content = content + '\0'; // fing a trick to enlarge a binary field? } */ - gdcmBinEntry * a; - a = (gdcmBinEntry *)TagHT[key]; + gdcmBinEntry* a = (gdcmBinEntry *)TagHT[key]; a->SetVoidArea(content); //a->SetLength(lgth); // ??? + return true; } @@ -955,8 +971,8 @@ bool gdcmDocument::SetEntryLengthByNumber(uint32_t l, /** * \brief Gets (from Header) the offset of a 'non string' element value * (LoadElementValues has already be executed) - * @param Group group number of the Entry - * @param Elem element number of the Entry + * @param group group number of the Entry + * @param elem element number of the Entry * @return File Offset of the Element Value */ size_t gdcmDocument::GetEntryOffsetByNumber(uint16_t group, uint16_t elem) @@ -973,8 +989,8 @@ size_t gdcmDocument::GetEntryOffsetByNumber(uint16_t group, uint16_t elem) /** * \brief Gets (from Header) a 'non string' element value * (LoadElementValues has already be executed) - * @param Group group number of the Entry - * @param Elem element number of the Entry + * @param group group number of the Entry + * @param elem element number of the Entry * @return Pointer to the 'non string' area */ void * gdcmDocument::GetEntryVoidAreaByNumber(uint16_t group, uint16_t elem) @@ -991,8 +1007,8 @@ void * gdcmDocument::GetEntryVoidAreaByNumber(uint16_t group, uint16_t elem) /** * \brief Loads (from disk) the element content * when a string is not suitable - * @param Group group number of the Entry - * @param Elem element number of the Entry + * @param group group number of the Entry + * @param elem element number of the Entry */ void *gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem) { @@ -1024,7 +1040,7 @@ void *gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem) /** * \brief Loads (from disk) the element content * when a string is not suitable - * @param Element Entry whose voidArea is going to be loaded + * @param element Entry whose voidArea is going to be loaded */ void *gdcmDocument::LoadEntryVoidArea(gdcmBinEntry *element) { @@ -1065,8 +1081,9 @@ bool gdcmDocument::SetEntryVoidAreaByNumber(void * area, { return false; } - // This was for multimap ? - (( gdcmBinEntry *)( ((TagHT.equal_range(key)).first)->second ))->SetVoidArea(area); + + // This was for multimap ? + (( gdcmBinEntry *)( ((TagHT.equal_range(key)).first)->second ))->SetVoidArea(area); return true; } @@ -1219,7 +1236,7 @@ uint32_t gdcmDocument::SwapLong(uint32_t a) a=( ((a<< 8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) ); break; default : - std::cout << "swapCode= " << SwapCode << std::endl; + //std::cout << "swapCode= " << SwapCode << std::endl; dbg.Error(" gdcmDocument::SwapLong : unset swap code"); a = 0; } @@ -1244,7 +1261,7 @@ uint16_t gdcmDocument::SwapShort(uint16_t a) { if ( SwapCode == 4321 || SwapCode == 2143 ) { - a =((( a << 8 ) & 0x0ff00 ) | (( a >> 8 ) & 0x00ff ) ); + a = ((( a << 8 ) & 0x0ff00 ) | (( a >> 8 ) & 0x00ff ) ); } return a; } @@ -1300,7 +1317,7 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, newValEntry->SetDepthLevel(depth); set->AddEntry(newValEntry); LoadDocEntry(newValEntry); - if (newValEntry->isItemDelimitor()) + if (newValEntry->IsItemDelimitor()) { break; } @@ -1384,7 +1401,6 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, } delete newDocEntry; } - return l; // Probably useless } @@ -1410,7 +1426,7 @@ long gdcmDocument::ParseSQ(gdcmSeqEntry *set, } if( delim_mode ) { - if ( newDocEntry->isSequenceDelimitor() ) + if ( newDocEntry->IsSequenceDelimitor() ) { set->SetSequenceDelimitationItem( newDocEntry ); break; @@ -1452,7 +1468,7 @@ long gdcmDocument::ParseSQ(gdcmSeqEntry *set, /** * \brief Loads the element content if its length doesn't exceed * the value specified with gdcmDocument::SetMaxSizeLoadEntry() - * @param Entry Header Entry (Dicom Element) to be dealt with + * @param entry Header Entry (Dicom Element) to be dealt with */ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) { @@ -1497,8 +1513,12 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) } // to be sure we are at the end of the value ... fseek(Fp, (long)entry->GetOffset()+(long)entry->GetLength(), SEEK_SET); - - return; //FIXME FIXME FIXME FIXME ???? + // Following return introduced by JPR on version 1.25. Since the + // treatement of a ValEntry is never executed (doh!) this means + // we were lucky up to now because we NEVER encountered a ValEntry + // whose length was bigger thant MaxSizeLoadEntry !? I can't believe + // this could ever work... + return; //FIXME FIXME FIXME FIXME JPR ???? // Be carefull : a BinEntry IS_A ValEntry ... if (gdcmValEntry* valEntryPtr = dynamic_cast< gdcmValEntry* >(entry) ) @@ -1529,7 +1549,7 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) if ( IsDocEntryAnInteger(entry) ) { uint32_t NewInt; - std::ostringstream s; + //std::ostringstream s; //shadow previous declaration int nbInt; // When short integer(s) are expected, read and convert the following // n *two characters properly i.e. consider them as short integers as @@ -1614,14 +1634,14 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) /** * \brief Find the value Length of the passed Header Entry - * @param Entry Header Entry whose length of the value shall be loaded. + * @param entry Header Entry whose length of the value shall be loaded. */ -void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) +void gdcmDocument::FindDocEntryLength( gdcmDocEntry *entry ) + throw ( gdcmFormatError ) { uint16_t element = entry->GetElement(); std::string vr = entry->GetVR(); - uint16_t length16; - + uint16_t length16; if ( Filetype == gdcmExplicitVR && !entry->IsImplicitVR() ) { @@ -1635,8 +1655,12 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) if ( vr == "OB" && length32 == 0xffffffff ) { - uint32_t lengthOB = FindDocEntryLengthOB(); - if ( errno == 1 ) + uint32_t lengthOB; + try + { + lengthOB = FindDocEntryLengthOB(); + } + catch ( gdcmFormatUnexpected ) { // Computing the length failed (this happens with broken // files like gdcm-JPEG-LossLess3a.dcm). We still have a @@ -1648,7 +1672,6 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) long lengthUntilEOF = ftell(Fp) - currentPosition; fseek(Fp, currentPosition, SEEK_SET); entry->SetLength(lengthUntilEOF); - errno = 0; return; } entry->SetLength(lengthOB); @@ -1688,12 +1711,12 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) // endian encoding". When this is the case, chances are we have got our // hands on a big endian encoded file: we switch the swap code to // big endian and proceed... - if ( (element == 0x0000) && (length16 == 0x0400) ) + if ( element == 0x0000 && length16 == 0x0400 ) { - if ( ! IsExplicitVRBigEndianTransferSyntax() ) + if ( !IsExplicitVRBigEndianTransferSyntax() ) { - dbg.Verbose(0, "gdcmDocument::FindLength", "not explicit VR"); - errno = 1; + throw gdcmFormatError( "gdcmDocument::FindDocEntryLength()", + " not explicit VR." ); return; } length16 = 4; @@ -1717,11 +1740,10 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) // Heuristic: well, some files are really ill-formed. if ( length16 == 0xffff) { + // 0xffff means that we deal with 'Unknown Length' Sequence length16 = 0; - // Length16= 0xffff means that we deal with - // 'Unknown Length' Sequence } - FixDocEntryFoundLength(entry, (uint32_t)length16); + FixDocEntryFoundLength( entry, (uint32_t)length16 ); return; } else @@ -1733,14 +1755,14 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) // not coexist in a Data Set and Data Sets nested within it".] // Length is on 4 bytes. - FixDocEntryFoundLength(entry, ReadInt32()); + FixDocEntryFoundLength( entry, ReadInt32() ); return; } } /** * \brief Find the Value Representation of the current Dicom Element. - * @param Entry + * @param entry */ void gdcmDocument::FindDocEntryVR( gdcmDocEntry *entry ) { @@ -1783,7 +1805,7 @@ void gdcmDocument::FindDocEntryVR( gdcmDocEntry *entry ) * \brief Check the correspondance between the VR of the header entry * and the taken VR. If they are different, the header entry is * updated with the new VR. - * @param Entry Header Entry to check + * @param entry Header Entry to check * @param vr Dicom Value Representation * @return false if the VR is incorrect of if the VR isn't referenced * otherwise, it returns true @@ -1801,7 +1823,7 @@ bool gdcmDocument::CheckDocEntryVR(gdcmDocEntry *entry, gdcmVRKey vr) // expected VR read happens to be non-ascii characters we consider // we hit falsely explicit VR tag. - if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) ) + if ( !isalpha(vr[0]) && !isalpha(vr[1]) ) { realExplicit = false; } @@ -1866,7 +1888,7 @@ bool gdcmDocument::CheckDocEntryVR(gdcmDocEntry *entry, gdcmVRKey vr) * \brief Get the transformed value of the header entry. The VR value * is used to define the transformation to operate on the value * \warning NOT end user intended method ! - * @param Entry + * @param entry entry to tranform * @return Transformed entry value */ std::string gdcmDocument::GetDocEntryValue(gdcmDocEntry *entry) @@ -1942,7 +1964,7 @@ std::string gdcmDocument::GetDocEntryValue(gdcmDocEntry *entry) * value is used to define the reverse transformation to operate on * the value * \warning NOT end user intended method ! - * @param Entry + * @param entry Entry to reverse transform * @return Reverse transformed entry value */ std::string gdcmDocument::GetDocEntryUnvalue(gdcmDocEntry* entry) @@ -1996,7 +2018,7 @@ std::string gdcmDocument::GetDocEntryUnvalue(gdcmDocEntry* entry) /** * \brief Skip a given Header Entry * \warning NOT end user intended method ! - * @param entry + * @param entry entry to skip */ void gdcmDocument::SkipDocEntry(gdcmDocEntry *entry) { @@ -2006,7 +2028,7 @@ void gdcmDocument::SkipDocEntry(gdcmDocEntry *entry) /** * \brief Skips to the begining of the next Header Entry * \warning NOT end user intended method ! - * @param entry + * @param entry entry to skip */ void gdcmDocument::SkipToNextDocEntry(gdcmDocEntry *entry) { @@ -2018,6 +2040,8 @@ void gdcmDocument::SkipToNextDocEntry(gdcmDocEntry *entry) * \brief When the length of an element value is obviously wrong (because * the parser went Jabberwocky) one can hope improving things by * applying some heuristics. + * @param entry entry to check + * @param foundLength fist assumption about length */ void gdcmDocument::FixDocEntryFoundLength(gdcmDocEntry *entry, uint32_t foundLength) @@ -2095,7 +2119,7 @@ void gdcmDocument::FixDocEntryFoundLength(gdcmDocEntry *entry, /** * \brief Apply some heuristics to predict whether the considered * element value contains/represents an integer or not. - * @param Entry The element value on which to apply the predicate. + * @param entry The element value on which to apply the predicate. * @return The result of the heuristical predicate. */ bool gdcmDocument::IsDocEntryAnInteger(gdcmDocEntry *entry) @@ -2148,6 +2172,7 @@ bool gdcmDocument::IsDocEntryAnInteger(gdcmDocEntry *entry) */ uint32_t gdcmDocument::FindDocEntryLengthOB() + throw( gdcmFormatUnexpected ) { // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data. long positionOnEntry = ftell(Fp); @@ -2156,26 +2181,33 @@ uint32_t gdcmDocument::FindDocEntryLengthOB() while ( !foundSequenceDelimiter ) { - uint16_t g = ReadInt16(); - uint16_t n = ReadInt16(); - if ( errno == 1 ) + uint16_t group; + uint16_t elem; + try { - return 0; + group = ReadInt16(); + elem = ReadInt16(); + } + catch ( gdcmFormatError ) + { + throw gdcmFormatError("gdcmDocument::FindDocEntryLengthOB()", + " group or element not present."); } // We have to decount the group and element we just read totalLength += 4; - if ( g != 0xfffe || ( n != 0xe0dd && n != 0xe000 ) ) + if ( group != 0xfffe || ( ( elem != 0xe0dd ) && ( elem != 0xe000 ) ) ) { - dbg.Verbose(1, "gdcmDocument::FindLengthOB: neither an Item tag " - "nor a Sequence delimiter tag."); + dbg.Verbose(1, "gdcmDocument::FindDocEntryLengthOB: neither an Item " + "tag nor a Sequence delimiter tag."); fseek(Fp, positionOnEntry, SEEK_SET); - errno = 1; - return 0; + throw gdcmFormatUnexpected("gdcmDocument::FindDocEntryLengthOB()", + "Neither an Item tag nor a Sequence " + "delimiter tag."); } - if ( n == 0xe0dd ) + if ( elem == 0xe0dd ) { foundSequenceDelimiter = true; } @@ -2200,6 +2232,7 @@ uint32_t gdcmDocument::FindDocEntryLengthOB() * @return read value */ uint16_t gdcmDocument::ReadInt16() + throw( gdcmFormatError ) { uint16_t g; size_t item_read = fread (&g, (size_t)2,(size_t)1, Fp); @@ -2207,12 +2240,10 @@ uint16_t gdcmDocument::ReadInt16() { if( ferror(Fp) ) { - dbg.Verbose(0, "gdcmDocument::ReadInt16", " File Error"); + throw gdcmFormatError( "gdcmDocument::ReadInt16()", " file error." ); } - errno = 1; - return 0; + throw gdcmFormatError( "gdcmDocument::ReadInt16()", "EOF." ); } - errno = 0; g = SwapShort(g); return g; } @@ -2223,6 +2254,7 @@ uint16_t gdcmDocument::ReadInt16() * @return read value */ uint32_t gdcmDocument::ReadInt32() + throw( gdcmFormatError ) { uint32_t g; size_t item_read = fread (&g, (size_t)4,(size_t)1, Fp); @@ -2230,12 +2262,10 @@ uint32_t gdcmDocument::ReadInt32() { if( ferror(Fp) ) { - dbg.Verbose(0, "gdcmDocument::ReadInt32", " File Error"); + throw gdcmFormatError( "gdcmDocument::ReadInt16()", " file error." ); } - errno = 1; - return 0; + throw gdcmFormatError( "gdcmDocument::ReadInt32()", "EOF." ); } - errno = 0; g = SwapLong(g); return g; } @@ -2476,7 +2506,7 @@ void gdcmDocument::SwitchSwapToBigEndian() /** * \brief during parsing, Header Elements too long are not loaded in memory - * @param NewSize + * @param newSize */ void gdcmDocument::SetMaxSizeLoadEntry(long newSize) { @@ -2496,7 +2526,7 @@ void gdcmDocument::SetMaxSizeLoadEntry(long newSize) /** * \brief Header Elements too long will not be printed * \todo See comments of \ref gdcmDocument::MAX_SIZE_PRINT_ELEMENT_VALUE - * @param NewSize + * @param newSize */ void gdcmDocument::SetMaxSizePrintEntry(long newSize) { @@ -2522,28 +2552,39 @@ void gdcmDocument::SetMaxSizePrintEntry(long newSize) * gets the VR, gets the length, gets the offset value) * @return On succes the newly created DocEntry, NULL on failure. */ -gdcmDocEntry *gdcmDocument::ReadNextDocEntry() +gdcmDocEntry* gdcmDocument::ReadNextDocEntry() { - uint16_t g = ReadInt16(); - uint16_t n = ReadInt16(); + uint16_t group; + uint16_t elem; - if (errno == 1) + try + { + group = ReadInt16(); + elem = ReadInt16(); + } + catch ( gdcmFormatError e ) { // We reached the EOF (or an error occured) therefore // header parsing has to be considered as finished. + //std::cout << e; return 0; } - gdcmDocEntry *newEntry = NewDocEntryByNumber(g, n); + gdcmDocEntry *newEntry = NewDocEntryByNumber(group, elem); FindDocEntryVR(newEntry); - FindDocEntryLength(newEntry); - if (errno == 1) + try + { + FindDocEntryLength(newEntry); + } + catch ( gdcmFormatError e ) { // Call it quits + //std::cout << e; delete newEntry; - return NULL; + return 0; } + newEntry->SetOffset(ftell(Fp)); return newEntry; @@ -2570,16 +2611,16 @@ uint32_t gdcmDocument::GenerateFreeTagKeyInGroup(uint16_t group) } /** - * \brief Assuming the internal file pointer \ref gdcmDocument::fp + * \brief Assuming the internal file pointer \ref gdcmDocument::Fp * is placed at the beginning of a tag check whether this * tag is (TestGroup, TestElement). - * \warning On success the internal file pointer \ref gdcmDocument::fp + * \warning On success the internal file pointer \ref gdcmDocument::Fp * is modified to point after the tag. * On failure (i.e. when the tag wasn't the expected tag * (TestGroup, TestElement) the internal file pointer - * \ref gdcmDocument::fp is restored to it's original position. - * @param TestGroup The expected group of the tag. - * @param TestElement The expected Element of the tag. + * \ref gdcmDocument::Fp is restored to it's original position. + * @param testGroup The expected group of the tag. + * @param testElement The expected Element of the tag. * @return True on success, false otherwise. */ bool gdcmDocument::ReadTag(uint16_t testGroup, uint16_t testElement) @@ -2610,23 +2651,23 @@ bool gdcmDocument::ReadTag(uint16_t testGroup, uint16_t testElement) } /** - * \brief Assuming the internal file pointer \ref gdcmDocument::fp + * \brief Assuming the internal file pointer \ref gdcmDocument::Fp * is placed at the beginning of a tag (TestGroup, TestElement), * read the length associated to the Tag. - * \warning On success the internal file pointer \ref gdcmDocument::fp + * \warning On success the internal file pointer \ref gdcmDocument::Fp * is modified to point after the tag and it's length. * On failure (i.e. when the tag wasn't the expected tag * (TestGroup, TestElement) the internal file pointer - * \ref gdcmDocument::fp is restored to it's original position. - * @param TestGroup The expected group of the tag. - * @param TestElement The expected Element of the tag. + * \ref gdcmDocument::Fp is restored to it's original position. + * @param testGroup The expected group of the tag. + * @param testElement The expected Element of the tag. * @return On success returns the length associated to the tag. On failure * returns 0. */ uint32_t gdcmDocument::ReadTagLength(uint16_t testGroup, uint16_t testElement) { - long PositionOnEntry = ftell(Fp); - (void)PositionOnEntry; + long positionOnEntry = ftell(Fp); + (void)positionOnEntry; if ( !ReadTag(testGroup, testElement) ) { @@ -2743,7 +2784,7 @@ void gdcmDocument::Parse7FE0 () // Make sure that at the end of the item we encounter a 'Sequence // Delimiter Item': - if ( ! ReadTag(0xfffe, 0xe0dd) ) + if ( !ReadTag(0xfffe, 0xe0dd) ) { dbg.Verbose(0, "gdcmDocument::Parse7FE0: no sequence delimiter item"); dbg.Verbose(0, " at end of RLE item sequence"); @@ -2769,7 +2810,7 @@ bool gdcmDocument::operator<(gdcmDocument &document) { return true; } - else if(s1 > s2) + else if( s1 > s2 ) { return false; } @@ -2784,7 +2825,7 @@ bool gdcmDocument::operator<(gdcmDocument &document) } else if ( s1 > s2 ) { - return true; + return false; } else { @@ -2803,7 +2844,7 @@ bool gdcmDocument::operator<(gdcmDocument &document) { // Serie Instance UID s1 = GetEntryByNumber(0x0020,0x000e); - s2 = document.GetEntryByNumber(0x0020,0x000e); + s2 = document.GetEntryByNumber(0x0020,0x000e); if ( s1 < s2 ) { return true;