]> Creatis software - gdcm.git/blobdiff - src/gdcmFile.cxx
* src/gdcmFile.cxx : now delete the PixelConvert instance.
[gdcm.git] / src / gdcmFile.cxx
index a528e28c3bfac903213acbd32c7c7726d5a12e3d..2faa65533b97252e4ae16041817351300ac834e9 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmFile.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/10/22 13:56:46 $
-  Version:   $Revision: 1.149 $
+  Date:      $Date: 2004/11/15 15:29:04 $
+  Version:   $Revision: 1.154 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -73,6 +73,7 @@ File::File(std::string const & filename )
  */
 void File::Initialise()
 {
+   PixelConverter = NULL; //just in case
    if ( HeaderInternal->IsReadable() )
    {
       ImageDataSizeRaw = ComputeDecompressedPixelDataSizeFromHeader();
@@ -85,7 +86,7 @@ void File::Initialise()
          ImageDataSize = ImageDataSizeRaw;
       }
 
-      PixelConverter = new PixelConvert;
+      PixelConverter = new PixelConvert;  //LEAK !
       PixelConverter->GrabInformationsFromHeader( HeaderInternal );
    }
    SaveInitialValues();
@@ -105,6 +106,11 @@ File::~File()
    HeaderInternal = 0;
 
    DeleteInitialValues();
+   if( PixelConverter )
+   {
+      delete PixelConverter;
+   }
+
 }
 
 /**
@@ -274,7 +280,7 @@ int File::ComputeDecompressedPixelDataSizeFromHeader()
    //  - it is not defined (i.e. it's value is 0)
    //  - it's 12, since we will expand the image to 16 bits (see
    //    PixelConvert::ConvertDecompress12BitsTo16Bits() )
-   if ( ( numberBitsAllocated == 0 ) || ( numberBitsAllocated == 12 ) )
+   if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
    {
       numberBitsAllocated = 16;
    } 
@@ -507,7 +513,7 @@ uint8_t* File::GetDecompressed()
       // The decompressed image migth not be loaded yet:
       std::ifstream* fp = HeaderInternal->OpenFile();
       PixelConverter->ReadAndDecompressPixelData( fp );
-      HeaderInternal->CloseFile();
+      if(fp) HeaderInternal->CloseFile();
       decompressed = PixelConverter->GetDecompressed();
       if ( ! decompressed )
       {
@@ -558,7 +564,7 @@ bool File::WriteRawData(std::string const & fileName)
   std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary );
    if (!fp1)
    {
-      printf("Fail to open (write) file [%s] \n", fileName.c_str());
+      dbg.Verbose(2, "Fail to open (write) file:", fileName.c_str());
       return false;
    }
    fp1.write((char*)Pixel_Data, ImageDataSize);
@@ -634,19 +640,17 @@ bool File::WriteBase (std::string const & fileName, FileType type)
                               std::ios::out | std::ios::binary);
    if (fp1 == NULL)
    {
-      printf("Failed to open (write) File [%s] \n", fileName.c_str());
+      dbg.Verbose(2, "Failed to open (write) File: " , fileName.c_str());
       return false;
    }
 
    if ( type == ImplicitVR || type == ExplicitVR )
    {
       // writing Dicom File Preamble
-      uint8_t* filePreamble = new uint8_t[128];
+      char filePreamble[128];
       memset(filePreamble, 0, 128);
-      fp1->write((char*)filePreamble, 128);
+      fp1->write(filePreamble, 128);
       fp1->write("DICM", 4);
-
-      delete[] filePreamble;
    }
 
    // --------------------------------------------------------------
@@ -702,6 +706,7 @@ bool File::WriteBase (std::string const & fileName, FileType type)
    }
    // ----------------- End of Special Patch ----------------
    fp1->close ();
+   delete fp1;
 
    return true;
 }