From 7ebe8d56fb7f5b5b9b29b0c8caee2c36c406fd19 Mon Sep 17 00:00:00 2001 From: Alexander Monakov Date: Sun, 25 Nov 2018 00:05:59 +0300 Subject: [PATCH] Monitor: use current ramp size in glfwSetGamma On X11, number of gamma ramp entries must match currently active ramp, so hardcoded size of 256 is not going to work if the hardware ramp has more entries. Solve this by querying current ramp size and allocating the needed amount dynamically. --- src/monitor.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/monitor.c b/src/monitor.c index 9ae3ac924..508f24f11 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -427,8 +427,8 @@ GLFWAPI const GLFWvidmode* glfwGetVideoMode(GLFWmonitor* handle) GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma) { - int i; - unsigned short values[256]; + int i, size; + unsigned short *values; GLFWgammaramp ramp; assert(handle != NULL); @@ -440,12 +440,19 @@ GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma) return; } - for (i = 0; i < 256; i++) + size = _glfwPlatformGetGammaRampSize((_GLFWmonitor*) handle); + + if (size < 2) + return; + + values = malloc(size * sizeof(unsigned short)); + + for (i = 0; i < size; i++) { float value; // Calculate intensity - value = i / 255.f; + value = 1.f / (size - 1) * i; // Apply gamma curve value = powf(value, 1.f / gamma) * 65535.f + 0.5f; @@ -459,9 +466,10 @@ GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma) ramp.red = values; ramp.green = values; ramp.blue = values; - ramp.size = 256; + ramp.size = size; glfwSetGammaRamp(handle, &ramp); + free(values); } GLFWAPI const GLFWgammaramp* glfwGetGammaRamp(GLFWmonitor* handle)