Git Product home page Git Product logo

Comments (13)

falahati avatar falahati commented on May 27, 2024

Gamma, brightness, and contrast are controlled via the Windows API even when modified from the NVidia Control panel. There is no API in NVAPI for them. NvAPI_VIO_SetGamma is about NVidia Capture Cards (VIO) and has nothing to do with monitors.

Specifically for gamma, the related methods are GetDeviceGammaRamp() and SetDeviceGammaRamp().

from nvapiwrapper.

falahati avatar falahati commented on May 27, 2024

It might also be possible to ask the monitor to change the related settings via I2C.

Take a look into the I2CSample.cs for an example about changing the brightness by communicating with the monitor. It might be possible to modify the code to change the monitor's gamma value. I know the contrast index but I don't have any info about the gamma.

This might be useful:
https://cgit.freedesktop.org/~aplattner/nvidia-settings/tree/src/libXNVCtrl/NVCtrl.h?id=b27db3d10d58b821e87fbe3f46166e02dc589855#n1329

And this:
http://boichat.ch/nicolas/ddcci/specs.html

Specially:
https://milek7.pl/ddcbacklight/mccs.pdf
@ Page 26 - 7.1 Image Adjustments

from nvapiwrapper.

Straafe avatar Straafe commented on May 27, 2024

Ah, ok I see. Thanks for the information. I found this wrapper and was giving it a try because I had already done some testing using SetDeviceGammaRamp(), but even though it appeared to run, my monitor's gamma never changed. I'd assumed it was because I had to deal with gamma through Nvidia instead, but now I see I was wrong. I'll be doing some more testing using the information you've provided, thanks again.

from nvapiwrapper.

falahati avatar falahati commented on May 27, 2024

I recently wrote a function to change the gamma ramp similar to what NVidia Control Panel sends to the API. Check it out:

private static ushort[] CalculateLUT(double brightness = 0.5, double contrast = 0.5, double gamma = 1)
{
    const int dataPoints = 256;

    // Limit gamma in range [0.4-2.8]
    gamma = Math.Min(Math.Max(gamma, 0.4), 2.8);

    // Normalize contrast in range [-1,1]
    contrast = (Math.Min(Math.Max(contrast, 0), 1) - 0.5) * 2;

    // Normalize brightness in range [-1,1]
    brightness = (Math.Min(Math.Max(brightness, 0), 1) - 0.5) * 2;

    // Calculate curve offset resulted from contrast
    var offset = contrast > 0 ? contrast * -25.4 : contrast * -32;

    // Calculate the total range of curve
    var range = (dataPoints - 1) + offset * 2;

    // Add brightness to the curve offset
    offset += brightness * (range / 5);

    // Fill the gamma curve
    var result = new ushort[dataPoints];
    for (var i = 0; i < result.Length; i++)
    {
        var factor = (i + offset) / range;

        factor = Math.Pow(factor, 1 / gamma);

        factor = Math.Min(Math.Max(factor, 0), 1);

        result[i] = (ushort)Math.Round(factor * ushort.MaxValue);
    }

    return result;
}

from nvapiwrapper.

falahati avatar falahati commented on May 27, 2024

Use this along with SetDeviceGammaRamp() Windows API to change the gamma, brightness, and/or contrast.

from nvapiwrapper.

Straafe avatar Straafe commented on May 27, 2024

That looks awesome, will check it out!

from nvapiwrapper.

incheon-kim avatar incheon-kim commented on May 27, 2024

I recently wrote a function to change the gamma ramp similar to what NVidia Control Panel sends to the API. Check it out:

private static ushort[] CalculateLUT(double brightness = 0.5, double contrast = 0.5, double gamma = 1)
{
    const int dataPoints = 256;

    // Limit gamma in range [0.4-2.8]
    gamma = Math.Min(Math.Max(gamma, 0.4), 2.8);

    // Normalize contrast in range [-1,1]
    contrast = (Math.Min(Math.Max(contrast, 0), 1) - 0.5) * 2;

    // Normalize brightness in range [-1,1]
    brightness = (Math.Min(Math.Max(brightness, 0), 1) - 0.5) * 2;

    // Calculate curve offset resulted from contrast
    var offset = contrast > 0 ? contrast * -25.4 : contrast * -32;

    // Calculate the total range of curve
    var range = (dataPoints - 1) + offset * 2;

    // Add brightness to the curve offset
    offset += brightness * (range / 5);

    // Fill the gamma curve
    var result = new ushort[dataPoints];
    for (var i = 0; i < result.Length; i++)
    {
        var factor = (i + offset) / range;

        factor = Math.Pow(factor, 1 / gamma);

        factor = Math.Min(Math.Max(factor, 0), 1);

        result[i] = (ushort)Math.Round(factor * ushort.MaxValue);
    }

    return result;
}

This code saved my day! Brilliant! Thanks.

from nvapiwrapper.

marsarCV avatar marsarCV commented on May 27, 2024

When I use the above code along with SetDeviceGammaRamp() function, it tries to change the display momentarily and then reverts back. I think it might be due to the way I am accessing the display handle or something else but not because of the CalculateLUT() function. Can you see what is the problem?

`
public class Win32
{
// Get Display control
[DllImport("user32")]
public static extern IntPtr GetDC(IntPtr hWnd);

    // Get Display control
    [DllImport("user32")]
    public static extern IntPtr ReleaseDC(IntPtr n, IntPtr hWnd);

    // Ramp function
    [DllImport("gdi32")]
    public static extern bool GetDeviceGammaRamp(IntPtr hDC, [Out] ushort[] lpRamp);
    
    [DllImport("gdi32")]
    public static extern bool GetGammaRamp(IntPtr hDC, [Out] ushort[] lpRamp);

    [DllImport("gdi32")]
    public static extern bool SetDeviceGammaRamp(IntPtr hDC, ushort[] lpRamp);
    
    [DllImport("gdi32")]
    public static extern bool SetGammaRamp(IntPtr hDC, ushort[] lpRamp);

    [DllImport("gdi32")]
    public static extern bool SupportsFullscreenGamma();

    public static IntPtr hDC;
    public static ushort[] originalRamps;
}


    class Program
{
    // Color Matchingg dictionary
    static void Main(string[] args)
    {

        // Control the display brightness, contrast and gamma
        ushort[] newValues = CalculateLUT(0.1, 0.4, 1.1);
        Win32.hDC = Win32.GetDC(IntPtr.Zero);
        Win32.originalRamps = new ushort[256];
        Win32.GetDeviceGammaRamp(Win32.hDC, Win32.originalRamps);

        // Change the brightness, gamma and contrast
        Win32.SetDeviceGammaRamp(Win32.hDC, newValues);

        Win32.ReleaseDC(IntPtr.Zero, Win32.hDC);
     }

}`

from nvapiwrapper.

falahati avatar falahati commented on May 27, 2024

My WindowsDisplayAPI library can be used to get information about monitors and manipulate their config including the GammaRamp. Take a look. You are probably interested in Display.GamaRamp property.

from nvapiwrapper.

marsarCV avatar marsarCV commented on May 27, 2024

Great! It worked, Thanks. I also want to extract the current values for brightness, contrast, gamma for the display. Is there any property in the API for this?

Can gamma ramp values be decomposed some way to get these values?

from nvapiwrapper.

falahati avatar falahati commented on May 27, 2024

No, unfortunately, the algorithm used is one way. NVidia control panel keeps the raw values in the registry for UI porpuses, otherwise guessing the values from the ramp is not practical.

from nvapiwrapper.

LikeyX avatar LikeyX commented on May 27, 2024

My WindowsDisplayAPI library can be used to get information about monitors and manipulate their config including the GammaRamp. Take a look. You are probably interested in Display.GamaRamp property.

Hey, did the Diplay.GammaRamp Property and Display.IsPrimary() Method removed?

from nvapiwrapper.

falahati avatar falahati commented on May 27, 2024

@LikeyX no, but I think they are on the prerelease package.

from nvapiwrapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.