]> Creatis software - gdcm.git/blobdiff - src/gdcmDocument.cxx
* Remove memory leaks
[gdcm.git] / src / gdcmDocument.cxx
index 958dfc887a4e22b39c273b836c79c9cc465ca709..d975fd54d4c2dcd30375f0153c1641eff50ffd51 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDocument.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/10/18 14:37:18 $
-  Version:   $Revision: 1.295 $
+  Date:      $Date: 2005/10/25 09:22:15 $
+  Version:   $Revision: 1.305 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -127,7 +127,7 @@ bool Document::DoTheLoadingDocumentJob(  )
 
    Group0002Parsed = false;
 
-   gdcmWarningMacro( "Starting parsing of file: " << Filename.c_str());
+   gdcmDebugMacro( "Starting parsing of file: " << Filename.c_str());
 
    Fp->seekg(0, std::ios::end);
    long lgt = Fp->tellg();       // total length of the file
@@ -277,7 +277,7 @@ bool Document::DoTheLoadingDocumentJob(  )
  */
 void Document::AddForceLoadElement (uint16_t group, uint16_t elem) 
 { 
-   Element el;
+   DicomElement el;
    el.Group = group;
    el.Elem  = elem;
    UserForceLoadList.push_back(el); 
@@ -604,7 +604,8 @@ void Document::WriteContent(std::ofstream *fp, FileType filetype)
 {
    // Skip if user wants to write an ACR-NEMA file
 
-   if ( filetype == ImplicitVR || filetype == ExplicitVR )
+   if ( filetype == ImplicitVR || filetype == ExplicitVR ||
+     filetype == JPEG )
    {
       // writing Dicom File Preamble
       char filePreamble[128];
@@ -659,7 +660,7 @@ void Document::LoadEntryBinArea(uint16_t group, uint16_t elem)
 /**
  * \brief Loads (from disk) the element content 
  *        when a string is not suitable
- * @param elem  Entry whose binArea is going to be loaded
+ * @param entry  Entry whose binArea is going to be loaded
  */
 void Document::LoadEntryBinArea(DataEntry *entry) 
 {
@@ -683,7 +684,7 @@ void Document::LoadEntryBinArea(DataEntry *entry)
       return;
    }
 
-   // Read the datas
+   // Read the data
    Fp->read((char*)data, l);
    if ( Fp->fail() || Fp->eof() )
    {
@@ -717,7 +718,7 @@ void Document::LoadEntryBinArea(DataEntry *entry)
       }
       case 8:
       {
-         gdcmWarningMacro("Can't swap 64 bits datas");
+         gdcmWarningMacro("Can't swap 64 bits data");
 /*         uint64_t *data64 = (uint64_t *)data;
          for(i=0;i<l/vrLgth;i++)
             data64[i] = SwapLongLong(data64[i]);*/
@@ -902,7 +903,7 @@ int Document::ComputeGroup0002Length( )
             // (no SQ, OW, UT in group 0x0002;)
                if ( vr == "OB" ) 
                {
-                  // explicit VR AND OB, OW, SQ, UT : 4 more bytes
+                  // explicit VR AND (OB, OW, SQ, UT) : 4 more bytes
                   groupLength +=  4;
                }
             //}
@@ -997,16 +998,22 @@ void Document::ParseDES(DocEntrySet *set, long offset,
       if ( newDataEntry )  
       {
          //////////////////////////// DataEntry
          vr = newDocEntry->GetVR();
+         // Useless checking, now !
+         /*
          if ( Filetype == ExplicitVR && 
                !Global::GetVR()->IsVROfBinaryRepresentable(vr) )
          { 
                ////// No DataEntry: should mean UNKOWN VR
                gdcmWarningMacro( std::hex << newDocEntry->GetGroup() 
                                  << "|" << newDocEntry->GetElement()
-                                 << " : No DataEntry." 
-                                 "Probably unknown VR.");
+                                 << " : unknown VR." 
+                                 " Probably 'Implicit VR' entry within "
+                                 "an explicit VR 'document'.");
          }
+         */
 
          if ( !set->AddEntry( newDataEntry ) )
          {
@@ -1018,6 +1025,7 @@ void Document::ParseDES(DocEntrySet *set, long offset,
          }
          else
          {
+            newDataEntry->Delete();
             // Load only if we can add (not a duplicate key)
             LoadDocEntry( newDataEntry );
          }
@@ -1050,7 +1058,7 @@ void Document::ParseDES(DocEntrySet *set, long offset,
                (!delim_mode && ((long)(Fp->tellg())-offset) >= l_max) )
          {
             if ( !used )
-               delete newDocEntry;
+               newDocEntry->Delete();
             break;
          }
 
@@ -1080,8 +1088,7 @@ void Document::ParseDES(DocEntrySet *set, long offset,
             if ( newDocEntry->GetGroup()%2 != 0 )
             {
                 Fp->seekg( l, std::ios::cur);
-                RemoveEntry( newDocEntry );  // Remove and delete
-                //used = false; // never used
+                newDocEntry->Delete();  // Delete, not in the set 
                 continue;  
             } 
          } 
@@ -1089,8 +1096,7 @@ void Document::ParseDES(DocEntrySet *set, long offset,
          {
            // User asked to skip *any* SeQuence
             Fp->seekg( l, std::ios::cur);
-            //used = false; // never used
-            RemoveEntry( newDocEntry );  // Remove and delete
+            newDocEntry->Delete(); // Delete, not in the set
             continue;
          }
          // delay the dynamic cast as late as possible
@@ -1103,11 +1109,9 @@ void Document::ParseDES(DocEntrySet *set, long offset,
          // is a Document, then we are building the first depth level.
          // Hence the SeqEntry we are building simply has a depth
          // level of one:
-//         SQItem *parentSQItem = dynamic_cast< SQItem* > ( set );
         if ( set == this ) // ( dynamic_cast< Document* > ( set ) )
          {
             newSeqEntry->SetDepthLevel( 1 );
-         //   newSeqEntry->SetKey( newSeqEntry->GetKey() );
          }
          // But when "set" is already a SQItem, we are building a nested
          // sequence, and hence the depth level of the new SeqEntry
@@ -1117,9 +1121,6 @@ void Document::ParseDES(DocEntrySet *set, long offset,
          else if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
          {
             newSeqEntry->SetDepthLevel( parentSQItem->GetDepthLevel() + 1 );
-
-          //  newSeqEntry->SetKey(  parentSQItem->GetBaseTagKey()
-          //                      + newSeqEntry->GetKey() );
          }
 
          if ( l != 0 )
@@ -1144,22 +1145,26 @@ void Document::ParseDES(DocEntrySet *set, long offset,
                                 << newSeqEntry->GetOffset() << " )" ); 
             used = false;
          }
+         else
+         {
+            newDocEntry->Delete();
+         }
  
          if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
          {
             if ( !used )
-               delete newDocEntry;  
-               break;
+               newDocEntry->Delete();
+            break;
          }
       }  // end SeqEntry : VR = "SQ"
 
       if ( !used )
       {
-         delete newDocEntry;
+         newDocEntry->Delete();
       }
       first = false;
    }                               // end While
-   gdcmWarningMacro( "Exit from ParseDES, delim-mode " << delim_mode );
+   gdcmDebugMacro( "Exit from ParseDES, delim-mode " << delim_mode );
 }
 
 /**
@@ -1189,12 +1194,13 @@ void Document::ParseSQ( SeqEntry *seqEntry,
          if ( newDocEntry->IsSequenceDelimitor() )
          {
             seqEntry->SetDelimitationItem( newDocEntry ); 
+            newDocEntry->Delete();
             break;
          }
       }
       if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
       {
-         delete newDocEntry;
+         newDocEntry->Delete();
          break;
       }
       // create the current SQItem
@@ -1212,7 +1218,6 @@ void Document::ParseSQ( SeqEntry *seqEntry,
 
       // Let's try :------------
       // remove fff0,e000, created out of the SQItem
-      delete newDocEntry;
       Fp->seekg(offsetStartCurrentSQItem, std::ios::beg);
       // fill up the current SQItem, starting at the beginning of fff0,e000
 
@@ -1222,6 +1227,7 @@ void Document::ParseSQ( SeqEntry *seqEntry,
       // end try -----------------
  
       seqEntry->AddSQItem( itemSQ, SQItemNumber ); 
+      newDocEntry->Delete();
       SQItemNumber++;
       if ( !delim_mode && ((long)(Fp->tellg())-offset ) >= l_max )
       {
@@ -1240,7 +1246,7 @@ DocEntry *Document::Backtrack(DocEntry *docEntry)
 {
    // delete the Item Starter, built erroneously out of any Sequence
    // it's not yet in the HTable/chained list
-   delete docEntry;
+   docEntry->Delete();
 
    // Get all info we can from PreviousDocEntry
    uint16_t group = PreviousDocEntry->GetGroup();
@@ -1495,7 +1501,7 @@ VRKey Document::FindDocEntryVR()
    VRKey vr;
    Fp->read(&(vr[0]),(size_t)2);
 
-   gdcmWarningMacro( "--> VR: " << vr )
+   gdcmDebugMacro( "--> VR: " << vr )
    if ( !CheckDocEntryVR(vr) )
    {
       gdcmWarningMacro( "Unknown VR '" << vr << "'" )
@@ -1646,9 +1652,9 @@ bool Document::IsDocEntryAnInteger(DocEntry *entry)
       }
       else 
       {
-         // Allthough this should never happen, still some images have a
+         // Although this should never happen, still some images have a
          // corrupted group length [e.g. have a glance at offset x(8336) of
-         // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm].
+         // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm.
          // Since for dicom compliant and well behaved headers, the present
          // test is useless (and might even look a bit paranoid), when we
          // encounter such an ill-formed image, we simply display a warning
@@ -1694,7 +1700,7 @@ bool Document::CheckSwap()
    char *entCur = deb + 128;
    if ( memcmp(entCur, "DICM", (size_t)4) == 0 )
    {
-      gdcmWarningMacro( "Looks like DICOM Version3 (preamble + DCM)" );
+      gdcmDebugMacro( "Looks like DICOM Version3 (preamble + DCM)" );
       
       // Group 0002 should always be VR, and the first element 0000
       // Let's be carefull (so many wrong headers ...)
@@ -1726,24 +1732,24 @@ bool Document::CheckSwap()
       // instead of just checking for UL, OB and UI !? group 0000 
       {
          Filetype = ExplicitVR;
-         gdcmWarningMacro( "Group 0002 : Explicit Value Representation");
+         gdcmDebugMacro( "Group 0002 : Explicit Value Representation");
       } 
       else 
       {
          Filetype = ImplicitVR;
-         gdcmWarningMacro( "Group 0002 :Not an explicit Value Representation;"
+         gdcmErrorMacro( "Group 0002 :Not an explicit Value Representation;"
                         << "Looks like a bugged Header!");
       }
       
       if ( net2host )
       {
          SwapCode = 4321;
-         gdcmWarningMacro( "HostByteOrder != NetworkByteOrder");
+         gdcmDebugMacro( "HostByteOrder != NetworkByteOrder");
       }
       else 
       {
          SwapCode = 1234;
-         gdcmWarningMacro( "HostByteOrder = NetworkByteOrder");
+         gdcmDebugMacro( "HostByteOrder = NetworkByteOrder");
       }
       
       // Position the file position indicator at first tag 
@@ -1790,7 +1796,7 @@ bool Document::CheckSwap()
            memcmp(entCur, "OB", (size_t)2) == 0 )
          {
             Filetype = ExplicitVR;
-            gdcmWarningMacro( "Group 0002 : Explicit Value Representation");
+            gdcmDebugMacro( "Group 0002 : Explicit Value Representation");
             return true;
           }
     }
@@ -1879,8 +1885,8 @@ bool Document::CheckSwap()
  */
 void Document::SwitchByteSwapCode() 
 {
-   gdcmWarningMacro( "Switching Byte Swap code from "<< SwapCode
-                     << " at :" <<std::hex << Fp->tellg() );
+   gdcmDebugMacro( "Switching Byte Swap code from "<< SwapCode
+                     << " at: 0x" << std::hex << Fp->tellg() );
    if ( SwapCode == 1234 ) 
    {
       SwapCode = 4321;
@@ -1897,6 +1903,7 @@ void Document::SwitchByteSwapCode()
    {
       SwapCode = 3412;
    }
+   gdcmDebugMacro( " Into: "<< SwapCode );
 }
 
 /**
@@ -1970,10 +1977,11 @@ DocEntry *Document::ReadNextDocEntry()
          if ( dictEntry )
          {
             realVR = dictEntry->GetVR();
+            dictEntry->Unregister();
          }
       }
    }
-   gdcmWarningMacro( "Found VR: " << vr << " / Real VR: " << realVR );
+   gdcmDebugMacro( "Found VR: " << vr << " / Real VR: " << realVR );
 
    DocEntry *newEntry;
    if ( Global::GetVR()->IsVROfSequence(realVR) )
@@ -2010,7 +2018,7 @@ DocEntry *Document::ReadNextDocEntry()
    catch ( FormatError )
    {
       // Call it quits
-      delete newEntry;
+      newEntry->Delete();
       return 0;
    }