diff options
Diffstat (limited to 'ep_layer_image.py')
-rw-r--r-- | ep_layer_image.py | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/ep_layer_image.py b/ep_layer_image.py new file mode 100644 index 0000000..683fe28 --- /dev/null +++ b/ep_layer_image.py @@ -0,0 +1,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 |