]> Creatis software - gdcm.git/blobdiff - src/gdcmFile.cxx
* FIX : now, the DocEntries are all deleted in the gdcmElementSet.
[gdcm.git] / src / gdcmFile.cxx
index f044feb11eb06bf4ffbd20d45c59ca749f2cb6af..837df465e8139d9a30e94288c38bbb71e6ec6dc0 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmFile.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/10/28 03:10:58 $
-  Version:   $Revision: 1.150 $
+  Date:      $Date: 2004/11/16 16:20:23 $
+  Version:   $Revision: 1.157 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -18,6 +18,8 @@
 
 #include "gdcmFile.h"
 #include "gdcmDebug.h"
+#include "gdcmUtil.h"
+#include "gdcmBinEntry.h"
 #include <fstream>
 
 namespace gdcm 
@@ -73,6 +75,7 @@ File::File(std::string const & filename )
  */
 void File::Initialise()
 {
+   PixelConverter = NULL; //just in case
    if ( HeaderInternal->IsReadable() )
    {
       ImageDataSizeRaw = ComputeDecompressedPixelDataSizeFromHeader();
@@ -105,6 +108,10 @@ File::~File()
    HeaderInternal = 0;
 
    DeleteInitialValues();
+   if( PixelConverter )
+   {
+      delete PixelConverter;
+   }
 }
 
 /**
@@ -130,7 +137,7 @@ void File::SaveInitialValues()
    InitialRedLUTData    = 0;
    InitialGreenLUTData  = 0;
    InitialBlueLUTData   = 0; 
-                
+
    if ( HeaderInternal->IsReadable() )
    {
       // the following values *may* be modified 
@@ -205,28 +212,29 @@ void File::RestoreInitialValues()
  */
 void File::DeleteInitialValues()
 { 
-
 // InitialLutDescriptors and InitialLutData
 // will have to be deleted if the don't belong any longer
 // to the Header H table when the header is deleted...
 
-   if ( InitialRedLUTDescr )           
+// FIXME 
+// We don't know if the InitialLutData are still in the header or not !
+/*   if ( InitialRedLUTDescr )
       delete InitialRedLUTDescr;
   
    if ( InitialGreenLUTDescr )
       delete InitialGreenLUTDescr;
       
-   if ( InitialBlueLUTDescr )      
-      delete InitialBlueLUTDescr; 
+   if ( InitialBlueLUTDescr )
+      delete InitialBlueLUTDescr;
        
-   if ( InitialRedLUTData )      
+   if ( InitialRedLUTData )
       delete InitialRedLUTData;
    
-   if ( InitialGreenLUTData != NULL)
+   if ( InitialGreenLUTData )
       delete InitialGreenLUTData;
       
-   if ( InitialBlueLUTData != NULL)      
-      delete InitialBlueLUTData;      
+   if ( InitialBlueLUTData )
+      delete InitialBlueLUTData;*/
 }
 
 //-----------------------------------------------------------------------------
@@ -341,7 +349,7 @@ uint8_t* File::GetImageData()
       if ( HeaderInternal->HasLUT() )
       {
          // The LUT interpretation failed
-         std::string photometricInterpretation = "MONOCHROME1 ";
+         std::string photometricInterpretation = Util::DicomString("MONOCHROME1");
          HeaderInternal->SetEntryByNumber( photometricInterpretation,
                                            0x0028, 0x0004 );
          PixelRead = 0; // no PixelRaw
@@ -371,12 +379,7 @@ uint8_t* File::GetImageData()
    }
 
    // We say the value *is* loaded.
-   GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
-      GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
-
-   // Will be 7fe0, 0010 in standard case
-   GetHeader()->SetEntryBinAreaByNumber( pixelData, 
-      GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel()); 
+   SetPixelData(pixelData);
 // END PIXELCONVERT CLEANME
 
    return pixelData;
@@ -423,9 +426,9 @@ size_t File::GetImageDataIntoVector (void* destination, size_t maxSize)
                         "than caller's expected MaxSize");
          return 0;
       }
-      memmove( destination,
-               (void*)PixelConverter->GetRGB(),
-               PixelConverter->GetRGBSize() );
+      memcpy( destination,
+              (void*)PixelConverter->GetRGB(),
+              PixelConverter->GetRGBSize() );
       return PixelConverter->GetRGBSize();
    }
 
@@ -436,9 +439,9 @@ size_t File::GetImageDataIntoVector (void* destination, size_t maxSize)
                      "than caller's expected MaxSize");
       return 0;
    }
-   memmove( destination,
-            (void*)PixelConverter->GetDecompressed(),
-            PixelConverter->GetDecompressedSize() );
+   memcpy( destination,
+           (void*)PixelConverter->GetDecompressed(),
+           PixelConverter->GetDecompressedSize() );
    return PixelConverter->GetDecompressedSize();
 }
 
@@ -486,12 +489,13 @@ uint8_t* File::GetImageDataRaw ()
    }
 
    // We say the value *is* loaded.
-   GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
+/*   GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
    GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
  
    // will be 7fe0, 0010 in standard cases
    GetHeader()->SetEntryBinAreaByNumber( decompressed,
-   GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
+   GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());*/
+   SetPixelData(decompressed);
  
    PixelRead = 1; // PixelRaw
 // END PIXELCONVERT CLEANME
@@ -507,7 +511,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 +562,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,16 +638,16 @@ 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[128];
+      char filePreamble[128];
       memset(filePreamble, 0, 128);
-      fp1->write((char*)filePreamble, 128);
+      fp1->write(filePreamble, 128);
       fp1->write("DICM", 4);
    }
 
@@ -700,6 +704,7 @@ bool File::WriteBase (std::string const & fileName, FileType type)
    }
    // ----------------- End of Special Patch ----------------
    fp1->close ();
+   delete fp1;
 
    return true;
 }
@@ -712,5 +717,23 @@ uint8_t* File::GetLutRGBA()
    return PixelConverter->GetLutRGBA();
 }
 
+//-----------------------------------------------------------------------------
+// Private
+void File::SetPixelData(uint8_t* data)
+{
+   GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
+      GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
+
+   // Will be 7fe0, 0010 in standard case
+   DocEntry* currentEntry = GetHeader()->GetDocEntryByNumber(GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
+   if ( currentEntry )
+   {
+      if ( BinEntry* binEntry = dynamic_cast<BinEntry *>(currentEntry) )
+         // Flag is to false because datas are kept in the gdcmPixelConvert
+         binEntry->SetBinArea( data, false );
+   }
+}
+
+//-----------------------------------------------------------------------------
 } // end namespace gdcm