]> Creatis software - gdcm.git/blobdiff - src/gdcmPixelReadConvert.cxx
ENH: Fix the this header. PLEASE do not touch if you dont know what you are doing
[gdcm.git] / src / gdcmPixelReadConvert.cxx
index 78075ab2a940da5e70cb2ff968c48d7e95d2e5b9..06dfb9deaeaa077da63f6947a475cc0cffcfac8d 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmPixelReadConvert.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/02/05 01:37:09 $
-  Version:   $Revision: 1.49 $
+  Date:      $Date: 2005/05/29 21:56:36 $
+  Version:   $Revision: 1.61 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -30,6 +30,9 @@
 
 namespace gdcm
 {
+
+//bool ReadMPEGFile (std::ifstream *fp, void *image_buffer, size_t lenght);
+bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* raw, size_t inputlength);
 //-----------------------------------------------------------------------------
 #define str2num(str, typeNum) *((typeNum *)(str))
 
@@ -115,6 +118,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file )
      || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian
      || Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::DeflatedExplicitVRLittleEndian;
 
+   IsMPEG          = Global::GetTS()->IsMPEG(ts);
    IsJPEG2000      = Global::GetTS()->IsJPEG2000(ts);
    IsJPEGLS        = Global::GetTS()->IsJPEGLS(ts);
    IsJPEGLossy     = Global::GetTS()->IsJPEGLossy(ts);
@@ -123,13 +127,15 @@ void PixelReadConvert::GrabInformationsFromFile( File *file )
 
    PixelOffset     = file->GetPixelOffset();
    PixelDataLength = file->GetPixelAreaLength();
-   RLEInfo  = file->GetRLEInfo();
-   JPEGInfo = file->GetJPEGInfo();
+   RLEInfo         = file->GetRLEInfo();
+   JPEGInfo        = file->GetJPEGInfo();
+
+   IsMonochrome    = file->IsMonochrome();
+   IsMonochrome1   = file->IsMonochrome1();
+   IsPaletteColor  = file->IsPaletteColor();
+   IsYBRFull       = file->IsYBRFull();
 
    PlanarConfiguration = file->GetPlanarConfiguration();
-   IsMonochrome = file->IsMonochrome();
-   IsPaletteColor = file->IsPaletteColor();
-   IsYBRFull = file->IsYBRFull();
 
    /////////////////////////////////////////////////////////////////
    // LUT section:
@@ -248,6 +254,13 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
          return false;
       }
    }
+   else if ( IsMPEG )
+   {
+      //gdcmWarningMacro( "Sorry, MPEG not yet taken into account" );
+      //return false;
+//      ReadMPEGFile(fp, Raw, PixelDataLength); // fp has already been seek to start of mpeg
+      return true;
+   }
    else
    {
       // Default case concerns JPEG family
@@ -262,6 +275,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
    //// Third stage: twigle the bytes and bits.
    ConvertReorderEndianity();
    ConvertReArrangeBits();
+   ConvertFixGreyLevels();
    ConvertHandleColor();
 
    return true;
@@ -383,14 +397,23 @@ bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
 {
    if ( IsJPEG2000 )
    {
-      gdcmWarningMacro( "Sorry, JPEG2000 not yet taken into account" );
+//      gdcmWarningMacro( "Sorry, JPEG2000 not yet taken into account" );
       fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
-//    if ( ! gdcm_read_JPEG2000_file( fp,Raw ) )
+    if ( ! gdcm_read_JPEG2000_file( fp,Raw, JPEGInfo->GetFirstFragment()->GetLength() ) )
           return false;
    }
 
    if ( IsJPEGLS )
    {
+   // WARNING : JPEG-LS is NOT the 'classical' Jpeg Lossless : 
+   // [JPEG-LS is the basis for new lossless/near-lossless compression
+   // standard for continuous-tone images intended for JPEG2000. The standard
+   // is based on the LOCO-I algorithm (LOw COmplexity LOssless COmpression
+   // for Images) developed at Hewlett-Packard Laboratories]
+   //
+   // see http://datacompression.info/JPEGLS.shtml
+   //
+
       gdcmWarningMacro( "Sorry, JPEG-LS not yet taken into account" );
       fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
 //    if ( ! gdcm_read_JPEGLS_file( fp,Raw ) )
@@ -641,9 +664,78 @@ void PixelReadConvert::ConvertReorderEndianity()
    }
 }
 
+/**
+ * \brief Deal with Grey levels i.e. re-arange them
+ *        to have low values = dark, high values = bright
+ */
+void PixelReadConvert::ConvertFixGreyLevels()
+{
+   if (!IsMonochrome1)
+      return;
+
+   uint32_t i; // to please M$VC6
+   int16_t j;
+
+   if (!PixelSign)
+   {
+      if ( BitsAllocated == 8 )
+      {
+         uint8_t *deb = (uint8_t *)Raw;
+         for (i=0; i<RawSize; i++)      
+         {
+            *deb = 255 - *deb;
+            deb++;
+         }
+         return;
+      }
+
+      if ( BitsAllocated == 16 )
+      {
+         uint16_t mask =1;
+         for (j=0; j<BitsStored-1; j++)
+         {
+            mask = (mask << 1) +1; // will be fff when BitsStored=12
+         }
+
+         uint16_t *deb = (uint16_t *)Raw;
+         for (i=0; i<RawSize/2; i++)      
+         {
+            *deb = mask - *deb;
+            deb++;
+         }
+         return;
+       }
+   }
+   else
+   {
+      if ( BitsAllocated == 8 )
+      {
+         uint8_t smask8 = 255;
+         uint8_t *deb = (uint8_t *)Raw;
+         for (i=0; i<RawSize; i++)      
+         {
+            *deb = smask8 - *deb;
+            deb++;
+         }
+         return;
+      }
+      if ( BitsAllocated == 16 )
+      {
+         uint16_t smask16 = 65535;
+         uint16_t *deb = (uint16_t *)Raw;
+         for (i=0; i<RawSize/2; i++)      
+         {
+            *deb = smask16 - *deb;
+            deb++;
+         }
+         return;
+      }
+   }
+}
+
 /**
  * \brief  Re-arrange the bits within the bytes.
- * @return Boolean
+ * @return Boolean always true
  */
 bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
 {
@@ -724,10 +816,10 @@ void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels()
    int l        = XSize * YSize;
    int nbFrames = ZSize;
 
-   uint8_t *a = copyRaw;
+   uint8_t *a = copyRaw + 0;
    uint8_t *b = copyRaw + l;
-   uint8_t *c = copyRaw + l + l;
-   double R, G, B;
+   uint8_t *c = copyRaw + l+ l;
+   int32_t R, G, B;
 
    /// \todo : Replace by the 'well known' integer computation
    ///         counterpart. Refer to
@@ -738,16 +830,20 @@ void PixelReadConvert::ConvertYcBcRPlanesToRGBPixels()
    {
       for ( int j = 0; j < l; j++ )
       {
-         R = 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
-         G = 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
-         B = 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
-
-         if (R < 0.0)   R = 0.0;
-         if (G < 0.0)   G = 0.0;
-         if (B < 0.0)   B = 0.0;
-         if (R > 255.0) R = 255.0;
-         if (G > 255.0) G = 255.0;
-         if (B > 255.0) B = 255.0;
+         R = 38142 *(*a-16) + 52298 *(*c -128);
+         G = 38142 *(*a-16) - 26640 *(*c -128) - 12845 *(*b -128);
+         B = 38142 *(*a-16) + 66093 *(*b -128);
+
+         R = (R+16384)>>15;
+         G = (G+16384)>>15;
+         B = (B+16384)>>15;
+
+         if (R < 0)   R = 0;
+         if (G < 0)   G = 0;
+         if (B < 0)   B = 0;
+         if (R > 255) R = 255;
+         if (G > 255) G = 255;
+         if (B > 255) B = 255;
 
          *(localRaw++) = (uint8_t)R;
          *(localRaw++) = (uint8_t)G;