]> Creatis software - gdcm.git/blobdiff - src/gdcmPixelConvert.cxx
* FIX: src/* fix the doxygen warnings.
[gdcm.git] / src / gdcmPixelConvert.cxx
index dccf178e9ae5cb32fcce25680d1fb8a88e761873..cf3c240aa84d1134816a5367a65cac50fe475bd8 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmPixelConvert.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/11/04 18:14:34 $
-  Version:   $Revision: 1.23 $
+  Date:      $Date: 2004/11/10 18:27:23 $
+  Version:   $Revision: 1.29 $
                                                                                 
   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
 {
@@ -173,8 +174,8 @@ bool PixelConvert::DecompressRLE16BitsFromRLE8Bits( int NumberOfFrames )
    {
       for ( unsigned int j = 0; j < PixelNumber; j++ )
       {
-         *(x++) = *(a++);
          *(x++) = *(b++);
+         *(x++) = *(a++);
       }
    }
 
@@ -424,7 +425,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 +433,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 +588,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 +609,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: "
@@ -884,7 +902,7 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
       }
    }
                                                                                 
-   header->CloseFile();
+   if(fp) header->CloseFile();
 }
 
 /**