X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmDocument.cxx;h=d4410d1b47a6ca947374137ca51b9c5e360ebba2;hb=cb6cf86b2b3cc6b43d40561fb9552ebd19fdbc99;hp=3f49d22649bfa5808acf1204d7d15b16d7cc7be6;hpb=1b14200990537794a9f66d43c89b030459237edc;p=gdcm.git diff --git a/src/gdcmDocument.cxx b/src/gdcmDocument.cxx index 3f49d226..d4410d1b 100644 --- a/src/gdcmDocument.cxx +++ b/src/gdcmDocument.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocument.cxx,v $ Language: C++ - Date: $Date: 2005/10/18 11:10:45 $ - Version: $Revision: 1.292 $ + Date: $Date: 2005/10/23 15:28:26 $ + Version: $Revision: 1.303 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -127,7 +127,7 @@ bool Document::DoTheLoadingDocumentJob( ) Group0002Parsed = false; - gdcmWarningMacro( "Starting parsing of file: " << Filename.c_str()); + gdcmDebugMacro( "Starting parsing of file: " << Filename.c_str()); Fp->seekg(0, std::ios::end); long lgt = Fp->tellg(); // total length of the file @@ -277,7 +277,7 @@ bool Document::DoTheLoadingDocumentJob( ) */ void Document::AddForceLoadElement (uint16_t group, uint16_t elem) { - Element el; + DicomElement el; el.Group = group; el.Elem = elem; UserForceLoadList.push_back(el); @@ -604,7 +604,8 @@ void Document::WriteContent(std::ofstream *fp, FileType filetype) { // Skip if user wants to write an ACR-NEMA file - if ( filetype == ImplicitVR || filetype == ExplicitVR ) + if ( filetype == ImplicitVR || filetype == ExplicitVR || + filetype == JPEG ) { // writing Dicom File Preamble char filePreamble[128]; @@ -659,7 +660,7 @@ void Document::LoadEntryBinArea(uint16_t group, uint16_t elem) /** * \brief Loads (from disk) the element content * when a string is not suitable - * @param elem Entry whose binArea is going to be loaded + * @param entry Entry whose binArea is going to be loaded */ void Document::LoadEntryBinArea(DataEntry *entry) { @@ -683,7 +684,7 @@ void Document::LoadEntryBinArea(DataEntry *entry) return; } - // Read the datas + // Read the data Fp->read((char*)data, l); if ( Fp->fail() || Fp->eof() ) { @@ -694,7 +695,8 @@ void Document::LoadEntryBinArea(DataEntry *entry) // Swap the data content if necessary uint32_t i; - unsigned short vrLgth = Global::GetVR()->GetAtomicElementLength(entry->GetVR()); + unsigned short vrLgth = + Global::GetVR()->GetAtomicElementLength(entry->GetVR()); if( entry->GetVR() == "OW" ) vrLgth = 1; @@ -716,7 +718,7 @@ void Document::LoadEntryBinArea(DataEntry *entry) } case 8: { - gdcmWarningMacro("Can't swap 64 bits datas"); + gdcmWarningMacro("Can't swap 64 bits data"); /* uint64_t *data64 = (uint64_t *)data; for(i=0;iGetVR(); + + // Useless checking, now ! + /* if ( Filetype == ExplicitVR && !Global::GetVR()->IsVROfBinaryRepresentable(vr) ) { ////// No DataEntry: should mean UNKOWN VR gdcmWarningMacro( std::hex << newDocEntry->GetGroup() << "|" << newDocEntry->GetElement() - << " : No DataEntry." - "Probably unknown VR."); + << " : unknown VR." + " Probably 'Implicit VR' entry within " + "an explicit VR 'document'."); } + */ if ( !set->AddEntry( newDataEntry ) ) { @@ -1079,8 +1087,7 @@ void Document::ParseDES(DocEntrySet *set, long offset, if ( newDocEntry->GetGroup()%2 != 0 ) { Fp->seekg( l, std::ios::cur); - RemoveEntry( newDocEntry ); // Remove and delete - //used = false; // never used + delete newDocEntry; // Delete, not in the set continue; } } @@ -1088,8 +1095,7 @@ void Document::ParseDES(DocEntrySet *set, long offset, { // User asked to skip *any* SeQuence Fp->seekg( l, std::ios::cur); - //used = false; // never used - RemoveEntry( newDocEntry ); // Remove and delete + delete newDocEntry; // Delete, not in the set continue; } // delay the dynamic cast as late as possible @@ -1102,11 +1108,9 @@ void Document::ParseDES(DocEntrySet *set, long offset, // is a Document, then we are building the first depth level. // Hence the SeqEntry we are building simply has a depth // level of one: -// SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ); if ( set == this ) // ( dynamic_cast< Document* > ( set ) ) { newSeqEntry->SetDepthLevel( 1 ); - // newSeqEntry->SetKey( newSeqEntry->GetKey() ); } // But when "set" is already a SQItem, we are building a nested // sequence, and hence the depth level of the new SeqEntry @@ -1116,9 +1120,6 @@ void Document::ParseDES(DocEntrySet *set, long offset, else if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) ) { newSeqEntry->SetDepthLevel( parentSQItem->GetDepthLevel() + 1 ); - - // newSeqEntry->SetKey( parentSQItem->GetBaseTagKey() - // + newSeqEntry->GetKey() ); } if ( l != 0 ) @@ -1158,7 +1159,7 @@ void Document::ParseDES(DocEntrySet *set, long offset, } first = false; } // end While - gdcmWarningMacro( "Exit from ParseDES, delim-mode " << delim_mode ); + gdcmDebugMacro( "Exit from ParseDES, delim-mode " << delim_mode ); } /** @@ -1274,7 +1275,7 @@ void Document::LoadDocEntry(DocEntry *entry, bool forceLoad) { uint16_t group = entry->GetGroup(); uint16_t elem = entry->GetElement(); - std::string vr = entry->GetVR(); + const VRKey &vr = entry->GetVR(); uint32_t length = entry->GetLength(); Fp->seekg((long)entry->GetOffset(), std::ios::beg); @@ -1285,7 +1286,7 @@ void Document::LoadDocEntry(DocEntry *entry, bool forceLoad) // (fffe e0dd) tells us the current SeQuence just ended // // (fffe 0000) is an 'impossible' tag value, - // found in MR-PHILIPS-16-Multi-Seq.dcm + // found in MR-PHILIPS-16-Multi-Seq.dcm if ( (group == 0xfffe && elem != 0x0000 ) || vr == "SQ" ) { @@ -1336,7 +1337,7 @@ void Document::LoadDocEntry(DocEntry *entry, bool forceLoad) void Document::FindDocEntryLength( DocEntry *entry ) throw ( FormatError ) { - std::string vr = entry->GetVR(); + const VRKey &vr = entry->GetVR(); uint16_t length16; if ( Filetype == ExplicitVR && !entry->IsImplicitVR() ) @@ -1477,10 +1478,10 @@ uint32_t Document::FindDocEntryLengthOBOrOW() * \brief Find the Value Representation of the current Dicom Element. * @return Value Representation of the current Entry */ -std::string Document::FindDocEntryVR() +VRKey Document::FindDocEntryVR() { if ( Filetype != ExplicitVR ) - return GDCM_UNKNOWN; + return GDCM_VRUNKNOWN; long positionOnEntry = Fp->tellg(); // Warning: we believe this is explicit VR (Value Representation) because @@ -1491,14 +1492,15 @@ std::string Document::FindDocEntryVR() // is in explicit VR and try to fix things if it happens not to be // the case. - char vr[3]; - Fp->read (vr, (size_t)2); - vr[2] = 0; + VRKey vr; + Fp->read(&(vr[0]),(size_t)2); + gdcmDebugMacro( "--> VR: " << vr ) if ( !CheckDocEntryVR(vr) ) { + gdcmWarningMacro( "Unknown VR '" << vr << "'" ) Fp->seekg(positionOnEntry, std::ios::beg); - return GDCM_UNKNOWN; + return GDCM_VRUNKNOWN; } return vr; } @@ -1511,12 +1513,9 @@ std::string Document::FindDocEntryVR() * @return false if the VR is incorrect or if the VR isn't referenced * otherwise, it returns true */ -bool Document::CheckDocEntryVR(VRKey vr) +bool Document::CheckDocEntryVR(const VRKey &vr) { - if ( !Global::GetVR()->IsValidVR(vr) ) - return false; - - return true; + return Global::GetVR()->IsValidVR(vr); } /** @@ -1557,7 +1556,7 @@ void Document::SkipToNextDocEntry(DocEntry *currentDocEntry) void Document::FixDocEntryFoundLength(DocEntry *entry, uint32_t foundLength) { - entry->SetReadLength( foundLength ); // will be updated only if a bug is found + entry->SetReadLength( foundLength );// will be updated only if a bug is found if ( foundLength == 0xffffffff) { foundLength = 0; @@ -1568,7 +1567,7 @@ void Document::FixDocEntryFoundLength(DocEntry *entry, if ( foundLength % 2) { - gdcmWarningMacro( "Warning : Tag with uneven length " << foundLength + gdcmWarningMacro( "Warning : Tag with uneven length " << foundLength << " in x(" << std::hex << gr << "," << elem <<")"); } @@ -1633,7 +1632,7 @@ bool Document::IsDocEntryAnInteger(DocEntry *entry) { uint16_t elem = entry->GetElement(); uint16_t group = entry->GetGroup(); - const std::string &vr = entry->GetVR(); + const VRKey &vr = entry->GetVR(); uint32_t length = entry->GetLength(); // When we have some semantics on the element we just read, and if we @@ -1647,9 +1646,9 @@ bool Document::IsDocEntryAnInteger(DocEntry *entry) } else { - // Allthough this should never happen, still some images have a + // Although this should never happen, still some images have a // corrupted group length [e.g. have a glance at offset x(8336) of - // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm]. + // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm. // Since for dicom compliant and well behaved headers, the present // test is useless (and might even look a bit paranoid), when we // encounter such an ill-formed image, we simply display a warning @@ -1695,7 +1694,7 @@ bool Document::CheckSwap() char *entCur = deb + 128; if ( memcmp(entCur, "DICM", (size_t)4) == 0 ) { - gdcmWarningMacro( "Looks like DICOM Version3 (preamble + DCM)" ); + gdcmDebugMacro( "Looks like DICOM Version3 (preamble + DCM)" ); // Group 0002 should always be VR, and the first element 0000 // Let's be carefull (so many wrong headers ...) @@ -1727,24 +1726,24 @@ bool Document::CheckSwap() // instead of just checking for UL, OB and UI !? group 0000 { Filetype = ExplicitVR; - gdcmWarningMacro( "Group 0002 : Explicit Value Representation"); + gdcmDebugMacro( "Group 0002 : Explicit Value Representation"); } else { Filetype = ImplicitVR; - gdcmWarningMacro( "Group 0002 :Not an explicit Value Representation;" + gdcmErrorMacro( "Group 0002 :Not an explicit Value Representation;" << "Looks like a bugged Header!"); } if ( net2host ) { SwapCode = 4321; - gdcmWarningMacro( "HostByteOrder != NetworkByteOrder"); + gdcmDebugMacro( "HostByteOrder != NetworkByteOrder"); } else { SwapCode = 1234; - gdcmWarningMacro( "HostByteOrder = NetworkByteOrder"); + gdcmDebugMacro( "HostByteOrder = NetworkByteOrder"); } // Position the file position indicator at first tag @@ -1791,7 +1790,7 @@ bool Document::CheckSwap() memcmp(entCur, "OB", (size_t)2) == 0 ) { Filetype = ExplicitVR; - gdcmWarningMacro( "Group 0002 : Explicit Value Representation"); + gdcmDebugMacro( "Group 0002 : Explicit Value Representation"); return true; } } @@ -1837,7 +1836,7 @@ bool Document::CheckSwap() // Only 0 or 4321 will be possible // (no oportunity to check for the formerly well known // ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian' - // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -3, 4, ..., 8-) + // if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc-3, 4, ..., 8-) // the file IS NOT ACR-NEMA nor DICOM V3 // Find a trick to tell it the caller... @@ -1868,7 +1867,7 @@ bool Document::CheckSwap() Filetype = ACR; return true; default : - gdcmWarningMacro("ACR/NEMA unfound swap info (Really hopeless !)"); + gdcmWarningMacro("ACR/NEMA unfound swap info (Hopeless !)"); Filetype = Unknown; return false; } @@ -1880,8 +1879,8 @@ bool Document::CheckSwap() */ void Document::SwitchByteSwapCode() { - gdcmWarningMacro( "Switching Byte Swap code from "<< SwapCode - << " at :" <tellg() ); + gdcmDebugMacro( "Switching Byte Swap code from "<< SwapCode + << " at: 0x" << std::hex << Fp->tellg() ); if ( SwapCode == 1234 ) { SwapCode = 4321; @@ -1898,6 +1897,7 @@ void Document::SwitchByteSwapCode() { SwapCode = 3412; } + gdcmDebugMacro( " Into: "<< SwapCode ); } /** @@ -1949,10 +1949,11 @@ DocEntry *Document::ReadNextDocEntry() if ( HasDCMPreamble ) HandleOutOfGroup0002(group, elem); - std::string vr = FindDocEntryVR(); - std::string realVR = vr; + VRKey vr = FindDocEntryVR(); + + VRKey realVR = vr; - if ( vr == GDCM_UNKNOWN ) + if ( vr == GDCM_VRUNKNOWN ) { if ( elem == 0x0000 ) // Group Length { @@ -1970,9 +1971,11 @@ DocEntry *Document::ReadNextDocEntry() if ( dictEntry ) { realVR = dictEntry->GetVR(); + dictEntry->Unregister(); } } } + gdcmDebugMacro( "Found VR: " << vr << " / Real VR: " << realVR ); DocEntry *newEntry; if ( Global::GetVR()->IsVROfSequence(realVR) ) @@ -1983,7 +1986,7 @@ DocEntry *Document::ReadNextDocEntry() static_cast(newEntry)->SetState(DataEntry::STATE_NOTLOADED); } - if ( vr == GDCM_UNKNOWN ) + if ( vr == GDCM_VRUNKNOWN ) { if ( Filetype == ExplicitVR ) { @@ -1993,8 +1996,9 @@ DocEntry *Document::ReadNextDocEntry() { std::string msg; int offset = Fp->tellg(); - msg = Util::Format("Entry (%04x,%04x) at 0x(%x) should be Explicit VR\n", - newEntry->GetGroup(), newEntry->GetElement(), offset ); + msg = Util::Format( + "Entry (%04x,%04x) at x(%x) should be Explicit VR\n", + newEntry->GetGroup(), newEntry->GetElement(), offset ); gdcmWarningMacro( msg.c_str() ); } } @@ -2025,7 +2029,8 @@ DocEntry *Document::ReadNextDocEntry() */ void Document::HandleBrokenEndian(uint16_t &group, uint16_t &elem) { - // Endian reversion. Some files contain groups of tags with reversed endianess. + // Endian reversion. + // Some files contain groups of tags with reversed endianess. static int reversedEndian = 0; // try to fix endian switching in the middle of headers if ((group == 0xfeff) && (elem == 0x00e0)) @@ -2050,7 +2055,7 @@ void Document::HandleBrokenEndian(uint16_t &group, uint16_t &elem) // Do what you want, it breaks ! //reversedEndian--; //SwitchByteSwapCode(); - gdcmWarningMacro( "Should never get here! reversed Sequence Terminator!" ); + gdcmWarningMacro( "Should never get here! reversed Sequence Terminator!" ); // fix the tag group = 0xfffe; elem = 0xe0dd; @@ -2068,7 +2073,8 @@ void Document::HandleBrokenEndian(uint16_t &group, uint16_t &elem) */ void Document::HandleOutOfGroup0002(uint16_t &group, uint16_t &elem) { - // Endian reversion. Some files contain groups of tags with reversed endianess. + // Endian reversion. + // Some files contain groups of tags with reversed endianess. if ( !Group0002Parsed && group != 0x0002) { Group0002Parsed = true; @@ -2082,9 +2088,11 @@ void Document::HandleOutOfGroup0002(uint16_t &group, uint16_t &elem) return; } - // Group 0002 is always 'Explicit ...' even when Transfer Syntax says 'Implicit ..." + // Group 0002 is always 'Explicit ...' + // even when Transfer Syntax says 'Implicit ..." - if ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian ) + if ( Global::GetTS()->GetSpecialTransferSyntax(ts) == + TS::ImplicitVRLittleEndian ) { Filetype = ImplicitVR; } @@ -2092,9 +2100,12 @@ void Document::HandleOutOfGroup0002(uint16_t &group, uint16_t &elem) // FIXME Strangely, this works with //'Implicit VR BigEndian Transfer Syntax (GE Private) // - // --> Probabely normal, since we considered we never have to trust manufacturers. - // (we find very often 'Implicit VR' tag, even when Transfer Syntax tells us it's Explicit ... - if ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian ) + // --> Probabely normal, since we considered we never have + // to trust manufacturers. + // (we find very often 'Implicit VR' tag, + // even when Transfer Syntax tells us it's Explicit ... + if ( Global::GetTS()->GetSpecialTransferSyntax(ts) == + TS::ExplicitVRBigEndian ) { gdcmWarningMacro("Transfer Syntax Name = [" << GetTransferSyntaxName() << "]" );