Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_merger_alphaimage.py
blob: 7e0c052950d0d5750c7c49d52f568a8f47ea6fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# coding: UTF8
# Copyright 2009 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import random
import cairo
import model_allele
import ka_random
import ka_importer

class AlphaImageMerger(model_allele.Allele):
    """AlphaImageMerger
    """
    constraint_wieght = (0.0, 0.5)

    def __init__(self, trunk):
        """Constructor for a simple merger."""
        super(AlphaImageMerger, self).__init__(trunk)
        self.selected_image = ''

    def __eq__(self, other):
        """Equality based on same instance."""
        equal = isinstance(other, AlphaImageMerger) \
                and self.selected_image == other.selected_image
        return equal

    def randomize(self):
        """No member variables. Nothing to do."""
        image_list = ka_importer.get_alpha_image_list()
        self.selected_image = image_list[random.randint(0, len(image_list)-1)]

    def mutate(self):
        """No member variables. Nothing to do."""
        if ka_random.is_mutating():
            image_list = ka_importer.get_alpha_image_list()
            self.selected_image = image_list[random.randint(0, len(image_list)-1)]

    def shuffle(self):
        """No member variables. Nothing to do."""
        pass

    def crossingover(self, other):
        """
        pre: isinstance(other, AlphaImageMerger)
        pre: isinstance(self, AlphaImageMerger)
        # check for distinct references, needs to copy content, not references
        post: __return__ is not self
        post: __return__ is not other
        """
        new_one = AlphaImageMerger(self.get_trunk())
        cross_sequence = ka_random.crossing_sequence(1)
        new_one.selected_image = self.selected_image if cross_sequence[0] \
                                                     else other.selected_image
        return new_one

    def draw_single_layer(self, single_layer, mask_layer, ctx, width, height):
        """
        pre: single_layer is not None
        pre: ctx is not None
        pre: width > 0
        pre: height > 0
        pre: width == height
        """
        # paint one layer
        ctx.save()

        msk_surface = cairo.ImageSurface.create_from_png(self.selected_image)
        ctx.save()
        ctx.translate(-0.5, -0.5)
        ctx.scale(1.0/msk_surface.get_width(), 1.0/msk_surface.get_height())
        ctx.mask_surface(msk_surface, 0.0, 0.0)
        ctx.restore()
        
        single_layer.draw(ctx, width, height)
        ctx.restore()

    def explain(self):
        """
        post len(__return__) == 3
        """
        png_surface = cairo.ImageSurface.create_from_png(self.selected_image)
        width = height = 48
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
        ctx = cairo.Context(surface)
        ctx.scale(width, height)
        ctx.scale(1.0/png_surface.get_width(), 1.0/png_surface.get_height())
        ctx.set_source_surface(png_surface)
        ctx.paint()

        return u'Transparent image merger, image ' \
                                            + unicode(self.selected_image), \
               surface, \
               self.selected_image

    def copy(self):
        """A copy constructor.
        post: isinstance(__return__, AlphaImageMerger)
        # check for distinct references, needs to copy content, not references
        post: __return__ is not self
        """
        new_one = AlphaImageMerger(self.get_trunk())
        new_one.selected_image = self.selected_image 
        return new_one