Mercurial > sdl-ios-xcode
annotate src/video/SDL_gamma.c @ 1358:c71e05b4dc2e
More header massaging... works great on Windows. ;-)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 10 Feb 2006 06:48:43 +0000 |
parents | 604d73db6802 |
children | d910939febfa |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
152
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* Gamma correction support */ | |
24 | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
25 #include "SDL_config.h" |
0 | 26 |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
27 #ifdef HAVE_MATH_H |
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
28 #include <math.h> /* Used for calculating gamma ramps */ |
1334
37d43bd654d7
Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
29 #else |
37d43bd654d7
Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
30 /* Math routines from uClibc: http://www.uclibc.org */ |
37d43bd654d7
Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
31 #include "math_private.h" |
37d43bd654d7
Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
32 #include "e_sqrt.h" |
37d43bd654d7
Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
33 #include "e_pow.h" |
37d43bd654d7
Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
34 #include "e_log.h" |
37d43bd654d7
Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
35 #define pow(x, y) __ieee754_pow(x, y) |
37d43bd654d7
Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
36 #define log(x) __ieee754_log(x) |
0 | 37 #endif |
38 | |
39 #include "SDL_sysvideo.h" | |
40 | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
41 |
0 | 42 static void CalculateGammaRamp(float gamma, Uint16 *ramp) |
43 { | |
44 int i; | |
45 | |
46 /* 0.0 gamma is all black */ | |
47 if ( gamma <= 0.0 ) { | |
48 for ( i=0; i<256; ++i ) { | |
49 ramp[i] = 0; | |
50 } | |
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; | |
57 } | |
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; | |
67 } | |
68 ramp[i] = (Uint16)value; | |
69 } | |
70 } | |
71 } | |
72 static void CalculateGammaFromRamp(float *gamma, Uint16 *ramp) | |
73 { | |
74 /* The following is adapted from a post by Garrett Bass on OpenGL | |
75 Gamedev list, March 4, 2000. | |
76 */ | |
77 float sum = 0.0; | |
78 int i, count = 0; | |
79 | |
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++; | |
87 } | |
88 } | |
89 if ( count && sum ) { | |
90 *gamma = 1.0f / (sum / count); | |
91 } | |
92 } | |
93 | |
94 int SDL_SetGamma(float red, float green, float blue) | |
95 { | |
96 int succeeded; | |
97 SDL_VideoDevice *video = current_video; | |
98 SDL_VideoDevice *this = current_video; | |
99 | |
100 succeeded = -1; | |
101 /* Prefer using SetGammaRamp(), as it's more flexible */ | |
102 { | |
103 Uint16 ramp[3][256]; | |
104 | |
105 CalculateGammaRamp(red, ramp[0]); | |
106 CalculateGammaRamp(green, ramp[1]); | |
107 CalculateGammaRamp(blue, ramp[2]); | |
108 succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]); | |
109 } | |
110 if ( (succeeded < 0) && video->SetGamma ) { | |
111 SDL_ClearError(); | |
112 succeeded = video->SetGamma(this, red, green, blue); | |
113 } | |
114 return succeeded; | |
115 } | |
116 | |
117 /* Calculating the gamma by integrating the gamma ramps isn't exact, | |
118 so this function isn't officially supported. | |
119 */ | |
120 int SDL_GetGamma(float *red, float *green, float *blue) | |
121 { | |
122 int succeeded; | |
123 SDL_VideoDevice *video = current_video; | |
124 SDL_VideoDevice *this = current_video; | |
125 | |
126 succeeded = -1; | |
127 /* Prefer using GetGammaRamp(), as it's more flexible */ | |
128 { | |
129 Uint16 ramp[3][256]; | |
130 | |
131 succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]); | |
132 if ( succeeded >= 0 ) { | |
133 CalculateGammaFromRamp(red, ramp[0]); | |
134 CalculateGammaFromRamp(green, ramp[1]); | |
135 CalculateGammaFromRamp(blue, ramp[2]); | |
136 } | |
137 } | |
138 if ( (succeeded < 0) && video->GetGamma ) { | |
139 SDL_ClearError(); | |
140 succeeded = video->GetGamma(this, red, green, blue); | |
141 } | |
142 return succeeded; | |
143 } | |
144 | |
669
37bb90b3d976
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
145 int SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16 *blue) |
0 | 146 { |
147 int succeeded; | |
148 SDL_VideoDevice *video = current_video; | |
149 SDL_VideoDevice *this = current_video; | |
150 SDL_Surface *screen = SDL_PublicSurface; | |
151 | |
152 /* Verify the screen parameter */ | |
153 if ( !screen ) { | |
154 SDL_SetError("No video mode has been set"); | |
155 return -1; | |
156 } | |
157 | |
158 /* Lazily allocate the gamma tables */ | |
159 if ( ! video->gamma ) { | |
160 SDL_GetGammaRamp(0, 0, 0); | |
161 } | |
162 | |
163 /* Fill the gamma table with the new values */ | |
164 if ( red ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1334
diff
changeset
|
165 SDL_memcpy(&video->gamma[0*256], red, 256*sizeof(*video->gamma)); |
0 | 166 } |
167 if ( green ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1334
diff
changeset
|
168 SDL_memcpy(&video->gamma[1*256], green, 256*sizeof(*video->gamma)); |
0 | 169 } |
170 if ( blue ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1334
diff
changeset
|
171 SDL_memcpy(&video->gamma[2*256], blue, 256*sizeof(*video->gamma)); |
0 | 172 } |
173 | |
174 /* Gamma correction always possible on split palettes */ | |
175 if ( (screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) { | |
176 SDL_Palette *pal = screen->format->palette; | |
177 | |
178 /* If physical palette has been set independently, use it */ | |
179 if(video->physpal) | |
180 pal = video->physpal; | |
181 | |
182 SDL_SetPalette(screen, SDL_PHYSPAL, | |
183 pal->colors, 0, pal->ncolors); | |
184 return 0; | |
185 } | |
186 | |
187 /* Try to set the gamma ramp in the driver */ | |
188 succeeded = -1; | |
189 if ( video->SetGammaRamp ) { | |
190 succeeded = video->SetGammaRamp(this, video->gamma); | |
191 } else { | |
192 SDL_SetError("Gamma ramp manipulation not supported"); | |
193 } | |
194 return succeeded; | |
195 } | |
196 | |
197 int SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue) | |
198 { | |
199 SDL_VideoDevice *video = current_video; | |
200 SDL_VideoDevice *this = current_video; | |
201 | |
202 /* Lazily allocate the gamma table */ | |
203 if ( ! video->gamma ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1334
diff
changeset
|
204 video->gamma = SDL_malloc(3*256*sizeof(*video->gamma)); |
0 | 205 if ( ! video->gamma ) { |
206 SDL_OutOfMemory(); | |
207 return -1; | |
208 } | |
209 if ( video->GetGammaRamp ) { | |
210 /* Get the real hardware gamma */ | |
211 video->GetGammaRamp(this, video->gamma); | |
212 } else { | |
213 /* Assume an identity gamma */ | |
214 int i; | |
215 for ( i=0; i<256; ++i ) { | |
216 video->gamma[0*256+i] = (i << 8) | i; | |
217 video->gamma[1*256+i] = (i << 8) | i; | |
218 video->gamma[2*256+i] = (i << 8) | i; | |
219 } | |
220 } | |
221 } | |
222 | |
223 /* Just copy from our internal table */ | |
224 if ( red ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1334
diff
changeset
|
225 SDL_memcpy(red, &video->gamma[0*256], 256*sizeof(*red)); |
0 | 226 } |
227 if ( green ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1334
diff
changeset
|
228 SDL_memcpy(green, &video->gamma[1*256], 256*sizeof(*green)); |
0 | 229 } |
230 if ( blue ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1334
diff
changeset
|
231 SDL_memcpy(blue, &video->gamma[2*256], 256*sizeof(*blue)); |
0 | 232 } |
233 return 0; | |
234 } |