annotate 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
rev   line source
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1 /*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
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
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
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
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
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
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
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
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
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
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
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
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
21 */
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
22 #include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24 /* Gamma correction support */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25
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
26 #ifdef HAVE_MATH_H
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
27 #include <math.h> /* Used for calculating gamma ramps */
1334
37d43bd654d7 Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
28 #else
37d43bd654d7 Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
29 /* Math routines from uClibc: http://www.uclibc.org */
37d43bd654d7 Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
30 #include "math_private.h"
37d43bd654d7 Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
31 #include "e_sqrt.h"
37d43bd654d7 Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
32 #include "e_pow.h"
37d43bd654d7 Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
33 #include "e_log.h"
37d43bd654d7 Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
34 #define pow(x, y) __ieee754_pow(x, y)
37d43bd654d7 Proper credit to uClibc :)
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
35 #define log(x) __ieee754_log(x)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
36 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
37
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
38 #include "SDL_sysvideo.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
39
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
40
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
41 static void
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
42 CalculateGammaRamp(float gamma, Uint16 * ramp)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
43 {
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
44 int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
45
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
46 /* 0.0 gamma is all black */
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
47 if (gamma <= 0.0f) {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
48 for (i = 0; i < 256; ++i) {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
49 ramp[i] = 0;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
50 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
51 return;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
52 } else
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
53 /* 1.0 gamma is identity */
1695
7179cb72dbba Date: Fri, 9 Jun 2006 11:35:23 +0300
Sam Lantinga <slouken@libsdl.org>
parents: 1672
diff changeset
54 if (gamma == 1.0f) {
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
55 for (i = 0; i < 256; ++i) {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
56 ramp[i] = (i << 8) | i;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
57 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
58 return;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
59 } else
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
60 /* Calculate a real gamma ramp */
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
61 {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
62 int value;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
63 gamma = 1.0f / gamma;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
64 for (i = 0; i < 256; ++i) {
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
65 value = (int) (pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
66 if (value > 65535) {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
67 value = 65535;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
68 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
69 ramp[i] = (Uint16) value;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
70 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
71 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
72 }
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
73 static void
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
74 CalculateGammaFromRamp(float *gamma, Uint16 * ramp)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
75 {
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
76 /* The following is adapted from a post by Garrett Bass on OpenGL
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
77 Gamedev list, March 4, 2000.
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
78 */
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
79 float sum = 0.0f;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
80 int i, count = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
81
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
82 *gamma = 1.0;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
83 for (i = 1; i < 256; ++i) {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
84 if ((ramp[i] != 0) && (ramp[i] != 65535)) {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
85 double B = (double) i / 256.0;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
86 double A = ramp[i] / 65535.0;
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
87 sum += (float) (log(A) / log(B));
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
88 count++;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
89 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
90 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
91 if (count && sum > 0.0f) {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
92 *gamma = 1.0f / (sum / count);
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
93 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
94 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
95
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
96 int
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
97 SDL_SetGamma(float red, float green, float blue)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
98 {
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
99 Uint16 ramp[3][256];
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
100
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
101 CalculateGammaRamp(red, ramp[0]);
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
102 CalculateGammaRamp(green, ramp[1]);
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
103 CalculateGammaRamp(blue, ramp[2]);
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
104
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
105 return SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
106 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
107
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
108 /* Calculating the gamma by integrating the gamma ramps isn't exact,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
109 so this function isn't officially supported.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
110 */
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
111 int
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
112 SDL_GetGamma(float *red, float *green, float *blue)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
113 {
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
114 int succeeded;
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
115 Uint16 ramp[3][256];
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
116
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
117 succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
118 if (succeeded >= 0) {
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
119 CalculateGammaFromRamp(red, ramp[0]);
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
120 CalculateGammaFromRamp(green, ramp[1]);
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
121 CalculateGammaFromRamp(blue, ramp[2]);
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
122 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
123 return succeeded;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
124 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
125
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
126 int
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
127 SDL_SetGammaRamp(const Uint16 * red, const Uint16 * green,
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
128 const Uint16 * blue)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
129 {
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
130 SDL_VideoDevice *_this = SDL_GetVideoDevice();
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
131 int succeeded;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
132
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
133 /* Lazily allocate the gamma tables */
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
134 if (!SDL_CurrentDisplay.gamma) {
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
135 SDL_GetGammaRamp(NULL, NULL, NULL);
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
136 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
137
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
138 /* Fill the gamma table with the new values */
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
139 if (red) {
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
140 SDL_memcpy(&SDL_CurrentDisplay.gamma[0 * 256], red,
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
141 256 * sizeof(*SDL_CurrentDisplay.gamma));
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
142 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
143 if (green) {
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
144 SDL_memcpy(&SDL_CurrentDisplay.gamma[1 * 256], green,
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
145 256 * sizeof(*SDL_CurrentDisplay.gamma));
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
146 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
147 if (blue) {
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
148 SDL_memcpy(&SDL_CurrentDisplay.gamma[2 * 256], blue,
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
149 256 * sizeof(*SDL_CurrentDisplay.gamma));
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
150 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
151
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
152 /* Try to set the gamma ramp in the driver */
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
153 succeeded = -1;
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
154 if (_this && _this->SetDisplayGammaRamp) {
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
155 succeeded =
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
156 _this->SetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
157 } else {
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
158 SDL_SetError("Gamma ramp manipulation not supported");
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
159 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
160 return succeeded;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
161 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
162
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
163 int
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
164 SDL_GetGammaRamp(Uint16 * red, Uint16 * green, Uint16 * blue)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
165 {
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
166 SDL_VideoDevice *_this = SDL_GetVideoDevice();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
167
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
168 /* Lazily allocate the gamma table */
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
169 if (!SDL_CurrentDisplay.gamma) {
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
170 size_t rampsize = (3 * 256 * sizeof(*SDL_CurrentDisplay.gamma));
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
171
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
172 SDL_CurrentDisplay.gamma = SDL_malloc(rampsize * 2);
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
173 if (!SDL_CurrentDisplay.gamma) {
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
174 SDL_OutOfMemory();
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
175 return -1;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
176 }
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
177 if (_this && _this->GetDisplayGammaRamp) {
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
178 /* Get the real hardware gamma */
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
179 _this->GetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
180 } else {
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
181 /* Assume an identity gamma */
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
182 int i;
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
183 for (i = 0; i < 256; ++i) {
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
184 SDL_CurrentDisplay.gamma[0 * 256 + i] = (i << 8) | i;
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
185 SDL_CurrentDisplay.gamma[1 * 256 + i] = (i << 8) | i;
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
186 SDL_CurrentDisplay.gamma[2 * 256 + i] = (i << 8) | i;
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
187 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
188 }
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
189 SDL_CurrentDisplay.saved_gamma = SDL_CurrentDisplay.gamma + (3 * 256);
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
190 SDL_memcpy(SDL_CurrentDisplay.saved_gamma, SDL_CurrentDisplay.gamma,
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
191 rampsize);
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
192 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
193
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
194 /* Just copy from our internal table */
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
195 if (red) {
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
196 SDL_memcpy(red, &SDL_CurrentDisplay.gamma[0 * 256],
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
197 256 * sizeof(*red));
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
198 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
199 if (green) {
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
200 SDL_memcpy(green, &SDL_CurrentDisplay.gamma[1 * 256],
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
201 256 * sizeof(*green));
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
202 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
203 if (blue) {
1733
0b1070f2f94d Implemented gamma correction on Windows.
Sam Lantinga <slouken@libsdl.org>
parents: 1695
diff changeset
204 SDL_memcpy(blue, &SDL_CurrentDisplay.gamma[2 * 256],
1668
4da1ee79c9af more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents: 1662
diff changeset
205 256 * sizeof(*blue));
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
206 }
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
207 return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
208 }
1662
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
209
782fd950bd46 Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
210 /* vi: set ts=4 sw=4 expandtab: */