; Wax Seal - create wax-seal-looking object from original picture
; Copyright (C) 1998 by Jaroslav Benkovsky <benkovsk@pha.pvt.cz>
; 
; Version: 0.1
;
; Requires:
;    plug-in-plasma
;    plug-in-emboss
;    plug-in-gauss-rle
;
; source image is rgb or grayscale, with black-on-white picture

(define (script-fu-wax-seal img drawable wax-color 
			    light-azimuth light-elevation light-depth 
			    border-thickness border-threshold 
			    inner-hollow? symbol-separate? symbol-thickness 
			    bump? bump-granularity bump-smoothness bump-seed)

  (let* ((width (car (gimp-drawable-width drawable)))
	 (height (car (gimp-drawable-height drawable)))
	 (aliassel TRUE)
	 (old-fg (car (gimp-palette-get-foreground)))
	 (old-bg (car (gimp-palette-get-background)))
	 (type (car (gimp-drawable-type drawable)))
	 (alpha-type (cond ((= type RGB_IMAGE) RGBA_IMAGE)
			   ((= type RGBA_IMAGE) RGBA_IMAGE)
			   ((= type GRAY_IMAGE) GRAYA_IMAGE)
			   ((= type GRAYA_IMAGE) GRAYA_IMAGE)))
	 (color-layer (car (gimp-layer-new img width height alpha-type "color layer" 100  NORMAL)))
	 (mask-layer (car (gimp-layer-new img width height alpha-type "mask layer" 100  NORMAL)))
         (symbol-layer (car (gimp-layer-copy drawable TRUE)))
)


    ; setup
    (gimp-image-disable-undo img)
    (gimp-selection-none img)
    (gimp-palette-set-foreground '(0 0 0))
    (gimp-palette-set-background '(255 255 255))

    (gimp-edit-clear img color-layer)
    (gimp-drawable-fill mask-layer WHITE-IMAGE-FILL)


    ; separating inner symbol
    (if (= symbol-separate? TRUE)
	(begin
	    (gimp-fuzzy-select img symbol-layer 1 1 (* 3 border-threshold) ADD TRUE FALSE 0 FALSE)
	    (gimp-selection-grow img 1)
	    (gimp-bucket-fill img symbol-layer FG-BUCKET-FILL NORMAL 100 0 FALSE 0 0)
	    (gimp-selection-none img)
	    (gimp-fuzzy-select img symbol-layer 1 1 (* 3 border-threshold) ADD TRUE FALSE 0 FALSE)
	    (gimp-selection-grow img 1)

	    (gimp-by-color-select img symbol-layer '(255 255 255) (* 0.3 border-threshold) ADD FALSE FALSE 0 FALSE)
	    (gimp-edit-clear img symbol-layer)
	    (gimp-selection-invert img)
	    (gimp-bucket-fill img drawable BG-BUCKET-FILL NORMAL 100 0 FALSE 0 0)
	    (gimp-selection-none img)
	    (if (> symbol-thickness 0)
		(plug-in-gauss-rle 1 img symbol-layer symbol-thickness TRUE TRUE))
	    ))


    ; prepare seal border for emboss
    (if (> border-thickness 0)
	(plug-in-gauss-rle 1 img drawable border-thickness TRUE TRUE))

    ; bumping seal top surface
    (if (= bump? TRUE)
	(begin
	    (gimp-image-add-layer img mask-layer -1)
	    (set! maska (car (gimp-layer-create-mask mask-layer WHITE-MASK)))
	    (gimp-image-add-layer-mask img mask-layer maska)
	    (plug-in-plasma 1 img maska bump-seed bump-granularity)
	    (if (> bump-smoothness 0)
		(plug-in-gauss-rle 1 img maska bump-smoothness TRUE TRUE))
	    (gimp-layer-set-opacity mask-layer 50)
	    (set! drawable (car (gimp-image-flatten img)))
	    (gimp-brightness-contrast img drawable 30 60)
	    ))

    ; reincluding inner symbol
    (if (= symbol-separate? TRUE) 
	(begin
	    (gimp-image-add-layer img symbol-layer -1)
	    (set! drawable (car (gimp-image-flatten img)))
	    ))

    ; embossing the seal - monochrome
    (if (= inner-hollow? FALSE)
	(begin 
	  (gimp-fuzzy-select img drawable 1 1 border-threshold ADD TRUE FALSE 0 FALSE))
	(begin
	  (gimp-by-color-select img drawable '(255 255 255) border-threshold ADD TRUE FALSE 0 FALSE)))

    (gimp-selection-invert img)
    (plug-in-emboss 1 img drawable light-azimuth light-elevation light-depth TRUE)

    ; create layer with wax color - colorize the seal
    (gimp-layer-add-alpha drawable)
    (gimp-layer-set-mode drawable VALUE)
    (gimp-layer-set-name drawable "seal layer")
    (gimp-image-add-layer img color-layer 1)
    (gimp-palette-set-foreground wax-color)
    (gimp-bucket-fill img color-layer FG-BUCKET-FILL NORMAL 100 0 FALSE 0 0)
    (gimp-image-merge-visible-layers img EXPAND-AS-NECESSARY)

    ; cleanup
    (gimp-selection-none img)
    (gimp-palette-set-foreground old-fg)
    (gimp-palette-set-background old-bg)
    (gimp-image-enable-undo img)
    (gimp-displays-flush)
))

(script-fu-register "script-fu-wax-seal"
		    "<Image>/Script-Fu/Stencil Ops/Wax Seal"
		    "Create wax-seal (or plasticine) looking object from original image. The image should be black on white, not blurred, and should not be too close to image bounds."
		    "Jaroslav Benkovsky <benkovsk@pha.pvt.cz>"
		    "Jaroslav Benkovsky"
		    "February 1998"
		    "RGB*, GRAY*"

		    SF-IMAGE "Image"               0
		    SF-DRAWABLE "Drawable"         0

		    SF-COLOR "Wax Color"           '(170 13 13)

		    SF-VALUE "Light Azimuth"       "315"
		    SF-VALUE "Light Elevation"     "45"
		    SF-VALUE "Light Depth"         "20"

		    SF-VALUE "Border Thickness"    "20"
		    SF-VALUE "Border Thresholdsel" "20"

		    SF-TOGGLE "Make Inner Hollow"  FALSE
		    SF-TOGGLE "Symbol Separate"    TRUE
		    SF-VALUE "Symbol Thickness"    "5"

		    SF-TOGGLE "Bump Border"        TRUE
		    SF-VALUE "Bump Granularity"    "3"
		    SF-VALUE "Bump Smoothness"     "13"
		    SF-VALUE "Bump Seed"           "0"
)

