]> Creatis software - gdcm.git/blobdiff - src/gdcmPixelReadConvert.cxx
BUG: Attempt to fix borland IsDirectory
[gdcm.git] / src / gdcmPixelReadConvert.cxx
index 18315f101dcacb92377bd29a1a0bbf61ceda0549..2970f1f01e73b5b3a57493f00659e6a54f808660 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmPixelReadConvert.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/02/04 16:51:36 $
-  Version:   $Revision: 1.48 $
+  Date:      $Date: 2005/03/31 09:46:51 $
+  Version:   $Revision: 1.54 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -123,13 +123,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:
@@ -160,7 +162,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file )
       LutRedData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1201 );
       if ( ! LutRedData )
       {
-         gdcmVerboseMacro( "Unable to read Red LUT data" );
+         gdcmWarningMacro( "Unable to read Red LUT data" );
       }
 
       // //// Green round:
@@ -168,7 +170,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file )
       LutGreenData = (uint8_t*)file->GetEntryBinArea(0x0028, 0x1202 );
       if ( ! LutGreenData)
       {
-         gdcmVerboseMacro( "Unable to read Green LUT data" );
+         gdcmWarningMacro( "Unable to read Green LUT data" );
       }
 
       // //// Blue round:
@@ -176,7 +178,7 @@ void PixelReadConvert::GrabInformationsFromFile( File *file )
       LutBlueData = (uint8_t*)file->GetEntryBinArea( 0x0028, 0x1203 );
       if ( ! LutBlueData )
       {
-         gdcmVerboseMacro( "Unable to read Blue LUT data" );
+         gdcmWarningMacro( "Unable to read Blue LUT data" );
       }
    }
 
@@ -195,14 +197,14 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
    //// First stage: get our hands on the Pixel Data.
    if ( !fp )
    {
-      gdcmVerboseMacro( "Unavailable file pointer." );
+      gdcmWarningMacro( "Unavailable file pointer." );
       return false;
    }
 
    fp->seekg( PixelOffset, std::ios::beg );
    if( fp->fail() || fp->eof())
    {
-      gdcmVerboseMacro( "Unable to find PixelOffset in file." );
+      gdcmWarningMacro( "Unable to find PixelOffset in file." );
       return false;
    }
 
@@ -222,7 +224,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
       // variable). But RawSize is the right size of the image !
       if( PixelDataLength != RawSize)
       {
-         gdcmVerboseMacro( "Mismatch between PixelReadConvert : "
+         gdcmWarningMacro( "Mismatch between PixelReadConvert : "
                             << PixelDataLength << " and RawSize : " << RawSize );
       }
       if( PixelDataLength > RawSize)
@@ -236,7 +238,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
 
       if ( fp->fail() || fp->eof())
       {
-         gdcmVerboseMacro( "Reading of Raw pixel data failed." );
+         gdcmWarningMacro( "Reading of Raw pixel data failed." );
          return false;
       }
    } 
@@ -244,7 +246,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
    {
       if ( ! RLEInfo->DecompressRLEFile( fp, Raw, XSize, YSize, ZSize, BitsAllocated ) )
       {
-         gdcmVerboseMacro( "RLE decompressor failed." );
+         gdcmWarningMacro( "RLE decompressor failed." );
          return false;
       }
    }
@@ -253,7 +255,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
       // Default case concerns JPEG family
       if ( ! ReadAndDecompressJPEGFile( fp ) )
       {
-         gdcmVerboseMacro( "JPEG decompressor failed." );
+         gdcmWarningMacro( "JPEG decompressor failed." );
          return false;
       }
    }
@@ -262,6 +264,7 @@ bool PixelReadConvert::ReadAndDecompressPixelData( std::ifstream *fp )
    //// Third stage: twigle the bytes and bits.
    ConvertReorderEndianity();
    ConvertReArrangeBits();
+   ConvertFixGreyLevels();
    ConvertHandleColor();
 
    return true;
@@ -383,7 +386,7 @@ bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
 {
    if ( IsJPEG2000 )
    {
-      gdcmVerboseMacro( "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 ) )
           return false;
@@ -391,7 +394,7 @@ bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
 
    if ( IsJPEGLS )
    {
-      gdcmVerboseMacro( "Sorry, JPEG-LS not yet taken into account" );
+      gdcmWarningMacro( "Sorry, JPEG-LS not yet taken into account" );
       fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
 //    if ( ! gdcm_read_JPEGLS_file( fp,Raw ) )
          return false;
@@ -452,7 +455,7 @@ void PixelReadConvert::BuildLUTRGBA()
                         &lengthR, &debR, &nbitsR );
    if( nbRead != 3 )
    {
-      gdcmVerboseMacro( "Wrong Red LUT descriptor" );
+      gdcmWarningMacro( "Wrong Red LUT descriptor" );
    }
                                                                                 
    int lengthG;  // Green LUT length in Bytes
@@ -463,7 +466,7 @@ void PixelReadConvert::BuildLUTRGBA()
                     &lengthG, &debG, &nbitsG );
    if( nbRead != 3 )
    {
-      gdcmVerboseMacro( "Wrong Green LUT descriptor" );
+      gdcmWarningMacro( "Wrong Green LUT descriptor" );
    }
                                                                                 
    int lengthB;  // Blue LUT length in Bytes
@@ -474,7 +477,7 @@ void PixelReadConvert::BuildLUTRGBA()
                     &lengthB, &debB, &nbitsB );
    if( nbRead != 3 )
    {
-      gdcmVerboseMacro( "Wrong Blue LUT descriptor" );
+      gdcmWarningMacro( "Wrong Blue LUT descriptor" );
    }
                                                                                 
    ////////////////////////////////////////////////////////
@@ -563,7 +566,7 @@ void PixelReadConvert::ConvertSwapZone()
             }
             break;
          default:
-            gdcmVerboseMacro("SwapCode value (16 bits) not allowed.");
+            gdcmWarningMacro("SwapCode value (16 bits) not allowed.");
       }
    }
    else if( BitsAllocated == 32 )
@@ -608,7 +611,7 @@ void PixelReadConvert::ConvertSwapZone()
             }
             break;
          default:
-            gdcmVerboseMacro("SwapCode value (32 bits) not allowed." );
+            gdcmWarningMacro("SwapCode value (32 bits) not allowed." );
       }
    }
 }
@@ -641,9 +644,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 )
 {
@@ -674,7 +746,7 @@ bool PixelReadConvert::ConvertReArrangeBits() throw ( FormatError )
       }
       else
       {
-         gdcmVerboseMacro("Weird image");
+         gdcmWarningMacro("Weird image");
          throw FormatError( "Weird image !?" );
       }
    }
@@ -724,10 +796,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 +810,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;
+         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.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;
+         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;
@@ -903,7 +979,7 @@ void PixelReadConvert::Print( std::ostream &os, std::string const &indent )
       }
       else
       {
-         gdcmVerboseMacro("Set as RLE file but NO RLEinfo present.");
+         gdcmWarningMacro("Set as RLE file but NO RLEinfo present.");
       }
    }
 
@@ -915,7 +991,7 @@ void PixelReadConvert::Print( std::ostream &os, std::string const &indent )
       }
       else
       {
-         gdcmVerboseMacro("Set as JPEG file but NO JPEGinfo present.");
+         gdcmWarningMacro("Set as JPEG file but NO JPEGinfo present.");
       }
    }
 }