gamma.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003     FILE:  gamma.h
00004 
00005     DESCRIPTION:  Gamma utilities
00006 
00007     CREATED BY: Dan Silva
00008 
00009     HISTORY: created 26 December 1995
00010 
00011  *> Copyright (c) 1995, All Rights Reserved.
00012  **********************************************************************/
00013 
00014 
00015 #pragma once
00016 #include "maxheap.h"
00017 #include <WTypes.h>
00018 #include "maxtypes.h"
00019 #include "coreexp.h"
00020 #include "color.h"
00021 
00022 #define WRDMAX 65535
00023 #define FWRDMAX 65535.0f
00024 
00025 #define RCBITS 13   // number of bits used to represent colors before gamma correction.
00026                     // this keeps the lookup table a reasonable size
00027 #define RCOLN (1<<RCBITS)    
00028 #define RCMAX (RCOLN-1) 
00029 #define FRCMAX ((float)RCMAX) 
00030 #define RCHALF (RCOLN>>1)
00031 #define RCSH (RCBITS-8)       /* shift amount from 8 bit to RCBITS */
00032 #define RCSH16 (16-RCBITS)    /* shift amount from 16 bit to RCBITS */
00033 #define RCFRACMASK ((ulong)((1<<RCSH)-1))     
00034 #define RC_SCL (1<<RCSH)
00035 #define RC_SCLHALF (1<<(RCSH-1))
00036 #define FRC_SCL ((float)RC_SCL)
00037 #define RCSHMASK (0xffffffffL<<RCSH)
00038 #define RCSHMAX (0xffL<<RCSH)
00039 
00040 
00041 #define GAMMA_NTSC      2.2f
00042 #define GAMMA_PAL       2.8f
00043 
00088 class GammaMgr: public MaxHeapOperators {
00089     public:
00090         BOOL enable;
00091         BOOL dithTrue;
00092         BOOL dithPaletted;
00093         float dispGamma;
00094         float fileInGamma;
00095         float fileOutGamma;
00096         UBYTE disp_gamtab[256];       // (8->8) display gamma for drawing color swatches (8->8) 
00097         UBYTE disp_gamtabw[RCOLN];    // (RCBITS->8) display gamma 
00098         UBYTE file_in_gamtab[256];    // (8->8) 
00099         UWORD file_in_degamtab[256];  // (8->16)  for de-gamifying bitmaps on input
00100         UWORD file_out_gamtab[RCOLN]; // (RCBITS->16) gamma correct for file output, before dither 
00101 
00108         inline COLORREF DisplayGammaCorrect(COLORREF col) {
00109             return RGB(disp_gamtab[GetRValue(col)], disp_gamtab[GetGValue(col)], disp_gamtab[GetBValue(col)]);      
00110             }
00111 
00118         CoreExport Color DisplayGammaCorrect(Color c);
00119             
00124         CoreExport void Enable(BOOL onOff);
00127         BOOL IsEnabled() { return enable;}
00128 
00133         CoreExport void  SetDisplayGamma(float gam);
00135         float GetDisplayGamma() { return dispGamma; }
00136 
00141         CoreExport void SetFileInGamma(float gam);
00143         float GetFileInGamma() { return fileInGamma; }
00144 
00149         CoreExport void SetFileOutGamma(float gam);
00151         float GetFileOutGamma() { return fileOutGamma; }
00152 
00154         GammaMgr();
00155 
00156 
00157     };
00158 
00159 CoreExport extern GammaMgr gammaMgr;
00160 
00161 
00167 inline COLORREF gammaCorrect(DWORD c) { return gammaMgr.DisplayGammaCorrect(c); }
00173 inline UBYTE gammaCorrect(UBYTE b) { return gammaMgr.disp_gamtab[b]; }
00174 
00175 
00176 #define GAMMA16to8(b)  gammaMgr.disp_gamtabw[b>>RCSH16]
00177 
00178 // Build Gamma table that maps 8->8  
00187 CoreExport void BuildGammaTab8(UBYTE gamtab[256], float gamma, int onoff=TRUE);
00188 
00189 // Build a Gamma table that maps 8->16
00198 CoreExport void BuildGammaTab8(UWORD gamtab[256], float gamma, int onoff=TRUE);
00199 
00200 // Build Gamma table that maps RCBITS->8
00209 CoreExport void BuildGammaTab(UBYTE gamtab[RCOLN], float gamma, int onoff=TRUE);
00210 
00211 // Build Gamma table that  maps RCBITS->16
00220 CoreExport void BuildGammaTab(UWORD gamtab[RCOLN], float gamma, int onoff=TRUE);
00221 
00230 CoreExport float gammaCorrect(float v, float gamma);
00238 CoreExport float deGammaCorrect(float v, float gamma);
00247 CoreExport UBYTE gammaCorrect(UBYTE v, float gamma);
00255 CoreExport UBYTE deGammaCorrect(UBYTE v, float gamma);
00264 CoreExport UWORD gammaCorrect(UWORD c, float gamma);
00272 CoreExport UWORD deGammaCorrect(UWORD c, float gamma);
00273 
00274 
00275 // Temporary table for converting 16->16.
00282 class GamConvert16: public MaxHeapOperators {
00283     float gamma;
00284     UWORD* gtab;
00285     public: 
00288         GamConvert16(float gam=1.0f);  
00290         ~GamConvert16();  
00296         void SetGamma(float gam);
00302         UWORD Convert(UWORD v) { return gtab[v>>RCSH16]; }
00303 
00304     };
00305 
00306 // Temporary table for converting 8->16.
00313 class GamConvert8: public MaxHeapOperators {
00314     float gamma;
00315     UWORD gtab[256];
00316     public: 
00319         GamConvert8(float gam=1.0f);  
00325         void SetGamma(float gam);
00331         UWORD Convert(UBYTE v) { return gtab[v]; }
00332 
00333     };
00334