# Phatch - Photo Batch Processor
# Copyright (C) 2007-2008 www.stani.be
#
# 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, see http://www.gnu.org/licenses/
#
# Phatch recommends SPE (http://pythonide.stani.be) for editing python files.

# Embedded icon is taken from www.openclipart.org (public domain)

# Follows PEP8

from core import models
from lib.reverse_translation import _t
from lib.formField import IMAGE_EFFECTS

#---PIL


def init():
    global Image, ImageFilter, imtools
    import Image
    import ImageFilter
    from lib import imtools


def effect(image, filter, amount=100, repeat=1):
    """Apply a filter
    - amount: 0-1
    - repeat: how many times it should be repeated"""
    filter = getattr(ImageFilter, filter)
    image = imtools.convert_safe_mode(image)
    for i in range(repeat):
        filtered = image.filter(filter)
        if imtools.has_alpha(image) and \
           filter in [ImageFilter.CONTOUR, ImageFilter.EMBOSS]:
            filtered.putalpha(imtools.get_alpha(image))
        if amount < 100:
            image = imtools.blend(image, filtered, amount / 100.0)
        else:
            image = filtered
    return image

#---Phatch


class Action(models.Action):
    """"""

    label = _t('Effect')
    author = 'Stani'
    email = 'spe.stani.be@gmail.com'
    init = staticmethod(init)
    pil = staticmethod(effect)
    version = '0.1'
    tags = [_t('filter')]
    tags_hidden = IMAGE_EFFECTS
    __doc__ = _t('Blur, Sharpen, Emboss, Smooth, ...')

    def interface(self, fields):
        fields[_t('Filter')] = self.ImageEffectField(
            self.IMAGE_EFFECTS[0])
        fields[_t('Repeat')] = self.SliderField(1, 1, 10)
        fields[_t('Amount')] = self.SliderField(100, 1, 100)

    icon = \
'x\xda\x01|\x07\x83\xf8\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\
\x00\x000\x08\x06\x00\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\x1469#@z\xf7n\x85nd\x16\
\x16\x19\xea\xc4\x03%@\x0b\xb2\x19\x87\xfa\xac\x10\xc8F\xc3\x94\x93\xc3\xcc\
\x7fp\xe8\xab\\\x12\x85hm\xd6b=SBM \xf5C\x00Go\xe6<\xda\xfa4\x00&\x08\x86\
\x1cyV\xd4&_\xfd\x11\xfb\xe7\xca\xdb\xb2\xbdg\x95\x80\x86\xf5\x8c\xbc6\x87\
\x86-\xb4j|8\x9b4GS\xa6\x16\xd0\x12\x96\xe0\xf4\x19*\x83c\xee{H\x17v\xfe\x05\
\x0e,\x02\x12A\xdb\xa0\xc1\x84&\xb2\x1bfk<\xcc2\x17\xd2\x9d\xf4\r\xaf\x8b\
\x07\x99b\xa1\x1a\xd0]yk\xe7\x81\xcaR\xfd\xd6\x05j!\xd3e(_|\x04\x8a\r\xfa\
\x0c\xf5\x83-\xb4c8\x7f\xc3\xc0\x03\xfc\xe9\xac\x13\x00\x18\xba;\xfe\x8a\xc4\
\x97{\xc6\xb7\x94\xdf\xf1\xa9\xb0D\xa0<\xa3\x86^=\x00\xceP\x15\x98\xc7o\xfaL\
\xdb\x17\xef-^\x99\xcd\x9a\x80\xc0\x86o\x8f\x0e\x846\xebi+\xf7\xebTX\x9a\x17\
1\x16:\xa5@g\xf5;\x7f\x121\xf7\x0e\xfc\x9a/\x155\xbc+\xb3\x0ed\x06"\xf5\xd7\
\xd0\x08/\xd2\x00\x9b\x12\xae\x99G[:yP~\xefw\xf2\xa1\x14B\xc6\x17\xca4\xbecG\
1\xd16\xa2\xd3uw\xbf\xcb\xc2\xe7\xf0\x18\x9d\x06D\xef\x93\xfc;\xc6\xfe\x95\
\x07\xd8R\x14\xef\xadR\xb8\xa4\xb4\x1f\xe1!\xec8y\x15\xffq\xe2[\xdd\x1a\xb8G\
\x02\xba.TZ\'\xe2\x0c\xfc\xb2\x14\xe9=T\x06+4\xe5,R[\x84\x19;\xd0\xf9l\xf79\
\xae\x19\xf8#/\x94\x85\t%\xf6FO\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