<s> """<STR_LIT>""" <EOL> from OpenGL import GL <EOL> from OpenGL . GL . ARB import texture_rg <EOL> import numpy <EOL> from pyrr . utils import parameters_as_numpy_arrays <EOL> import numpy_utils <EOL> import gl <EOL> def active_unit ( ) : <EOL> return GL . glGetInteger ( GL . GL_ACTIVE_TEXTURE ) <EOL> def active_texture ( target , unit ) : <EOL> """<STR_LIT>""" <EOL> unit_ = active_unit ( ) <EOL> if unit_ != unit : <EOL> GL . glActiveTexture ( GL . GL_TEXTURE0 + unit ) <EOL> query_enums = { <EOL> GL . GL_TEXTURE_1D : GL . GL_TEXTURE_BINDING_1D , <EOL> GL . GL_TEXTURE_2D : GL . GL_TEXTURE_BINDING_2D , <EOL> GL . GL_TEXTURE_3D : GL . GL_TEXTURE_BINDING_3D , <EOL> } <EOL> enum = query_enums [ target ] <EOL> handle = GL . glGetInteger ( enum ) <EOL> if unit_ != unit : <EOL> GL . glActiveTexture ( GL . GL_TEXTURE0 + unit_ ) <EOL> return handle <EOL> def format ( format ) : <EOL> if isinstance ( format , basestring ) : <EOL> pass <EOL> else : <EOL> return format <EOL> class Texture ( object ) : <EOL> """<STR_LIT>""" <EOL> inferred_targets = { <EOL> <NUM_LIT:1> : GL . GL_TEXTURE_1D , <EOL> <NUM_LIT:2> : GL . GL_TEXTURE_2D , <EOL> <NUM_LIT:3> : GL . GL_TEXTURE_3D , <EOL> } <EOL> target_ndims = { <EOL> GL . GL_TEXTURE_1D : <NUM_LIT:1> , <EOL> GL . GL_TEXTURE_2D : <NUM_LIT:2> , <EOL> GL . GL_TEXTURE_3D : <NUM_LIT:3> , <EOL> } <EOL> inferred_formats = { <EOL> <NUM_LIT:1> : GL . GL_RED , <EOL> <NUM_LIT:2> : texture_rg . GL_RG , <EOL> <NUM_LIT:3> : GL . GL_RGB , <EOL> <NUM_LIT:4> : GL . GL_RGBA , <EOL> } <EOL> funcs = { <EOL> <NUM_LIT:1> : ( GL . glTexImage1D , GL . glTexSubImage1D ) , <EOL> <NUM_LIT:2> : ( GL . glTexImage2D , GL . glTexSubImage2D ) , <EOL> <NUM_LIT:3> : ( GL . glTexImage3D , GL . glTexSubImage3D ) , <EOL> } <EOL> channels = { <EOL> '<STR_LIT:r>' : GL . GL_RED , <EOL> '<STR_LIT:g>' : GL . GL_GREEN , <EOL> '<STR_LIT:b>' : GL . GL_BLUE , <EOL> '<STR_LIT:1>' : GL . GL_ONE , <EOL> '<STR_LIT:0>' : GL . GL_ZERO , <EOL> } <EOL> @ classmethod <EOL> def from_file ( cls , filename , ** kwargs ) : <EOL> from PIL import Image <EOL> image = Image . open ( filename ) <EOL> return cls . from_image ( image , ** kwargs ) <EOL> @ classmethod <EOL> def from_image ( cls , image , ** kwargs ) : <EOL> texture = cls ( ** kwargs ) <EOL> texture . bind ( ) <EOL> texture . set_pil_image ( image ) <EOL> texture . unbind ( ) <EOL> return texture <EOL> @ parameters_as_numpy_arrays ( '<STR_LIT:data>' ) <EOL> def __init__ ( <EOL> self , <EOL> data = None , <EOL> target = None , <EOL> internal_format = None , <EOL> min_filter = GL . GL_NEAREST , <EOL> mag_filter = GL . GL_NEAREST , <EOL> wrap_s = GL . GL_REPEAT , <EOL> wrap_t = GL . GL_REPEAT , <EOL> shape = None , <EOL> format = None , <EOL> data_type = None , <EOL> border = False , <EOL> ** kwargs <EOL> ) : <EOL> """<STR_LIT>""" <EOL> super ( Texture , self ) . __init__ ( ) <EOL> if data != None : <EOL> if not target : <EOL> target = Texture . inferred_targets [ data . ndims - <NUM_LIT:1> ] <EOL> if not shape : <EOL> shape = data . shape [ : - <NUM_LIT:1> ] <EOL> if not data_type : <EOL> data_type = numpy_utils . dtype_gl_enum ( data . dtype ) <EOL> if not internal_format : <EOL> internal_format = Texture . inferred_formats [ data . shape [ - <NUM_LIT:1> ] ] <EOL> self . _target = target <EOL> self . _internal_format = internal_format <EOL> self . _handle = GL . glGenTextures ( <NUM_LIT:1> ) <EOL> self . _border = border <EOL> self . _shape = shape <EOL> self . bind ( ) <EOL> self . min_filter = min_filter <EOL> self . mag_filter = mag_filter <EOL> self . wrap_s = wrap_s <EOL> self . wrap_t = wrap_t <EOL> if shape and data_type and internal_format : <EOL> self . allocate ( shape , data_type , self . _internal_format , ** kwargs ) <EOL> if data != None : <EOL> self . set_data ( data , format = format , shape = shape , data_type = data_type , ** kwargs ) <EOL> self . unbind ( ) <EOL> @ property <EOL> def handle ( self ) : <EOL> return self . _handle <EOL> @ property <EOL> def target ( self ) : <EOL> return self . _target <EOL> @ property <EOL> def internal_format ( self ) : <EOL> return self . _internal_format <EOL> @ property <EOL> def border ( self ) : <EOL> return self . _border <EOL> @ property <EOL> def shape ( self ) : <EOL> return self . _shape <EOL> @ property <EOL> def min_filter ( self ) : <EOL> return self . _mag_filter <EOL> @ min_filter . setter <EOL> def min_filter ( self , mode ) : <EOL> if mode not in [ GL . GL_NEAREST , GL . GL_LINEAR , GL . GL_NEAREST_MIPMAP_NEAREST , GL . GL_LINEAR_MIPMAP_NEAREST , GL . GL_NEAREST_MIPMAP_LINEAR , GL . GL_LINEAR_MIPMAP_LINEAR ] : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _min_filter = mode <EOL> GL . glTexParameteri ( self . _target , GL . GL_TEXTURE_MIN_FILTER , mode ) <EOL> @ property <EOL> def mag_filter ( self ) : <EOL> return self . _mag_filter <EOL> @ mag_filter . setter <EOL> def mag_filter ( self , mode ) : <EOL> if mode not in [ GL . GL_NEAREST , GL . GL_LINEAR , GL . GL_NEAREST_MIPMAP_NEAREST , GL . GL_LINEAR_MIPMAP_NEAREST , GL . GL_NEAREST_MIPMAP_LINEAR , GL . GL_LINEAR_MIPMAP_LINEAR ] : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _mag_filter = mode <EOL> GL . glTexParameteri ( self . _target , GL . GL_TEXTURE_MAG_FILTER , mode ) <EOL> @ property <EOL> def wrap_s ( self ) : <EOL> return self . _wrap_s <EOL> @ wrap_s . setter <EOL> def wrap_s ( self , mode ) : <EOL> if mode not in [ GL . GL_CLAMP_TO_EDGE , GL . GL_CLAMP_TO_BORDER , GL . GL_MIRRORED_REPEAT , GL . GL_REPEAT ] : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _wrap_s = mode <EOL> GL . glTexParameteri ( self . _target , GL . GL_TEXTURE_WRAP_S , mode ) <EOL> @ property <EOL> def wrap_t ( self ) : <EOL> return self . _wrap_t <EOL> @ wrap_t . setter <EOL> def wrap_t ( self , mode ) : <EOL> if mode not in [ GL . GL_CLAMP_TO_EDGE , GL . GL_CLAMP_TO_BORDER , GL . GL_MIRRORED_REPEAT , GL . GL_REPEAT ] : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _wrap_t = mode <EOL> GL . glTexParameteri ( self . _target , GL . GL_TEXTURE_WRAP_T , mode ) <EOL> def bind ( self ) : <EOL> GL . glBindTexture ( self . _target , self . _handle ) <EOL> def unbind ( self ) : <EOL> GL . glBindTexture ( self . _target , <NUM_LIT:0> ) <EOL> def allocate ( self , shape , data_type , internal_format = None , ** kwargs ) : <EOL> if not internal_format : <EOL> internal_format = self . _internal_format <EOL> else : <EOL> self . _internal_format = internal_format <EOL> ndims = Texture . target_ndims [ self . _target ] <EOL> func , _ = Texture . funcs [ ndims ] <EOL> border = <NUM_LIT:1> if self . _border else <NUM_LIT:0> <EOL> self . _shape = tuple ( shape ) <EOL> if not data_type : <EOL> data_type = numpy_utils . dtype_gl_enum ( data . dtype ) <EOL> else : <EOL> if isinstance ( data_type , numpy . dtype ) : <EOL> data_type = numpy_utils . dtype_gl_enum ( data_type ) <EOL> elif isinstance ( data_type , basestring ) : <EOL> data_type = numpy_utils . dtype_gl_enum ( numpy . dtype ( data_type ) ) <EOL> args = [ <EOL> self . target , <EOL> <NUM_LIT:0> , <EOL> internal_format , ] + list ( shape ) + [ <EOL> border , <EOL> GL . GL_RED , <EOL> data_type , <EOL> None , <EOL> ] <EOL> func ( * args ) <EOL> @ parameters_as_numpy_arrays ( '<STR_LIT:data>' ) <EOL> def set_data ( self , data , format = None , shape = None , offset = None , data_type = None , level = <NUM_LIT:0> , swizzle = None , ** kwargs ) : <EOL> def set_swizzle ( swizzle ) : <EOL> if isinstance ( swizzle , ( list , tuple ) ) : <EOL> swizzle = ( GL . GLint * len ( swizzle ) ) ( * swizzle ) <EOL> elif isinstance ( swizzle , basestring ) : <EOL> result = [ <EOL> Texture . channels [ channel ] <EOL> for channel in swizzle . lower ( ) <EOL> ] <EOL> swizzle = ( GL . GLint * len ( result ) ) ( * result ) <EOL> GL . glTexParameteriv ( self . _target , GL . GL_TEXTURE_SWIZZLE_RGBA , swizzle ) <EOL> ndims = Texture . target_ndims [ self . _target ] <EOL> _ , func = Texture . funcs [ ndims ] <EOL> if not shape : <EOL> if data . ndims != ( ndims + <NUM_LIT:1> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> shape = data . shape [ : - <NUM_LIT:1> ] <EOL> if not offset : <EOL> offset = [ <NUM_LIT:0> ] * ndims <EOL> if not data_type : <EOL> data_type = numpy_utils . dtype_gl_enum ( data . dtype ) <EOL> if not format : <EOL> num_channels = data . shape [ - <NUM_LIT:1> ] <EOL> format = Texture . inferred_formats [ num_channels ] <EOL> if gl . is_core ( ) : <EOL> if swizzle : <EOL> set_swizzle ( swizzle ) <EOL> args = [ <EOL> self . target , <EOL> level , ] + list ( offset ) + list ( shape ) + [ <EOL> format , <EOL> data_type , <EOL> data , <EOL> ] <EOL> func ( * args ) <EOL> def set_pil_image ( self , image , level = <NUM_LIT:0> , flip = True , ** kwargs ) : <EOL> def legacy_format ( format ) : <EOL> return { <EOL> '<STR_LIT>' : GL . GL_RGB , <EOL> '<STR_LIT>' : GL . GL_RGBA , <EOL> '<STR_LIT>' : GL . GL_RGBA , <EOL> '<STR_LIT>' : GL . GL_RGBA , <EOL> '<STR_LIT:1>' : GL . GL_LUMINANCE , <EOL> '<STR_LIT:L>' : GL . GL_LUMINANCE , <EOL> '<STR_LIT>' : GL . GL_LUMINANCE_ALPHA , <EOL> '<STR_LIT:F>' : GL . GL_LUMINANCE , <EOL> '<STR_LIT:I>' : GL . GL_LUMINANCE , <EOL> } [ format ] <EOL> def core_format ( format ) : <EOL> return { <EOL> '<STR_LIT>' : GL . GL_RGB , <EOL> '<STR_LIT>' : GL . GL_RGBA , <EOL> '<STR_LIT>' : GL . GL_RGBA , <EOL> '<STR_LIT>' : GL . GL_RGBA , <EOL> '<STR_LIT:1>' : GL . GL_RED , <EOL> '<STR_LIT:L>' : GL . GL_RED , <EOL> '<STR_LIT>' : texture_rg . GL_RG , <EOL> '<STR_LIT:F>' : GL . GL_RGB , <EOL> '<STR_LIT:I>' : GL . GL_RGB , <EOL> } [ format ] <EOL> types = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , GL . GL_RGB8 ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , GL . GL_RGBA8 ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , GL . GL_RGBA8 ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , GL . GL_RGBA8 ) , <EOL> '<STR_LIT:1>' : ( '<STR_LIT>' , GL . GL_RGBA8 ) , <EOL> '<STR_LIT:L>' : ( '<STR_LIT>' , GL . GL_RGBA8 ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , GL . GL_RGBA8 ) , <EOL> '<STR_LIT:F>' : ( '<STR_LIT>' , GL . GL_RGBA32F ) , <EOL> '<STR_LIT:I>' : ( '<STR_LIT>' , GL . GL_RGBA32I ) , <EOL> } <EOL> from PIL import Image <EOL> if self . _target != GL . GL_TEXTURE_2D : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if image . format == '<STR_LIT>' : <EOL> image = image . convert ( '<STR_LIT>' ) <EOL> if image . mode in [ '<STR_LIT:P>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> image = image . convert ( '<STR_LIT>' ) <EOL> dtype , internal_format = types [ image . mode ] <EOL> swizzle = None <EOL> if gl . is_legacy ( ) : <EOL> format = legacy_format ( image . mode ) <EOL> else : <EOL> format = core_format ( image . mode ) <EOL> swizzles = { <EOL> '<STR_LIT:1>' : '<STR_LIT>' , <EOL> '<STR_LIT:L>' : '<STR_LIT>' , <EOL> '<STR_LIT:I>' : '<STR_LIT>' , <EOL> '<STR_LIT:F>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> if image . mode in swizzles : <EOL> swizzle = swizzles [ image . mode ] <EOL> if flip : <EOL> image = image . transpose ( Image . FLIP_TOP_BOTTOM ) <EOL> data = numpy . array ( image . getdata ( ) , dtype = dtype ) <EOL> data . shape = ( image . size [ <NUM_LIT:0> ] , image . size [ <NUM_LIT:1> ] , - <NUM_LIT:1> ) <EOL> self . allocate ( image . size , dtype , internal_format ) <EOL> self . set_data ( <EOL> data = data , <EOL> shape = image . size , <EOL> format = format , <EOL> level = level , <EOL> swizzle = swizzle , <EOL> ) <EOL> class Texture1D ( Texture ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Texture1D , self ) . __init__ ( target = GL . GL_TEXTURE_1D , * args , ** kwargs ) <EOL> @ property <EOL> def width ( self ) : <EOL> return self . _shape [ <NUM_LIT:0> ] <EOL> class Texture2D ( Texture ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Texture2D , self ) . __init__ ( target = GL . GL_TEXTURE_2D , * args , ** kwargs ) <EOL> @ property <EOL> def width ( self ) : <EOL> return self . _shape [ <NUM_LIT:0> ] <EOL> @ property <EOL> def height ( self ) : <EOL> return self . _shape [ <NUM_LIT:1> ] <EOL> class Texture3D ( Texture ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Texture3D , self ) . __init__ ( target = GL . GL_TEXTURE_3D , * args , ** kwargs ) <EOL> @ property <EOL> def width ( self ) : <EOL> return self . _shape [ <NUM_LIT:0> ] <EOL> @ property <EOL> def height ( self ) : <EOL> return self . _shape [ <NUM_LIT:1> ] <EOL> @ property <EOL> def depth ( self ) : <EOL> return self . _shape [ <NUM_LIT:2> ] </s>
