X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmDocument.cxx;h=c037ba6de931a36addc304ce7cb3272daa6acfe7;hb=879f56a62d0772c95e92d0657882eb1886b4153d;hp=c355fd4e78a38b2b384939c40ff7c115eeedf119;hpb=64ef25e1cadb4b840a4cedfe0adaddecb2d7ca42;p=gdcm.git diff --git a/src/gdcmDocument.cxx b/src/gdcmDocument.cxx index c355fd4e..c037ba6d 100644 --- a/src/gdcmDocument.cxx +++ b/src/gdcmDocument.cxx @@ -3,12 +3,12 @@ Program: gdcm Module: $RCSfile: gdcmDocument.cxx,v $ Language: C++ - Date: $Date: 2004/09/22 21:01:55 $ - Version: $Revision: 1.87 $ + Date: $Date: 2004/10/10 03:03:10 $ + Version: $Revision: 1.101 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or - http://www.creatis.insa-lyon.fr/Public/Gdcm/License.htm for details. + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR @@ -20,7 +20,6 @@ #include "gdcmValEntry.h" #include "gdcmBinEntry.h" #include "gdcmSeqEntry.h" - #include "gdcmGlobal.h" #include "gdcmUtil.h" #include "gdcmDebug.h" @@ -34,7 +33,7 @@ #include #endif -# include +#include // Implicit VR Little Endian #define UI1_2_840_10008_1_2 "1.2.840.10008.1.2" @@ -107,7 +106,7 @@ gdcmDocument::gdcmDocument( std::string const & filename ) long beg = ftell(Fp); lgt -= beg; - (void)ParseDES( this, beg, lgt, false); // le Load sera fait a la volee + ParseDES( this, beg, lgt, false); // le Load sera fait a la volee rewind(Fp); @@ -116,7 +115,7 @@ gdcmDocument::gdcmDocument( std::string const & filename ) std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004); if( PhotometricInterpretation == "PALETTE COLOR " ) { - LoadEntryVoidArea(0x0028,0x1200); // gray LUT + LoadEntryBinArea(0x0028,0x1200); // gray LUT /// FIXME FIXME FIXME /// The tags refered by the three following lines used to be CORRECTLY /// defined as having an US Value Representation in the public @@ -132,19 +131,19 @@ gdcmDocument::gdcmDocument( std::string const & filename ) /// also used as gdcmBinEntry, which requires the proper conversion, /// - OW, and hence loaded as gdcmBinEntry, but afterwards also used /// as gdcmValEntry, which requires the proper conversion. - LoadEntryVoidArea(0x0028,0x1201); // R LUT - LoadEntryVoidArea(0x0028,0x1202); // G LUT - LoadEntryVoidArea(0x0028,0x1203); // B LUT + LoadEntryBinArea(0x0028,0x1201); // R LUT + LoadEntryBinArea(0x0028,0x1202); // G LUT + LoadEntryBinArea(0x0028,0x1203); // B LUT // Segmented Red Palette Color LUT Data - LoadEntryVoidArea(0x0028,0x1221); + LoadEntryBinArea(0x0028,0x1221); // Segmented Green Palette Color LUT Data - LoadEntryVoidArea(0x0028,0x1222); + LoadEntryBinArea(0x0028,0x1222); // Segmented Blue Palette Color LUT Data - LoadEntryVoidArea(0x0028,0x1223); + LoadEntryBinArea(0x0028,0x1223); } //FIXME later : how to use it? - LoadEntryVoidArea(0x0028,0x3006); //LUT Data (CTX dependent) + LoadEntryBinArea(0x0028,0x3006); //LUT Data (CTX dependent) CloseFile(); @@ -192,8 +191,16 @@ gdcmDocument::gdcmDocument() */ gdcmDocument::~gdcmDocument () { - RefPubDict = 0; - RefShaDict = 0; + RefPubDict = NULL; + RefShaDict = NULL; + + // Recursive clean up of sequences + for (TagDocEntryHT::const_iterator it = TagHT.begin(); + it != TagHT.end(); ++it ) + { + //delete it->second; //temp remove + } + TagHT.clear(); } //----------------------------------------------------------------------------- @@ -222,7 +229,7 @@ void gdcmDocument::PrintShaDict(std::ostream & os) /** * \brief Get the public dictionary used */ -gdcmDict *gdcmDocument::GetPubDict() +gdcmDict* gdcmDocument::GetPubDict() { return RefPubDict; } @@ -230,7 +237,7 @@ gdcmDict *gdcmDocument::GetPubDict() /** * \brief Get the shadow dictionary used */ -gdcmDict *gdcmDocument::GetShaDict() +gdcmDict* gdcmDocument::GetShaDict() { return RefShaDict; } @@ -442,6 +449,22 @@ bool gdcmDocument::IsJPEG2000() || IsGivenTransferSyntax(UI1_2_840_10008_1_2_4_91) ); } +/** + * \brief Determines if the Transfer Syntax corresponds to encapsulated + * of encoded Pixel Data (as opposed to native). + * @return True when encapsulated. False when native. + */ +bool gdcmDocument::IsEncapsulateTransferSyntax() +{ + return ( IsJPEGBaseLineProcess1TransferSyntax() + || IsJPEGExtendedProcess2_4TransferSyntax() + || IsJPEGExtendedProcess3_5TransferSyntax() + || IsJPEGSpectralSelectionProcess6_8TransferSyntax() + || IsRLELossLessTransferSyntax() + || IsJPEGLossless() + || IsJPEG2000() ); +} + /** * \brief Predicate for dicom version 3 file. * @return True when the file is a dicom version 3. @@ -586,7 +609,7 @@ void gdcmDocument::Write(FILE* fp,FileType filetype) * \return pointer to the modified/created Header Entry (NULL when creation * failed). */ -gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber( +gdcmValEntry* gdcmDocument::ReplaceOrCreateByNumber( std::string const & value, uint16_t group, uint16_t elem, @@ -603,7 +626,7 @@ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber( // Find out if the tag we received is in the dictionaries: gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict(); - gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem); + gdcmDictEntry* dictEntry = pubDict->GetDictEntryByNumber(group, elem); if (!dictEntry) { currentEntry = NewDocEntryByNumber(group, elem,VR); @@ -658,18 +681,18 @@ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber( /* * \brief Modifies the value of a given Header Entry (Dicom Element) * when it exists. Create it with the given value when unexistant. - * @param voidArea (binary) value to be set + * @param binArea (binary) 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). */ -gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber( - void *voidArea, +gdcmBinEntry* gdcmDocument::ReplaceOrCreateByNumber( + uint8_t* binArea, int lgth, uint16_t group, uint16_t elem, - std::string const & VR ) + std::string const& VR ) { gdcmBinEntry* binEntry = 0; gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem); @@ -729,7 +752,7 @@ gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber( } } - SetEntryByNumber(voidArea, lgth, group, elem); + SetEntryByNumber(binArea, lgth, group, elem); return binEntry; } @@ -743,7 +766,7 @@ gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber( * \return pointer to the modified/created SeqEntry (NULL when creation * failed). */ -gdcmSeqEntry * gdcmDocument::ReplaceOrCreateByNumber( +gdcmSeqEntry* gdcmDocument::ReplaceOrCreateByNumber( uint16_t group, uint16_t elem) { @@ -803,9 +826,9 @@ bool gdcmDocument::CheckIfEntryExistByNumber(uint16_t group, uint16_t element ) * @return Corresponding element value when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ -std::string gdcmDocument::GetEntryByName(TagName const & tagName) +std::string gdcmDocument::GetEntryByName(TagName const& tagName) { - gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); + gdcmDictEntry* dictEntry = RefPubDict->GetDictEntryByName(tagName); if( !dictEntry ) { return GDCM_UNFOUND; @@ -827,7 +850,7 @@ std::string gdcmDocument::GetEntryByName(TagName const & tagName) * @return Corresponding element value representation when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ -std::string gdcmDocument::GetEntryVRByName(TagName const & tagName) +std::string gdcmDocument::GetEntryVRByName(TagName const& tagName) { gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); if( dictEntry == NULL) @@ -928,7 +951,7 @@ bool gdcmDocument::SetEntryByName(std::string const & content,std::string const * @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 const & content, +bool gdcmDocument::SetEntryByNumber(std::string const& content, uint16_t group, uint16_t element) { @@ -958,12 +981,12 @@ bool gdcmDocument::SetEntryByNumber(std::string const & content, gdcmVRKey vr = valEntry->GetVR(); if( vr == "US" || vr == "SS" ) { - c = CountSubstring(content, "\\") + 1; // for multivaluated items + c = gdcmUtil::CountSubstring(content, "\\") + 1; // for multivaluated items l = c*2; } else if( vr == "UL" || vr == "SL" ) { - c = CountSubstring(content, "\\") + 1; // for multivaluated items + c = gdcmUtil::CountSubstring(content, "\\") + 1; // for multivaluated items l = c*4;; } } @@ -975,12 +998,12 @@ bool gdcmDocument::SetEntryByNumber(std::string const & content, * \brief Accesses an existing gdcmDocEntry (i.e. a Dicom Element) * through it's (group, element) and modifies it's content with * the given value. - * @param content new value (void *) to substitute with + * @param content new value (void* -> uint8_t*) to substitute with * @param lgth new value length * @param group group number of the Dicom Element to modify * @param element element number of the Dicom Element to modify */ -bool gdcmDocument::SetEntryByNumber(void *content, +bool gdcmDocument::SetEntryByNumber(uint8_t*content, int lgth, uint16_t group, uint16_t element) @@ -1000,7 +1023,7 @@ bool gdcmDocument::SetEntryByNumber(void *content, } */ gdcmBinEntry* a = (gdcmBinEntry *)TagHT[key]; - a->SetVoidArea(content); + a->SetBinArea(content); a->SetLength(lgth); a->SetValue(GDCM_BINLOADED); @@ -1062,7 +1085,7 @@ size_t gdcmDocument::GetEntryOffsetByNumber(uint16_t group, uint16_t elem) * @param elem element number of the Entry * @return Pointer to the 'non string' area */ -void * gdcmDocument::GetEntryVoidAreaByNumber(uint16_t group, uint16_t elem) +void* gdcmDocument::GetEntryBinAreaByNumber(uint16_t group, uint16_t elem) { gdcmDocEntry* entry = GetDocEntryByNumber(group, elem); if (!entry) @@ -1070,7 +1093,7 @@ void * gdcmDocument::GetEntryVoidAreaByNumber(uint16_t group, uint16_t elem) dbg.Verbose(1, "gdcmDocument::GetDocEntryByNumber: no entry"); return 0; } - return ((gdcmBinEntry *)entry)->GetVoidArea(); + return ((gdcmBinEntry *)entry)->GetBinArea(); } /** @@ -1079,7 +1102,7 @@ void * gdcmDocument::GetEntryVoidAreaByNumber(uint16_t group, uint16_t elem) * @param group group number of the Entry * @param elem element number of the Entry */ -void* gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem) +void* gdcmDocument::LoadEntryBinArea(uint16_t group, uint16_t elem) { gdcmDocEntry *docElement = GetDocEntryByNumber(group, elem); if ( !docElement ) @@ -1089,10 +1112,10 @@ void* gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem) size_t o =(size_t)docElement->GetOffset(); fseek(Fp, o, SEEK_SET); size_t l = docElement->GetLength(); - char* a = new char[l]; + uint8_t* a = new uint8_t[l]; if(!a) { - dbg.Verbose(0, "gdcmDocument::LoadEntryVoidArea cannot allocate a"); + dbg.Verbose(0, "gdcmDocument::LoadEntryBinArea cannot allocate a"); return NULL; } size_t l2 = fread(a, 1, l , Fp); @@ -1102,29 +1125,29 @@ void* gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem) return NULL; } /// \todo Drop any already existing void area! JPR - if( !SetEntryVoidAreaByNumber( a, group, elem ) ); + if( !SetEntryBinAreaByNumber( a, group, elem ) ); { - dbg.Verbose(0, "gdcmDocument::LoadEntryVoidArea setting failed."); + dbg.Verbose(0, "gdcmDocument::LoadEntryBinArea setting failed."); } return a; } /** * \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 binArea is going to be loaded */ -void *gdcmDocument::LoadEntryVoidArea(gdcmBinEntry *element) +void* gdcmDocument::LoadEntryBinArea(gdcmBinEntry* element) { size_t o =(size_t)element->GetOffset(); fseek(Fp, o, SEEK_SET); size_t l = element->GetLength(); - char* a = new char[l]; + uint8_t* a = new uint8_t[l]; if( !a ) { - dbg.Verbose(0, "gdcmDocument::LoadEntryVoidArea cannot allocate a"); + dbg.Verbose(0, "gdcmDocument::LoadEntryBinArea cannot allocate a"); return NULL; } - element->SetVoidArea((void *)a); + element->SetBinArea((uint8_t*)a); /// \todo check the result size_t l2 = fread(a, 1, l , Fp); if( l != l2 ) @@ -1143,7 +1166,7 @@ void *gdcmDocument::LoadEntryVoidArea(gdcmBinEntry *element) * @param element Element number of the searched Dicom Element * @return */ -bool gdcmDocument::SetEntryVoidAreaByNumber(void * area, +bool gdcmDocument::SetEntryBinAreaByNumber(uint8_t* area, uint16_t group, uint16_t element) { @@ -1154,7 +1177,7 @@ bool gdcmDocument::SetEntryVoidAreaByNumber(void * area, } if ( gdcmBinEntry* binEntry = dynamic_cast(currentEntry) ) { - binEntry->SetVoidArea( area ); + binEntry->SetBinArea( area ); return true; } return true; @@ -1354,14 +1377,12 @@ uint16_t gdcmDocument::UnswapShort(uint16_t a) * \brief Parses a DocEntrySet (Zero-level DocEntries or SQ Item DocEntries) * @return length of the parsed set. */ - -long gdcmDocument::ParseDES(gdcmDocEntrySet *set, +void gdcmDocument::ParseDES(gdcmDocEntrySet *set, long offset, long l_max, bool delim_mode) { gdcmDocEntry *newDocEntry = 0; - unsigned long l = 0; while (true) { @@ -1450,29 +1471,28 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, if (newDocEntry->GetGroup() == 0x7fe0 && newDocEntry->GetElement() == 0x0010 ) { - if (newDocEntry->GetReadLength()==0xffffffff) + if ( IsRLELossLessTransferSyntax() ) { - // Broken US.3405.1.dcm - Parse7FE0(); // to skip the pixels - // (multipart JPEG/RLE are trouble makers) + long PositionOnEntry = ftell(Fp); + fseek(Fp, newDocEntry->GetOffset(), SEEK_SET); + ComputeRLEInfo(); + fseek(Fp, PositionOnEntry, SEEK_SET); } else { SkipToNextDocEntry(newDocEntry); - l = newDocEntry->GetFullLength(); } } else { // to be sure we are at the beginning SkipToNextDocEntry(newDocEntry); - l = newDocEntry->GetFullLength(); } } else { // VR = "SQ" - l = newDocEntry->GetReadLength(); + unsigned long l = newDocEntry->GetReadLength(); if ( l != 0 ) // don't mess the delim_mode for zero-length sequence { if ( l == 0xffffffff ) @@ -1512,9 +1532,9 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, if ( l != 0 ) { // Don't try to parse zero-length sequences - (void)ParseSQ( newSeqEntry, - newDocEntry->GetOffset(), - l, delim_mode); + ParseSQ( newSeqEntry, + newDocEntry->GetOffset(), + l, delim_mode); } set->AddEntry( newSeqEntry ); if ( !delim_mode && (ftell(Fp)-offset) >= l_max) @@ -1524,14 +1544,13 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, } delete newDocEntry; } - return l; // Probably useless } /** * \brief Parses a Sequence ( SeqEntry after SeqEntry) * @return parsed length for this level */ -long gdcmDocument::ParseSQ( gdcmSeqEntry* seqEntry, +void gdcmDocument::ParseSQ( gdcmSeqEntry* seqEntry, long offset, long l_max, bool delim_mode) { int SQItemNumber = 0; @@ -1576,7 +1595,7 @@ long gdcmDocument::ParseSQ( gdcmSeqEntry* seqEntry, dlm_mod = false; } - (void)ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod); + ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod); seqEntry->AddEntry( itemSQ, SQItemNumber ); SQItemNumber++; @@ -1585,9 +1604,6 @@ long gdcmDocument::ParseSQ( gdcmSeqEntry* seqEntry, break; } } - - int lgth = ftell(Fp) - offset; - return lgth; } /** @@ -1664,7 +1680,7 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) { s << GDCM_BINLOADED; binEntryPtr->SetValue(s.str()); - LoadEntryVoidArea(binEntryPtr); // last one, not to erase length ! + LoadEntryBinArea(binEntryPtr); // last one, not to erase length ! return; } @@ -1769,17 +1785,19 @@ void gdcmDocument::FindDocEntryLength( gdcmDocEntry *entry ) { if ( vr == "OB" || vr == "OW" || vr == "SQ" || vr == "UN" ) { - // The following reserved two bytes (see PS 3.5-2001, section - // 7.1.2 Data element structure with explicit vr p27) must be + // The following reserved two bytes (see PS 3.5-2003, section + // "7.1.2 Data element structure with explicit vr", p 27) must be // skipped before proceeding on reading the length on 4 bytes. fseek(Fp, 2L, SEEK_CUR); uint32_t length32 = ReadInt32(); - if ( vr == "OB" && length32 == 0xffffffff ) + if ( (vr == "OB" || vr == "OW") && length32 == 0xffffffff ) { uint32_t lengthOB; try { + /// \todo rename that to FindDocEntryLengthOBOrOW since + /// the above test is on both OB and OW... lengthOB = FindDocEntryLengthOB(); } catch ( gdcmFormatUnexpected ) @@ -1934,7 +1952,7 @@ void gdcmDocument::FindDocEntryVR( gdcmDocEntry *entry ) */ bool gdcmDocument::CheckDocEntryVR(gdcmDocEntry *entry, gdcmVRKey vr) { - char msg[100]; // for sprintf + std::string msg; bool realExplicit = true; // Assume we are reading a falsely explicit VR file i.e. we reached @@ -1962,9 +1980,9 @@ bool gdcmDocument::CheckDocEntryVR(gdcmDocEntry *entry, gdcmVRKey vr) { // We thought this was explicit VR, but we end up with an // implicit VR tag. Let's backtrack. - sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n", - entry->GetGroup(), entry->GetElement()); - dbg.Verbose(1, "gdcmDocument::FindVR: ",msg); + msg = gdcmUtil::Format("Falsely explicit vr file (%04x,%04x)\n", + entry->GetGroup(), entry->GetElement()); + dbg.Verbose(1, "gdcmDocument::FindVR: ", msg.c_str()); if( entry->GetGroup() % 2 && entry->GetElement() == 0x0000) { @@ -2102,7 +2120,7 @@ std::string gdcmDocument::GetDocEntryUnvalue(gdcmDocEntry* entry) uint16_t newInt16; tokens.erase( tokens.begin(), tokens.end()); // clean any previous value - Tokenize (((gdcmValEntry *)entry)->GetValue(), tokens, "\\"); + gdcmUtil::Tokenize (((gdcmValEntry *)entry)->GetValue(), tokens, "\\"); for (unsigned int i=0; iGetValue(), tokens, "\\"); + gdcmUtil::Tokenize (((gdcmValEntry *)entry)->GetValue(), tokens, "\\"); for (unsigned int i=0; i 1) + // Deduce from both the RLE Header and the frameLength the + // fragment length, and again store this info in a + // gdcmRLEFramesInfo. + long rleSegmentLength[15]; + // skipping (not reading) RLE Segments + if ( nbRleSegments > 1) + { + for(unsigned int k = 1; k <= nbRleSegments-1; k++) { - for(unsigned int k = 1; k <= nbRleSegments-1; k++) - { - rleSegmentLength[k] = rleSegmentOffsetTable[k+1] - - rleSegmentOffsetTable[k]; - ftellRes = ftell(Fp); - SkipBytes(rleSegmentLength[k]); - } + rleSegmentLength[k] = rleSegmentOffsetTable[k+1] + - rleSegmentOffsetTable[k]; + SkipBytes(rleSegmentLength[k]); } + } - rleSegmentLength[nbRleSegments] = fragmentLength - - rleSegmentOffsetTable[nbRleSegments]; - ftellRes = ftell(Fp); - SkipBytes(rleSegmentLength[nbRleSegments]); - } + rleSegmentLength[nbRleSegments] = frameLength + - rleSegmentOffsetTable[nbRleSegments]; + SkipBytes(rleSegmentLength[nbRleSegments]); - // Make sure that at the end of the item we encounter a 'Sequence - // Delimiter Item': - if ( !ReadTag(0xfffe, 0xe0dd) ) - { - dbg.Verbose(0, "gdcmDocument::Parse7FE0: no sequence delimiter item"); - dbg.Verbose(0, " at end of RLE item sequence"); - } + // Store the collected info + gdcmRLEFrame* newFrameInfo = new gdcmRLEFrame; + newFrameInfo->NumberFragments = nbRleSegments; + for( unsigned int uk = 1; uk <= nbRleSegments; uk++ ) + { + newFrameInfo->Offset[uk] = frameOffset + rleSegmentOffsetTable[uk]; + newFrameInfo->Length[uk] = rleSegmentLength[uk]; + } + RLEInfo.Frames.push_back( newFrameInfo ); + } + + // Make sure that at the end of the item we encounter a 'Sequence + // Delimiter Item': + if ( !ReadTag(0xfffe, 0xe0dd) ) + { + dbg.Verbose(0, "gdcmDocument::ComputeRLEInfo: no sequence delimiter "); + dbg.Verbose(0, " item at end of RLE item sequence"); } }