]> Creatis software - gdcm.git/blobdiff - src/gdcmPixelConvert.cxx
* Doc/Dicom2004_Part5.pdf added (thanks to JPR).
[gdcm.git] / src / gdcmPixelConvert.cxx
index 4649cbb11bf3d980077c0f5e663700503907e559..2afdb86c089c44217172bbf3344103314c46f655 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmPixelConvert.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/10/18 12:49:22 $
-  Version:   $Revision: 1.15 $
+  Date:      $Date: 2004/10/22 13:56:46 $
+  Version:   $Revision: 1.20 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -24,6 +24,7 @@
 
 #include "gdcmDebug.h"
 #include "gdcmPixelConvert.h"
+#include <fstream>
 
 namespace gdcm
 {
@@ -31,18 +32,18 @@ namespace gdcm
 #define str2num(str, typeNum) *((typeNum *)(str))
 
 // For JPEG 2000, body in file gdcmJpeg2000.cxx
-bool gdcm_read_JPEG2000_file (FILE* fp, void* image_buffer);
+bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* image_buffer);
 
 // For JPEG 8 Bits, body in file gdcmJpeg8.cxx
-bool gdcm_read_JPEG_file8    (FILE* fp, void* image_buffer);
+bool gdcm_read_JPEG_file8    (std::ifstream* fp, void* image_buffer);
 
 // For JPEG 12 Bits, body in file gdcmJpeg12.cxx
-bool gdcm_read_JPEG_file12   (FILE* fp, void* image_buffer);
+bool gdcm_read_JPEG_file12   (std::ifstream* fp, void* image_buffer);
 
 // For JPEG 16 Bits, body in file gdcmJpeg16.cxx
 // Beware this is misleading there is no 16bits DCT algorithm, only
 // jpeg lossless compression exist in 16bits.
-bool gdcm_read_JPEG_file16   (FILE* fp, void* image_buffer);
+bool gdcm_read_JPEG_file16   (std::ifstream* fp, void* image_buffer);
 
 
 //-----------------------------------------------------------------------------
@@ -100,7 +101,7 @@ void PixelConvert::AllocateDecompressed()
  * \brief Read from file a 12 bits per pixel image and decompress it
  *        into a 16 bits per pixel image.
  */
-void PixelConvert::ReadAndDecompress12BitsTo16Bits( FILE* fp )
+void PixelConvert::ReadAndDecompress12BitsTo16Bits( std::ifstream* fp )
                throw ( FormatError )
 {
    int nbPixels = XSize * YSize;
@@ -109,24 +110,23 @@ void PixelConvert::ReadAndDecompress12BitsTo16Bits( FILE* fp )
    for( int p = 0; p < nbPixels; p += 2 )
    {
       uint8_t b0, b1, b2;
-      size_t ItemRead;
-                                                                                
-      ItemRead = fread( &b0, 1, 1, fp );
-      if ( ItemRead != 1 )
+
+      fp->read( (char*)&b0, 1);
+      if ( fp->fail() || fp->eof() )//Fp->gcount() == 1
       {
          throw FormatError( "PixelConvert::ReadAndDecompress12BitsTo16Bits()",
                                 "Unfound first block" );
       }
-                                                                                
-      ItemRead = fread( &b1, 1, 1, fp );
-      if ( ItemRead != 1 )
+
+      fp->read( (char*)&b1, 1 );
+      if ( fp->fail() || fp->eof())//Fp->gcount() == 1
       {
          throw FormatError( "PixelConvert::ReadAndDecompress12BitsTo16Bits()",
                                 "Unfound second block" );
       }
-                                                                                
-      ItemRead = fread( &b2, 1, 1, fp );
-      if ( ItemRead != 1 )
+
+      fp->read( (char*)&b2, 1 );
+      if ( fp->fail() || fp->eof())//Fp->gcount() == 1
       {
          throw FormatError( "PixelConvert::ReadAndDecompress12BitsTo16Bits()",
                                 "Unfound second block" );
@@ -199,7 +199,7 @@ bool PixelConvert::DecompressRLE16BitsFromRLE8Bits( int NumberOfFrames )
 bool PixelConvert::ReadAndDecompressRLEFragment( uint8_t* subDecompressed,
                                                  long fragmentSize,
                                                  long decompressedSegmentSize,
-                                                 FILE* fp )
+                                                 std::ifstream* fp )
 {
    int8_t count;
    long numberOfOutputBytes = 0;
@@ -207,7 +207,7 @@ bool PixelConvert::ReadAndDecompressRLEFragment( uint8_t* subDecompressed,
                                                                                 
    while( numberOfOutputBytes < decompressedSegmentSize )
    {
-      fread( &count, 1, 1, fp );
+      fp->read( (char*)&count, 1 );
       numberOfReadBytes += 1;
       if ( count >= 0 )
       // Note: count <= 127 comparison is always true due to limited range
@@ -215,7 +215,7 @@ bool PixelConvert::ReadAndDecompressRLEFragment( uint8_t* subDecompressed,
       //       signed integer of width N is 2^(N-1) - 1, which for int8_t
       //       is 127].
       {
-         fread( subDecompressed, count + 1, 1, fp);
+         fp->read( (char*)subDecompressed, count + 1);
          numberOfReadBytes   += count + 1;
          subDecompressed     += count + 1;
          numberOfOutputBytes += count + 1;
@@ -225,7 +225,7 @@ bool PixelConvert::ReadAndDecompressRLEFragment( uint8_t* subDecompressed,
          if ( ( count <= -1 ) && ( count >= -127 ) )
          {
             int8_t newByte;
-            fread( &newByte, 1, 1, fp);
+            fp->read( (char*)&newByte, 1);
             numberOfReadBytes += 1;
             for( int i = 0; i < -count + 1; i++ )
             {
@@ -254,7 +254,7 @@ bool PixelConvert::ReadAndDecompressRLEFragment( uint8_t* subDecompressed,
  *            at which the pixel data should be copied
  * @return    Boolean
  */
-bool PixelConvert::ReadAndDecompressRLEFile( FILE* fp )
+bool PixelConvert::ReadAndDecompressRLEFile( std::ifstream* fp )
 {
    uint8_t* subDecompressed = Decompressed;
    long decompressedSegmentSize = XSize * YSize;
@@ -266,9 +266,10 @@ bool PixelConvert::ReadAndDecompressRLEFile( FILE* fp )
       ++it )
    {
       // Loop on the fragments
-      for( int k = 1; k <= (*it)->NumberFragments; k++ )
+      for( unsigned int k = 1; k <= (*it)->NumberFragments; k++ )
       {
-         fseek( fp, (*it)->Offset[k] ,SEEK_SET );
+         //fseek( fp, (*it)->Offset[k] ,SEEK_SET );
+         fp->seekg(  (*it)->Offset[k] , std::ios_base::beg );
          (void)ReadAndDecompressRLEFragment( subDecompressed,
                                              (*it)->Length[k],
                                              decompressedSegmentSize, 
@@ -399,7 +400,7 @@ void PixelConvert::ConvertReorderEndianity()
  * @param     fp File Pointer
  * @return    Boolean
  */
-bool PixelConvert::ReadAndDecompressJPEGFile( FILE* fp )
+bool PixelConvert::ReadAndDecompressJPEGFile( std::ifstream* fp )
 {
    uint8_t* localDecompressed = Decompressed;
    // Loop on the fragment[s]
@@ -408,7 +409,8 @@ bool PixelConvert::ReadAndDecompressJPEGFile( FILE* fp )
         it != JPEGInfo->Fragments.end();
       ++it )
    {
-      fseek( fp, (*it)->Offset, SEEK_SET );
+      //fseek( fp, (*it)->Offset, SEEK_SET );
+      fp->seekg( (*it)->Offset, std::ios_base::beg);
 
       if ( IsJPEG2000 )
       {
@@ -580,7 +582,7 @@ void PixelConvert::ConvertRGBPlanesToRGBPixels()
    delete[] copyDecompressed;
 }
 
-bool PixelConvert::ReadAndDecompressPixelData( FILE* fp )
+bool PixelConvert::ReadAndDecompressPixelData( std::ifstream* fp )
 {
    ComputeDecompressedAndRGBSizes();
    AllocateDecompressed();
@@ -593,7 +595,9 @@ bool PixelConvert::ReadAndDecompressPixelData( FILE* fp )
       return false;
    }
                                                                                 
-   if ( fseek( fp, PixelOffset, SEEK_SET ) == -1 )
+   //if ( fseek( fp, PixelOffset, SEEK_SET ) == -1 )
+   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." );
@@ -608,8 +612,8 @@ bool PixelConvert::ReadAndDecompressPixelData( FILE* fp )
    }
    else if ( IsDecompressed )
    {
-      size_t ItemRead = fread( Decompressed, PixelDataLength, 1, fp );
-      if ( ItemRead != 1 )
+      fp->read( (char*)Decompressed, PixelDataLength);
+      if ( fp->fail() || fp->eof())//Fp->gcount() == 1
       {
          dbg.Verbose( 0, "PixelConvert::ReadAndDecompressPixelData: "
                          "reading of decompressed pixel data failed." );
@@ -710,7 +714,7 @@ void PixelConvert::ConvertHandleColor()
 
 /**
  * \brief Predicate to know wether the image[s] (once decompressed) is RGB.
- * \note See comments of \ref HandleColor
+ * \note See comments of \ref ConvertHandleColor
  */
 bool PixelConvert::IsDecompressedRGB()
 {
@@ -748,7 +752,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.
-   FILE* 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();
@@ -779,15 +783,16 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
    PixelSize = header->GetPixelSize();
    PixelSign = header->IsSignedPixelData();
    SwapCode  = header->GetSwapCode();
+   TransferSyntaxType ts = header->GetTransferSyntax();
    IsDecompressed =
         ( ! header->IsDicomV3() )
-     || header->IsImplicitVRLittleEndianTransferSyntax()
-     || header->IsExplicitVRLittleEndianTransferSyntax()
-     || header->IsExplicitVRBigEndianTransferSyntax()
-     || header->IsDeflatedExplicitVRLittleEndianTransferSyntax();
+     || ts == ImplicitVRLittleEndian
+     || ts == ExplicitVRLittleEndian
+     || ts == ExplicitVRBigEndian
+     || ts == DeflatedExplicitVRLittleEndian;
    IsJPEG2000     = header->IsJPEG2000();
    IsJPEGLossless = header->IsJPEGLossless();
-   IsRLELossless  = header->IsRLELossLessTransferSyntax();
+   IsRLELossless  =  ( ts == RLELossless );
    PixelOffset     = header->GetPixelOffset();
    PixelDataLength = header->GetPixelAreaLength();
    RLEInfo  = header->GetRLEInfo();
@@ -829,11 +834,11 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
          DocEntry* lutRedDataEntry = header->GetDocEntryByNumber( 0x0028,
                                                                   0x1201 );
          LutRedData = new uint8_t[ lutRedDataEntry->GetLength() ];
-         fseek( fp, lutRedDataEntry->GetOffset() ,SEEK_SET );
-         int numberItem = fread( LutRedData,
-                                 (size_t)lutRedDataEntry->GetLength(),
-                                 1, fp );
-         if ( numberItem != 1 )
+         //fseek( fp, lutRedDataEntry->GetOffset() ,SEEK_SET );
+         fp->seekg(  lutRedDataEntry->GetOffset() ,std::ios_base::beg );
+          fp->read( (char*)LutRedData, (size_t)lutRedDataEntry->GetLength());
+        //if ( numberItem != 1 )
+        if ( fp->fail() || fp->eof())//Fp->gcount() == 1
          {
             dbg.Verbose(0, "PixelConvert::GrabInformationsFromHeader: "
                             "unable to read red LUT data" );
@@ -849,11 +854,11 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
          DocEntry* lutGreenDataEntry = header->GetDocEntryByNumber( 0x0028,
                                                                     0x1202 );
          LutGreenData = new uint8_t[ lutGreenDataEntry->GetLength() ];
-         fseek( fp, lutGreenDataEntry->GetOffset() ,SEEK_SET );
-         int numberItem = fread( LutGreenData,
-                                 (size_t)lutGreenDataEntry->GetLength(),
-                                 1, fp );
-         if ( numberItem != 1 )
+         //fseek( fp, lutGreenDataEntry->GetOffset() ,SEEK_SET );
+         fp->seekg( lutGreenDataEntry->GetOffset() , std::ios_base::beg );
+          fp->read( (char*)LutGreenData, (size_t)lutGreenDataEntry->GetLength() );
+        //if ( numberItem != 1 )
+        if ( fp->fail() || fp->eof())//Fp->gcount() == 1
          {
             dbg.Verbose(0, "PixelConvert::GrabInformationsFromHeader: "
                            "unable to read green LUT data" );
@@ -869,11 +874,11 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
          DocEntry* lutBlueDataEntry  = header->GetDocEntryByNumber( 0x0028,
                                                                     0x1203 );
          LutBlueData = new uint8_t[ lutBlueDataEntry->GetLength() ];
-         fseek( fp, lutBlueDataEntry->GetOffset() ,SEEK_SET );
-         int numberItem = fread( LutBlueData,
-                                 (size_t)lutBlueDataEntry->GetLength(),
-                                 1, fp );
-         if ( numberItem != 1 )
+         //fseek( fp, lutBlueDataEntry->GetOffset() ,SEEK_SET );
+         fp->seekg(  lutBlueDataEntry->GetOffset() , std::ios_base::beg );
+         fp->read( (char*)LutBlueData, (size_t)lutBlueDataEntry->GetLength() );
+        //if ( numberItem != 1 )
+        if ( fp->fail() || fp->eof())//Fp->gcount() == 1
          {
             dbg.Verbose(0, "PixelConvert::GrabInformationsFromHeader: "
                            "unable to read blue LUT data" );
@@ -900,7 +905,7 @@ void PixelConvert::GrabInformationsFromHeader( Header* header )
  *   no known Dicom reader deals with them :-(
  * @return a RGBA Lookup Table
  */
-void PixelConvert::BuildLUTRGBA( FILE* fp )
+void PixelConvert::BuildLUTRGBA()
 {
    if ( LutRGBA )
    {
@@ -1023,12 +1028,25 @@ void PixelConvert::BuildLUTRGBA( FILE* fp )
 /**
  * \brief Build the RGB image from the Decompressed imagage and the LUTs.
  */
-bool PixelConvert::BuildRGBImage( FILE* fp )
+bool PixelConvert::BuildRGBImage()
 {
-   BuildLUTRGBA( fp );
+   if ( RGB )
+   {
+      // The job is allready done.
+      return true;
+   }
+
+   if ( ! Decompressed )
+   {
+      // The job can't be done
+      return false;
+   }
+
+   BuildLUTRGBA();
    if ( ! LutRGBA )
    {
-       return false;
+      // The job can't be done
+      return false;
    }
                                                                                 
    // Build RGB Pixels
@@ -1036,7 +1054,7 @@ bool PixelConvert::BuildRGBImage( FILE* fp )
    uint8_t* localRGB = RGB;
    for (size_t i = 0; i < DecompressedSize; ++i )
    {
-      int j  = Decompressed[i] * 4; // \todo I don't get this 4 coefficient !
+      int j  = Decompressed[i] * 4;
       *localRGB++ = LutRGBA[j];
       *localRGB++ = LutRGBA[j+1];
       *localRGB++ = LutRGBA[j+2];
@@ -1044,6 +1062,49 @@ bool PixelConvert::BuildRGBImage( FILE* fp )
    return true;
 }
 
+/**
+ * \brief        Print self.
+ * @param indent Indentation string to be prepended during printing.
+ * @param os     Stream to print to.
+ */
+void PixelConvert::Print( std::string indent, std::ostream &os )
+{
+   os << indent
+      << "--- Pixel information -------------------------"
+      << std::endl;
+   os << indent
+      << "Pixel Data: offset " << PixelOffset
+      << " x" << std::hex << PixelOffset << std::dec
+      << "   length " << PixelDataLength
+      << " x" << std::hex << PixelDataLength << std::dec
+      << std::endl;
+
+   if ( IsRLELossless )
+   {
+      if ( RLEInfo )
+      {
+         RLEInfo->Print( indent, os );
+      }
+      else
+      {
+         dbg.Verbose(0, "PixelConvert::Print: set as RLE file "
+                        "but NO RLEinfo present.");
+      }
+   }
+
+   if ( IsJPEG2000 || IsJPEGLossless )
+   {
+      if ( JPEGInfo )
+      {
+         JPEGInfo->Print( indent, os );
+      }
+      else
+      {
+         dbg.Verbose(0, "PixelConvert::Print: set as JPEG file "
+                        "but NO JPEGinfo present.");
+      }
+   }
+}
 
 } // end namespace gdcm