; 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 2 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.

(define (script-fu-sg-tooninator image drawable)
  (unless (zero? (car (gimp-drawable-is-layer drawable)))
    (gimp-image-undo-group-start image)
    (let ((gray-mode? (= (car (gimp-image-base-type image)) 1))
          (retinex-layer 0)
          (edge-layer 0) )
      (if gray-mode?
        (gimp-image-convert-rgb image) )
      (set! retinex-layer (car (gimp-layer-copy drawable TRUE)))
      (gimp-drawable-set-name retinex-layer "Retinex")
      (gimp-image-add-layer image retinex-layer -1)
      (plug-in-retinex RUN-NONINTERACTIVE image retinex-layer 16 3 0 0.1)
      (gimp-image-set-active-layer image retinex-layer)
      (gimp-layer-set-mode retinex-layer VALUE-MODE)
      (set! edge-layer (car (gimp-layer-copy drawable TRUE)))
      (gimp-drawable-set-name edge-layer "Edges")
      (gimp-image-add-layer image edge-layer -1)
      (let ((retinex-mask (car (gimp-layer-create-mask retinex-layer 0)))
            (buffer (car (gimp-edit-named-copy drawable "orig"))) )
        (gimp-layer-add-mask retinex-layer retinex-mask)
        (gimp-floating-sel-anchor (car (gimp-edit-named-paste retinex-mask buffer TRUE)))
        (gimp-threshold retinex-mask 36 255) )
      (let ((temp-layer (car (gimp-layer-copy retinex-layer TRUE))))
        (gimp-image-set-active-layer image edge-layer)
        (gimp-image-add-layer image temp-layer -1)
        (set! edge-layer (car (gimp-image-merge-down image temp-layer EXPAND-AS-NECESSARY))) )
      (plug-in-gauss RUN-NONINTERACTIVE image edge-layer 2.0 2.0 0)
      (plug-in-gauss RUN-NONINTERACTIVE image edge-layer 2.0 2.0 0)
      (plug-in-laplace RUN-NONINTERACTIVE image edge-layer)
      (plug-in-despeckle RUN-NONINTERACTIVE image edge-layer 1 1 0 255)
      (when (zero? (car (gimp-selection-is-empty image)))
         (gimp-selection-invert image)
         (gimp-edit-clear edge-layer)
         (gimp-edit-clear retinex-layer)
         (gimp-selection-invert image) )
      (if gray-mode?
        (gimp-image-convert-grayscale image) ))
    (gimp-image-undo-group-end image) )
  (gimp-displays-flush)
  )
        
(script-fu-register "script-fu-sg-tooninator"
  "Toon-inator"
  "Create two adjuvant layers which result in a cartoon-like appearance"
  "Saul Goode"
  "Saul Goode"
  "Sept 2011"
  "RGB*,GRAY*"
  SF-IMAGE    "Image"    0
  SF-DRAWABLE "Layer" 0
  )

(script-fu-menu-register "script-fu-sg-tooninator"
 "<Image>/Filters/Artistic/"
 )


