gamma.py :  » Game-2D-3D » PsychoPy » PsychoPy-0.96.02 » psychopy » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PsychoPy 
PsychoPy » PsychoPy 0.96.02 » psychopy » gamma.py
#set the gamma LUT using platform-specific hardware calls
#this currently requires a pyglet window (to identify the current scr/display)

import numpy, sys, ctypes, ctypes.util

#import platform specific C++ libs for controlling gamma
if sys.platform=='win32':
    from ctypes import windll
elif sys.platform=='darwin':
    carbon = ctypes.CDLL('/System/Library/Carbon.framework/Carbon')
elif sys.platform.startswith('linux'):
    #we need XF86VidMode
    xf86vm=ctypes.CDLL(ctypes.util.find_library('Xxf86vm'))
    
def setGamma(pygletWindow=None, newGamma=1.0):
    #make sure gamma is 3x1 array
    if type(newGamma) in [float,int]:
        newGamma = numpy.tile(newGamma, [3,1])
    elif type(newGamma) in [list, tuple]:
        newGamma=numpy.array(newGamma)
        newGamma.shape=[3,1]
    elif type(newGamma) is numpy.ndarray:
        newGamma.shape=[3,1]
    #combine with the linear ramp    
    ramp = numpy.tile(numpy.arange(256, dtype=float)/255.0,(3,1))# (3x256) array
    newLUT = ramp**(1/numpy.array(newGamma))# correctly handles 1 or 3x1 gamma vals

    setGammaRamp(pygletWindow, newLUT)
    
def setGammaRamp(pygletWindow, newRamp):
    """Ramp should be provided as 3x256 array in range 0:1
    """
    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'
        
    if sys.platform=='darwin':  
        newRamp= (newRamp).astype(numpy.float32)
        error =carbon.CGSetDisplayTransferByTable(pygletWindow._screen.id, 256,
                   newRamp[0,:].ctypes, newRamp[1,:].ctypes, newRamp[2,:].ctypes)
        if error: raise AssertionError, 'CGSetDisplayTransferByTable failed'
        
    if sys.platform.startswith('linux'):
        newRamp= (65536*newRamp).astype(numpy.uint16)
        success = xf86vm.XF86VidModeSetGammaRamp(pygletWindow._x_display, pygletWindow._x_screen_id, 256,
                    newRamp[0,:].ctypes, newRamp[1,:].ctypes, newRamp[2,:].ctypes)
        if not success: raise AssertionError, 'XF86VidModeSetGammaRamp failed'
        
def getGammaRamp(pygletWindow):     
    """Ramp will be returned as 3x256 array in range 0:1
    """
    if sys.platform=='win32':  
        origramps = numpy.empty((3, 256), dtype=numpy.uint16) # init R, G, and B ramps
        success = windll.gdi32.GetDeviceGammaRamp(pygletWindow._dc, origramps.ctypes)
        if not success: raise AssertionError, 'SetDeviceGammaRamp failed'
        origramps.byteswap(True)
        origramps=origramps/255.0#rescale to 0:1
        
    if sys.platform=='darwin':          
        origramps = numpy.empty((3, 256), dtype=numpy.float32) # init R, G, and B ramps
        n = numpy.empty([1],dtype=numpy.int)
        error =carbon.CGGetDisplayTransferByTable(pygletWindow._screen.id, 256,
                   origramps[0,:].ctypes, origramps[1,:].ctypes, origramps[2,:].ctypes, n.ctypes);
        if error: raise AssertionError, 'CGSetDisplayTransferByTable failed'

    if sys.platform.startswith('linux'):
        origramps = numpy.empty((3, 256), dtype=numpy.uint16) 
        success = xf86vm.XF86VidModeGetGammaRamp(pygletWindow._x_display, pygletWindow._x_screen_id, 256,
                    origramps[0,:].ctypes, origramps[1,:].ctypes, origramps[2,:].ctypes)
        if not success: raise AssertionError, 'XF86VidModeGetGammaRamp failed'
        
    return origramps
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.