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