Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_merger_layermask.py
blob: 98272e5a81d04529d4b5e5859817e59e91010126 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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

class LayermaskMerger(model_allele.Allele):
    """LayermaskMerger
    inv: self.scale_x >= 1.0
    inv: self.scale_y >= 1.0
    """

    def __init__(self, trunk):
        """Constructor for a simple merger."""
        super(LayermaskMerger, self).__init__(trunk)
        self.scale_x = 1.0
        self.scale_y = 1.0
        self.constraint_scale = (1.0, 2.5)

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

    def randomize(self):
        """No member variables. Nothing to do."""
        self.scale_x = random.uniform(self.constraint_scale[0], self.constraint_scale[1])
        self.scale_y = random.uniform(self.constraint_scale[0], self.constraint_scale[1])

    def mutate(self):
        """No member variables. Nothing to do."""
        if ka_random.is_mutating():
            self.scale_x += ka_random.jitter_constrained(self.constraint_scale)
            self.scale_x = ka_random.limitate_range(self.scale_x, self.constraint_scale[0], self.constraint_scale[1])
        if ka_random.is_mutating():
            self.scale_y += ka_random.jitter_constrained(self.constraint_scale)
            self.scale_y = ka_random.limitate_range(self.scale_y, self.constraint_scale[0], self.constraint_scale[1])

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

    def crossingover(self, other):
        """
        pre: isinstance(other, LayermaskMerger)
        pre: isinstance(self, LayermaskMerger)
        # check for distinct references, needs to copy content, not references
        post: __return__ is not self
        post: __return__ is not other
        """
        new_one = LayermaskMerger(self.get_trunk())
        cross_sequence = ka_random.crossing_sequence(2)
        new_one.scale_x = self.scale_x if cross_sequence[0] else other.scale_x
        new_one.scale_y = self.scale_y if cross_sequence[1] else other.scale_y
        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 = ctx.get_target().create_similar(cairo.CONTENT_ALPHA, 
                                                      width, height)
        # draw mask. 
        msk_ctx = cairo.Context(msk_surface)
        sx, sy = self.scale_x**2, self.scale_y**2
#        sx, sy = 2.5**2, 2.5**2
        msk_ctx.scale(sx*width, sy*height)
#        msk_ctx.translate(0.5-0.5/sx, 0.5-0.5/sy)
        msk_ctx.translate(0.5/sx, 0.5/sy)
        
        # draw mask background
        msk_ctx.set_operator(cairo.OPERATOR_SOURCE)
        msk_ctx.set_source_rgba(0.0, 0.0, 0.0, 0.5)
        msk_ctx.paint()
        # draw mask layer
        msk_ctx.set_operator(cairo.OPERATOR_SOURCE)
        mask_layer.draw(msk_ctx, width, height)
#        dummy = ep_layer_referencepattern.ReferencePattern(self.path+'/TODO')
#        dummy.operator = cairo.OPERATOR_SOURCE
#        dummy.draw(msk_ctx, width, height)
#        msk_surface.write_to_png('/dev/shm/TODO.png')

        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)
        ctx.restore()

        single_layer.draw(ctx, width, height)
        ctx.restore()

    def explain(self):
        """
        post len(__return__) == 3
        """
        return 'Layermask merger, scale_x**2=' + unicode(self.scale_x**2) \
                                + u' scale_y**2=' + unicode(self.scale_y**2), \
               None, \
               None

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