X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmDocument.cxx;h=7fb1cbdb0888efa88a2bb394e214c725ca40fa8b;hb=e62d57cbd05c8e864fd2ba197a271cdcac9a2128;hp=10fd52f39b03b6b8e7301947ab205a10aa755220;hpb=ab62b1a706df8e2ab1fbf26fa5037d6c22293655;p=gdcm.git diff --git a/src/gdcmDocument.cxx b/src/gdcmDocument.cxx index 10fd52f3..7fb1cbdb 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/02 16:42:14 $ - Version: $Revision: 1.65 $ + Date: $Date: 2004/09/24 11:39:21 $ + Version: $Revision: 1.91 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -81,7 +81,7 @@ const unsigned int gdcmDocument::MAX_SIZE_PRINT_ELEMENT_VALUE = 0x7fffffff; /** * \brief constructor - * @param inFilename file to be opened for parsing + * @param filename file to be opened for parsing */ gdcmDocument::gdcmDocument( std::string const & filename ) : gdcmElementSet(-1) @@ -94,7 +94,7 @@ gdcmDocument::gdcmDocument( std::string const & filename ) { return; } - + dbg.Verbose(0, "gdcmDocument::gdcmDocument: starting parsing of file: ", Filename.c_str()); rewind(Fp); @@ -107,10 +107,7 @@ gdcmDocument::gdcmDocument( std::string const & filename ) long beg = ftell(Fp); lgt -= beg; - SQDepthLevel = 0; - - long l = ParseDES( this, beg, lgt, false); // le Load sera fait a la volee - (void)l; //is l used anywhere ? + (void)ParseDES( this, beg, lgt, false); // le Load sera fait a la volee rewind(Fp); @@ -119,17 +116,35 @@ gdcmDocument::gdcmDocument( std::string const & filename ) std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004); if( PhotometricInterpretation == "PALETTE COLOR " ) { - LoadEntryVoidArea(0x0028,0x1200); // gray LUT - LoadEntryVoidArea(0x0028,0x1201); // R LUT - LoadEntryVoidArea(0x0028,0x1202); // G LUT - LoadEntryVoidArea(0x0028,0x1203); // B 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 + /// dictionnary. BUT the semantics implied by the three following + /// lines state that the corresponding tag contents are in fact + /// the ones of a gdcmBinEntry. + /// In order to fix things "Quick and Dirty" the dictionnary was + /// altered on PURPOUS but now contains a WRONG value. + /// In order to fix things and restore the dictionary to its + /// correct value, one needs to decided of the semantics by deciding + /// wether the following tags are either: + /// - multivaluated US, and hence loaded as gdcmValEntry, but afterwards + /// 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. + LoadEntryBinArea(0x0028,0x1201); // R LUT + LoadEntryBinArea(0x0028,0x1202); // G LUT + LoadEntryBinArea(0x0028,0x1203); // B LUT - LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data - LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data - LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data + // Segmented Red Palette Color LUT Data + LoadEntryBinArea(0x0028,0x1221); + // Segmented Green Palette Color LUT Data + LoadEntryBinArea(0x0028,0x1222); + // Segmented Blue Palette Color LUT Data + LoadEntryBinArea(0x0028,0x1223); } //FIXME later : how to use it? - LoadEntryVoidArea(0x0028,0x3006); //LUT Data (CTX dependent) + LoadEntryBinArea(0x0028,0x3006); //LUT Data (CTX dependent) CloseFile(); @@ -184,7 +199,7 @@ gdcmDocument::~gdcmDocument () for (TagDocEntryHT::const_iterator it = TagHT.begin(); it != TagHT.end(); ++it ) { - delete it->second; + //delete it->second; //temp remove } TagHT.clear(); } @@ -215,7 +230,7 @@ void gdcmDocument::PrintShaDict(std::ostream & os) /** * \brief Get the public dictionary used */ -gdcmDict *gdcmDocument::GetPubDict() +gdcmDict* gdcmDocument::GetPubDict() { return RefPubDict; } @@ -223,7 +238,7 @@ gdcmDict *gdcmDocument::GetPubDict() /** * \brief Get the shadow dictionary used */ -gdcmDict *gdcmDocument::GetShaDict() +gdcmDict* gdcmDocument::GetShaDict() { return RefShaDict; } @@ -260,8 +275,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; } @@ -280,7 +293,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. @@ -525,13 +538,10 @@ bool gdcmDocument::CloseFile() void gdcmDocument::Write(FILE* fp,FileType filetype) { /// \todo move the following lines (and a lot of others, to be written) - /// to a future function CheckAndCorrectHeader - - /// WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA - /// no way (check : FileType est un champ de gdcmDocument ...) - /// a moins de se livrer a un tres complique ajout des champs manquants. - /// faire un CheckAndCorrectHeader (?) - + /// to a future function CheckAndCorrectHeader + /// (necessary if user wants to write a DICOM V3 file + /// starting from an ACR-NEMA (V2) gdcmHeader + if (filetype == gdcmImplicitVR) { std::string implicitVRTransfertSyntax = UI1_2_840_10008_1_2; @@ -577,25 +587,40 @@ 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 + * @param VR V(alue) R(epresentation) of the Entry -if private Entry- * \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 ) + uint16_t elem, + std::string const & VR ) { gdcmValEntry* valEntry = 0; - gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem); + if (!currentEntry) { - // The entry wasn't present and we simply create the required ValEntry: - currentEntry = NewDocEntryByNumber(group, elem); + // check if (group,element) DictEntry exists + // if it doesn't, create an entry in gdcmDictSet::VirtualEntry + // and use it + + // Find out if the tag we received is in the dictionaries: + gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict(); + gdcmDictEntry* dictEntry = pubDict->GetDictEntryByNumber(group, elem); + if (!dictEntry) + { + currentEntry = NewDocEntryByNumber(group, elem,VR); + } + else + { + currentEntry = NewDocEntryByNumber(group, elem); + } + if (!currentEntry) { dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: call to" @@ -612,7 +637,9 @@ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber( else { valEntry = dynamic_cast< gdcmValEntry* >(currentEntry); - if ( !valEntry ) + if ( !valEntry ) // Euuuuh? It wasn't a ValEntry + // then we change it to a ValEntry ? + // Shouldn't it be considered as an error ? { // We need to promote the gdcmDocEntry to a gdcmValEntry: valEntry = new gdcmValEntry(currentEntry); @@ -639,44 +666,117 @@ 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 ) +{ + gdcmBinEntry* binEntry = 0; + gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem); + if (!currentEntry) + { + + // check if (group,element) DictEntry exists + // if it doesn't, create an entry in gdcmDictSet::VirtualEntry + // and use it + + // Find out if the tag we received is in the dictionaries: + gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict(); + gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem); + + if (!dictEntry) + { + currentEntry = NewDocEntryByNumber(group, elem, VR); + } + else + { + currentEntry = NewDocEntryByNumber(group, elem); + } + if (!currentEntry) + { + dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: call to" + " NewDocEntryByNumber failed."); + return NULL; + } + binEntry = new gdcmBinEntry(currentEntry); + if ( !AddEntry(binEntry)) + { + dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: AddEntry" + " failed allthough this is a creation."); + } + } + else + { + binEntry = dynamic_cast< gdcmBinEntry* >(currentEntry); + if ( !binEntry ) // Euuuuh? It wasn't a BinEntry + // then we change it to a BinEntry ? + // Shouldn't it be considered as an error ? + { + // We need to promote the gdcmDocEntry to a gdcmBinEntry: + binEntry = new gdcmBinEntry(currentEntry); + if (!RemoveEntry(currentEntry)) + { + dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: removal" + " of previous DocEntry failed."); + return NULL; + } + if ( !AddEntry(binEntry)) + { + dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: adding" + " promoted BinEntry failed."); + return NULL; + } + } + } + + SetEntryByNumber(binArea, lgth, group, elem); + + return binEntry; +} + + +/* + * \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) { - gdcmBinEntry* b = 0; + gdcmSeqEntry* b = 0; gdcmDocEntry* a = GetDocEntryByNumber( group, elem); if (!a) { - a = NewBinEntryByNumber(group, elem); + a = NewSeqEntryByNumber(group, elem); if (!a) { return 0; } - b = new gdcmBinEntry(a); + b = new gdcmSeqEntry(a, 1); // FIXME : 1 (Depth) AddEntry(b); - b->SetVoidArea(voidArea); } - SetEntryByNumber(voidArea, lgth, group, elem); - //b->SetVoidArea(voidArea); //what if b == 0 !! - 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, @@ -694,9 +794,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); @@ -711,9 +811,9 @@ int 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; @@ -735,7 +835,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) @@ -748,7 +848,6 @@ std::string gdcmDocument::GetEntryVRByName(TagName const & tagName) return elem->GetVR(); } - /** * \brief Searches within Header Entries (Dicom Elements) parsed with * the public and private dictionaries @@ -837,10 +936,13 @@ 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) { + int c; + int l; + gdcmValEntry* valEntry = GetValEntryByNumber(group, element); if (!valEntry ) { @@ -849,28 +951,31 @@ bool gdcmDocument::SetEntryByNumber(std::string const & content, return false; } // Non even content must be padded with a space (020H)... - std::string evenContent = content; - if( evenContent.length() % 2 ) + std::string finalContent = content; + if( finalContent.length() % 2 ) { - evenContent += '\0'; // ... therefore we padd with (000H) .!?! + finalContent += '\0'; // ... therefore we padd with (000H) .!?! } - valEntry->SetValue(evenContent); + valEntry->SetValue(finalContent); // Integers have a special treatement for their length: - gdcmVRKey vr = valEntry->GetVR(); - if( vr == "US" || vr == "SS" ) - { - valEntry->SetLength(2); - } - else if( vr == "UL" || vr == "SL" ) - { - valEntry->SetLength(4); - } - else - { - valEntry->SetLength(evenContent.length()); - } + l = finalContent.length(); + if ( l != 0) // To avoid to be cheated by 'zero length' integers + { + gdcmVRKey vr = valEntry->GetVR(); + if( vr == "US" || vr == "SS" ) + { + c = CountSubstring(content, "\\") + 1; // for multivaluated items + l = c*2; + } + else if( vr == "UL" || vr == "SL" ) + { + c = CountSubstring(content, "\\") + 1; // for multivaluated items + l = c*4;; + } + } + valEntry->SetLength(l); return true; } @@ -878,12 +983,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) @@ -903,8 +1008,9 @@ bool gdcmDocument::SetEntryByNumber(void *content, } */ gdcmBinEntry* a = (gdcmBinEntry *)TagHT[key]; - a->SetVoidArea(content); - //a->SetLength(lgth); // ??? + a->SetBinArea(content); + a->SetLength(lgth); + a->SetValue(GDCM_BINLOADED); return true; } @@ -942,8 +1048,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) @@ -960,11 +1066,11 @@ 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) +void* gdcmDocument::GetEntryBinAreaByNumber(uint16_t group, uint16_t elem) { gdcmDocEntry* entry = GetDocEntryByNumber(group, elem); if (!entry) @@ -972,16 +1078,16 @@ 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(); } /** * \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) +void* gdcmDocument::LoadEntryBinArea(uint16_t group, uint16_t elem) { gdcmDocEntry *docElement = GetDocEntryByNumber(group, elem); if ( !docElement ) @@ -991,10 +1097,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); @@ -1004,27 +1110,29 @@ void *gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem) return NULL; } /// \todo Drop any already existing void area! JPR - SetEntryVoidAreaByNumber(a, group, elem); - + if( !SetEntryBinAreaByNumber( a, group, elem ) ); + { + 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 ) @@ -1043,19 +1151,20 @@ 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) { - gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element); - if ( !TagHT.count(key)) + gdcmDocEntry* currentEntry = GetDocEntryByNumber(group, element); + if ( !currentEntry ) { return false; } - - // This was for multimap ? - (( gdcmBinEntry *)( ((TagHT.equal_range(key)).first)->second ))->SetVoidArea(area); - + if ( gdcmBinEntry* binEntry = dynamic_cast(currentEntry) ) + { + binEntry->SetBinArea( area ); + return true; + } return true; } @@ -1207,7 +1316,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; } @@ -1260,10 +1369,8 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, bool delim_mode) { gdcmDocEntry *newDocEntry = 0; - gdcmValEntry *newValEntry = 0; unsigned long l = 0; - int depth = set->GetDepthLevel(); while (true) { if ( !delim_mode && (ftell(Fp)-offset) >= l_max) @@ -1282,12 +1389,29 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, if ( gdcmGlobal::GetVR()->IsVROfGdcmStringRepresentable(vr) ) { - /////// ValEntry - newValEntry = new gdcmValEntry(newDocEntry->GetDictEntry()); - newValEntry->Copy(newDocEntry); - newValEntry->SetDepthLevel(depth); - set->AddEntry(newValEntry); - LoadDocEntry(newValEntry); + /////////////////////// ValEntry + gdcmValEntry* newValEntry = + new gdcmValEntry( newDocEntry->GetDictEntry() ); + newValEntry->Copy( newDocEntry ); + + // When "set" is a gdcmDocument, then we are at the top of the + // hierarchy and the Key is simply of the form ( group, elem )... + if (gdcmDocument* dummy = dynamic_cast< gdcmDocument* > ( set ) ) + { + (void)dummy; + newValEntry->SetKey( newValEntry->GetKey() ); + } + // ...but when "set" is a gdcmSQItem, we are inserting this new + // valEntry in a sequence item. Hence the key has the + // generalized form (refer to \ref gdcmBaseTagKey): + if (gdcmSQItem* parentSQItem = dynamic_cast< gdcmSQItem* > ( set ) ) + { + newValEntry->SetKey( parentSQItem->GetBaseTagKey() + + newValEntry->GetKey() ); + } + + set->AddEntry( newValEntry ); + LoadDocEntry( newValEntry ); if (newValEntry->IsItemDelimitor()) { break; @@ -1306,11 +1430,29 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, "nor BinEntry. Probably unknown VR."); } - ////// BinEntry or UNKOWN VR: - gdcmBinEntry *bn = new gdcmBinEntry(newDocEntry->GetDictEntry()); - bn->Copy(newDocEntry); - set->AddEntry(bn); - LoadDocEntry(bn); + //////////////////// BinEntry or UNKOWN VR: + gdcmBinEntry* newBinEntry = + new gdcmBinEntry( newDocEntry->GetDictEntry() ); + newBinEntry->Copy( newDocEntry ); + + // When "this" is a gdcmDocument the Key is simply of the + // form ( group, elem )... + if (gdcmDocument* dummy = dynamic_cast< gdcmDocument* > ( set ) ) + { + (void)dummy; + newBinEntry->SetKey( newBinEntry->GetKey() ); + } + // but when "this" is a SQItem, we are inserting this new + // valEntry in a sequence item, and the kay has the + // generalized form (refer to \ref gdcmBaseTagKey): + if (gdcmSQItem* parentSQItem = dynamic_cast< gdcmSQItem* > ( set ) ) + { + newBinEntry->SetKey( parentSQItem->GetBaseTagKey() + + newBinEntry->GetKey() ); + } + + set->AddEntry( newBinEntry ); + LoadDocEntry( newBinEntry ); } if (newDocEntry->GetGroup() == 0x7fe0 && @@ -1351,20 +1493,38 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, } } // no other way to create it ... - gdcmSeqEntry *sq = new gdcmSeqEntry(newDocEntry->GetDictEntry(), - set->GetDepthLevel()); - sq->Copy(newDocEntry); - sq->SetDelimitorMode(delim_mode); - sq->SetDepthLevel(depth); + gdcmSeqEntry* newSeqEntry = + new gdcmSeqEntry( newDocEntry->GetDictEntry() ); + newSeqEntry->Copy( newDocEntry ); + newSeqEntry->SetDelimitorMode( delim_mode ); + + // At the top of the hierarchy, stands a gdcmDocument. When "set" + // is a gdcmDocument, then we are building the first depth level. + // Hence the gdcmSeqEntry we are building simply has a depth + // level of one: + if (gdcmDocument* dummy = dynamic_cast< gdcmDocument* > ( set ) ) + { + (void)dummy; + newSeqEntry->SetDepthLevel( 1 ); + newSeqEntry->SetKey( newSeqEntry->GetKey() ); + } + // But when "set" is allready a SQItem, we are building a nested + // sequence, and hence the depth level of the new gdcmSeqEntry + // we are building, is one level deeper: + if (gdcmSQItem* parentSQItem = dynamic_cast< gdcmSQItem* > ( set ) ) + { + newSeqEntry->SetDepthLevel( parentSQItem->GetDepthLevel() + 1 ); + newSeqEntry->SetKey( parentSQItem->GetBaseTagKey() + + newSeqEntry->GetKey() ); + } if ( l != 0 ) { // Don't try to parse zero-length sequences - long lgt = ParseSQ( sq, - newDocEntry->GetOffset(), - l, delim_mode); - (void)lgt; //not used... + (void)ParseSQ( newSeqEntry, + newDocEntry->GetOffset(), + l, delim_mode); } - set->AddEntry(sq); + set->AddEntry( newSeqEntry ); if ( !delim_mode && (ftell(Fp)-offset) >= l_max) { break; @@ -1372,7 +1532,6 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, } delete newDocEntry; } - return l; // Probably useless } @@ -1380,17 +1539,15 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set, * \brief Parses a Sequence ( SeqEntry after SeqEntry) * @return parsed length for this level */ -long gdcmDocument::ParseSQ(gdcmSeqEntry *set, - long offset, long l_max, bool delim_mode) +long gdcmDocument::ParseSQ( gdcmSeqEntry* seqEntry, + long offset, long l_max, bool delim_mode) { int SQItemNumber = 0; bool dlm_mod; - //int depth = set->GetDepthLevel(); - //(void)depth; //not used while (true) { - gdcmDocEntry *newDocEntry = ReadNextDocEntry(); + gdcmDocEntry* newDocEntry = ReadNextDocEntry(); if ( !newDocEntry ) { // FIXME Should warn user @@ -1400,7 +1557,7 @@ long gdcmDocument::ParseSQ(gdcmSeqEntry *set, { if ( newDocEntry->IsSequenceDelimitor() ) { - set->SetSequenceDelimitationItem( newDocEntry ); + seqEntry->SetSequenceDelimitationItem( newDocEntry ); break; } } @@ -1409,8 +1566,13 @@ long gdcmDocument::ParseSQ(gdcmSeqEntry *set, break; } - gdcmSQItem *itemSQ = new gdcmSQItem(set->GetDepthLevel()); - itemSQ->AddEntry(newDocEntry); + gdcmSQItem *itemSQ = new gdcmSQItem( seqEntry->GetDepthLevel() ); + std::ostringstream newBase; + newBase << seqEntry->GetKey() + << "/" + << SQItemNumber + << "#"; + itemSQ->SetBaseTagKey( newBase.str() ); unsigned int l = newDocEntry->GetReadLength(); if ( l == 0xffffffff ) @@ -1422,12 +1584,11 @@ long gdcmDocument::ParseSQ(gdcmSeqEntry *set, dlm_mod = false; } - int lgr = ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod); - (void)lgr; //FIXME not used + (void)ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod); - set->AddEntry(itemSQ, SQItemNumber); + seqEntry->AddEntry( itemSQ, SQItemNumber ); SQItemNumber++; - if ( !delim_mode && (ftell(Fp)-offset) >= l_max) + if ( !delim_mode && ( ftell(Fp) - offset ) >= l_max ) { break; } @@ -1440,7 +1601,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) { @@ -1476,44 +1637,42 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) if (length > MaxSizeLoadEntry) { if (gdcmBinEntry* binEntryPtr = dynamic_cast< gdcmBinEntry* >(entry) ) - { - s << "gdcm::NotLoaded (BinEntry)"; + { + //s << "gdcm::NotLoaded (BinEntry)"; + s << GDCM_NOTLOADED; s << " Address:" << (long)entry->GetOffset(); s << " Length:" << entry->GetLength(); s << " x(" << std::hex << entry->GetLength() << ")"; binEntryPtr->SetValue(s.str()); } - // to be sure we are at the end of the value ... - fseek(Fp, (long)entry->GetOffset()+(long)entry->GetLength(), SEEK_SET); - // 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) ) + else if (gdcmValEntry* valEntryPtr = dynamic_cast< gdcmValEntry* >(entry) ) { - s << "gdcm::NotLoaded. (ValEntry)"; + // s << "gdcm::NotLoaded. (ValEntry)"; + s << GDCM_NOTLOADED; s << " Address:" << (long)entry->GetOffset(); s << " Length:" << entry->GetLength(); s << " x(" << std::hex << entry->GetLength() << ")"; valEntryPtr->SetValue(s.str()); } + else + { + // fusible + std::cout<< "MaxSizeLoadEntry exceeded, neither a BinEntry " + << "nor a ValEntry ?! Should never print that !" << std::endl; + } + // to be sure we are at the end of the value ... fseek(Fp,(long)entry->GetOffset()+(long)entry->GetLength(),SEEK_SET); - return; } // When we find a BinEntry not very much can be done : if (gdcmBinEntry* binEntryPtr = dynamic_cast< gdcmBinEntry* >(entry) ) { - - LoadEntryVoidArea(binEntryPtr); - s << "gdcm::Loaded (BinEntry)"; + s << GDCM_BINLOADED; binEntryPtr->SetValue(s.str()); + LoadEntryBinArea(binEntryPtr); // last one, not to erase length ! return; } @@ -1521,7 +1680,6 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) if ( IsDocEntryAnInteger(entry) ) { uint32_t NewInt; - std::ostringstream s; 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 @@ -1582,7 +1740,7 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) { dbg.Verbose(1, "gdcmDocument::LoadDocEntry", "unread element value"); - valEntry->SetValue("gdcm::UnRead"); + valEntry->SetValue(GDCM_UNREAD); return; } @@ -1606,15 +1764,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 ) throw ( gdcmFormatError ) { uint16_t element = entry->GetElement(); std::string vr = entry->GetVR(); - uint16_t length16; - + uint16_t length16; if ( Filetype == gdcmExplicitVR && !entry->IsImplicitVR() ) { @@ -1735,7 +1892,7 @@ void gdcmDocument::FindDocEntryLength( gdcmDocEntry *entry ) /** * \brief Find the Value Representation of the current Dicom Element. - * @param Entry + * @param entry */ void gdcmDocument::FindDocEntryVR( gdcmDocEntry *entry ) { @@ -1778,7 +1935,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 @@ -1861,7 +2018,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) @@ -1937,7 +2094,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) @@ -1991,7 +2148,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) { @@ -2001,7 +2158,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) { @@ -2013,6 +2170,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) @@ -2090,7 +2249,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) @@ -2477,7 +2636,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) { @@ -2497,7 +2656,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) { @@ -2537,7 +2696,7 @@ gdcmDocEntry* gdcmDocument::ReadNextDocEntry() { // We reached the EOF (or an error occured) therefore // header parsing has to be considered as finished. - std::cout << e; + //std::cout << e; return 0; } @@ -2551,7 +2710,7 @@ gdcmDocEntry* gdcmDocument::ReadNextDocEntry() catch ( gdcmFormatError e ) { // Call it quits - std::cout << e; + //std::cout << e; delete newEntry; return 0; } @@ -2582,16 +2741,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) @@ -2622,16 +2781,16 @@ 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. */ @@ -2661,7 +2820,6 @@ uint32_t gdcmDocument::ReadTagLength(uint16_t testGroup, uint16_t testElement) /** * \brief Parse pixel data from disk for multi-fragment Jpeg/Rle files * No other way so 'skip' the Data - * */ void gdcmDocument::Parse7FE0 () { @@ -2763,6 +2921,97 @@ void gdcmDocument::Parse7FE0 () } } +/** + * \brief Walk recursively the given \ref gdcmDocEntrySet, and feed + * the given hash table (\ref TagDocEntryHT) with all the + * \ref gdcmDocEntry (Dicom entries) encountered. + * This method does the job for \ref BuildFlatHashTable. + * @param builtHT Where to collect all the \ref gdcmDocEntry encountered + * when recursively walking the given set. + * @param set The structure to be traversed (recursively). + */ +void gdcmDocument::BuildFlatHashTableRecurse( TagDocEntryHT& builtHT, + gdcmDocEntrySet* set ) +{ + if (gdcmElementSet* elementSet = dynamic_cast< gdcmElementSet* > ( set ) ) + { + TagDocEntryHT* currentHT = elementSet->GetTagHT(); + for( TagDocEntryHT::const_iterator i = currentHT->begin(); + i != currentHT->end(); + ++i) + { + gdcmDocEntry* entry = i->second; + if ( gdcmSeqEntry* seqEntry = dynamic_cast(entry) ) + { + ListSQItem& items = seqEntry->GetSQItems(); + for( ListSQItem::const_iterator item = items.begin(); + item != items.end(); + ++item) + { + BuildFlatHashTableRecurse( builtHT, *item ); + } + continue; + } + builtHT[entry->GetKey()] = entry; + } + return; + } + + if (gdcmSQItem* SQItemSet = dynamic_cast< gdcmSQItem* > ( set ) ) + { + ListDocEntry& currentList = SQItemSet->GetDocEntries(); + for (ListDocEntry::iterator i = currentList.begin(); + i != currentList.end(); + ++i) + { + gdcmDocEntry* entry = *i; + if ( gdcmSeqEntry* seqEntry = dynamic_cast(entry) ) + { + ListSQItem& items = seqEntry->GetSQItems(); + for( ListSQItem::const_iterator item = items.begin(); + item != items.end(); + ++item) + { + BuildFlatHashTableRecurse( builtHT, *item ); + } + continue; + } + builtHT[entry->GetKey()] = entry; + } + + } +} + +/** + * \brief Build a \ref TagDocEntryHT (i.e. a std::map<>) from the current + * gdcmDocument. + * + * The structure used by a gdcmDocument (through \ref gdcmElementSet), + * in order to old the parsed entries of a Dicom header, is a recursive + * one. This is due to the fact that the sequences (when present) + * can be nested. Additionaly, the sequence items (represented in + * gdcm as \ref gdcmSQItem) add an extra complexity to the data + * structure. Hence, a gdcm user whishing to visit all the entries of + * a Dicom header will need to dig in the gdcm internals (which + * implies exposing all the internal data structures to the API). + * In order to avoid this burden to the user, \ref BuildFlatHashTable + * recursively builds a temporary hash table, which holds all the + * Dicom entries in a flat structure (a \ref TagDocEntryHT i.e. a + * std::map<>). + * \warning Of course there is NO integrity constrain between the + * returned \ref TagDocEntryHT and the \ref gdcmElementSet used + * to build it. Hence if the underlying \ref gdcmElementSet is + * altered, then it is the caller responsability to invoke + * \ref BuildFlatHashTable again... + * @return The flat std::map<> we juste build. + */ +TagDocEntryHT* gdcmDocument::BuildFlatHashTable() +{ + TagDocEntryHT* FlatHT = new TagDocEntryHT; + BuildFlatHashTableRecurse( *FlatHT, this ); + return FlatHT; +} + /** @@ -2781,7 +3030,7 @@ bool gdcmDocument::operator<(gdcmDocument &document) { return true; } - else if(s1 > s2) + else if( s1 > s2 ) { return false; } @@ -2796,7 +3045,7 @@ bool gdcmDocument::operator<(gdcmDocument &document) } else if ( s1 > s2 ) { - return true; + return false; } else { @@ -2815,7 +3064,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;