On 14-03-04 01:29 AM, Greg Ewing wrote:
Lenard Lindstrom wrote:
If the bytecode is exposed at the Python end then any expression can
be encoded, a different one for each channel if desired. Conceivably,
a Python expression, or function, could be compiled into a blit
operation.
That would be even more awesome!
How does this look as an example:
def alpha_blend(source, target):
"""pygame.Surface.blit alpha blit blend
blit_blend(Color, Color) => Color
return a new target pixel value
"""
source_alpha = source.a
target.r = element_blend(source.r, target.r, source_alpha)
target.g = element_blend(source.g, target.g, source_alpha)
target.b = element_blend(source.b, target.b, source_alpha)
target.a = alpha_element_blend(source_alpha, target.a)
return target
def element_blend(source, target, alpha):
return (((source - target) * alpha + source) >> 8) + target
def alpha_element_blend(source, target):
return source + target - ((source * target) // 255)
"""
>>> from pygame import Color
>>> print alpha_blend(Color('red'), Color('green'))
(255, 0, 0, 255)
>>> print alpha_blend(Color(255, 0, 0, 128), Color('green'))
(128, 127, 0, 255)
>>> print alpha_blend(Color('red'), Color(0, 255, 0, 128))
(255, 0, 0, 255)
>>> print alpha_blend(Color(255, 0, 0, 128), Color(0, 255, 0, 128))
(128, 127, 0, 192)
>>>
"""
This shows how the blend function can be tested in Python. For the JIT,
arguments are treated as pass-by-value, so changes to 'target' will not
affect the target surface. Instead, the target pixel is set to the
function's return value.
The Python function is traced to generate JIT code. For now, branching
and recursion are unsupported. The JIT takes care of adding the row and
column loops.
Inspired by PyPy RPython.
Lenard Lindstrom