Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_merger_layermask.py
diff options
context:
space:
mode:
Diffstat (limited to 'ep_merger_layermask.py')
-rw-r--r--ep_merger_layermask.py134
1 files changed, 134 insertions, 0 deletions
diff --git a/ep_merger_layermask.py b/ep_merger_layermask.py
new file mode 100644
index 0000000..98272e5
--- /dev/null
+++ b/ep_merger_layermask.py
@@ -0,0 +1,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