;;; round-corner-of-selection.scm -*-scheme-*-
;;; Time-stamp: <1997/10/30 23:02:43 narazaki@InetQ.or.jp>
;;; This file is a part of:
;;;   The GIMP (Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis)
;;; Author: Shuji Narazaki (narazaki@InetQ.or.jp)
;;; Version: 0.8

(if (not (symbol-bound? 'script-fu-round-corner-of-selection-radius
			(the-environment)))
    (define script-fu-round-corner-of-selection-radius 15))

(define (script-fu-round-corner-of-selection img drawable radius)
  (let* ((width (car (gimp-image-width img)))
	 (height (car (gimp-image-height img)))
	 (current-brush (car (gimp-brushes-get-brush)))
	 (current-paint-mode (car (gimp-brushes-get-paint-mode)))
	 (current-foreground (car (gimp-palette-get-foreground)))
	 (*white* '(255 255 255))
	 (original-selection #f)
	 (selection #f)
	 (new-brush (cond ((<= radius 3) (set! radius 3)
					 "Circle (3)")
			  ((<= radius 5) (set! radius 5)
					 "Circle (5)")
			  ((<= radius 7) (set! radius 7)
					 "Circle (7)")
			  ((<= radius 9) (set! radius 9)
					 "Circle (9)")
			  ((<= radius 11) (set! radius 11)
					  "Circle (11)")
			  ((<= radius 13) (set! radius 13)
					  "Circle (13)")
			  ((<= radius 15) (set! radius 15)
					  "Circle (15)")
			  ((<= radius 17) (set! radius 17)
					  "Circle (17)")
			  ('else (set! radius 19)
				 "Circle (19)"))))
    (gimp-palette-set-foreground *white*)
    (gimp-image-disable-undo img)
    (set! original-selection (car (gimp-selection-save img)))
    (gimp-selection-shrink img (/ (+ radius 1) 2))
    (set! selection (car (gimp-selection-save img)))
    (gimp-image-set-active-channel img selection)
    (gimp-brushes-set-brush new-brush)
    (gimp-brushes-set-paint-mode NORMAL)
    (gimp-edit-stroke img selection)
    (gimp-image-enable-undo img)
    (gimp-selection-load img original-selection)
    (gimp-image-disable-undo img)
    ;; back to the original state
    (gimp-image-remove-channel img original-selection)
    (gimp-palette-set-foreground current-foreground)
    (gimp-brushes-set-brush current-brush)
    (gimp-brushes-set-paint-mode current-paint-mode)
    (gimp-image-unset-active-channel img)
    (if (and (= 0 (car (gimp-drawable-layer drawable)))
	     (= 0 (car (gimp-drawable-layer-mask drawable))))
	(gimp-image-set-active-channel img drawable))
    (gimp-image-enable-undo img)
    (gimp-selection-load img selection)
    (gimp-image-remove-channel img selection)
    (set! script-fu-round-corner-of-selection-radius radius)
    (gimp-displays-flush)))

(script-fu-register
 "script-fu-round-corner-of-selection"
 "<Image>/Script-Fu/Selection/Round Corner of Selection"
 "Round the corners of the current selection. This script can work well, EVEN IF THE SELECTION IS NOT RECTANGULAR."
 "Shuji Narazaki <narazaki@InetQ.or.jp>"
 "Shuji Narazaki"
 "1997"
 "RGB*, GRAY*"
 SF-IMAGE "Input Image" 0
 SF-DRAWABLE "Drawable" 0
 SF-VALUE "Radius" (number->string script-fu-round-corner-of-selection-radius)
)

;;; round-corner-of-selection.scm ends here
