Mercurial > sdl-ios-xcode
annotate src/video/SDL_gamma.c @ 1330:450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 06 Feb 2006 08:28:51 +0000 |
parents | c9b51268668f |
children | 37d43bd654d7 |
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 */ |
0 | 29 #endif |
30 | |
31 #include "SDL_error.h" | |
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
|
32 #include "SDL_stdlib.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
|
33 #include "SDL_string.h" |
0 | 34 #include "SDL_sysvideo.h" |
35 | |
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
|
36 #ifndef 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
|
37 #include "math_private.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
|
38 #include "e_sqrt.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
|
39 #include "e_pow.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
|
40 #include "e_log.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
|
41 #define pow(x, y) __ieee754_pow(x, y) |
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
|
42 #define log(x) __ieee754_log(x) |
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
|
43 #endif |
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
|
44 |
0 | 45 static void CalculateGammaRamp(float gamma, Uint16 *ramp) |
46 { | |
47 int i; | |
48 | |
49 /* 0.0 gamma is all black */ | |
50 if ( gamma <= 0.0 ) { | |
51 for ( i=0; i<256; ++i ) { | |
52 ramp[i] = 0; | |
53 } | |
54 return; | |
55 } else | |
56 /* 1.0 gamma is identity */ | |
57 if ( gamma == 1.0 ) { | |
58 for ( i=0; i<256; ++i ) { | |
59 ramp[i] = (i << 8) | i; | |
60 } | |
61 return; | |
62 } else | |
63 /* Calculate a real gamma ramp */ | |
64 { int value; | |
65 gamma = 1.0f / gamma; | |
66 for ( i=0; i<256; ++i ) { | |
67 value = (int)(pow((double)i/256.0, gamma)*65535.0+0.5); | |
68 if ( value > 65535 ) { | |
69 value = 65535; | |
70 } | |
71 ramp[i] = (Uint16)value; | |
72 } | |
73 } | |
74 } | |
75 static void CalculateGammaFromRamp(float *gamma, Uint16 *ramp) | |
76 { | |
77 /* The following is adapted from a post by Garrett Bass on OpenGL | |
78 Gamedev list, March 4, 2000. | |
79 */ | |
80 float sum = 0.0; | |
81 int i, count = 0; | |
82 | |
83 *gamma = 1.0; | |
84 for ( i = 1; i < 256; ++i ) { | |
85 if ( (ramp[i] != 0) && (ramp[i] != 65535) ) { | |
86 double B = (double)i / 256.0; | |
87 double A = ramp[i] / 65535.0; | |
88 sum += (float) ( log(A) / log(B) ); | |
89 count++; | |
90 } | |
91 } | |
92 if ( count && sum ) { | |
93 *gamma = 1.0f / (sum / count); | |
94 } | |
95 } | |
96 | |
97 int SDL_SetGamma(float red, float green, float blue) | |
98 { | |
99 int succeeded; | |
100 SDL_VideoDevice *video = current_video; | |
101 SDL_VideoDevice *this = current_video; | |
102 | |
103 succeeded = -1; | |
104 /* Prefer using SetGammaRamp(), as it's more flexible */ | |
105 { | |
106 Uint16 ramp[3][256]; | |
107 | |
108 CalculateGammaRamp(red, ramp[0]); | |
109 CalculateGammaRamp(green, ramp[1]); | |
110 CalculateGammaRamp(blue, ramp[2]); | |
111 succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]); | |
112 } | |
113 if ( (succeeded < 0) && video->SetGamma ) { | |
114 SDL_ClearError(); | |
115 succeeded = video->SetGamma(this, red, green, blue); | |
116 } | |
117 return succeeded; | |
118 } | |
119 | |
120 /* Calculating the gamma by integrating the gamma ramps isn't exact, | |
121 so this function isn't officially supported. | |
122 */ | |
123 int SDL_GetGamma(float *red, float *green, float *blue) | |
124 { | |
125 int succeeded; | |
126 SDL_VideoDevice *video = current_video; | |
127 SDL_VideoDevice *this = current_video; | |
128 | |
129 succeeded = -1; | |
130 /* Prefer using GetGammaRamp(), as it's more flexible */ | |
131 { | |
132 Uint16 ramp[3][256]; | |
133 | |
134 succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]); | |
135 if ( succeeded >= 0 ) { | |
136 CalculateGammaFromRamp(red, ramp[0]); | |
137 CalculateGammaFromRamp(green, ramp[1]); | |
138 CalculateGammaFromRamp(blue, ramp[2]); | |
139 } | |
140 } | |
141 if ( (succeeded < 0) && video->GetGamma ) { | |
142 SDL_ClearError(); | |
143 succeeded = video->GetGamma(this, red, green, blue); | |
144 } | |
145 return succeeded; | |
146 } | |
147 | |
669
37bb90b3d976
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
148 int SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16 *blue) |
0 | 149 { |
150 int succeeded; | |
151 SDL_VideoDevice *video = current_video; | |
152 SDL_VideoDevice *this = current_video; | |
153 SDL_Surface *screen = SDL_PublicSurface; | |
154 | |
155 /* Verify the screen parameter */ | |
156 if ( !screen ) { | |
157 SDL_SetError("No video mode has been set"); | |
158 return -1; | |
159 } | |
160 | |
161 /* Lazily allocate the gamma tables */ | |
162 if ( ! video->gamma ) { | |
163 SDL_GetGammaRamp(0, 0, 0); | |
164 } | |
165 | |
166 /* Fill the gamma table with the new values */ | |
167 if ( red ) { | |
168 memcpy(&video->gamma[0*256], red, 256*sizeof(*video->gamma)); | |
169 } | |
170 if ( green ) { | |
171 memcpy(&video->gamma[1*256], green, 256*sizeof(*video->gamma)); | |
172 } | |
173 if ( blue ) { | |
174 memcpy(&video->gamma[2*256], blue, 256*sizeof(*video->gamma)); | |
175 } | |
176 | |
177 /* Gamma correction always possible on split palettes */ | |
178 if ( (screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) { | |
179 SDL_Palette *pal = screen->format->palette; | |
180 | |
181 /* If physical palette has been set independently, use it */ | |
182 if(video->physpal) | |
183 pal = video->physpal; | |
184 | |
185 SDL_SetPalette(screen, SDL_PHYSPAL, | |
186 pal->colors, 0, pal->ncolors); | |
187 return 0; | |
188 } | |
189 | |
190 /* Try to set the gamma ramp in the driver */ | |
191 succeeded = -1; | |
192 if ( video->SetGammaRamp ) { | |
193 succeeded = video->SetGammaRamp(this, video->gamma); | |
194 } else { | |
195 SDL_SetError("Gamma ramp manipulation not supported"); | |
196 } | |
197 return succeeded; | |
198 } | |
199 | |
200 int SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue) | |
201 { | |
202 SDL_VideoDevice *video = current_video; | |
203 SDL_VideoDevice *this = current_video; | |
204 | |
205 /* Lazily allocate the gamma table */ | |
206 if ( ! video->gamma ) { | |
207 video->gamma = malloc(3*256*sizeof(*video->gamma)); | |
208 if ( ! video->gamma ) { | |
209 SDL_OutOfMemory(); | |
210 return -1; | |
211 } | |
212 if ( video->GetGammaRamp ) { | |
213 /* Get the real hardware gamma */ | |
214 video->GetGammaRamp(this, video->gamma); | |
215 } else { | |
216 /* Assume an identity gamma */ | |
217 int i; | |
218 for ( i=0; i<256; ++i ) { | |
219 video->gamma[0*256+i] = (i << 8) | i; | |
220 video->gamma[1*256+i] = (i << 8) | i; | |
221 video->gamma[2*256+i] = (i << 8) | i; | |
222 } | |
223 } | |
224 } | |
225 | |
226 /* Just copy from our internal table */ | |
227 if ( red ) { | |
228 memcpy(red, &video->gamma[0*256], 256*sizeof(*red)); | |
229 } | |
230 if ( green ) { | |
231 memcpy(green, &video->gamma[1*256], 256*sizeof(*green)); | |
232 } | |
233 if ( blue ) { | |
234 memcpy(blue, &video->gamma[2*256], 256*sizeof(*blue)); | |
235 } | |
236 return 0; | |
237 } |