1 '''Mutations to apply to images'''
3 from pygame.transform import flip, rotate, scale
5 from naja.constants import EIGHT_BIT_SCALE
7 from pygame import surface
8 import pygame.locals as pgl
11 class Mutator(object):
12 def __init__(self, func, *args):
14 self._args = tuple(args)
16 def __call__(self, image):
17 return self._func(image, *self._args)
20 return hash((self._func, self._args))
22 def __eq__(self, other):
23 if not isinstance(other, Mutator):
25 return (self._func == other._func) and (self._args == other._args)
28 return '<%s %r>' % (self.__class__.__name__, self._args)
32 return Mutator(rotate, angle)
36 return Mutator(scale, size)
39 def scale_multiplier(image, factor):
40 size = image.get_width() * factor, image.get_height() * factor
41 return scale(image, size)
44 def blend_add(image, colour, flags=pgl.BLEND_ADD):
45 """Overlay the image with the given colour using BLEND_ADD"""
46 blend = surface.Surface(image.get_size())
49 blended_image = image.copy()
50 blended_image.blit(blend, (0, 0), special_flags=flags)
54 def blender(colour, flags=pgl.BLEND_ADD):
55 return Mutator(blend_add, tuple(colour), flags)
59 NULL = Mutator(lambda x: x)
66 FLIP_H = Mutator(flip, True, False)
67 FLIP_V = Mutator(flip, False, True)
69 EIGHT_BIT = Mutator(scale_multiplier, EIGHT_BIT_SCALE)