Correct usage of SetDeviceGammaRamp

前端 未结 2 894
日久生厌
日久生厌 2020-12-06 17:19

I\'d like to add the ability to adjust screen gamma at application startup and reset it at exit. While it\'s debatable whether one should tamper with gamma at all (personal

2条回答
  •  独厮守ぢ
    2020-12-06 18:08

    I haven't tested this, but if I had to guess, early graphics cards were non-standard in their implementation of SetDeviceGammaRamp() when Doom was written and sometimes used the LOBYTE and sometimes used the HIBYTE of the WORD value. The consensus moved to only using the HIBYTE, hence the word_value = byte_value<<8.

    Here's another datapoint, from the PsychoPy library (in python) which is just swapping LOBYTE and HIBYTE:

     """Sets the hardware look-up table, using platform-specific ctypes functions. 
     For use with pyglet windows only (pygame has its own routines for this). 
     Ramp should be provided as 3x256 or 3x1024 array in range 0:1.0 
     """ 
    if sys.platform=='win32':   
        newRamp= (255*newRamp).astype(numpy.uint16) 
        newRamp.byteswap(True)#necessary, according to pyglet post from Martin Spacek 
        success = windll.gdi32.SetDeviceGammaRamp(pygletWindow._dc, newRamp.ctypes) 
        if not success: raise AssertionError, 'SetDeviceGammaRamp failed' 
    

    It also appears that Windows doesn't allow all gamma settings, see: http://jonls.dk/2010/09/windows-gamma-adjustments/

    Update:

    The first Windows APIs to offer gamma control are Windows Graphics Device Interface (GDI)’s SetDeviceGammaRamp and GetDeviceGammaRamp. These APIs work with three 256-entry arrays of WORDs, with each WORD encoding zero up to one, represented by WORD values 0 and 65535. The extra precision of a WORD typically isn’t available in actual hardware lookup tables, but these APIs were intended to be flexible. These APIs, in contrast to the others described later in this section, allow only a small deviation from an identity function. In fact, any entry in the ramp must be within 32768 of the identity value. This restriction means that no app can turn the display completely black or to some other unreadable color.

    http://msdn.microsoft.com/en-us/library/windows/desktop/jj635732(v=vs.85).aspx

提交回复
热议问题