Updated to latest source.
[AROS-Contrib.git] / SDL / SDL_gamma.c
blob9c20d148ae80935250174778b28058fd6a117215
1 /*
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
19 Sam Lantinga
20 slouken@libsdl.org
23 #ifdef SAVE_RCSID
24 static char rcsid =
25 "@(#) $Id$";
26 #endif
28 /* Gamma correction support */
30 #define USE_MATH_H /* Used for calculating gamma ramps */
32 #ifdef USE_MATH_H
33 #include <math.h>
34 #endif
35 #include <stdlib.h>
36 #include <string.h>
38 #include "SDL_error.h"
39 #include "SDL_sysvideo.h"
41 #ifdef USE_MATH_H
42 static void CalculateGammaRamp(float gamma, Uint16 *ramp)
44 int i;
46 /* 0.0 gamma is all black */
47 if ( gamma <= 0.0 ) {
48 for ( i=0; i<256; ++i ) {
49 ramp[i] = 0;
51 return;
52 } else
53 /* 1.0 gamma is identity */
54 if ( gamma == 1.0 ) {
55 for ( i=0; i<256; ++i ) {
56 ramp[i] = (i << 8) | i;
58 return;
59 } else
60 /* Calculate a real gamma ramp */
61 { int value;
62 gamma = 1.0f / gamma;
63 for ( i=0; i<256; ++i ) {
64 value = (int)(pow((double)i/256.0, gamma)*65535.0+0.5);
65 if ( value > 65535 ) {
66 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.
77 float sum = 0.0;
78 int i, count = 0;
80 *gamma = 1.0;
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) );
86 count++;
89 if ( count && sum ) {
90 *gamma = 1.0f / (sum / count);
93 #endif /* USE_MATH_H */
95 int SDL_SetGamma(float red, float green, float blue)
97 int succeeded;
98 SDL_VideoDevice *video = current_video;
99 SDL_VideoDevice *this = current_video;
101 succeeded = -1;
102 #ifdef USE_MATH_H
103 /* Prefer using SetGammaRamp(), as it's more flexible */
105 Uint16 ramp[3][256];
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]);
112 #else
113 SDL_SetError("Gamma correction not supported");
114 #endif
115 if ( (succeeded < 0) && video->SetGamma ) {
116 SDL_ClearError();
117 succeeded = video->SetGamma(this, red, green, blue);
119 return succeeded;
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)
127 int succeeded;
128 SDL_VideoDevice *video = current_video;
129 SDL_VideoDevice *this = current_video;
131 succeeded = -1;
132 #ifdef USE_MATH_H
133 /* Prefer using GetGammaRamp(), as it's more flexible */
135 Uint16 ramp[3][256];
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]);
144 #else
145 SDL_SetError("Gamma correction not supported");
146 #endif
147 if ( (succeeded < 0) && video->GetGamma ) {
148 SDL_ClearError();
149 succeeded = video->GetGamma(this, red, green, blue);
151 return succeeded;
154 int SDL_SetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
156 int succeeded;
157 SDL_VideoDevice *video = current_video;
158 SDL_VideoDevice *this = current_video;
159 SDL_Surface *screen = SDL_PublicSurface;
161 /* Verify the screen parameter */
162 if ( !screen ) {
163 SDL_SetError("No video mode has been set");
164 return -1;
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 */
173 if ( red ) {
174 memcpy(&video->gamma[0*256], red, 256*sizeof(*video->gamma));
176 if ( green ) {
177 memcpy(&video->gamma[1*256], green, 256*sizeof(*video->gamma));
179 if ( blue ) {
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 */
188 if(video->physpal)
189 pal = video->physpal;
191 SDL_SetPalette(screen, SDL_PHYSPAL,
192 pal->colors, 0, pal->ncolors);
193 return 0;
196 /* Try to set the gamma ramp in the driver */
197 succeeded = -1;
198 if ( video->SetGammaRamp ) {
199 succeeded = video->SetGammaRamp(this, video->gamma);
200 } else {
201 SDL_SetError("Gamma ramp manipulation not supported");
203 return succeeded;
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 ) {
215 SDL_OutOfMemory();
216 return -1;
218 if ( video->GetGammaRamp ) {
219 /* Get the real hardware gamma */
220 video->GetGammaRamp(this, video->gamma);
221 } else {
222 /* Assume an identity gamma */
223 int i;
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 */
233 if ( red ) {
234 memcpy(red, &video->gamma[0*256], 256*sizeof(*red));
236 if ( green ) {
237 memcpy(green, &video->gamma[1*256], 256*sizeof(*green));
239 if ( blue ) {
240 memcpy(blue, &video->gamma[2*256], 256*sizeof(*blue));
242 return 0;