; Various Nebula Scripts
; (c) 2007 Michael Banack

(define (polarRadius img)
	(let* ((width 0) (height 0) (r 0) )
		(set! width (car (gimp-image-width img)) )
		(set! height (car (gimp-image-height img)))
		(set! r (+ -1 (/ (min height width) 2) ))
	)
)

(define ( selectCircle img x y r sMode antialias feather featherRad)
	(let*	( (ex 0) (ey 0 )(ew 0 ) (eh 0) )
		(set! ex (+ x (- r)) )
		(set! ey (+ y (- r)) )
		(set! ew (* r 2) )
		(set! eh (* r 2) )

		(gimp-ellipse-select img ex ey ew eh sMode antialias feather featherRad)
	)
)

(define ( selectCenterCircle img r sMode antialias feather featherRad)
	(let* ((x 0) (y 0) (width 0) (height 0))
		(set! width (car (gimp-image-width img)) )
		(set! height (car (gimp-image-height img)))
		(set! x (/ width 2) )
		(set! y (/ height 2))
		(selectCircle img x y r sMode antialias feather featherRad)
	)
)

(define ( selectPolarCircle img sMode antialias feather featherRad)
	(selectCenterCircle img (polarRadius img) sMode antialias feather featherRad)
)

(define ( selectPolarCircleHard img)
	(selectPolarCircle img 2 0 0 0)
)

(define (script-fu-select-polar-circle img)
	(gimp-image-undo-group-start img)
	(selectPolarCircleHard img)
	(gimp-image-undo-group-end img)
)


(script-fu-register "script-fu-select-polar-circle"
		    _"Select Polar _Circle..."
		    "Selects the Polar Circle"
		    "Michael Banack"
		    "2007"
		    "20070817140142"
		    ""
		    SF-IMAGE "Image" 0
		    SF-DRAWABLE "Drawable" 0 )

(script-fu-menu-register "script-fu-select-polar-circle" 
			 _"<Image>/Select")


(define ( smoothPolarCircle img draw)
	(gimp-image-undo-group-start img)
	(selectCenterCircle img (+  2 (polarRadius img)) 2 0 0 0)
	(selectCenterCircle img (+ -2 (polarRadius img)) 1 0 0 0)
	(plug-in-gauss 1 img draw 5 5 0)
	(gimp-selection-none img)
	(gimp-image-undo-group-end img)
)

(define (script-fu-smooth-polar-circle img draw)
	(smoothPolarCircle img draw)
	(gimp-displays-flush)
)

(script-fu-register "script-fu-smooth-polar-circle"
		    _"Smooth Polar _Circle..."
		    "Blurs the edges of the polar circle"
		    "Michael Banack"
		    "2007"
		    "20070817140142"
		    ""
		    SF-IMAGE "Image" 0
		    SF-DRAWABLE "Drawable" 0 )

(script-fu-menu-register "script-fu-smooth-polar-circle" 
			 _"<Image>/Filters/Globes")

		
(define (script-fu-polar-nebula img draw seed smooth)
	(gimp-image-undo-group-start img)
	(let* ((top 0))
		(set! top (car (gimp-layer-new-from-drawable draw img)))
		(gimp-image-add-layer img top -1)

		(plug-in-plasma 1 img top seed 1 )
		(plug-in-make-seamless 1 img top)	
		(plug-in-polar-coords 1 img top 100 0 1 1 1 )		
		(selectPolarCircleHard img)
		(gimp-selection-invert img)
		(gimp-edit-clear top)
		(gimp-selection-none img)

		(if (= smooth TRUE)(smoothPolarCircle img top))
	)
	(gimp-displays-flush)
	(gimp-image-undo-group-end img)
)

(script-fu-register "script-fu-polar-nebula"
	_"_Polar Nebula..."
	"Renders a polar nebula"
	"Michael Banack"
	"2007"
	"20070817163327"
	""
	SF-IMAGE "Image" 0
	SF-DRAWABLE "Drawable" 0
	SF-VALUE "Seed" "1"
	SF-TOGGLE "Smooth Edges?" FALSE
)

(script-fu-menu-register "script-fu-polar-nebula" _"<Image>/Filters/Globes")

(define (script-fu-recursive-polar-nebula img origDraw angle count seed smooth seamless)
	(gimp-image-undo-group-start img)
	(let* ((draw (car (gimp-layer-new-from-drawable origDraw img))))
		(gimp-image-add-layer img draw -1)
	
		(plug-in-plasma 1 img draw seed 1 )
		(if (= seamless TRUE) (plug-in-make-seamless 1 img draw))
		(gimp-displays-flush)	
		(plug-in-polar-coords 1 img draw 100 angle 1 1 1 )
		(gimp-displays-flush)
		
		(define ( plasmaBurn img count seed)
			(if (> count 0) (begin
				(if (= seamless TRUE) (begin
					(let* ((top 0)(paste 0))
						(set! top (car (gimp-layer-new-from-drawable draw img)))
						(gimp-image-add-layer img top -1)
						(plug-in-plasma 1 img top seed 1 )
						(plug-in-make-seamless 1 img top)
						(gimp-selection-all img)
						(gimp-edit-copy top)
						(selectPolarCircleHard img)
						(gimp-selection-invert img)
						(set! paste (car (gimp-edit-paste draw 1)))
						(gimp-floating-sel-anchor paste)
						(gimp-image-remove-layer img top)
					)
				))
				(if (= seamless FALSE) (begin
					(selectPolarCircleHard img)				
					(gimp-selection-invert img)
					(plug-in-plasma 1 img draw seed 1 )
				))
				(gimp-displays-flush)
				(gimp-selection-none img)
				(if (= smooth TRUE)(smoothPolarCircle img draw))
				(plug-in-polar-coords 1 img draw 100 angle 1 1 1 )
				(gimp-displays-flush)
				; 37 is a random prime
				(plasmaBurn img (+ -1 count) (+ seed 37))
			))
		)
	
		(plasmaBurn img (max 1 count) (+ seed 37))
	)
	
	(gimp-displays-flush)
	(gimp-image-undo-group-end img)
)

(script-fu-register "script-fu-recursive-polar-nebula"
	_"_Recursive Polar Nebula..."
	"Recursively renders a polarized nebula"
	"Michael Banack"
	"2007"
	"20070817103628"
	""
	SF-IMAGE "Image" 0
	SF-DRAWABLE "Drawable" 0
	SF-VALUE "Angle" "135"
	SF-VALUE "Iterations" "6"
	SF-VALUE "Seed"  "1"
	SF-TOGGLE "Smooth Edges?" FALSE
	SF-TOGGLE "Seamless?" TRUE
)

(script-fu-menu-register "script-fu-recursive-polar-nebula" _"<Image>/Filters/Globes")

(define (script-fu-swirled-nebula img draw angle count seed smooth)
	(gimp-image-undo-group-start img)
	(plug-in-plasma 1 img draw seed 1 )
	(plug-in-make-seamless 1 img draw)
	(gimp-displays-flush)	
	(plug-in-polar-coords 1 img draw 100 angle 1 1 1 )
	(gimp-displays-flush)
	
	(define ( plasmaSwirl img count seed)
		(if (> count 0) (begin
			(selectPolarCircleHard img)
			(gimp-selection-invert img)
			(plug-in-plasma 1 img draw seed 1 )
			(gimp-selection-none img)
			(plug-in-make-seamless 1 img draw)
			(if (= smooth TRUE)(smoothPolarCircle img draw))
			(gimp-displays-flush)
			(plug-in-polar-coords 1 img draw 100 angle 1 1 1 )
			(gimp-displays-flush)
			; 37 is a random prime
			(plasmaSwirl img (+ -1 count) (+ seed 37))
		))
	)

	(plasmaSwirl img (max 1 count) (+ 37 seed))

	
	(gimp-displays-flush)
	(gimp-image-undo-group-end img)
)

(script-fu-register "script-fu-swirled-nebula"
	_"Swirled _Nebula..."
	"Renders a swirled nebula-globe"
	"Michael Banack"
	"2007"
	"20070817144002"
	""
	SF-IMAGE "Image" 0
	SF-DRAWABLE "Drawable" 0
	SF-VALUE "Angle" "135"
	SF-VALUE "Iterations" "5"
	SF-VALUE "Seed"  "1"
	SF-TOGGLE "Smooth Edges?" FALSE
)

(script-fu-menu-register "script-fu-swirled-nebula" _"<Image>/Filters/Globes")

(define (script-fu-swirl img draw angle count smooth)
	(gimp-image-undo-group-start img)
	(let* ((top 0)(mid 0))
		(set! top (car (gimp-layer-new-from-drawable draw img)))
		(set! mid (car (gimp-layer-new-from-drawable draw img)))
		(gimp-image-add-layer img mid -1)
		(gimp-image-add-layer img top -1)
		
		(plug-in-polar-coords 1 img top 100 angle 1 1 1 )		
		(selectPolarCircleHard img)
		(gimp-selection-invert img)
		(gimp-edit-clear top)
		(gimp-selection-none img)
		(set! top (car (gimp-image-merge-down img top 0)))

		(if (= smooth TRUE)(smoothPolarCircle img top))
		(gimp-displays-flush)
		
		(define ( Swirl img count)
			(if (> count 0) (begin
				(set! mid top)
				(set! top (car (gimp-layer-new-from-drawable top img)))
				(gimp-image-add-layer img top -1)

				
				(plug-in-make-seamless 1 img top)
				(plug-in-polar-coords 1 img top 100 angle 1 1 1 )		
				(selectPolarCircleHard img)
				(gimp-selection-invert img)
				(gimp-edit-clear top)
				(gimp-selection-none img)
				(set! top (car (gimp-image-merge-down img top 0)))
				
				(if (= smooth TRUE)(smoothPolarCircle img top))
				(gimp-displays-flush)

				(Swirl img (+ -1 count))
			))
		)
		
		(Swirl img (max 0 (+ -1 count ) ))

		(selectPolarCircleHard img)
		(gimp-selection-invert img)
		(gimp-edit-clear top)
		(gimp-selection-none img)
	)
	(gimp-displays-flush)
	(gimp-image-undo-group-end img)
)

(script-fu-register "script-fu-swirl"
	_"_Swirl..."
	"Swirls the image"
	"Michael Banack"
	"2007"
	"20070817144002"
	""
	SF-IMAGE "Image" 0
	SF-DRAWABLE "Drawable" 0
	SF-VALUE "Angle" "135"
	SF-VALUE "Iterations" "5"
	SF-TOGGLE "Smooth Edges?" FALSE
)

(script-fu-menu-register "script-fu-swirl" _"<Image>/Filters/Globes")


(define (script-fu-tiled-inversion img draw)
	(gimp-image-undo-group-start img)
	(let* ((w 0)(h 0)(top 0)(paste 0))
		(set! w (car (gimp-image-width img)))
		(set! h (car (gimp-image-height img)))
		(set! top (car (gimp-layer-new-from-drawable draw img)))
		(gimp-image-add-layer img top -1)
		
		(define (migrateSelection x y nx ny)
			(gimp-image-set-active-layer img draw)
			(gimp-rect-select img x y (/ w 2) (/ h 2) 2 0 0)
			(gimp-edit-copy draw)
			(set! paste (car (gimp-edit-paste top 0)))
			(gimp-layer-translate paste (+ nx (- x)) (+ ny (- y)) )
			(gimp-floating-sel-anchor paste)
		)

		(migrateSelection 0 0 ( / w 2) (/ h 2))
		(migrateSelection (/ w 2) (/ h 2) 0 0)
		(migrateSelection 0 (/ h 2) (/ w 2) 0)
		(migrateSelection (/ w 2) 0 0 (/ h 2))

		(gimp-selection-all img)
		(gimp-image-set-active-layer img top)
		(gimp-edit-copy top)
		(gimp-image-set-active-layer img draw)
		(set! paste (car (gimp-edit-paste draw 0)))
		(gimp-floating-sel-anchor paste)
		(gimp-image-remove-layer img top)
	)
	(gimp-displays-flush)
	(gimp-image-undo-group-end img)
)


(script-fu-register "script-fu-tiled-inversion"
	_"Tiled Inversion..."
	"Swaps the quadrants of the image"
	"Michael Banack"
	"2007"
	"20070817171027"
	""
	SF-IMAGE "Image" 0
	SF-DRAWABLE "Drawable" 0
)

(script-fu-menu-register "script-fu-tiled-inversion" _"<Image>/Filters/Map")

