home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / BCBViewer / EditableBitmap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-18  |  3.5 KB  |  129 lines

  1. //
  2. // Copyright (c) 1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // Permission to use, redistribute, and copy this file is granted
  9. // without a fee so long as as the following conditions are adhered to:
  10. //
  11. // o The user assumes all risk for using this software. The authors of this
  12. //   software shall be liable for no damages of any kind.
  13. //
  14. // o If the source code is distributed then this copyright notice must
  15. //   remain unaltered and any modification must be noted.
  16. //
  17. // o If this code is shipped in binary format the accompanying documentation
  18. //   should state that "this software is based, in part, on the work of
  19. //   Colosseum Builders, Inc."
  20. //
  21.  
  22. //
  23. //  Title:  Sample Image Viewer/Format Conversion Application
  24. //
  25. //  Author:  John M. Miano miano@colosseumbuilders.com
  26. //
  27. #include "EditableBitmap.h"
  28.  
  29. #include <math.h>
  30. #include "bitimage.h"
  31. #include "jfif.h"
  32.  
  33. const double MaxColor = 255.0 ;
  34.  
  35. EditableBitmapImage &EditableBitmapImage::operator=(
  36.                                    const BitmapImage &source)
  37. {
  38.   BitmapImage::operator=(source) ;
  39.   return *this ;
  40. }
  41.  
  42.  
  43.  
  44. void EditableBitmapImage::GammaCorrect (double gamma)
  45. {
  46.   if (BitCount () == 24)
  47.   {
  48.     for (unsigned int ii = 0 ; ii < Height () ; ++ ii)
  49.     {
  50.       for (unsigned int jj = 0 ; jj < 3 * Width () ; ++ jj)
  51.       {
  52.         if ((*this)[ii][jj] != 0)
  53.         {
  54.           double value ;
  55.           value = MaxColor * exp (gamma * log ((*this) [ii][jj]/MaxColor)) ;
  56.           (*this) [ii][jj] = value + 0.5 ;
  57.         }
  58.       }
  59.     }
  60.   }
  61.   else
  62.   {
  63.     for (unsigned int ii = 0 ; ii < ColorCount () ; ++ ii)
  64.     {
  65.       double value ;
  66.       if (ColorMap (ii).red != 0)
  67.       {
  68.         value = MaxColor * exp (gamma * log (ColorMap (ii).red/MaxColor)) ;
  69.         ColorMap (ii).red = value + 0.5 ;
  70.       }
  71.       if (ColorMap (ii).green != 0)
  72.       {
  73.         value = MaxColor * exp (gamma * log (ColorMap (ii).green/MaxColor)) ;
  74.         ColorMap (ii).green = value + 0.5 ;
  75.       }
  76.       if (ColorMap (ii).blue != 0)
  77.       {
  78.         value = MaxColor * exp (gamma * log (ColorMap (ii).blue/MaxColor)) ;
  79.         ColorMap (ii).blue = value + 0.5 ;
  80.       }
  81.     }
  82.   }
  83.   return ;
  84. }
  85.  
  86. void EditableBitmapImage::ToGrayscale ()
  87. {
  88.   BitmapImage image ;
  89.   if (BitCount () == 24)
  90.   {
  91.     image.SetSize (256, 8, Width (), Height ()) ;
  92.     for (unsigned int ii = 0 ; ii < 256 ; ++ ii)
  93.     {
  94.       image.ColorMap (ii).red = ii ;
  95.       image.ColorMap (ii).green = ii ;
  96.       image.ColorMap (ii).blue = ii ;
  97.     }
  98.     for (unsigned int ii = 0 ; ii < Height () ; ++ ii)
  99.     {
  100.       for (unsigned int jj = 0 ; jj < Width () ; ++ jj)
  101.       {
  102.         unsigned int value = RGBToY (
  103.           (*this)[ii][3*jj+BitmapImage::RedOffset],
  104.           (*this)[ii][3*jj+BitmapImage::GreenOffset],
  105.           (*this)[ii][3*jj+BitmapImage::BlueOffset]) ;
  106.         image [ii][jj] = value ;
  107.       }
  108.     }
  109.   }
  110.   else
  111.   {
  112.     // Color adjustments are much easier on palette images.
  113.     image = *this ;
  114.     for (unsigned int ii = 0 ; ii < ColorCount () ; ++ ii)
  115.     {
  116.       unsigned int value = RGBToY (
  117.           ColorMap (ii).red,
  118.           ColorMap (ii).green,
  119.           ColorMap (ii).blue) ;
  120.       ColorMap (ii).red = value ;
  121.       ColorMap (ii).green = value ;
  122.       ColorMap (ii).blue = value ;
  123.     }
  124.   }
  125.   *this = image ;
  126.   return ;
  127. }
  128.  
  129.