1 //========================================================================
2 // GLFW 3.0 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org>
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would
17 // be appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not
20 // be misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source
23 // distribution.
24 //
25 //========================================================================
26
27 #include "internal.h"
28
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #if defined(_MSC_VER)
34 #include <malloc.h>
35 #endif
36
37
38 //////////////////////////////////////////////////////////////////////////
39 ////// GLFW internal API //////
40 //////////////////////////////////////////////////////////////////////////
41
_glfwAllocGammaArrays(GLFWgammaramp * ramp,unsigned int size)42 void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size)
43 {
44 ramp->red = calloc(size, sizeof(unsigned short));
45 ramp->green = calloc(size, sizeof(unsigned short));
46 ramp->blue = calloc(size, sizeof(unsigned short));
47 ramp->size = size;
48 }
49
_glfwFreeGammaArrays(GLFWgammaramp * ramp)50 void _glfwFreeGammaArrays(GLFWgammaramp* ramp)
51 {
52 free(ramp->red);
53 free(ramp->green);
54 free(ramp->blue);
55
56 memset(ramp, 0, sizeof(GLFWgammaramp));
57 }
58
59
60 //////////////////////////////////////////////////////////////////////////
61 ////// GLFW public API //////
62 //////////////////////////////////////////////////////////////////////////
63
glfwSetGamma(GLFWmonitor * handle,float gamma)64 GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma)
65 {
66 int i;
67 unsigned short values[256];
68 GLFWgammaramp ramp;
69
70 _GLFW_REQUIRE_INIT();
71
72 if (gamma <= 0.f)
73 {
74 _glfwInputError(GLFW_INVALID_VALUE,
75 "Gamma value must be greater than zero");
76 return;
77 }
78
79 for (i = 0; i < 256; i++)
80 {
81 double value;
82
83 // Calculate intensity
84 value = i / 255.0;
85 // Apply gamma curve
86 value = pow(value, 1.0 / gamma) * 65535.0 + 0.5;
87
88 // Clamp to value range
89 if (value > 65535.0)
90 value = 65535.0;
91
92 values[i] = (unsigned short) value;
93 }
94
95 ramp.red = values;
96 ramp.green = values;
97 ramp.blue = values;
98 ramp.size = 256;
99
100 glfwSetGammaRamp(handle, &ramp);
101 }
102
glfwGetGammaRamp(GLFWmonitor * handle)103 GLFWAPI const GLFWgammaramp* glfwGetGammaRamp(GLFWmonitor* handle)
104 {
105 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
106
107 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
108
109 _glfwFreeGammaArrays(&monitor->currentRamp);
110 _glfwPlatformGetGammaRamp(monitor, &monitor->currentRamp);
111
112 return &monitor->currentRamp;
113 }
114
glfwSetGammaRamp(GLFWmonitor * handle,const GLFWgammaramp * ramp)115 GLFWAPI void glfwSetGammaRamp(GLFWmonitor* handle, const GLFWgammaramp* ramp)
116 {
117 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
118
119 _GLFW_REQUIRE_INIT();
120
121 if (!monitor->originalRamp.size)
122 _glfwPlatformGetGammaRamp(monitor, &monitor->originalRamp);
123
124 _glfwPlatformSetGammaRamp(monitor, ramp);
125 }
126
127