#!/usr/bin/env python
# -*- coding: iso8859-1 -*-
# $Id: blackink1.py 3252 2007-08-18 21:17:09Z svn $
# version 1.0  - ok with gimp 2.3.19
# Copyright (C) 2007 Jerome Dumonteil <jd@hamete.org>
#
# 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/>.
"""
Transform an image to a B&W drawing made with ink.
"""

from gimpenums import *
from gimpfu import *

def blackink(img, layer, autolevel, poster, smooth, blur, lev_high, gamma, black):
    # Disable Undo
    pdb.gimp_image_undo_group_start(img)
    #layer=img.active_layer
    if not pdb.gimp_drawable_is_rgb(layer):
        pdb.gimp_image_convert_rgb(img)
    # work on a scaled image if original too small
    h=pdb.gimp_image_height(img)
    w=pdb.gimp_image_width(img)
    size=max(h,w)
    if size<2000:
        resizing=True
        sh=2000.0/size*h
        sw=2000.0/size*w
        pdb.gimp_image_scale(img,sw,sh)
    else:
        resizing=False
    if autolevel:
        pdb.gimp_levels_stretch(layer)
    if poster>0:
        pdb.gimp_posterize(layer,poster)
    pdb.gimp_desaturate_full(layer,1)
    if blur>0:
        pdb.plug_in_gauss_iir2(img, layer, blur, blur)
    if smooth>0:
        radius=smooth*1.0
        delta=smooth
        pdb.plug_in_sel_gauss(img, layer, radius, delta)
    drawing_mask=layer.copy(1)
    black_mask=layer.copy(1)
    img.add_layer(drawing_mask, 0)
    img.add_layer(black_mask, 0)
    pdb.gimp_drawable_set_visible(black_mask,False)
    drawing_mask.mode=MULTIPLY_MODE
    pdb.gimp_invert(drawing_mask)
    pdb.plug_in_edge(img, drawing_mask, max(0.5,0.5+blur/2.0), 1, 1)
    layer=pdb.gimp_image_merge_visible_layers(img,0)
    pdb.gimp_invert(layer)
    lev_low=128
    pdb.gimp_levels(layer,0,lev_low,lev_high,gamma,0,255)
    if blur>0:
        pdb.plug_in_dilate(img,layer,0,0,0.5,0,0,255)
        pdb.gimp_levels(layer,0,lev_low,lev_high,gamma,0,255)
    pdb.gimp_drawable_set_visible(black_mask,True)
    black_mask.mode = MULTIPLY_MODE
    pdb.gimp_threshold(black_mask,black,255)
    pdb.plug_in_erode(img,black_mask,0,0,0.5,0,0,255)
    pdb.plug_in_gauss_iir2(img, black_mask, 1.0,1.0)
    pdb.gimp_by_color_select(black_mask,(255,255,255),64,0,True,False,0,False)
    pdb.gimp_edit_cut(black_mask)
    pdb.gimp_selection_none(img)
    layer=pdb.gimp_image_merge_visible_layers(img,0)
    pdb.gimp_image_convert_grayscale(img)
    if resizing:
        pdb.gimp_image_scale(img,w,h)
    # Enable Undo
    pdb.gimp_image_undo_group_end(img)
    pdb.gimp_displays_flush()

register(
    "python_fu_blackink1",
    "Black Ink rendering",
    "Transform an image to a B&W drawing made with ink",
    "Jerome Dumonteil",
    "Jerome Dumonteil, GPLv3",
    "2007",
    "<Image>/Python-Fu/Black Ink 1",
    "",
    [
        (PF_TOGGLE,     "autolevel",    "Autolevel",    0),
        (PF_SPINNER,    "poster",       "Posterize",    0,(0, 16, 4)),
        (PF_SPINNER,    "smooth",       "Smooth",       6, (0, 10, 1)),
        (PF_SPINNER,    "blur",         "Strength",     1, (0, 20, 1)),
        (PF_SPINNER,    "lev_high",     "Darkness",     240, (1, 254, 1)),
        (PF_SLIDER,     "gamma",        "Gamma",        0.2,(0.1, 2.0, 0.01)),
        (PF_SPINNER,    "black",        "Black",        64 ,(0,254,1))
    ],
    [],
    blackink)

main()
