2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /* Gamma correction support */
30 #define USE_MATH_H /* Used for calculating gamma ramps */
38 #include "SDL_error.h"
39 #include "SDL_sysvideo.h"
42 static void CalculateGammaRamp(float gamma
, Uint16
*ramp
)
46 /* 0.0 gamma is all black */
48 for ( i
=0; i
<256; ++i
) {
53 /* 1.0 gamma is identity */
55 for ( i
=0; i
<256; ++i
) {
56 ramp
[i
] = (i
<< 8) | i
;
60 /* Calculate a real gamma ramp */
63 for ( i
=0; i
<256; ++i
) {
64 value
= (int)(pow((double)i
/256.0, gamma
)*65535.0+0.5);
65 if ( value
> 65535 ) {
68 ramp
[i
] = (Uint16
)value
;
72 static void CalculateGammaFromRamp(float *gamma
, Uint16
*ramp
)
74 /* The following is adapted from a post by Garrett Bass on OpenGL
75 Gamedev list, March 4, 2000.
81 for ( i
= 1; i
< 256; ++i
) {
82 if ( (ramp
[i
] != 0) && (ramp
[i
] != 65535) ) {
83 double B
= (double)i
/ 256.0;
84 double A
= ramp
[i
] / 65535.0;
85 sum
+= (float) ( log(A
) / log(B
) );
90 *gamma
= 1.0f
/ (sum
/ count
);
93 #endif /* USE_MATH_H */
95 int SDL_SetGamma(float red
, float green
, float blue
)
98 SDL_VideoDevice
*video
= current_video
;
99 SDL_VideoDevice
*this = current_video
;
103 /* Prefer using SetGammaRamp(), as it's more flexible */
107 CalculateGammaRamp(red
, ramp
[0]);
108 CalculateGammaRamp(green
, ramp
[1]);
109 CalculateGammaRamp(blue
, ramp
[2]);
110 succeeded
= SDL_SetGammaRamp(ramp
[0], ramp
[1], ramp
[2]);
113 SDL_SetError("Gamma correction not supported");
115 if ( (succeeded
< 0) && video
->SetGamma
) {
117 succeeded
= video
->SetGamma(this, red
, green
, blue
);
122 /* Calculating the gamma by integrating the gamma ramps isn't exact,
123 so this function isn't officially supported.
125 int SDL_GetGamma(float *red
, float *green
, float *blue
)
128 SDL_VideoDevice
*video
= current_video
;
129 SDL_VideoDevice
*this = current_video
;
133 /* Prefer using GetGammaRamp(), as it's more flexible */
137 succeeded
= SDL_GetGammaRamp(ramp
[0], ramp
[1], ramp
[2]);
138 if ( succeeded
>= 0 ) {
139 CalculateGammaFromRamp(red
, ramp
[0]);
140 CalculateGammaFromRamp(green
, ramp
[1]);
141 CalculateGammaFromRamp(blue
, ramp
[2]);
145 SDL_SetError("Gamma correction not supported");
147 if ( (succeeded
< 0) && video
->GetGamma
) {
149 succeeded
= video
->GetGamma(this, red
, green
, blue
);
154 int SDL_SetGammaRamp(Uint16
*red
, Uint16
*green
, Uint16
*blue
)
157 SDL_VideoDevice
*video
= current_video
;
158 SDL_VideoDevice
*this = current_video
;
159 SDL_Surface
*screen
= SDL_PublicSurface
;
161 /* Verify the screen parameter */
163 SDL_SetError("No video mode has been set");
167 /* Lazily allocate the gamma tables */
168 if ( ! video
->gamma
) {
169 SDL_GetGammaRamp(0, 0, 0);
172 /* Fill the gamma table with the new values */
174 memcpy(&video
->gamma
[0*256], red
, 256*sizeof(*video
->gamma
));
177 memcpy(&video
->gamma
[1*256], green
, 256*sizeof(*video
->gamma
));
180 memcpy(&video
->gamma
[2*256], blue
, 256*sizeof(*video
->gamma
));
183 /* Gamma correction always possible on split palettes */
184 if ( (screen
->flags
& SDL_HWPALETTE
) == SDL_HWPALETTE
) {
185 SDL_Palette
*pal
= screen
->format
->palette
;
187 /* If physical palette has been set independently, use it */
189 pal
= video
->physpal
;
191 SDL_SetPalette(screen
, SDL_PHYSPAL
,
192 pal
->colors
, 0, pal
->ncolors
);
196 /* Try to set the gamma ramp in the driver */
198 if ( video
->SetGammaRamp
) {
199 succeeded
= video
->SetGammaRamp(this, video
->gamma
);
201 SDL_SetError("Gamma ramp manipulation not supported");
206 int SDL_GetGammaRamp(Uint16
*red
, Uint16
*green
, Uint16
*blue
)
208 SDL_VideoDevice
*video
= current_video
;
209 SDL_VideoDevice
*this = current_video
;
211 /* Lazily allocate the gamma table */
212 if ( ! video
->gamma
) {
213 video
->gamma
= malloc(3*256*sizeof(*video
->gamma
));
214 if ( ! video
->gamma
) {
218 if ( video
->GetGammaRamp
) {
219 /* Get the real hardware gamma */
220 video
->GetGammaRamp(this, video
->gamma
);
222 /* Assume an identity gamma */
224 for ( i
=0; i
<256; ++i
) {
225 video
->gamma
[0*256+i
] = (i
<< 8) | i
;
226 video
->gamma
[1*256+i
] = (i
<< 8) | i
;
227 video
->gamma
[2*256+i
] = (i
<< 8) | i
;
232 /* Just copy from our internal table */
234 memcpy(red
, &video
->gamma
[0*256], 256*sizeof(*red
));
237 memcpy(green
, &video
->gamma
[1*256], 256*sizeof(*green
));
240 memcpy(blue
, &video
->gamma
[2*256], 256*sizeof(*blue
));