Mercurial > sdl-ios-xcode
comparison src/video/SDL_gamma.c @ 1668:4da1ee79c9af SDL-1.3
more tweaking indent options
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 29 May 2006 04:04:35 +0000 |
parents | 782fd950bd46 |
children | 8e754b82cecc |
comparison
equal
deleted
inserted
replaced
1667:1fddae038bc8 | 1668:4da1ee79c9af |
---|---|
37 | 37 |
38 #include "SDL_sysvideo.h" | 38 #include "SDL_sysvideo.h" |
39 | 39 |
40 | 40 |
41 static void | 41 static void |
42 CalculateGammaRamp (float gamma, Uint16 * ramp) | 42 CalculateGammaRamp(float gamma, Uint16 * ramp) |
43 { | 43 { |
44 int i; | 44 int i; |
45 | 45 |
46 /* 0.0 gamma is all black */ | 46 /* 0.0 gamma is all black */ |
47 if (gamma <= 0.0f) { | 47 if (gamma <= 0.0f) { |
60 /* Calculate a real gamma ramp */ | 60 /* Calculate a real gamma ramp */ |
61 { | 61 { |
62 int value; | 62 int value; |
63 gamma = 1.0f / gamma; | 63 gamma = 1.0f / gamma; |
64 for (i = 0; i < 256; ++i) { | 64 for (i = 0; i < 256; ++i) { |
65 value = (int) (pow ((double) i / 256.0, gamma) * 65535.0 + 0.5); | 65 value = (int) (pow((double) i / 256.0, gamma) * 65535.0 + 0.5); |
66 if (value > 65535) { | 66 if (value > 65535) { |
67 value = 65535; | 67 value = 65535; |
68 } | 68 } |
69 ramp[i] = (Uint16) value; | 69 ramp[i] = (Uint16) value; |
70 } | 70 } |
71 } | 71 } |
72 } | 72 } |
73 static void | 73 static void |
74 CalculateGammaFromRamp (float *gamma, Uint16 * ramp) | 74 CalculateGammaFromRamp(float *gamma, Uint16 * ramp) |
75 { | 75 { |
76 /* The following is adapted from a post by Garrett Bass on OpenGL | 76 /* The following is adapted from a post by Garrett Bass on OpenGL |
77 Gamedev list, March 4, 2000. | 77 Gamedev list, March 4, 2000. |
78 */ | 78 */ |
79 float sum = 0.0f; | 79 float sum = 0.0f; |
82 *gamma = 1.0; | 82 *gamma = 1.0; |
83 for (i = 1; i < 256; ++i) { | 83 for (i = 1; i < 256; ++i) { |
84 if ((ramp[i] != 0) && (ramp[i] != 65535)) { | 84 if ((ramp[i] != 0) && (ramp[i] != 65535)) { |
85 double B = (double) i / 256.0; | 85 double B = (double) i / 256.0; |
86 double A = ramp[i] / 65535.0; | 86 double A = ramp[i] / 65535.0; |
87 sum += (float) (log (A) / log (B)); | 87 sum += (float) (log(A) / log(B)); |
88 count++; | 88 count++; |
89 } | 89 } |
90 } | 90 } |
91 if (count && sum > 0.0f) { | 91 if (count && sum > 0.0f) { |
92 *gamma = 1.0f / (sum / count); | 92 *gamma = 1.0f / (sum / count); |
93 } | 93 } |
94 } | 94 } |
95 | 95 |
96 int | 96 int |
97 SDL_SetGamma (float red, float green, float blue) | 97 SDL_SetGamma(float red, float green, float blue) |
98 { | 98 { |
99 SDL_VideoDevice *_this = SDL_GetVideoDevice (); | 99 SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
100 int succeeded; | 100 int succeeded; |
101 | 101 |
102 succeeded = -1; | 102 succeeded = -1; |
103 /* Prefer using SetGammaRamp(), as it's more flexible */ | 103 /* Prefer using SetGammaRamp(), as it's more flexible */ |
104 { | 104 { |
105 Uint16 ramp[3][256]; | 105 Uint16 ramp[3][256]; |
106 | 106 |
107 CalculateGammaRamp (red, ramp[0]); | 107 CalculateGammaRamp(red, ramp[0]); |
108 CalculateGammaRamp (green, ramp[1]); | 108 CalculateGammaRamp(green, ramp[1]); |
109 CalculateGammaRamp (blue, ramp[2]); | 109 CalculateGammaRamp(blue, ramp[2]); |
110 succeeded = SDL_SetGammaRamp (ramp[0], ramp[1], ramp[2]); | 110 succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]); |
111 } | 111 } |
112 if ((succeeded < 0) && _this->SetGamma) { | 112 if ((succeeded < 0) && _this->SetGamma) { |
113 SDL_ClearError (); | 113 SDL_ClearError(); |
114 succeeded = _this->SetGamma (_this, red, green, blue); | 114 succeeded = _this->SetGamma(_this, red, green, blue); |
115 } | 115 } |
116 return succeeded; | 116 return succeeded; |
117 } | 117 } |
118 | 118 |
119 /* Calculating the gamma by integrating the gamma ramps isn't exact, | 119 /* Calculating the gamma by integrating the gamma ramps isn't exact, |
120 so this function isn't officially supported. | 120 so this function isn't officially supported. |
121 */ | 121 */ |
122 int | 122 int |
123 SDL_GetGamma (float *red, float *green, float *blue) | 123 SDL_GetGamma(float *red, float *green, float *blue) |
124 { | 124 { |
125 SDL_VideoDevice *_this = SDL_GetVideoDevice (); | 125 SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
126 int succeeded; | 126 int succeeded; |
127 | 127 |
128 succeeded = -1; | 128 succeeded = -1; |
129 /* Prefer using GetGammaRamp(), as it's more flexible */ | 129 /* Prefer using GetGammaRamp(), as it's more flexible */ |
130 { | 130 { |
131 Uint16 ramp[3][256]; | 131 Uint16 ramp[3][256]; |
132 | 132 |
133 succeeded = SDL_GetGammaRamp (ramp[0], ramp[1], ramp[2]); | 133 succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]); |
134 if (succeeded >= 0) { | 134 if (succeeded >= 0) { |
135 CalculateGammaFromRamp (red, ramp[0]); | 135 CalculateGammaFromRamp(red, ramp[0]); |
136 CalculateGammaFromRamp (green, ramp[1]); | 136 CalculateGammaFromRamp(green, ramp[1]); |
137 CalculateGammaFromRamp (blue, ramp[2]); | 137 CalculateGammaFromRamp(blue, ramp[2]); |
138 } | 138 } |
139 } | 139 } |
140 if ((succeeded < 0) && _this->GetGamma) { | 140 if ((succeeded < 0) && _this->GetGamma) { |
141 SDL_ClearError (); | 141 SDL_ClearError(); |
142 succeeded = _this->GetGamma (_this, red, green, blue); | 142 succeeded = _this->GetGamma(_this, red, green, blue); |
143 } | 143 } |
144 return succeeded; | 144 return succeeded; |
145 } | 145 } |
146 | 146 |
147 int | 147 int |
148 SDL_SetGammaRamp (const Uint16 * red, const Uint16 * green, | 148 SDL_SetGammaRamp(const Uint16 * red, const Uint16 * green, |
149 const Uint16 * blue) | 149 const Uint16 * blue) |
150 { | 150 { |
151 SDL_VideoDevice *_this = SDL_GetVideoDevice (); | 151 SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
152 int succeeded; | 152 int succeeded; |
153 SDL_Surface *screen = SDL_PublicSurface; | 153 SDL_Surface *screen = SDL_PublicSurface; |
154 | 154 |
155 /* Verify the screen parameter */ | 155 /* Verify the screen parameter */ |
156 if (!screen) { | 156 if (!screen) { |
157 SDL_SetError ("No video mode has been set"); | 157 SDL_SetError("No video mode has been set"); |
158 return -1; | 158 return -1; |
159 } | 159 } |
160 | 160 |
161 /* Lazily allocate the gamma tables */ | 161 /* Lazily allocate the gamma tables */ |
162 if (!SDL_CurrentWindow.gamma) { | 162 if (!SDL_CurrentWindow.gamma) { |
163 SDL_GetGammaRamp (0, 0, 0); | 163 SDL_GetGammaRamp(0, 0, 0); |
164 } | 164 } |
165 | 165 |
166 /* Fill the gamma table with the new values */ | 166 /* Fill the gamma table with the new values */ |
167 if (red) { | 167 if (red) { |
168 SDL_memcpy (&SDL_CurrentWindow.gamma[0 * 256], red, | 168 SDL_memcpy(&SDL_CurrentWindow.gamma[0 * 256], red, |
169 256 * sizeof (*SDL_CurrentWindow.gamma)); | 169 256 * sizeof(*SDL_CurrentWindow.gamma)); |
170 } | 170 } |
171 if (green) { | 171 if (green) { |
172 SDL_memcpy (&SDL_CurrentWindow.gamma[1 * 256], green, | 172 SDL_memcpy(&SDL_CurrentWindow.gamma[1 * 256], green, |
173 256 * sizeof (*SDL_CurrentWindow.gamma)); | 173 256 * sizeof(*SDL_CurrentWindow.gamma)); |
174 } | 174 } |
175 if (blue) { | 175 if (blue) { |
176 SDL_memcpy (&SDL_CurrentWindow.gamma[2 * 256], blue, | 176 SDL_memcpy(&SDL_CurrentWindow.gamma[2 * 256], blue, |
177 256 * sizeof (*SDL_CurrentWindow.gamma)); | 177 256 * sizeof(*SDL_CurrentWindow.gamma)); |
178 } | 178 } |
179 | 179 |
180 /* Try to set the gamma ramp in the driver */ | 180 /* Try to set the gamma ramp in the driver */ |
181 succeeded = -1; | 181 succeeded = -1; |
182 if (_this->SetGammaRamp) { | 182 if (_this->SetGammaRamp) { |
183 succeeded = _this->SetGammaRamp (_this, SDL_CurrentWindow.gamma); | 183 succeeded = _this->SetGammaRamp(_this, SDL_CurrentWindow.gamma); |
184 } else { | 184 } else { |
185 SDL_SetError ("Gamma ramp manipulation not supported"); | 185 SDL_SetError("Gamma ramp manipulation not supported"); |
186 } | 186 } |
187 return succeeded; | 187 return succeeded; |
188 } | 188 } |
189 | 189 |
190 int | 190 int |
191 SDL_GetGammaRamp (Uint16 * red, Uint16 * green, Uint16 * blue) | 191 SDL_GetGammaRamp(Uint16 * red, Uint16 * green, Uint16 * blue) |
192 { | 192 { |
193 SDL_VideoDevice *_this = SDL_GetVideoDevice (); | 193 SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
194 | 194 |
195 /* Lazily allocate the gamma table */ | 195 /* Lazily allocate the gamma table */ |
196 if (!SDL_CurrentWindow.gamma) { | 196 if (!SDL_CurrentWindow.gamma) { |
197 SDL_CurrentWindow.gamma = | 197 SDL_CurrentWindow.gamma = |
198 SDL_malloc (3 * 256 * sizeof (*SDL_CurrentWindow.gamma)); | 198 SDL_malloc(3 * 256 * sizeof(*SDL_CurrentWindow.gamma)); |
199 if (!SDL_CurrentWindow.gamma) { | 199 if (!SDL_CurrentWindow.gamma) { |
200 SDL_OutOfMemory (); | 200 SDL_OutOfMemory(); |
201 return -1; | 201 return -1; |
202 } | 202 } |
203 if (_this->GetGammaRamp) { | 203 if (_this->GetGammaRamp) { |
204 /* Get the real hardware gamma */ | 204 /* Get the real hardware gamma */ |
205 _this->GetGammaRamp (_this, SDL_CurrentWindow.gamma); | 205 _this->GetGammaRamp(_this, SDL_CurrentWindow.gamma); |
206 } else { | 206 } else { |
207 /* Assume an identity gamma */ | 207 /* Assume an identity gamma */ |
208 int i; | 208 int i; |
209 for (i = 0; i < 256; ++i) { | 209 for (i = 0; i < 256; ++i) { |
210 SDL_CurrentWindow.gamma[0 * 256 + i] = (i << 8) | i; | 210 SDL_CurrentWindow.gamma[0 * 256 + i] = (i << 8) | i; |
214 } | 214 } |
215 } | 215 } |
216 | 216 |
217 /* Just copy from our internal table */ | 217 /* Just copy from our internal table */ |
218 if (red) { | 218 if (red) { |
219 SDL_memcpy (red, &SDL_CurrentWindow.gamma[0 * 256], | 219 SDL_memcpy(red, &SDL_CurrentWindow.gamma[0 * 256], |
220 256 * sizeof (*red)); | 220 256 * sizeof(*red)); |
221 } | 221 } |
222 if (green) { | 222 if (green) { |
223 SDL_memcpy (green, &SDL_CurrentWindow.gamma[1 * 256], | 223 SDL_memcpy(green, &SDL_CurrentWindow.gamma[1 * 256], |
224 256 * sizeof (*green)); | 224 256 * sizeof(*green)); |
225 } | 225 } |
226 if (blue) { | 226 if (blue) { |
227 SDL_memcpy (blue, &SDL_CurrentWindow.gamma[2 * 256], | 227 SDL_memcpy(blue, &SDL_CurrentWindow.gamma[2 * 256], |
228 256 * sizeof (*blue)); | 228 256 * sizeof(*blue)); |
229 } | 229 } |
230 return 0; | 230 return 0; |
231 } | 231 } |
232 | 232 |
233 /* vi: set ts=4 sw=4 expandtab: */ | 233 /* vi: set ts=4 sw=4 expandtab: */ |