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
|
# 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 os
import cairo
import model_layer
import ka_random
import ka_importer
class ImageLayer(model_layer.Layer):
"""ImageLayer
"""
def __init__(self, trunk):
"""Constructor for bitmap image layer."""
super(ImageLayer, self).__init__(trunk)
self.selected_image = ''
self.alpha_blending = 1.0
def __eq__(self, other):
"""Equality based on the cells color components."""
equal = isinstance(other, ImageLayer) \
and super(ImageLayer, self).__eq__(other) \
and self.selected_image == other.selected_image \
and self.alpha_blending == other.alpha_blending
return equal
def randomize(self):
"""Randomize the layers components."""
super(ImageLayer, self).randomize()
image_list = ka_importer.get_rgb_image_list()
if len(image_list) > 0:
self.selected_image = \
image_list[random.randint(0, len(image_list)-1)]
self.alpha_blending = random.random()
def mutate(self):
"""Make small random changes to the layers components."""
super(ImageLayer, self).mutate()
if ka_random.is_mutating():
image_list = ka_importer.get_rgb_image_list()
if len(image_list) > 0:
self.selected_image = \
image_list[random.randint(0, len(image_list)-1)]
if ka_random.is_mutating():
self.alpha_blending += ka_random.jitter(0.2)
self.alpha_blending = ka_random.limitate(self.alpha_blending)
def shuffle(self):
"""Shuffle similar componets."""
super(ImageLayer, self).shuffle()
def crossingover(self, other):
"""
pre: isinstance(other, ImageLayer)
pre: isinstance(self, ImageLayer)
# check for distinct references, needs to copy content, not references
post: __return__ is not self
post: __return__ is not other
"""
new_one = ImageLayer(self.get_trunk())
cross_sequence = self.crossingover_base(new_one, other, 2)
new_one.selected_image = self.selected_image if cross_sequence[0] \
else other.selected_image
new_one.alpha_blending = self.alpha_blending if cross_sequence[0] \
else other.alpha_blending
return new_one
def draw(self, ctx, width, height):
"""
pre: ctx is not None
pre: width > 0
pre: height > 0
pre: width == height
"""
self.begin_draw(ctx, width, height)
if os.path.isfile(self.selected_image):
surface = cairo.ImageSurface.create_from_png(self.selected_image)
ctx.translate(-0.5, -0.5)
ctx.scale(1.0/surface.get_width(), 1.0/surface.get_height())
ctx.set_source_surface(surface)
ctx.paint_with_alpha(self.alpha_blending)
def explain(self, formater):
"""
pre: formater is not None
"""
formater.alfa_item(self.alpha_blending, 'alfa blendig: %d%%' \
% (100*self.alpha_blending))
if os.path.isfile(self.selected_image):
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()
formater.surface_item(surface,
'image file:' + self.selected_image,
self.selected_image)
def copy(self):
"""The Voronoi diagram layers copy constructor.
# check for distinct references, needs to copy content, not references
post: __return__ is not self
"""
new_one = ImageLayer(self.get_trunk())
self.copy_base(new_one)
new_one.selected_image = self.selected_image
new_one.alpha_blending = self.alpha_blending
return new_one
|