home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / GL / images.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  17.8 KB  |  585 lines

  1. """Image-handling routines
  2.  
  3. ### Unresolved:
  4.  
  5.     Following methods are not yet resolved due to my not being sure how the 
  6.     function should be wrapped:
  7.     
  8.         glCompressedTexImage3D
  9.         glCompressedTexImage2D
  10.         glCompressedTexImage1D
  11.         glCompressedTexSubImage3D
  12.         glCompressedTexSubImage2D
  13.         glCompressedTexSubImage1D
  14. """
  15. from OpenGL.raw import GL as simple
  16. from OpenGL import images, arrays, wrapper, platform
  17. import ctypes
  18.  
  19. def asInt( value ):
  20.     if isinstance( value, float ):
  21.         return int(round(value,0))
  22.     return value
  23.  
  24. ## update the image tables with standard image types...
  25. #images.FORMAT_BITS.update( {
  26. #    simple.GL_BITMAP : 1, # must be GL_UNSIGNED_BYTE
  27. #    
  28. #    simple.GL_RED : 8,
  29. #    simple.GL_GREEN : 8,
  30. #    simple.GL_BLUE : 8,
  31. #    simple.GL_ALPHA : 8,
  32. #    simple.GL_LUMINANCE : 8,
  33. #    simple.GL_LUMINANCE_ALPHA : 8,
  34. #    simple.GL_COLOR_INDEX : 8,
  35. #    simple.GL_STENCIL_INDEX : 8,
  36. #    simple.GL_DEPTH_COMPONENT : 8,
  37. #    simple.GL_RGB : 24,
  38. #    simple.GL_BGR : 24,
  39. #
  40. #    simple.GL_RGBA : 32,
  41. #    simple.GL_BGRA : 32,
  42. #    simple.GL_ABGR_EXT : 32,
  43. #    simple.GL_CMYK_EXT : 32,
  44. #
  45. #    simple.GL_CMYKA_EXT : 40,
  46. #    
  47. #    simple.GL_YCRCB_422_SGIX : 8, # must be GL_UNSIGNED_BYTE
  48. #    simple.GL_YCRCB_444_SGIX : 8, # must be GL_UNSIGNED_SHORT
  49. #
  50. #    simple.GL_FORMAT_SUBSAMPLE_24_24_OML : 32, # must be GL_UNSIGNED_INT_10_10_10_2
  51. #    simple.GL_FORMAT_SUBSAMPLE_244_244_OML : 32, # must be GL_UNSIGNED_INT_10_10_10_2
  52. #} )
  53. images.COMPONENT_COUNTS.update( {
  54.     simple.GL_BITMAP : 1, # must be GL_UNSIGNED_BYTE
  55.     
  56.     simple.GL_RED : 1,
  57.     simple.GL_GREEN : 1,
  58.     simple.GL_BLUE : 1,
  59.     simple.GL_ALPHA : 1,
  60.     simple.GL_LUMINANCE : 1,
  61.     simple.GL_LUMINANCE_ALPHA : 2,
  62.     simple.GL_COLOR_INDEX : 1,
  63.     simple.GL_STENCIL_INDEX : 1,
  64.     simple.GL_DEPTH_COMPONENT : 1,
  65.     simple.GL_RGB : 3,
  66.     simple.GL_BGR : 3,
  67.  
  68.     simple.GL_RGBA : 4,
  69.     simple.GL_BGRA : 4,
  70.     simple.GL_ABGR_EXT : 4,
  71.     simple.GL_CMYK_EXT : 4,
  72.  
  73.     simple.GL_CMYKA_EXT : 5,
  74.     
  75.     simple.GL_YCRCB_422_SGIX : 1, # must be GL_UNSIGNED_BYTE
  76.     simple.GL_YCRCB_444_SGIX : 1, # must be GL_UNSIGNED_SHORT
  77.  
  78.     simple.GL_FORMAT_SUBSAMPLE_24_24_OML : 1, # must be GL_UNSIGNED_INT_10_10_10_2
  79.     simple.GL_FORMAT_SUBSAMPLE_244_244_OML : 1, # must be GL_UNSIGNED_INT_10_10_10_2
  80. } )
  81.  
  82. #images.TYPE_TO_BITS.update( {
  83. #    simple.GL_UNSIGNED_BYTE_3_3_2 : 8,
  84. #    simple.GL_UNSIGNED_BYTE_2_3_3_REV : 8,
  85. #    simple.GL_UNSIGNED_SHORT_4_4_4_4 : 16,
  86. #    simple.GL_UNSIGNED_SHORT_4_4_4_4_REV : 16,
  87. #    simple.GL_UNSIGNED_SHORT_5_5_5_1 : 16,
  88. #    simple.GL_UNSIGNED_SHORT_1_5_5_5_REV : 16,
  89. #    simple.GL_UNSIGNED_SHORT_5_6_5 : 16,
  90. #    simple.GL_UNSIGNED_SHORT_5_6_5_REV : 16,
  91. #    simple.GL_UNSIGNED_INT_8_8_8_8 : 32,
  92. #    simple.GL_UNSIGNED_INT_8_8_8_8_REV : 32,
  93. #    simple.GL_UNSIGNED_INT_10_10_10_2 : 32,
  94. #    simple.GL_UNSIGNED_INT_2_10_10_10_REV : 32,
  95. #    simple.GL_UNSIGNED_BYTE : ctypes.sizeof(simple.GLubyte) * 8,
  96. #    simple.GL_BYTE: ctypes.sizeof(simple.GLbyte) * 8,
  97. #    simple.GL_UNSIGNED_SHORT :  ctypes.sizeof(simple.GLushort) * 8,
  98. #    simple.GL_SHORT :  ctypes.sizeof(simple.GLshort) * 8,
  99. #    simple.GL_UNSIGNED_INT : ctypes.sizeof(simple.GLuint) * 8,
  100. #    simple.GL_INT : ctypes.sizeof(simple.GLint) * 8,
  101. #    simple.GL_FLOAT : ctypes.sizeof(simple.GLfloat) * 8,
  102. #    simple.GL_DOUBLE : ctypes.sizeof(simple.GLdouble) * 8,
  103. #} )
  104. images.TYPE_TO_ARRAYTYPE.update( {
  105.     simple.GL_UNSIGNED_BYTE_3_3_2 : simple.GL_UNSIGNED_BYTE,
  106.     simple.GL_UNSIGNED_BYTE_2_3_3_REV : simple.GL_UNSIGNED_BYTE,
  107.     simple.GL_UNSIGNED_SHORT_4_4_4_4 : simple.GL_UNSIGNED_SHORT,
  108.     simple.GL_UNSIGNED_SHORT_4_4_4_4_REV : simple.GL_UNSIGNED_SHORT,
  109.     simple.GL_UNSIGNED_SHORT_5_5_5_1 : simple.GL_UNSIGNED_SHORT,
  110.     simple.GL_UNSIGNED_SHORT_1_5_5_5_REV : simple.GL_UNSIGNED_SHORT,
  111.     simple.GL_UNSIGNED_SHORT_5_6_5 : simple.GL_UNSIGNED_SHORT,
  112.     simple.GL_UNSIGNED_SHORT_5_6_5_REV : simple.GL_UNSIGNED_SHORT,
  113.     simple.GL_UNSIGNED_INT_8_8_8_8 : simple.GL_UNSIGNED_INT,
  114.     simple.GL_UNSIGNED_INT_8_8_8_8_REV : simple.GL_UNSIGNED_INT,
  115.     simple.GL_UNSIGNED_INT_10_10_10_2 : simple.GL_UNSIGNED_INT,
  116.     simple.GL_UNSIGNED_INT_2_10_10_10_REV : simple.GL_UNSIGNED_INT,
  117.     simple.GL_UNSIGNED_BYTE : simple.GL_UNSIGNED_BYTE,
  118.     simple.GL_BYTE: simple.GL_BYTE,
  119.     simple.GL_UNSIGNED_SHORT : simple.GL_UNSIGNED_SHORT,
  120.     simple.GL_SHORT :  simple.GL_SHORT,
  121.     simple.GL_UNSIGNED_INT : simple.GL_UNSIGNED_INT,
  122.     simple.GL_INT : simple.GL_INT,
  123.     simple.GL_FLOAT : simple.GL_FLOAT,
  124.     simple.GL_DOUBLE : simple.GL_DOUBLE,
  125. } )
  126. images.RANK_PACKINGS.update( {
  127.     4: [
  128.         (simple.glPixelStorei,simple.GL_PACK_SKIP_VOLUMES_SGIS, 0),
  129.         (simple.glPixelStorei,simple.GL_PACK_IMAGE_DEPTH_SGIS, 0),
  130.     ],
  131.     3: [
  132.         (simple.glPixelStorei,simple.GL_PACK_SKIP_IMAGES, 0),
  133.         (simple.glPixelStorei,simple.GL_PACK_IMAGE_HEIGHT, 0),
  134.     ],
  135.     2: [
  136.         (simple.glPixelStorei,simple.GL_PACK_ROW_LENGTH, 0),
  137.         (simple.glPixelStorei,simple.GL_PACK_SKIP_ROWS, 0),
  138.         (simple.glPixelStorei,simple.GL_PACK_ALIGNMENT, 1),
  139.     ],
  140.     1: [
  141.         (simple.glPixelStorei,simple.GL_PACK_SKIP_PIXELS, 0),
  142.     ],
  143. } )
  144.  
  145.  
  146. __all__ = (
  147.     'glReadPixels',
  148.     'glReadPixelsb',
  149.     'glReadPixelsd',
  150.     'glReadPixelsf',
  151.     'glReadPixelsi',
  152.     'glReadPixelss',
  153.     'glReadPixelsub',
  154.     'glReadPixelsui',
  155.     'glReadPixelsus',
  156.     
  157.     'glGetTexImage',
  158.     
  159.     'glDrawPixels',
  160.     'glDrawPixelsb',
  161.     'glDrawPixelsf',
  162.     'glDrawPixelsi',
  163.     'glDrawPixelss',
  164.     'glDrawPixelsub',
  165.     'glDrawPixelsui',
  166.     'glDrawPixelsus',
  167.     
  168.     
  169.     'glTexSubImage2D',
  170.     'glTexSubImage1D',
  171.     #'glTexSubImage3D',
  172.     
  173.     'glTexImage1D',
  174.     'glTexImage2D',
  175.     #'glTexImage3D',
  176.     
  177.     'glGetTexImageb',
  178.     'glGetTexImaged',
  179.     'glGetTexImagef',
  180.     'glGetTexImagei',
  181.     'glGetTexImages',
  182.     'glGetTexImageub',
  183.     'glGetTexImageui',
  184.     'glGetTexImageus',
  185.     'glTexImage1Db',
  186.     'glTexImage2Db',
  187.     #'glTexImage3Db',
  188.     'glTexSubImage1Db',
  189.     'glTexSubImage2Db',
  190.     #'glTexSubImage3Db',
  191.     'glTexImage1Df',
  192.     'glTexImage2Df',
  193.     #'glTexImage3Df',
  194.     'glTexSubImage1Df',
  195.     'glTexSubImage2Df',
  196.     #'glTexSubImage3Df',
  197.     'glTexImage1Di',
  198.     'glTexImage2Di',
  199.     #'glTexImage3Di',
  200.     'glTexSubImage1Di',
  201.     'glTexSubImage2Di',
  202.     #'glTexSubImage3Di',
  203.     'glTexImage1Ds',
  204.     'glTexImage2Ds',
  205.     #'glTexImage3Ds',
  206.     'glTexSubImage1Ds',
  207.     'glTexSubImage2Ds',
  208.     #'glTexSubImage3Ds',
  209.     'glTexImage1Dub',
  210.     'glTexImage2Dub',
  211.     #'glTexImage3Dub',
  212.     'glTexSubImage1Dub',
  213.     'glTexSubImage2Dub',
  214.     #'glTexSubImage3Dub',
  215.     'glTexImage1Dui',
  216.     'glTexImage2Dui',
  217.     #'glTexImage3Dui',
  218.     'glTexSubImage1Dui',
  219.     'glTexSubImage2Dui',
  220.     #'glTexSubImage3Dui',
  221.     'glTexImage1Dus',
  222.     'glTexImage2Dus',
  223.     #'glTexImage3Dus',
  224.     'glTexSubImage1Dus',
  225.     'glTexSubImage2Dus',
  226.     #'glTexSubImage3Dus',
  227.     
  228.     #'glColorTable',
  229.     #'glGetColorTable',
  230.     #'glColorSubTable',
  231.     
  232.     #'glConvolutionFilter1D',
  233.     #'glConvolutionFilter2D',
  234.     #'glGetConvolutionFilter',
  235.     #'glSeparableFilter2D',
  236.     #'glGetSeparableFilter',
  237.     
  238.     #'glGetMinmax',
  239. )
  240.  
  241. for suffix,type in [
  242.     ('b',simple.GL_BYTE),
  243.     ('d',simple.GL_DOUBLE),
  244.     ('f',simple.GL_FLOAT),
  245.     ('i',simple.GL_INT),
  246.     ('s',simple.GL_SHORT),
  247.     ('ub',simple.GL_UNSIGNED_BYTE),
  248.     ('ui',simple.GL_UNSIGNED_INT),
  249.     ('us',simple.GL_UNSIGNED_SHORT),
  250. ]:
  251.     def glReadPixels( x,y,width,height,format,type=type ):
  252.         """Read specified pixels from the current display buffer
  253.         
  254.         This typed version returns data in your specified default 
  255.         array data-type format
  256.         """
  257.         x,y,width,height = asInt(x),asInt(y),asInt(width),asInt(height)
  258.         array = images.SetupPixelRead( format, (width,height), type )
  259.         arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ]
  260.         imageData = arrayType.dataPointer(array)
  261.         simple.glReadPixels( 
  262.             x,y,
  263.             width, height,
  264.             format,type, 
  265.             ctypes.c_void_p( imageData )
  266.         )
  267.         return array
  268.     globals()["glReadPixels%s"%(suffix,)] = glReadPixels
  269.     def glGetTexImage( target, level,format,type=type ):
  270.         """Get a texture-level as an image"""
  271.         from OpenGL.GL import glget
  272.         dims = [glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_WIDTH )]
  273.         if target != simple.GL_TEXTURE_1D:
  274.             dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_HEIGHT ) )
  275.             if target != simple.GL_TEXTURE_2D:
  276.                 dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_DEPTH ) )
  277.         array = images.SetupPixelRead( format, tuple(dims), type )
  278.         arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ]
  279.         simple.glGetTexImage( 
  280.             target, level, format, type, ctypes.c_void_p( arrayType.dataPointer(array)) 
  281.         )
  282.         return array
  283.     globals()["glGetTexImage%s"%(suffix,)] = glGetTexImage
  284. ##    def glGetTexSubImage( target, level,format,type ):
  285. ##        """Get a texture-level as an image"""
  286. ##        from OpenGL.GL import glget
  287. ##        dims = [glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_WIDTH )]
  288. ##        if target != simple.GL_TEXTURE_1D:
  289. ##            dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_HEIGHT ) )
  290. ##            if target != simple.GL_TEXTURE_2D:
  291. ##                dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_DEPTH ) )
  292. ##        array = images.SetupPixelRead( format, tuple(dims), type )
  293. ##        arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ]
  294. ##        simple.glGetTexImage( 
  295. ##            target, level, format, type, ctypes.c_void_p( arrayType.dataPointer(array)) 
  296. ##        )
  297. ##        return array
  298. ##    "%s = glGetTexImage"%(suffix)
  299.     del suffix,type
  300. # Now the real glReadPixels...
  301. def glReadPixels( x,y,width,height,format,type, outputType=str ):
  302.     """Read specified pixels from the current display buffer
  303.     
  304.     x,y,width,height -- location and dimensions of the image to read 
  305.         from the buffer
  306.     format -- pixel format for the resulting data
  307.     type -- data-format for the resulting data
  308.     outputType -- default (str) provides string output of the 
  309.         results iff OpenGL.UNSIGNED_BYTE_IMAGES_AS_STRING is True 
  310.         and type == GL_UNSIGNED_BYTE.  Any other value will cause 
  311.         output in the default array output format.
  312.     
  313.     returns the pixel data array in the format defined by the 
  314.     format, type and outputType
  315.     """
  316.     x,y,width,height = asInt(x),asInt(y),asInt(width),asInt(height)
  317.     array = images.SetupPixelRead( format, (width,height), type )
  318.     arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ]
  319.     imageData = arrayType.dataPointer(array)
  320.     simple.glReadPixels( 
  321.         x,y,width,height,
  322.         format,type, 
  323.         ctypes.c_void_p( imageData ) 
  324.     )
  325.     if outputType is str:
  326.         return images.returnFormat( array, type )
  327.     else:
  328.         return array
  329.  
  330. def glGetTexImage( target, level,format,type, outputType=str ):
  331.     """Get a texture-level as an image
  332.     
  333.     target -- enum constant for the texture engine to be read 
  334.     level -- the mip-map level to read 
  335.     format -- image format to read out the data 
  336.     type -- data-type into which to read the data
  337.     
  338.     outputType -- default (str) provides string output of the 
  339.         results iff OpenGL.UNSIGNED_BYTE_IMAGES_AS_STRING is True 
  340.         and type == GL_UNSIGNED_BYTE.  Any other value will cause 
  341.         output in the default array output format.
  342.     
  343.     returns the pixel data array in the format defined by the 
  344.     format, type and outputType
  345.     """
  346.     from OpenGL.GL import glget
  347.     dims = [glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_WIDTH )]
  348.     if target != simple.GL_TEXTURE_1D:
  349.         dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_HEIGHT ) )
  350.         if target != simple.GL_TEXTURE_2D:
  351.             dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_DEPTH ) )
  352.     array = images.SetupPixelRead( format, tuple(dims), type )
  353.     arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ]
  354.     simple.glGetTexImage( 
  355.         target, level, format, type, ctypes.c_void_p( arrayType.dataPointer(array)) 
  356.     )
  357.     if outputType is str:
  358.         return images.returnFormat( array, type )
  359.     else:
  360.         return array
  361.  
  362.  
  363. INT_DIMENSION_NAMES = [
  364.     'width','height','depth','x','y','z',
  365.     'xoffset','yoffset','zoffset',
  366.     'start', 'count',
  367. ]
  368. def asWrapper( value ):
  369.     if not isinstance( value, wrapper.Wrapper ):
  370.         return wrapper.wrapper( value )
  371.     return value
  372.  
  373. def asIntConverter( value, *args ):
  374.     if isinstance( value, float ):
  375.         return int(round(value,0))
  376.     return value
  377.  
  378. def setDimensionsAsInts( baseOperation ):
  379.     """Set arguments with names in INT_DIMENSION_NAMES to asInt processing"""
  380.     baseOperation = asWrapper( baseOperation )
  381.     argNames = getattr( baseOperation, 'pyConverterNames', baseOperation.argNames )
  382.     for i,argName in enumerate(argNames):
  383.         if argName in INT_DIMENSION_NAMES:
  384.             baseOperation.setPyConverter( argName, asIntConverter )
  385.     return baseOperation
  386.  
  387.     
  388.  
  389. class ImageInputConverter( object ):
  390.     def __init__( self, rank, pixelsName=None, typeName='type' ):
  391.         self.rank = rank
  392.         self.typeName = typeName
  393.         self.pixelsName = pixelsName
  394.     def finalise( self, wrapper ):
  395.         """Get our pixel index from the wrapper"""
  396.         self.typeIndex = wrapper.pyArgIndex( self.typeName )
  397.         self.pixelsIndex = wrapper.pyArgIndex( self.pixelsName )
  398.     def __call__( self, arg, baseOperation, pyArgs ):
  399.         """pyConverter for the pixels argument"""
  400.         images.setupDefaultTransferMode()
  401.         images.rankPacking( self.rank )
  402.         type = pyArgs[ self.typeIndex ]
  403.         try:
  404.             arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE[ type ] ]
  405.         except TypeError, err:
  406.             import pdb
  407.             pdb.set_trace()
  408.         return arrayType.asArray( arg )
  409.     def cResolver( self, array ):
  410.         return ctypes.c_void_p( arrays.ArrayDatatype.dataPointer( array ) )
  411.     
  412. class TypedImageInputConverter( ImageInputConverter ):
  413.     def __init__( self, rank, pixelsName, arrayType, typeName=None ):
  414.         self.rank = rank
  415.         self.arrayType = arrayType
  416.         self.pixelsName = pixelsName
  417.         self.typeName = typeName
  418.     def __call__( self, arg, baseOperation, pyArgs ):
  419.         """The pyConverter for the pixels"""
  420.         images.setupDefaultTransferMode()
  421.         images.rankPacking( self.rank )
  422.         return self.arrayType.asArray( arg )
  423.     def finalise( self, wrapper ):
  424.         """Get our pixel index from the wrapper"""
  425.         self.pixelsIndex = wrapper.pyArgIndex( self.pixelsName )
  426.     def width( self, pyArgs, index, wrappedOperation ):
  427.         """Extract the width from the pixels argument"""
  428.         return self.arrayType.dimensions( pyArgs[self.pixelsIndex] )[0]
  429.     def height( self, pyArgs, index, wrappedOperation ):
  430.         """Extract the height from the pixels argument"""
  431.         return self.arrayType.dimensions( pyArgs[self.pixelsIndex] )[1]
  432.     def depth( self, pyArgs, index, wrappedOperation ):
  433.         """Extract the depth from the pixels argument"""
  434.         return self.arrayType.dimensions( pyArgs[self.pixelsIndex] )[2]
  435.     def type( self, pyArgs, index, wrappedOperation ):
  436.         """Provide the item-type argument from our stored value
  437.         
  438.         This is used for pre-bound processing where we want to provide 
  439.         the type by implication...
  440.         """
  441.         return self.typeName
  442.  
  443. class CompressedImageConverter( object ):
  444.     def finalise( self, wrapper ):
  445.         """Get our pixel index from the wrapper"""
  446.         self.dataIndex = wrapper.pyArgIndex( 'data' )
  447.     def __call__( self, pyArgs, index, wrappedOperation ):
  448.         """Create a data-size measurement for our image"""
  449.         arg = pyArgs[ self.dataIndex ]
  450.         return arrays.ArrayType.arrayByteCount( arg )
  451.  
  452.  
  453.  
  454. DIMENSION_NAMES = (
  455.     'width','height','depth'
  456. )
  457. PIXEL_NAMES = (
  458.     'pixels', 'row', 'column',
  459. )
  460. DATA_SIZE_NAMES = (
  461.     'imageSize',
  462. )
  463.  
  464. def setImageInput( 
  465.     baseOperation, arrayType=None, dimNames=DIMENSION_NAMES, 
  466.     pixelName="pixels", typeName=None 
  467. ):
  468.     """Determine how to convert "pixels" into an image-compatible argument"""
  469.     baseOperation = asWrapper( baseOperation )
  470.     # rank is the count of width,height,depth arguments...
  471.     rank = len([
  472.         # rank is the number of dims we want, not the number we give...
  473.         argName for argName in baseOperation.argNames 
  474.         if argName in dimNames
  475.     ]) + 1
  476.     if arrayType:
  477.         converter = TypedImageInputConverter( rank, pixelName, arrayType, typeName=typeName )
  478.         for i,argName in enumerate(baseOperation.argNames):
  479.             if argName in dimNames:
  480.                 baseOperation.setPyConverter( argName )
  481.                 baseOperation.setCConverter( argName, getattr(converter,argName) )
  482.             elif argName == 'type' and typeName is not None:
  483.                 baseOperation.setPyConverter( argName )
  484.                 baseOperation.setCConverter( argName, converter.type )
  485.     else:
  486.         converter = ImageInputConverter( rank, pixelsName=pixelName, typeName=typeName or 'type' )
  487.     for argName in baseOperation.argNames:
  488.         if argName in DATA_SIZE_NAMES:
  489.             baseOperation.setPyConverter( argName )
  490.             baseOperation.setCConverter( argName, converter.imageDataSize )
  491.     baseOperation.setPyConverter(
  492.         pixelName, converter,
  493.     )
  494.     baseOperation.setCResolver(
  495.         pixelName, converter.cResolver
  496.     )
  497.     return baseOperation
  498.  
  499. glDrawPixels = setDimensionsAsInts(
  500.     setImageInput(
  501.         simple.glDrawPixels
  502.     )
  503. )
  504. glTexSubImage2D = setDimensionsAsInts(
  505.     setImageInput(
  506.         simple.glTexSubImage2D
  507.     )
  508. )
  509. glTexSubImage1D = setDimensionsAsInts(
  510.     setImageInput(
  511.         simple.glTexSubImage1D
  512.     )
  513. )
  514. glTexImage2D = setDimensionsAsInts(
  515.     setImageInput(
  516.         simple.glTexImage2D
  517.     )
  518. )
  519. glTexImage1D = setDimensionsAsInts(
  520.     setImageInput(
  521.         simple.glTexImage1D
  522.     )
  523. )
  524.  
  525. def typedImageFunction( suffix, arrayConstant,  baseFunction ):
  526.     """Produce a typed version of the given image function"""
  527.     if baseFunction:
  528.         arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ arrayConstant ]
  529.         function = setDimensionsAsInts(
  530.             setImageInput(
  531.                 baseFunction, 
  532.                 arrayType,
  533.                 typeName = arrayConstant,
  534.             )
  535.         )
  536.         functionName = baseFunction.__name__
  537.         functionName = '%(functionName)s%(suffix)s'%locals()
  538.         return functionName, function
  539.     else:
  540.         return baseFunction.__name__, baseFunction
  541.  
  542. def _setDataSize( baseFunction, argument='imageSize' ):
  543.     """Set the data-size value to come from the data field"""
  544.     if baseFunction:
  545.         converter = CompressedImageConverter()
  546.         return asWrapper( baseFunction ).setPyConverter(
  547.             argument
  548.         ).setCConverter( argument, converter )
  549.     else:
  550.         return baseFunction
  551.  
  552. def compressedImageFunction( baseFunction ):
  553.     """Set the imageSize and dimensions-as-ints converters for baseFunction"""
  554.     if baseFunction:
  555.         return setDimensionsAsInts(
  556.             _setDataSize( 
  557.                 baseFunction, argument='imageSize'
  558.             )
  559.         )
  560.     else:
  561.         return baseFunction
  562.  
  563. for suffix,arrayConstant in [
  564.     ('b', simple.GL_BYTE),
  565.     ('f', simple.GL_FLOAT),
  566.     ('i', simple.GL_INT),
  567.     ('s', simple.GL_SHORT),
  568.     ('ub', simple.GL_UNSIGNED_BYTE),
  569.     ('ui', simple.GL_UNSIGNED_INT),
  570.     ('us', simple.GL_UNSIGNED_SHORT),
  571. ]:
  572.     for functionName in (
  573.         'glTexImage1D','glTexImage2D',
  574.         'glTexSubImage1D','glTexSubImage2D',
  575.         'glDrawPixels',
  576.         #'glTexSubImage3D','glTexImage3D', # extension/1.2 standard
  577.     ):
  578.         functionName, function = typedImageFunction(
  579.             suffix, arrayConstant, getattr(simple,functionName),
  580.         )
  581.         globals()[functionName] = function
  582.         del function, functionName
  583.     del suffix,arrayConstant
  584.  
  585.