; following steps in glass ball text tutorial by Ella Louise
; found at link: http://gimpchat.com/viewtopic.php?f=11&t=10818&start=10#p144012
; author: Tin Tran
; date: 2014

(define (script-fu-glass-ball image layer
         )
	(let* 
		(
		(dummy-variable-to-make-let-happy 0)
		(image-height (car (gimp-image-height image)))
		(image-width (car (gimp-image-width image)))
		(copied-layer)
		(shadow-layer)
		(gradient-layer)
		(ellipse-layer)
		(sparkle-layer)
		(brush-name)
		(point-array (cons-array 2 'double))
		)
	(gimp-image-undo-group-start image)                   ;undo-group in one step
	
	
	(gimp-layer-add-alpha layer)
	(set! copied-layer (car (gimp-layer-copy layer TRUE)))
	(gimp-image-insert-layer image copied-layer (car (gimp-item-get-parent layer)) 0) ; Add it
	
	(plug-in-whirl-pinch 1 image copied-layer
	                             0 ;whirl
								 -0.250 ; pinch
								 1.000  ;radius
	)
	(plug-in-applylens 1 image copied-layer
							2.20 ; refraction
							FALSE ;keep lens surroundings
	                        FALSE ; set lens surroundings to bg VALUE
							TRUE  ; set lens surroundings transparent
	)
	(gimp-layer-scale-full copied-layer
	                        (* (/ 300 683) (min image-width image-height))
							(* (/ 300 683) (min image-width image-height))
	                        TRUE ;use local origin
							2 ; 1-interpolation-cubic
	)
	(gimp-layer-resize-to-image-size copied-layer)
	(gimp-item-transform-rotate copied-layer 
	                                  3.1415
									  TRUE ;auto center
									  0 ;center-x
									  0 ;center-y
									  )
	(gimp-image-select-item image 2 ;2-channel-op-replace
	                        copied-layer)
	(gimp-selection-invert image)
	(script-fu-drop-shadow image copied-layer
	                             (* (/ -15 683) (min image-width image-height)) ;offset x
	                             (* (/ -15 683) (min image-width image-height)) ;offset y
								 (* (/ 120 683) (min image-width image-height)) ;blur radius 1/3 or more of globe size
	                             '(0 0 0)
								 45        ;opacity
	                              FALSE    ;allow resizing
	)
	(set! shadow-layer (car (gimp-image-get-layer-by-name image "Drop Shadow")))
	(gimp-layer-resize-to-image-size shadow-layer)
	(gimp-selection-none image)
	(gimp-colorize shadow-layer
	                         180 ;hue
							 50 ;saturation
							 85 ;lightness
	)					  
	(set! gradient-layer (car (gimp-layer-new image image-width image-height 1 "globe gradient" 100 0)))
	(gimp-image-insert-layer image gradient-layer (car (gimp-item-get-parent layer)) 0)
	(gimp-image-select-item image 2 ;2-channel-op-replace
	                        copied-layer)
	(gimp-context-set-foreground '(255 255 255))
	(gimp-edit-blend gradient-layer 2 ;2-FG-TRANPARENT-MODE
	                                0 ;paint-mode 0-normal
									7 ; 7-gradient-shapeburst-spherical
									100 ;opacity
									0   ;offset
									0   ;0repeatnone
									FALSE ;reverse
									FALSE ;supersample
									3     ;maxdepth used with supersample
									.2    ;threshold supersample
									TRUE  ;dither
									(/ image-width 2)
									(/ image-height 2)
									(+ (/ image-width 2) (* (/ 150 683) (min image-width image-height)))
									(/ image-height 2)
									)
	(gimp-layer-set-mode gradient-layer 5 ;5-overlaymode
	)
	(set! ellipse-layer (car (gimp-layer-new image image-width image-height 1 "globe ellipse" 100 0)))
	(gimp-image-insert-layer image ellipse-layer (car (gimp-item-get-parent layer)) 0)
	(gimp-image-select-ellipse image 2 ;2-channel-op-replace
	                                   (- (/ image-width 2) (* (/ 110 683) (min image-width image-height)));x
									   (- (/ image-height 2) (* (/ 150 683) (min image-width image-height)));y
									   (* (/ 220 683) (min image-width image-height));width
									   (* (/ 118 683) (min image-width image-height));height
	)
	(gimp-image-select-item image 3 ;3-channel-op-intersect
								copied-layer)
	(gimp-edit-blend ellipse-layer 2 ;2-FG-TRANPARENT-MODE
	                                0 ;paint-mode 0-normal
									0 ; 0-linear 7-gradient-shapeburst-spherical
									100 ;opacity
									0   ;offset
									0   ;0repeatnone
									FALSE ;reverse
									FALSE ;supersample
									3     ;maxdepth used with supersample
									.2    ;threshold supersample
									TRUE  ;dither
									(/ image-width 2)
									(- (/ image-height 2) (* (/ 150 683) (min image-width image-height)));y
									(/ image-width 2)
									(- (/ image-height 2) (* (/ 20 683) (min image-width image-height)));y
									)							
	(gimp-layer-set-mode ellipse-layer 21 ;5-overlaymode 21-grain-merge
	)
	(set! sparkle-layer (car (gimp-layer-new image image-width image-height 1 "globe gradient" 100 0)))
	(gimp-image-insert-layer image sparkle-layer (car (gimp-item-get-parent layer)) 0)
	(set! brush-name (car (gimp-brush-new "sparkle")))
	(gimp-brush-set-radius brush-name (* (/ 80.3 683) (min image-width image-height)))
	(gimp-brush-set-spikes brush-name 4)
	(gimp-brush-set-hardness brush-name 0.01)
	(gimp-brush-set-aspect-ratio brush-name 1.5)
	(gimp-brush-set-angle brush-name 15.0)
	(gimp-brush-set-spacing brush-name 20.0)
	(gimp-brush-set-shape brush-name 2) ;2 diamond
	
	(gimp-context-set-brush brush-name)
	(gimp-context-set-brush-size (* (/ 150 683) (min image-width image-height)))
	
	(gimp-selection-none image)
	(aset point-array 0 (/ image-width 2))
	(aset point-array 1 (- (/ image-height 2) (* (/ 32 683) (min image-width image-height))))
	(gimp-paintbrush sparkle-layer
	                              0 ;fade-out
								  2  ;num-strokes
								  point-array
								  0 ;0-paint-constant
								  0 ;length of gradient to draw
	)
	
	(aset point-array 0 (+ (/ image-width 2) (*(cos (* (/ 220 360) 2 3.1415))(* (/ 150 683) (min image-width image-height)))))
	(aset point-array 1 (+ (/ image-height 2) (*(sin (* (/ 220 360) 2 3.1415))(* (/ 150 683) (min image-width image-height)))))
	(gimp-paintbrush sparkle-layer
	                              0 ;fade-out
								  2  ;num-strokes
								  point-array
								  0 ;0-paint-constant
								  0 ;length of gradient to draw
	)
	
	(aset point-array 0 (+ (/ image-width 2) (*(cos (* (/ 0 360) 2 3.1415))(* (/ 150 683) (min image-width image-height)))))
	(aset point-array 1 (+ (/ image-height 2) (*(sin (* (/ 0 360) 2 3.1415))(* (/ 150 683) (min image-width image-height)))))
	(gimp-paintbrush sparkle-layer
	                              0 ;fade-out
								  2  ;num-strokes
								  point-array
								  0 ;0-paint-constant
								  0 ;length of gradient to draw
	)
	(gimp-brush-delete brush-name)
	
	
	(gimp-image-undo-group-end image) 
	(gimp-displays-flush)
	) ;end of let
)     ;end of define
	  
(script-fu-register
  "script-fu-glass-ball"         ;function name
  "<Image>/Script-Fu/Create New/Glass ball"    ;menu register
  "Creates glass ball effect."       ;description
  "Tin Tran"                          ;author name
  "copyright info and description"         ;copyright info or description
  "2014"                          ;date
  "RGB*, GRAY*"                        ;mode
  SF-IMAGE      "Image" 0                   
  SF-DRAWABLE   "Layer" 0
  ;SF-COLOR       "Overlay Color"       '(203 172 132)
  ;SF-PATTERN     "Overlay Pattern"       "Wood1.jpg"
  ;SF-COLOR       "Color overlay color"    '(9 57 23)
  ;SF-GRADIENT    "Stroke gradient"       "2Gold"
  ;SF-PATTERN     "Overlay pattern"       "Paisley Pattern"
)