]> Creatis software - gdcm.git/blobdiff - src/gdcmDocument.cxx
ENH: Slightly bigger patch:
[gdcm.git] / src / gdcmDocument.cxx
index f2c4fb9585f8350f347c00480f1800ccf41e05c9..821b7027da0546aae3dbf59c3cd3228320ed2b99 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDocument.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/11/10 16:13:18 $
-  Version:   $Revision: 1.120 $
+  Date:      $Date: 2004/11/16 02:54:35 $
+  Version:   $Revision: 1.124 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -186,6 +186,8 @@ Document::Document() : ElementSet(-1)
 
    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
    Initialise();
+   SwapCode = 0;
+   Filetype = ExplicitVR;
    PrintLevel = 1;  // 'Medium' print level by default
 }
 
@@ -416,6 +418,7 @@ FileType Document::GetFileType()
  */
 std::ifstream* Document::OpenFile()
 {
+   if (Filename.length() == 0) return 0;
    if(Fp)
    {
       dbg.Verbose( 0,
@@ -1041,13 +1044,13 @@ void*  Document::GetEntryBinAreaByNumber(uint16_t group, uint16_t elem)
  * @param group   group number of the Entry 
  * @param elem  element number of the Entry
  */
-void* Document::LoadEntryBinArea(uint16_t group, uint16_t elem)
+void Document::LoadEntryBinArea(uint16_t group, uint16_t elem)
 {
+   // Search the corresponding DocEntry
    DocEntry *docElement = GetDocEntryByNumber(group, elem);
    if ( !docElement )
-   {
-      return NULL;
-   }
+      return;
+
    size_t o =(size_t)docElement->GetOffset();
    Fp->seekg( o, std::ios_base::beg);
    size_t l = docElement->GetLength();
@@ -1055,27 +1058,30 @@ void* Document::LoadEntryBinArea(uint16_t group, uint16_t elem)
    if(!a)
    {
       dbg.Verbose(0, "Document::LoadEntryBinArea cannot allocate a");
-      return NULL;
+      return;
    }
+
+   // Read the value
    Fp->read((char*)a, l);
    if( Fp->fail() || Fp->eof() )//Fp->gcount() == 1
    {
       delete[] a;
-      return NULL;
+      return;
    }
-  /// \todo Drop any already existing void area! JPR
+
+   // Set the value to the DocEntry
    if( !SetEntryBinAreaByNumber( a, group, elem ) )
    {
+      delete[] a;
       dbg.Verbose(0, "Document::LoadEntryBinArea setting failed.");
    }
-   return a;
 }
 /**
  * \brief         Loads (from disk) the element content 
  *                when a string is not suitable
  * @param element  Entry whose binArea is going to be loaded
  */
-void* Document::LoadEntryBinArea(BinEntry* element) 
+void Document::LoadEntryBinArea(BinEntry* element) 
 {
    size_t o =(size_t)element->GetOffset();
    Fp->seekg(o, std::ios_base::beg);
@@ -1084,18 +1090,18 @@ void* Document::LoadEntryBinArea(BinEntry* element)
    if( !a )
    {
       dbg.Verbose(0, "Document::LoadEntryBinArea cannot allocate a");
-      return NULL;
+      return;
    }
-   element->SetBinArea((uint8_t*)a);
+
    /// \todo check the result 
    Fp->read((char*)a, l);
    if( Fp->fail() || Fp->eof()) //Fp->gcount() == 1
    {
       delete[] a;
-      return NULL;
+      return;
    }
 
-   return a;
+   element->SetBinArea((uint8_t*)a);
 }
 
 /**
@@ -1113,12 +1119,14 @@ bool Document::SetEntryBinAreaByNumber(uint8_t* area,
    {
       return false;
    }
+
    if ( BinEntry* binEntry = dynamic_cast<BinEntry*>(currentEntry) )
    {
       binEntry->SetBinArea( area );
       return true;
    }
-   return true;
+
+   return false;
 }
 
 /**
@@ -1338,7 +1346,7 @@ void Document::ParseDES(DocEntrySet *set, long offset,
          {
          /////////////////////// ValEntry
             ValEntry* newValEntry =
-               new ValEntry( newDocEntry->GetDictEntry() );
+               new ValEntry( newDocEntry->GetDictEntry() ); //LEAK
             newValEntry->Copy( newDocEntry );
              
             // When "set" is a Document, then we are at the top of the
@@ -1357,7 +1365,11 @@ void Document::ParseDES(DocEntrySet *set, long offset,
                                    + newValEntry->GetKey() );
             }
              
-            set->AddEntry( newValEntry );
+            if( !set->AddEntry( newValEntry ) )
+            {
+              // If here expect big troubles
+              delete newValEntry; //otherwise mem leak
+            }
             LoadDocEntry( newValEntry );
             if (newValEntry->IsItemDelimitor())
             {
@@ -1398,7 +1410,11 @@ void Document::ParseDES(DocEntrySet *set, long offset,
                                    + newBinEntry->GetKey() );
             }
 
-            set->AddEntry( newBinEntry );
+            if( !set->AddEntry( newBinEntry ) )
+            {
+              //Expect big troubles if here
+              delete newBinEntry;
+            }
             LoadDocEntry( newBinEntry );
          }
 
@@ -1588,7 +1604,7 @@ void Document::LoadDocEntry(DocEntry* entry)
          s << " x(" << std::hex << entry->GetLength() << ")";
          binEntryPtr->SetValue(s.str());
       }
-       // Be carefull : a BinEntry IS_A ValEntry ... 
+      // Be carefull : a BinEntry IS_A ValEntry ... 
       else if (ValEntry* valEntryPtr = dynamic_cast< ValEntry* >(entry) )
       {
         // s << "gdcm::NotLoaded. (ValEntry)";
@@ -2634,6 +2650,35 @@ void Document::SetMaxSizePrintEntry(long newSize)
 
 
 
+/**
+ * \brief   Handle broken private tag from Philips NTSCAN
+ *          where the endianess is being switch to BigEndian for no
+ *          apparent reason
+ * @return  no return
+ */
+void Document::HandleBrokenEndian(uint16_t group, uint16_t elem)
+{
+   // 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))
+   {
+     // start endian swap mark for group found
+     reversedEndian++;
+     SwitchSwapToBigEndian();
+     // fix the tag
+     group = 0xfffe;
+     elem = 0xe000;
+   } 
+   else if ((group == 0xfffe) && (elem == 0xe00d) && reversedEndian) 
+   {
+     // end of reversed endian group
+     reversedEndian--;
+     SwitchSwapToBigEndian();
+   }
+
+}
+
 /**
  * \brief   Read the next tag but WITHOUT loading it's value
  *          (read the 'Group Number', the 'Element Number',
@@ -2659,6 +2704,7 @@ DocEntry* Document::ReadNextDocEntry()
       return 0;
    }
 
+   HandleBrokenEndian(group, elem);
    DocEntry *newEntry = NewDocEntryByNumber(group, elem);
    FindDocEntryVR(newEntry);