1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 
5 This file is part of Quake III Arena source code.
6 
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11 
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22 
23 #ifdef USE_LOCAL_HEADERS
24 #	include "SDL.h"
25 #else
26 #	include <SDL.h>
27 #endif
28 
29 #include "../renderer/tr_local.h"
30 #include "../qcommon/qcommon.h"
31 
32 extern SDL_Window *SDL_window;
33 
34 /*
35 =================
36 GLimp_SetGamma
37 =================
38 */
GLimp_SetGamma(unsigned char red[256],unsigned char green[256],unsigned char blue[256])39 void GLimp_SetGamma( unsigned char red[256], unsigned char green[256], unsigned char blue[256] )
40 {
41 	Uint16 table[3][256];
42 	int i, j;
43 
44 	if( !glConfig.deviceSupportsGamma || r_ignorehwgamma->integer > 0 )
45 		return;
46 
47 	for (i = 0; i < 256; i++)
48 	{
49 		table[0][i] = ( ( ( Uint16 ) red[i] ) << 8 ) | red[i];
50 		table[1][i] = ( ( ( Uint16 ) green[i] ) << 8 ) | green[i];
51 		table[2][i] = ( ( ( Uint16 ) blue[i] ) << 8 ) | blue[i];
52 	}
53 
54 #ifdef _WIN32
55 #include <windows.h>
56 
57 	// Win2K and newer put this odd restriction on gamma ramps...
58 	{
59 		OSVERSIONINFO	vinfo;
60 
61 		vinfo.dwOSVersionInfoSize = sizeof( vinfo );
62 		GetVersionEx( &vinfo );
63 		if( vinfo.dwMajorVersion >= 5 && vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT )
64 		{
65 			ri.Printf( PRINT_DEVELOPER, "performing gamma clamp.\n" );
66 			for( j = 0 ; j < 3 ; j++ )
67 			{
68 				for( i = 0 ; i < 128 ; i++ )
69 				{
70 					if( table[ j ] [ i] > ( ( 128 + i ) << 8 ) )
71 						table[ j ][ i ] = ( 128 + i ) << 8;
72 				}
73 
74 				if( table[ j ] [127 ] > 254 << 8 )
75 					table[ j ][ 127 ] = 254 << 8;
76 			}
77 		}
78 	}
79 #endif
80 
81 	// enforce constantly increasing
82 	for (j = 0; j < 3; j++)
83 	{
84 		for (i = 1; i < 256; i++)
85 		{
86 			if (table[j][i] < table[j][i-1])
87 				table[j][i] = table[j][i-1];
88 		}
89 	}
90 
91 	if (SDL_SetWindowGammaRamp(SDL_window, table[0], table[1], table[2]) < 0)
92 	{
93 		ri.Printf( PRINT_DEVELOPER, "SDL_SetWindowGammaRamp() failed: %s\n", SDL_GetError() );
94 	}
95 }
96 
97