comparison src/video/SDL_gamma.c @ 1733:0b1070f2f94d SDL-1.3

Implemented gamma correction on Windows. Added general code to restore the video mode and gamma when windows lose focus.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 09 Jul 2006 09:02:26 +0000
parents 7179cb72dbba
children f7c667ded87d
comparison
equal deleted inserted replaced
1732:fd65f12b6de6 1733:0b1070f2f94d
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 Uint16 ramp[3][256];
100 int succeeded; 100
101 101 CalculateGammaRamp(red, ramp[0]);
102 succeeded = -1; 102 CalculateGammaRamp(green, ramp[1]);
103 /* Prefer using SetGammaRamp(), as it's more flexible */ 103 CalculateGammaRamp(blue, ramp[2]);
104 { 104
105 Uint16 ramp[3][256]; 105 return SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
106
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]);
111 }
112 if ((succeeded < 0) && _this && _this->SetGamma) {
113 SDL_ClearError();
114 succeeded = _this->SetGamma(_this, red, green, blue);
115 }
116 return succeeded;
117 } 106 }
118 107
119 /* Calculating the gamma by integrating the gamma ramps isn't exact, 108 /* Calculating the gamma by integrating the gamma ramps isn't exact,
120 so this function isn't officially supported. 109 so this function isn't officially supported.
121 */ 110 */
122 int 111 int
123 SDL_GetGamma(float *red, float *green, float *blue) 112 SDL_GetGamma(float *red, float *green, float *blue)
124 { 113 {
114 int succeeded;
115 Uint16 ramp[3][256];
116
117 succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
118 if (succeeded >= 0) {
119 CalculateGammaFromRamp(red, ramp[0]);
120 CalculateGammaFromRamp(green, ramp[1]);
121 CalculateGammaFromRamp(blue, ramp[2]);
122 }
123 return succeeded;
124 }
125
126 int
127 SDL_SetGammaRamp(const Uint16 * red, const Uint16 * green,
128 const Uint16 * blue)
129 {
125 SDL_VideoDevice *_this = SDL_GetVideoDevice(); 130 SDL_VideoDevice *_this = SDL_GetVideoDevice();
126 int succeeded; 131 int succeeded;
127 132
128 succeeded = -1;
129 /* Prefer using GetGammaRamp(), as it's more flexible */
130 {
131 Uint16 ramp[3][256];
132
133 succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
134 if (succeeded >= 0) {
135 CalculateGammaFromRamp(red, ramp[0]);
136 CalculateGammaFromRamp(green, ramp[1]);
137 CalculateGammaFromRamp(blue, ramp[2]);
138 }
139 }
140 if ((succeeded < 0) && _this && _this->GetGamma) {
141 SDL_ClearError();
142 succeeded = _this->GetGamma(_this, red, green, blue);
143 }
144 return succeeded;
145 }
146
147 int
148 SDL_SetGammaRamp(const Uint16 * red, const Uint16 * green,
149 const Uint16 * blue)
150 {
151 SDL_VideoDevice *_this = SDL_GetVideoDevice();
152 int succeeded;
153
154 /* Lazily allocate the gamma tables */ 133 /* Lazily allocate the gamma tables */
155 if (!SDL_CurrentWindow.gamma) { 134 if (!SDL_CurrentDisplay.gamma) {
156 SDL_GetGammaRamp(0, 0, 0); 135 SDL_GetGammaRamp(NULL, NULL, NULL);
157 } 136 }
158 137
159 /* Fill the gamma table with the new values */ 138 /* Fill the gamma table with the new values */
160 if (red) { 139 if (red) {
161 SDL_memcpy(&SDL_CurrentWindow.gamma[0 * 256], red, 140 SDL_memcpy(&SDL_CurrentDisplay.gamma[0 * 256], red,
162 256 * sizeof(*SDL_CurrentWindow.gamma)); 141 256 * sizeof(*SDL_CurrentDisplay.gamma));
163 } 142 }
164 if (green) { 143 if (green) {
165 SDL_memcpy(&SDL_CurrentWindow.gamma[1 * 256], green, 144 SDL_memcpy(&SDL_CurrentDisplay.gamma[1 * 256], green,
166 256 * sizeof(*SDL_CurrentWindow.gamma)); 145 256 * sizeof(*SDL_CurrentDisplay.gamma));
167 } 146 }
168 if (blue) { 147 if (blue) {
169 SDL_memcpy(&SDL_CurrentWindow.gamma[2 * 256], blue, 148 SDL_memcpy(&SDL_CurrentDisplay.gamma[2 * 256], blue,
170 256 * sizeof(*SDL_CurrentWindow.gamma)); 149 256 * sizeof(*SDL_CurrentDisplay.gamma));
171 } 150 }
172 151
173 /* Try to set the gamma ramp in the driver */ 152 /* Try to set the gamma ramp in the driver */
174 succeeded = -1; 153 succeeded = -1;
175 if (_this && _this->SetGammaRamp) { 154 if (_this && _this->SetDisplayGammaRamp) {
176 succeeded = _this->SetGammaRamp(_this, SDL_CurrentWindow.gamma); 155 succeeded =
156 _this->SetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
177 } else { 157 } else {
178 SDL_SetError("Gamma ramp manipulation not supported"); 158 SDL_SetError("Gamma ramp manipulation not supported");
179 } 159 }
180 return succeeded; 160 return succeeded;
181 } 161 }
184 SDL_GetGammaRamp(Uint16 * red, Uint16 * green, Uint16 * blue) 164 SDL_GetGammaRamp(Uint16 * red, Uint16 * green, Uint16 * blue)
185 { 165 {
186 SDL_VideoDevice *_this = SDL_GetVideoDevice(); 166 SDL_VideoDevice *_this = SDL_GetVideoDevice();
187 167
188 /* Lazily allocate the gamma table */ 168 /* Lazily allocate the gamma table */
189 if (!SDL_CurrentWindow.gamma) { 169 if (!SDL_CurrentDisplay.gamma) {
190 SDL_CurrentWindow.gamma = 170 size_t rampsize = (3 * 256 * sizeof(*SDL_CurrentDisplay.gamma));
191 SDL_malloc(3 * 256 * sizeof(*SDL_CurrentWindow.gamma)); 171
192 if (!SDL_CurrentWindow.gamma) { 172 SDL_CurrentDisplay.gamma = SDL_malloc(rampsize * 2);
173 if (!SDL_CurrentDisplay.gamma) {
193 SDL_OutOfMemory(); 174 SDL_OutOfMemory();
194 return -1; 175 return -1;
195 } 176 }
196 if (_this && _this->GetGammaRamp) { 177 if (_this && _this->GetDisplayGammaRamp) {
197 /* Get the real hardware gamma */ 178 /* Get the real hardware gamma */
198 _this->GetGammaRamp(_this, SDL_CurrentWindow.gamma); 179 _this->GetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
199 } else { 180 } else {
200 /* Assume an identity gamma */ 181 /* Assume an identity gamma */
201 int i; 182 int i;
202 for (i = 0; i < 256; ++i) { 183 for (i = 0; i < 256; ++i) {
203 SDL_CurrentWindow.gamma[0 * 256 + i] = (i << 8) | i; 184 SDL_CurrentDisplay.gamma[0 * 256 + i] = (i << 8) | i;
204 SDL_CurrentWindow.gamma[1 * 256 + i] = (i << 8) | i; 185 SDL_CurrentDisplay.gamma[1 * 256 + i] = (i << 8) | i;
205 SDL_CurrentWindow.gamma[2 * 256 + i] = (i << 8) | i; 186 SDL_CurrentDisplay.gamma[2 * 256 + i] = (i << 8) | i;
206 } 187 }
207 } 188 }
189 SDL_CurrentDisplay.saved_gamma = SDL_CurrentDisplay.gamma + (3 * 256);
190 SDL_memcpy(SDL_CurrentDisplay.saved_gamma, SDL_CurrentDisplay.gamma,
191 rampsize);
208 } 192 }
209 193
210 /* Just copy from our internal table */ 194 /* Just copy from our internal table */
211 if (red) { 195 if (red) {
212 SDL_memcpy(red, &SDL_CurrentWindow.gamma[0 * 256], 196 SDL_memcpy(red, &SDL_CurrentDisplay.gamma[0 * 256],
213 256 * sizeof(*red)); 197 256 * sizeof(*red));
214 } 198 }
215 if (green) { 199 if (green) {
216 SDL_memcpy(green, &SDL_CurrentWindow.gamma[1 * 256], 200 SDL_memcpy(green, &SDL_CurrentDisplay.gamma[1 * 256],
217 256 * sizeof(*green)); 201 256 * sizeof(*green));
218 } 202 }
219 if (blue) { 203 if (blue) {
220 SDL_memcpy(blue, &SDL_CurrentWindow.gamma[2 * 256], 204 SDL_memcpy(blue, &SDL_CurrentDisplay.gamma[2 * 256],
221 256 * sizeof(*blue)); 205 256 * sizeof(*blue));
222 } 206 }
223 return 0; 207 return 0;
224 } 208 }
225 209