Op 10-01-10 13:02, Mikhail Gusarov schreef:
> 
> Twas brillig at 00:59:32 10.01.2010 UTC+01 when [email protected] did 
> gyre and gimble:
> 
>  ÉP> Do you think it could be useful? If so, I can try to clean it up
>  ÉP> and send it again :-)
> 
> It would be useful. Some remarks:
> 
>  * do not output raw gamma values or at least make it use less screen
>    estate and put it after the output it belongs to.
> 
>  * it would be nice to mark gamma/brightness fields as approximate in
>    output, unless it can be rounded in a useful manner (I have got gamma
>    1.0000006 and brightness 0.6999985 instead of 1.0 and 0.7).
> 
Hello,
Here is complete version. I've refined the math behind, and tried my
best to have the code understandable. My conclusion: it's amazing how
complicated it can be to keep things simple!

It should work with mostly any value, excepted negative brightnesses
(which are not very common, and would make the code much more complex).

Eric
From 18caad03dde15fabbecdc09e3871b93c2c2f7ffe Mon Sep 17 00:00:00 2001
From: Eric Piel <e...@triangle.(none)>
Date: Sun, 10 Jan 2010 00:08:53 +0100
Subject: [PATCH] xrandr: get gamma and brightness
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Even in verbose query mode, gamma and brigthness were not displayed.
That's because they are not stored in the server the same way they are
specified on the command line: they are stored as 256 * 3 u16 while
the command line is 3 + 1 floats.  Still, this is useful info for the
users, and they don't care about how it's stored in the server.

So we do a regression over the values stored to recover info in the same
way as on the command line: gamma and brightness.

Signed-off-by: Éric Piel <[email protected]>
---
 xrandr.c   |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xrandr.man |   10 ++++++--
 2 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/xrandr.c b/xrandr.c
index 65e0623..ab7bb87 100644
--- a/xrandr.c
+++ b/xrandr.c
@@ -1055,6 +1055,72 @@ set_output_info (output_t *output, RROutput xid, 
XRROutputInfo *output_info)
               rotation_name (output->rotation),
               reflection_name (output->rotation));
 
+    /* set gamma */
+    if (!(output->changes & changes_gamma))
+    {
+       if (output->crtc_info) {
+           crtc_t *crtc = output->crtc_info;
+           XRRCrtcGamma *gamma;
+           double i1, v1, i2, v2;
+           int i, size, middle, last;
+
+           size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
+           if (!size)
+               fatal("output %s cannot get gamma size\n", 
output->output.string);
+
+           gamma = XRRGetCrtcGamma(dpy, crtc->crtc.xid);
+           if (!gamma)
+               fatal("output %s cannot get gamma\n", output->output.string);
+
+           /*
+            * Here is a bit tricky because gamma is a whole curve for each
+            * color.  So, typically, we need to represent 3 * 256 values as 3 
+ 1
+            * values.  Therefore, we approximate the gamma curve (v) by 
supposing
+            * it always follows the way we set it: a power function (i^g)
+            * multiplied by a brightness (b).
+            * v = i^g * b
+            * so g = (ln(v) - ln(b))/ln(i)
+            * and b can be found using two points (v1,i1) and (v2, i2):
+            * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
+            * For the best resolution, we select i2 at the highest place not
+            * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
+            * cases), then b = v2.
+            */
+           for (i = size - 1; i > 0; i--) {
+               if ((gamma->red[i] + gamma->green[i] + gamma->blue[i]) < (255 * 
256 * 3))
+                   break;
+           }
+           last = i + 1;
+           middle = last / 2;
+           i1 = (double)middle / size;
+           v1 = (double)(gamma->red[middle - 1] + gamma->green[middle - 1]
+                         + gamma->blue[middle - 1]) / (255 * 256 * 3);
+           i2 = (double)last / size;
+           v2 = (double)(gamma->red[last - 1] + gamma->green[last - 1]
+                         + gamma->blue[last - 1]) / (255 * 256 * 3);
+           if (v2 < 0.0001) { /* The screen is black */
+               output->brightness = 0;
+               output->gamma.red = 1;
+               output->gamma.green = 1;
+               output->gamma.blue = 1;
+           } else {
+               if (last == size)
+                   output->brightness = v2;
+               else {
+                   output->brightness = exp((log(v2)*log(i1) - 
log(v1)*log(i2))/log(i1/i2));
+               }
+               output->gamma.red = log((double)(gamma->red[middle]) / 
output->brightness
+                                       / 65535) / log(i1);
+               output->gamma.green = log((double)(gamma->green[middle]) / 
output->brightness
+                                         / 65535) / log(i1);
+               output->gamma.blue = log((double)(gamma->blue[middle]) / 
output->brightness
+                                        / 65535) / log(i1);
+           }
+
+           XRRFreeGamma(gamma);
+       }
+    }
+
     /* set transformation */
     if (!(output->changes & changes_transform))
     {
@@ -2919,6 +2985,9 @@ main (int argc, char **argv)
                printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
                printf ("\tTimestamp:  %d\n", (int)output_info->timestamp);
                printf ("\tSubpixel:   %s\n", 
order[output_info->subpixel_order]);
+               printf ("\tGamma:      %#.2g:%#.2g:%#.2g\n",
+                       output->gamma.red, output->gamma.green, 
output->gamma.blue);
+               printf ("\tBrightness: %#.2g\n", output->brightness);
                printf ("\tClones:    ");
                for (j = 0; j < output_info->nclone; j++)
                {
diff --git a/xrandr.man b/xrandr.man
index 335b7c0..aa1d9c5 100644
--- a/xrandr.man
+++ b/xrandr.man
@@ -105,7 +105,9 @@ Print out a summary of the usage and exit.
 Print out the RandR version reported by the X server and exit.
 .IP \-\-verbose
 Causes xrandr to be more verbose. When used with \-q (or without other
-options), xrandr will display more information about the server state. When
+options), xrandr will display more information about the server state. Please
+note that the gamma and brightness informations are only approximations of the
+complete color profile stored in the server. When
 used along with options that reconfigure the system, progress will be
 reported while executing the configuration changes.
 .IP "\-q, \-\-query"
@@ -287,11 +289,13 @@ for some reason, this option can override the normal 
selection.
 .IP "\-\-gamma \fIred\fP:\fIgreen\fP:\fIblue\fP"
 Set the specified floating point values as gamma correction on the crtc 
 currently attached to this output. Note that you cannot get two different 
values
-for cloned outputs and that switching an output to another crtc doesn't change
+for cloned outputs (i.e.: which share the same crtc) and that switching an 
output to another crtc doesn't change
 the crtc gamma corrections at all.
 .IP "\-\-brightness \fIbrightness\fP"
 Multiply the gamma values on the crtc currently attached to the output to
 specified floating value. Useful for overly bright or overly dim outputs.
+However, this is a software only modification, if your hardware has support to
+actually change the brightness, you will probably prefer to use 
\fBxbacklight\fR.
 .PP
 .SH "RandR version 1.1 options"
 These options are available for X servers supporting RandR version 1.1 or
@@ -342,7 +346,7 @@ when the projector is slightly above the screen:
 xrandr --fb 1024x768 --output VGA --transform 
1.24,0.16,-124,0,1.24,0,0,0.000316,1
 .RE
 .SH "SEE ALSO"
-Xrandr(3), cvt(1), xkeystone(1)
+Xrandr(3), cvt(1), xkeystone(1), xbacklight(1)
 .SH AUTHORS
 Keith Packard,
 Open Source Technology Center, Intel Corporation.
-- 
1.6.6

_______________________________________________
xorg-devel mailing list
[email protected]
http://lists.x.org/mailman/listinfo/xorg-devel

Reply via email to