;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License

(define (script-fu-sg-height-bevel image drawable iterations)
  (gimp-image-undo-group-start image)
  (gimp-selection-none image)
  (gimp-layer-set-mode drawable DARKEN-ONLY-MODE)
  (let loop ((iterations iterations)
             (layers (list drawable)) )
    (if (zero? iterations)
      (map (lambda (layer low-output high-input)
             (gimp-levels layer HISTOGRAM-VALUE 0 high-input 1.0 low-output 255) )
           (cdr (reverse layers))
           '( 32  64  96 128 160 192 224)
           '(224 192 160 128  96  64  32) )
      (let ((layer (car (gimp-layer-copy (car layers) FALSE))))
        (gimp-image-insert-layer image layer 0 -1)
        (gimp-image-lower-layer image layer)
        (gimp-layer-set-mode layer DARKEN-ONLY-MODE)
        (plug-in-vpropagate RUN-NONINTERACTIVE
                            image
                            layer
                            1 ; darker
                            15 ; all channels
                            1.0 ; rate 0-1
                            15 ; all directions
                            0
                            255 )
        (loop (pred iterations) (cons layer layers)) )))
  (gimp-image-undo-group-end image)
  (gimp-displays-flush)
  )
                           
(script-fu-register "script-fu-sg-height-bevel"
"Height Bevel per Pegleg44"
"Adds beveled layers per Pegleg44"
"Saul Goode"
"Saul Goode"
"July 2013"
"*"
SF-IMAGE    "Image"    0
SF-DRAWABLE "Drawable" 0
SF-ADJUSTMENT "Iterations" '(15 0 1000 1 1 0 1)
)
(script-fu-menu-register "script-fu-sg-height-bevel"
  "<Image>/Filters/Decor/"
  )