;; smooth corners, a GIMP Script-Fu script.
;; http://www.poboxes.com/kevint/gimp/smooth.html
;; by Kevin Turner, kevint@poboxes.com

;; Repeats guassian blur and threshold.  The objective being
;; to round corners...  
;;
;; Possible enhancements: Threshold at the mean or median 
;; value or something, instead of just at 127.

(define (script-fu-smooth-corners img layer size reps)
  (set! i 0)
  (gimp-undo-push-group-start img)
  (while (< i reps)
	 (plug-in-gauss-rle 1 img layer size 1 1)
	 (gimp-threshold img layer 127 255)
	 (gimp-displays-flush)
	 (set! i (+ i 1)))
  (gimp-undo-push-group-end img))

(script-fu-register "script-fu-smooth-corners"
		    "<Image>/Filters/Distorts/Smooth corners"
		    "Rounds the corners of a two-color image."
		    "Kevin Turner"
		    "Kevin Turner"
		    "1998"
		    "RGB*, GRAY*"
		    SF-IMAGE "Image" 0
		    SF-DRAWABLE "Layer" 0
		    SF-VALUE "Blur strength" "5"
		    SF-VALUE "Repetitions" "1")

(define (script-fu-smooth-corners-anim img layer size reps 
				    out-img new-img)

  (set! width (car (gimp-drawable-width layer)))
  (set! height (car (gimp-drawable-height layer)))

  (if (= new-img TRUE) 
      (set! out-img (car (gimp-image-new width height GRAY))))

  (gimp-image-disable-undo out-img)
  (gimp-undo-push-group-start img)

  (set! out-layer (car (gimp-layer-new out-img width height
				   GRAYA_IMAGE "layer 1" 
				   100 NORMAL)))
  (gimp-image-add-layer out-img out-layer 0)
  (gimp-display-new out-img)
  (gimp-edit-copy img layer)

  (gimp-floating-sel-anchor 
   (car (gimp-edit-paste out-img out-layer FALSE)))

  (set! i 0)
  (while (< i reps)
	 (plug-in-gauss-rle 1 img layer size 1 1)
	 (gimp-threshold img layer 127 255)
	 (gimp-edit-copy img layer)

	 (gimp-floating-sel-to-layer 
	  (car (gimp-edit-paste out-img out-layer FALSE)))
	 (set! i (+ i 1)))

  (gimp-undo-push-group-end img)
  (gimp-image-enable-undo out-img)
  (gimp-displays-flush))

(script-fu-register "script-fu-smooth-corners-anim"
		    "<Image>/Script-Fu/Animators/Smooth Corners"
		    "Produces an animation of the Smooth Corners effect"
		    "Kevin Turner"
		    "Kevin Turner"
		    "1998"
		    "RGB*, GRAY*"
		    SF-IMAGE "Image" 0
		    SF-DRAWABLE "Layer" 0
		    SF-VALUE "Blur strength" "5"
		    SF-VALUE "Repetitions" "1"
		    SF-IMAGE "Add to this image" 0
		    SF-TOGGLE "Make a new image instead" TRUE)