From 61b9e1c9019822a6e4bfcbfbded46c48c4d4446d Mon Sep 17 00:00:00 2001
From: Mavridis Philippe <mavridisf@gmail.com>
Date: Sun, 24 Nov 2024 20:47:11 +0200
Subject: libtderandr: update gamma setting code

This fixes TDE/tdebase#163.

Signed-off-by: Mavridis Philippe <mavridisf@gmail.com>
---
 tderandr/libtderandr.cpp | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/tderandr/libtderandr.cpp b/tderandr/libtderandr.cpp
index 50c7a1673..412ead858 100644
--- a/tderandr/libtderandr.cpp
+++ b/tderandr/libtderandr.cpp
@@ -31,6 +31,8 @@
 #include <tdemessagebox.h>
 #include <tdeapplication.h>
 
+#include <kdebug.h>
+
 #include <stdlib.h>
 #include <unistd.h>
 #include <cmath>
@@ -1113,30 +1115,45 @@ void KRandrSimpleAPI::applyDisplayGamma(TQPtrList<SingleScreenData> screenInfoAr
 			if (!current_crtc) {
 				continue;
 			}
-			// vvvvvvvvv This chunk of code is borrowed from xrandr vvvvvvvvvv
+			// vvvvvvvvv This chunk of code is based on code from the function set_gamma() of xrandr vvvvvvvvvv
 			int size = XRRGetCrtcGammaSize(randr_display, current_crtc->id);
-			if (!size) {
+			if (!size || size > 65536) {
+				kdWarning() << "Gamma correction table has wrong size." << endl;
 				continue;
 			}
 			gamma = XRRAllocGamma(size);
 			if (!gamma) {
+				kdWarning() << "Gamma allocation failed." << endl;
 				continue;
 			}
 			for (i = 0; i < size; i++) {
 				if (screendata->gamma_red == 1.0)
-					gamma->red[i] = i << 8;
+				{
+					gamma->red[i] = (double)i / (double)(size - 1) * 65530.0;
+				}
 				else
-					gamma->red[i] = (pow((double)i/(double)(size-1), (double)screendata->gamma_red) * (double)(size-1)*256);
+				{
+					gamma->red[i] = fmin(pow((double)i / (double)(size - 1), screendata->gamma_red), 1.0) * 65530.0;
+				}
 
 				if (screendata->gamma_green == 1.0)
-					gamma->green[i] = i << 8;
+				{
+					gamma->green[i] = (double)i / (double)(size - 1) * 65530.0;
+				}
 				else
-					gamma->green[i] = (pow((double)i/(double)(size-1), (double)screendata->gamma_green) * (double)(size-1)*256);
+				{
+					gamma->green[i] = fmin(pow((double)i / (double)(size - 1), screendata->gamma_green), 1.0) * 65530.0;
+				}
 
 				if (screendata->gamma_blue == 1.0)
-					gamma->blue[i] = i << 8;
+				{
+					gamma->blue[i] = (double)i / (double)(size - 1) * 65530.0;
+				}
 				else
-					gamma->blue[i] = (pow((double)i/(double)(size-1), (double)screendata->gamma_blue) * (double)(size-1)*256);
+				{
+					gamma->blue[i] = fmin(pow((double)i / (double)(size - 1), screendata->gamma_blue), 1.0) * 65530.0;
+				}
+
 			}
 			XRRSetCrtcGamma(randr_display, current_crtc->id, gamma);
 			free(gamma);
-- 
cgit v1.2.3

