Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

win_gamma.c

Go to the documentation of this file.
00001 /*
00002 ===========================================================================
00003 Copyright (C) 1999-2005 Id Software, Inc.
00004 
00005 This file is part of Quake III Arena source code.
00006 
00007 Quake III Arena source code is free software; you can redistribute it
00008 and/or modify it under the terms of the GNU General Public License as
00009 published by the Free Software Foundation; either version 2 of the License,
00010 or (at your option) any later version.
00011 
00012 Quake III Arena source code is distributed in the hope that it will be
00013 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Foobar; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 ===========================================================================
00021 */
00022 /*
00023 ** WIN_GAMMA.C
00024 */
00025 #include <assert.h>
00026 #include "../renderer/tr_local.h"
00027 #include "../qcommon/qcommon.h"
00028 #include "glw_win.h"
00029 #include "win_local.h"
00030 
00031 static unsigned short s_oldHardwareGamma[3][256];
00032 
00033 /*
00034 ** WG_CheckHardwareGamma
00035 **
00036 ** Determines if the underlying hardware supports the Win32 gamma correction API.
00037 */
00038 void WG_CheckHardwareGamma( void )
00039 {
00040     HDC         hDC;
00041 
00042     glConfig.deviceSupportsGamma = qfalse;
00043 
00044     if ( qwglSetDeviceGammaRamp3DFX )
00045     {
00046         glConfig.deviceSupportsGamma = qtrue;
00047 
00048         hDC = GetDC( GetDesktopWindow() );
00049         glConfig.deviceSupportsGamma = qwglGetDeviceGammaRamp3DFX( hDC, s_oldHardwareGamma );
00050         ReleaseDC( GetDesktopWindow(), hDC );
00051 
00052         return;
00053     }
00054 
00055     // non-3Dfx standalone drivers don't support gamma changes, period
00056     if ( glConfig.driverType == GLDRV_STANDALONE )
00057     {
00058         return;
00059     }
00060 
00061     if ( !r_ignorehwgamma->integer )
00062     {
00063         hDC = GetDC( GetDesktopWindow() );
00064         glConfig.deviceSupportsGamma = GetDeviceGammaRamp( hDC, s_oldHardwareGamma );
00065         ReleaseDC( GetDesktopWindow(), hDC );
00066 
00067         if ( glConfig.deviceSupportsGamma )
00068         {
00069             //
00070             // do a sanity check on the gamma values
00071             //
00072             if ( ( HIBYTE( s_oldHardwareGamma[0][255] ) <= HIBYTE( s_oldHardwareGamma[0][0] ) ) ||
00073                  ( HIBYTE( s_oldHardwareGamma[1][255] ) <= HIBYTE( s_oldHardwareGamma[1][0] ) ) ||
00074                  ( HIBYTE( s_oldHardwareGamma[2][255] ) <= HIBYTE( s_oldHardwareGamma[2][0] ) ) )
00075             {
00076                 glConfig.deviceSupportsGamma = qfalse;
00077                 ri.Printf( PRINT_WARNING, "WARNING: device has broken gamma support, generated gamma.dat\n" );
00078             }
00079 
00080             //
00081             // make sure that we didn't have a prior crash in the game, and if so we need to
00082             // restore the gamma values to at least a linear value
00083             //
00084             if ( ( HIBYTE( s_oldHardwareGamma[0][181] ) == 255 ) )
00085             {
00086                 int g;
00087 
00088                 ri.Printf( PRINT_WARNING, "WARNING: suspicious gamma tables, using linear ramp for restoration\n" );
00089 
00090                 for ( g = 0; g < 255; g++ )
00091                 {
00092                     s_oldHardwareGamma[0][g] = g << 8;
00093                     s_oldHardwareGamma[1][g] = g << 8;
00094                     s_oldHardwareGamma[2][g] = g << 8;
00095                 }
00096             }
00097         }
00098     }
00099 }
00100 
00101 /*
00102 void mapGammaMax( void ) {
00103     int     i, j;
00104     unsigned short table[3][256];
00105 
00106     // try to figure out what win2k will let us get away with setting
00107     for ( i = 0 ; i < 256 ; i++ ) {
00108         if ( i >= 128 ) {
00109             table[0][i] = table[1][i] = table[2][i] = 0xffff;
00110         } else {
00111             table[0][i] = table[1][i] = table[2][i] = i<<9;
00112         }
00113     }
00114 
00115     for ( i = 0 ; i < 128 ; i++ ) {
00116         for ( j = i*2 ; j < 255 ; j++ ) {
00117             table[0][i] = table[1][i] = table[2][i] = j<<8;
00118             if ( !SetDeviceGammaRamp( glw_state.hDC, table ) ) {
00119                 break;
00120             }
00121         }
00122         table[0][i] = table[1][i] = table[2][i] = i<<9;
00123         Com_Printf( "index %i max: %i\n", i, j-1 );
00124     }
00125 }
00126 */
00127 
00128 /*
00129 ** GLimp_SetGamma
00130 **
00131 ** This routine should only be called if glConfig.deviceSupportsGamma is TRUE
00132 */
00133 void GLimp_SetGamma( unsigned char red[256], unsigned char green[256], unsigned char blue[256] ) {
00134     unsigned short table[3][256];
00135     int     i, j;
00136     int     ret;
00137     OSVERSIONINFO   vinfo;
00138 
00139     if ( !glConfig.deviceSupportsGamma || r_ignorehwgamma->integer || !glw_state.hDC ) {
00140         return;
00141     }
00142 
00143 //mapGammaMax();
00144 
00145     for ( i = 0; i < 256; i++ ) {
00146         table[0][i] = ( ( ( unsigned short ) red[i] ) << 8 ) | red[i];
00147         table[1][i] = ( ( ( unsigned short ) green[i] ) << 8 ) | green[i];
00148         table[2][i] = ( ( ( unsigned short ) blue[i] ) << 8 ) | blue[i];
00149     }
00150 
00151     // Win2K puts this odd restriction on gamma ramps...
00152     vinfo.dwOSVersionInfoSize = sizeof(vinfo);
00153     GetVersionEx( &vinfo );
00154     if ( vinfo.dwMajorVersion == 5 && vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
00155         Com_DPrintf( "performing W2K gamma clamp.\n" );
00156         for ( j = 0 ; j < 3 ; j++ ) {
00157             for ( i = 0 ; i < 128 ; i++ ) {
00158                 if ( table[j][i] > ( (128+i) << 8 ) ) {
00159                     table[j][i] = (128+i) << 8;
00160                 }
00161             }
00162             if ( table[j][127] > 254<<8 ) {
00163                 table[j][127] = 254<<8;
00164             }
00165         }
00166     } else {
00167         Com_DPrintf( "skipping W2K gamma clamp.\n" );
00168     }
00169 
00170     // enforce constantly increasing
00171     for ( j = 0 ; j < 3 ; j++ ) {
00172         for ( i = 1 ; i < 256 ; i++ ) {
00173             if ( table[j][i] < table[j][i-1] ) {
00174                 table[j][i] = table[j][i-1];
00175             }
00176         }
00177     }
00178 
00179 
00180     if ( qwglSetDeviceGammaRamp3DFX )
00181     {
00182         qwglSetDeviceGammaRamp3DFX( glw_state.hDC, table );
00183     }
00184     else
00185     {
00186         ret = SetDeviceGammaRamp( glw_state.hDC, table );
00187         if ( !ret ) {
00188             Com_Printf( "SetDeviceGammaRamp failed.\n" );
00189         }
00190     }
00191 }
00192 
00193 /*
00194 ** WG_RestoreGamma
00195 */
00196 void WG_RestoreGamma( void )
00197 {
00198     if ( glConfig.deviceSupportsGamma )
00199     {
00200         if ( qwglSetDeviceGammaRamp3DFX )
00201         {
00202             qwglSetDeviceGammaRamp3DFX( glw_state.hDC, s_oldHardwareGamma );
00203         }
00204         else
00205         {
00206             HDC hDC;
00207             
00208             hDC = GetDC( GetDesktopWindow() );
00209             SetDeviceGammaRamp( hDC, s_oldHardwareGamma );
00210             ReleaseDC( GetDesktopWindow(), hDC );
00211         }
00212     }
00213 }
00214 

Generated on Thu Aug 25 12:38:05 2005 for Quake III Arena by  doxygen 1.3.9.1