]> Creatis software - gdcm.git/blobdiff - src/gdcmDocument.cxx
* ENH: added some utility method that builds a flat dictionnary
[gdcm.git] / src / gdcmDocument.cxx
index 7ab750eca46dfc0d5d2b30e5a4bc5def20170afa..16adce6fa9f73eb7c9a63c7058b7094c6276c464 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDocument.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/09/03 15:11:35 $
-  Version:   $Revision: 1.70 $
+  Date:      $Date: 2004/09/17 13:11:16 $
+  Version:   $Revision: 1.81 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -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);
    
@@ -120,13 +117,31 @@ gdcmDocument::gdcmDocument( std::string const & filename )
    if( PhotometricInterpretation == "PALETTE COLOR " )
    {
       LoadEntryVoidArea(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.
       LoadEntryVoidArea(0x0028,0x1201);  // R    LUT
       LoadEntryVoidArea(0x0028,0x1202);  // G    LUT
       LoadEntryVoidArea(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
+      LoadEntryVoidArea(0x0028,0x1221);
+      // Segmented Green Palette Color LUT Data
+      LoadEntryVoidArea(0x0028,0x1222);
+      // Segmented Blue  Palette Color LUT Data
+      LoadEntryVoidArea(0x0028,0x1223);
    } 
    //FIXME later : how to use it?
    LoadEntryVoidArea(0x0028,0x3006);  //LUT Data (CTX dependent) 
@@ -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();
 }
@@ -581,15 +596,16 @@ void gdcmDocument::Write(FILE* fp,FileType filetype)
  * \return  pointer to the modified/created Header Entry (NULL when creation
  *          failed).
  */
-  
+
+/*  
 gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
                                          std::string const & value, 
                                          uint16_t group, 
                                          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:
@@ -610,7 +626,95 @@ 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);
+         if (!RemoveEntry(currentEntry))
+         {
+            dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: removal"
+                           " of previous DocEntry failed.");
+            return NULL;
+         }
+         if ( !AddEntry(valEntry))
+         {
+            dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: adding"
+                           " promoted ValEntry failed.");
+            return NULL;
+         }
+      }
+   }
+
+   SetEntryByNumber(value, group, elem);
+
+   return valEntry;
+}   
+*/
+
+/**
+ * \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   VR  V(alue) R(epresentation) of the Entry -if private Entry-
+ * \return  pointer to the modified/created Header Entry (NULL when creation
+ *          failed).
+ */
+ // TODO : write something clever, using default value for VR
+ //        to avoid code duplication
+ //        (I don't know how to tell NewDocEntryByNumber
+ //         that ReplaceOrCreateByNumber  was called with a default value)
+gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
+                                         std::string const & value, 
+                                         uint16_t group, 
+                                         uint16_t elem,
+                                         std::string const & VR )
+{
+   gdcmValEntry* valEntry = 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;
+      }
+      valEntry = new gdcmValEntry(currentEntry);
+      if ( !AddEntry(valEntry))
+      {
+         dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: AddEntry"
+                        " failed allthough this is a creation.");
+      }
+   }
+   else
+   {
+      valEntry = dynamic_cast< gdcmValEntry* >(currentEntry);
+      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);
@@ -647,26 +751,70 @@ gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber(
                                          void *voidArea,
                                          int lgth, 
                                          uint16_t group, 
-                                         uint16_t elem)
+                                         uint16_t elem,
+                                         std::string const & VR )
 {
-   gdcmBinEntry* b = 0;
-   gdcmDocEntry* a = GetDocEntryByNumber( group, elem);
-   if (!a)
+   gdcmBinEntry* binEntry = 0;
+   gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem);
+   if (!currentEntry)
    {
-      a = NewBinEntryByNumber(group, elem);
-      if (!a)
+
+      // 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)
       {
-         return 0;
+         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;
+         }
+      }
+   }
 
-      b = new gdcmBinEntry(a);
-      AddEntry(b);
-      b->SetVoidArea(voidArea);
-   }   
    SetEntryByNumber(voidArea, lgth, group, elem);
-   //b->SetVoidArea(voidArea);  //what if b == 0 !!
 
-   return b;
+   return binEntry;
 }  
 
 
@@ -868,6 +1016,9 @@ bool gdcmDocument::SetEntryByNumber(std::string const & content,
                                     uint16_t group,
                                     uint16_t element) 
 {
+   int c;
+   int l;
+
    gdcmValEntry* valEntry = GetValEntryByNumber(group, element);
    if (!valEntry )
    {
@@ -876,28 +1027,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;
 } 
 
@@ -931,7 +1085,7 @@ bool gdcmDocument::SetEntryByNumber(void *content,
 */      
    gdcmBinEntry* a = (gdcmBinEntry *)TagHT[key];           
    a->SetVoidArea(content);  
-   //a->SetLength(lgth);  // ???  
+   a->SetLength(lgth);
 
    return true;
 } 
@@ -1008,7 +1162,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)
+voidgdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem)
 {
    gdcmDocEntry *docElement = GetDocEntryByNumber(group, elem);
    if ( !docElement )
@@ -1031,7 +1185,10 @@ void *gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem)
       return NULL;
    }
    /// \todo Drop any already existing void area! JPR
-   SetEntryVoidAreaByNumber(a, group, elem);
+   if( !SetEntryVoidAreaByNumber( a, group, elem ) );
+   {
+      dbg.Verbose(0, "gdcmDocument::LoadEntryVoidArea setting failed.");
+   }
 
    return a;
 }
@@ -1074,15 +1231,16 @@ bool gdcmDocument::SetEntryVoidAreaByNumber(void * 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<gdcmBinEntry*>(currentEntry) )
+   {
+      binEntry->SetVoidArea( area );
+      return true;
+   }
    return true;
 }
 
@@ -1287,10 +1445,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)
@@ -1309,12 +1465,28 @@ 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 ) )
+            {
+               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;
@@ -1333,11 +1505,28 @@ 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 ) )
+            {
+               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 && 
@@ -1378,20 +1567,37 @@ 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 ) )
+         {
+            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;
@@ -1406,17 +1612,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();   
+      gdcmDocEntrynewDocEntry = ReadNextDocEntry();   
       if ( !newDocEntry )
       {
          // FIXME Should warn user
@@ -1426,7 +1630,7 @@ long gdcmDocument::ParseSQ(gdcmSeqEntry *set,
       {
          if ( newDocEntry->IsSequenceDelimitor() )
          {
-            set->SetSequenceDelimitationItem( newDocEntry );
+            seqEntry->SetSequenceDelimitationItem( newDocEntry );
             break;
          }
       }
@@ -1435,8 +1639,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 )
@@ -1448,12 +1657,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;
       }
@@ -1502,44 +1710,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)";
       binEntryPtr->SetValue(s.str());
+      LoadEntryVoidArea(binEntryPtr); // last one, not to erase length !
       return;
    }
     
@@ -1547,7 +1753,6 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry)
    if ( IsDocEntryAnInteger(entry) )
    {   
       uint32_t NewInt;
-      //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
@@ -1608,7 +1813,7 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry)
       {
          dbg.Verbose(1, "gdcmDocument::LoadDocEntry",
                         "unread element value");
-         valEntry->SetValue("gdcm::UnRead");
+         valEntry->SetValue(GDCM_UNREAD);
          return;
       }
 
@@ -2688,7 +2893,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 ()
 {
@@ -2790,6 +2994,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<gdcmSeqEntry*>(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<gdcmSeqEntry*>(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 olds 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 gdcmElemenSet used
+ *        to build it. Hence if the underlying \ref gdcmElemenSet 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;
+}
+
 
 
 /**