]> Creatis software - gdcm.git/blobdiff - src/gdcmPixelConvert.cxx
* src/gdcmDocEntryArchive.[h|cxx] : bug fix and add a method to temporary
[gdcm.git] / src / gdcmPixelConvert.cxx
index 5abd6c4ef99cb8ca080c10fe28bf4300a9cc478d..05fe19d642c77f7c4cd49814c38d75b5b2dfb02a 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmPixelConvert.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/10/28 22:21:57 $
-  Version:   $Revision: 1.22 $
+  Date:      $Date: 2004/11/24 10:23:47 $
+  Version:   $Revision: 1.32 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -25,6 +25,7 @@
 #include "gdcmDebug.h"
 #include "gdcmPixelConvert.h"
 #include <fstream>
+#include <stdio.h>
 
 namespace gdcm
 {
@@ -65,14 +66,19 @@ void PixelConvert::Squeeze()
    {
       delete [] RGB;
    } 
+   RGB = 0;
+
    if ( Decompressed )
    {
       delete [] Decompressed;
    }
+   Decompressed = 0;
+
    if ( LutRGBA )
    {
       delete [] LutRGBA;
    }
+   LutRGBA = 0;
 }
 
 PixelConvert::~PixelConvert() 
@@ -173,8 +179,8 @@ bool PixelConvert::DecompressRLE16BitsFromRLE8Bits( int NumberOfFrames )
    {
       for ( unsigned int j = 0; j < PixelNumber; j++ )
       {
-         *(x++) = *(a++);
          *(x++) = *(b++);
+         *(x++) = *(a++);
       }
    }
 
@@ -424,7 +430,7 @@ bool PixelConvert::ReadAndDecompressJPEGFile( std::ifstream* fp )
             return false;
          }
       }
-      else if ( BitsStored == 12)
+      else if ( BitsStored <= 12)
       {
          // Reading Fragment pixels
          if ( ! gdcm_read_JPEG_file12 ( fp, localDecompressed ) )
@@ -432,7 +438,7 @@ bool PixelConvert::ReadAndDecompressJPEGFile( std::ifstream* fp )
             return false;
          }
       }
-      else if ( BitsStored == 16)
+      else if ( BitsStored <= 16)
       {
          // Reading Fragment pixels
          if ( ! gdcm_read_JPEG_file16 ( fp, localDecompressed ) )
@@ -587,16 +593,16 @@ bool PixelConvert::ReadAndDecompressPixelData( std::ifstream* fp )
    //// First stage: get our hands on the Pixel Data.
    if ( !fp )
    {
-     dbg.Verbose( 0, "PixelConvert::ReadAndDecompressPixelData: "
-                     "unavailable file pointer." );
+      dbg.Verbose( 0, "PixelConvert::ReadAndDecompressPixelData: "
+                      "unavailable file pointer." );
       return false;
    }
 
    fp->seekg( PixelOffset, std::ios_base::beg );
    if( fp->fail() || fp->eof()) //Fp->gcount() == 1
    {
-     dbg.Verbose( 0, "PixelConvert::ReadAndDecompressPixelData: "
-                     "unable to find PixelOffset in file." );
+      dbg.Verbose( 0, "PixelConvert::ReadAndDecompressPixelData: "
+                      "unable to find PixelOffset in file." );
       return false;
    }
 
@@ -608,7 +614,24 @@ bool PixelConvert::ReadAndDecompressPixelData( std::ifstream* fp )
    }
    else if ( IsDecompressed )
    {
-      fp->read( (char*)Decompressed, PixelDataLength);
+      // This problem can be found when some obvious informations are found
+      // after the field containing the image datas. In this case, these
+      // bad datas are added to the size of the image (in the PixelDataLength
+      // variable). But DecompressedSize is the right size of the image !
+      if( PixelDataLength != DecompressedSize)
+      {
+         dbg.Verbose( 0, "PixelConvert::ReadAndDecompressPixelData: "
+                      "Mismatch between PixelConvert and DecompressedSize." );
+      }
+      if( PixelDataLength > DecompressedSize)
+      {
+         fp->read( (char*)Decompressed, DecompressedSize);
+      }
+      else
+      {
+         fp->read( (char*)Decompressed, PixelDataLength);
+      }
+
       if ( fp->fail() || fp->eof())//Fp->gcount() == 1
       {
          dbg.Verbose( 0, "PixelConvert::ReadAndDecompressPixelData: "
@@ -755,7 +778,7 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
 {
    // Just in case some access to a Header element requires disk access.
    // Note: gdcmDocument::Fp is leaved open after OpenFile.
-  std::ifstream* fp = header->OpenFile();
+   std::ifstream* fp = header->OpenFile();
    // Number of Bits Allocated for storing a Pixel is defaulted to 16
    // when absent from the header.
    BitsAllocated = header->GetBitsAllocated();
@@ -790,6 +813,7 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
    IsDecompressed =
         ( ! header->IsDicomV3() )
      || ts == ImplicitVRLittleEndian
+     || ts == ImplicitVRLittleEndianDLXGE
      || ts == ExplicitVRLittleEndian
      || ts == ExplicitVRBigEndian
      || ts == DeflatedExplicitVRLittleEndian;
@@ -846,7 +870,7 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
             return;
          }
       }
-   
+
       ////// Green round:
       LutGreenData = (uint8_t*)header->GetEntryBinAreaByNumber(0x0028, 0x1202 );
       if ( ! LutGreenData)
@@ -864,7 +888,7 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
             return;
          }
       }
-                                                                                   
+
       ////// Blue round:
       LutBlueData = (uint8_t*)header->GetEntryBinAreaByNumber( 0x0028, 0x1203 );
       if ( ! LutBlueData )
@@ -883,8 +907,8 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
          }
       }
    }
-                                                                                
-   header->CloseFile();
+
+   if(fp) header->CloseFile();
 }
 
 /**