; Vivid Light rel 0.01
; Created by Graechan
; 
; Comments directed to http://gimpchat.com or http://gimpscripts.com
;
; License: GPLv3
;    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 3 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.
;
;    To view a copy of the GNU General Public License
;    visit: http://www.gnu.org/licenses/gpl.html
;
;
; ------------
;| Change Log |
; ------------ 
; Rel 0.01 - Initial Release
;
; Gradients blend direction list
(define list-blend-dir '("Left to Right" "Top to Bottom" "Diagonal to centre" "Diagonal from centre"))
;
; Include layer Procedure
(define (vivid-light-include-layer image newlayer oldlayer stack)	;stack 0=above 1=below
	(cond ((defined? 'gimp-image-get-item-position) ;test for 2.8 compatability
            (gimp-image-insert-layer image newlayer (car (gimp-item-get-parent oldlayer)) 
			(+ (car (gimp-image-get-item-position image oldlayer)) stack))                                     ;For GIMP 2.8 
          )
          (else
           (gimp-image-add-layer image newlayer (+ (car (gimp-image-get-layer-position image oldlayer)) stack)) ;For GIMP 2.6 
          )
    ) ;end cond
) ;end add layer procedure
;
(define (script-fu-vivid-light image layer
                               val
							   keep-selection
							   conserve
							   )
							   
	(gimp-image-undo-group-start image)						  

 (let* (
            (width (car (gimp-image-width image)))
			(height (car (gimp-image-height image)))
			(original-width width)
			(original-height height)
			(area (* 1000 1000))
			(alpha (car (gimp-drawable-has-alpha layer)))
		    (sel (car (gimp-selection-is-empty image)))
		    (layer-name (cond ((defined? 'gimp-image-get-item-position) (car (gimp-item-get-name layer)))
		            (else (car (gimp-drawable-get-name layer)))))
			(active-gradient (car (gimp-context-get-gradient)))
		    (active-fg (car (gimp-context-get-foreground)))
		    (active-bg (car (gimp-context-get-background)))
		    (selection-channel 0)
			(ver 2.8)
			(x1 0)
		    (y1 0)
		    (x2 0)
		    (y2 0)
			(matrix 0) 
			(channels 0)
			(copy-layer 0)
			(pasted 0)
			
        )
	(cond ((not (defined? 'gimp-image-get-item-position)) (set! ver 2.6))) ;define the gimp version
	
	(gimp-context-push)
    (gimp-context-set-paint-method "gimp-paintbrush")
	(cond ((defined? 'gimp-context-set-dynamics) (gimp-context-set-dynamics "Dynamics Off")))
;	(if (= ver 2.8) (gimp-context-set-dynamics "Dynamics Off"))
    (gimp-context-set-foreground '(0 0 0))
	(gimp-context-set-background '(255 255 255))

;;;;create selection-channel (gimp-selection-load selection-channel)	
	(set! selection-channel (car (gimp-selection-save image)))
	(cond ((= ver 2.8) (gimp-item-set-name selection-channel "selection-channel"))
	(else (gimp-drawable-set-name selection-channel "selection-channel"))
    ) ;endcond
	(gimp-image-set-active-layer image layer)	
	
;;;;begin the script	

;;;;create the vivid light matrix array	
	(set! matrix (cons-array 25 'long))    
    (aset matrix 0 val) (aset matrix 1 -1.0) (aset matrix 2 -1.0) (aset matrix 3 -1.0) (aset matrix 4 -1.0)        
    (aset matrix 5 -1.0) (aset matrix 6 val) (aset matrix 7 -1.0) (aset matrix 8 -1.0) (aset matrix 9 -1.0)        
    (aset matrix 10 -1.0) (aset matrix 11 -1.0) (aset matrix 12 val) (aset matrix 13 -1.0) (aset matrix 14 -1.0)        
    (aset matrix 15 -1.0) (aset matrix 16 -1.0) (aset matrix 17 -1.0) (aset matrix 18 val) (aset matrix 19 -1.0)
    (aset matrix 20 -1.0) (aset matrix 21 -1.0) (aset matrix 22 -1.0) (aset matrix 23 -1.0) (aset matrix 24 val)

;;;;create the vivid light channel array	
	(set! channels (cons-array 5 'long))
    (aset channels 0 1) (aset channels 1 1) (aset channels 2 1) (aset channels 3 1) (aset channels 4 0)
    
;;;;create the copy layer
    (set! copy-layer (car (gimp-layer-new image (car (gimp-image-width image)) (car (gimp-image-height image)) RGBA-IMAGE "copy-layer" 100 GRAIN-MERGE-MODE)))
    (vivid-light-include-layer image copy-layer layer 0)	;stack 0=above 1=below
	(gimp-edit-copy-visible image)
	(set! pasted (car (gimp-edit-paste copy-layer FALSE)))
	(gimp-floating-sel-anchor pasted)
	(cond ((= ver 2.8) (gimp-image-select-item image 2 selection-channel))
	(else (gimp-selection-load selection-channel))
	) ;endcond
  
	(plug-in-convmatrix RUN-NONINTERACTIVE image 
	copy-layer ;drawable 
	25 ;argc-matrix [The number of elements in the following array. Should be always 25.]  
    matrix
    FALSE ;alpha-alg [Enable weighting by alpha channel]
    50 ;divisor
    0 ;offset
    5 ;argc-channels [The number of elements in following array. Should be always 5.]
    channels
    2 ;bmode [Mode for treating image borders] { EXTEND (0), WRAP (1), CLEAR (2) }
    )
	


;;;;finish the script	
    (if (= conserve FALSE) (set! layer (car (gimp-image-merge-down image copy-layer EXPAND-AS-NECESSARY))))
    (cond ((= ver 2.8) (gimp-item-set-name layer layer-name))
	(else (gimp-drawable-set-name layer layer-name))
    ) ;endcond
    (if (= keep-selection FALSE) (gimp-selection-none image))
    (if (= conserve FALSE) (gimp-image-remove-channel image selection-channel))
    (if (and (= conserve FALSE) (= alpha FALSE)) (gimp-layer-flatten layer))	

	(gimp-displays-flush)
	(gimp-image-undo-group-end image)
	(gimp-context-pop)

 )
)

(script-fu-register "script-fu-vivid-light"        		    
  "Vivid Light..."
  "Implements a vivid light effect"
  "Graechan"
  "Graechan - http://gimpchat.com"
  "July 2013"
  "RGB* GRAY*"
  SF-IMAGE      "image"      0
  SF-DRAWABLE   "drawable"   0
  SF-ADJUSTMENT "Vivid Light Amount" '(15.5 0 25 .1 1 1 0)
  SF-TOGGLE     "Keep selection"          FALSE
  SF-TOGGLE     "Keep the Layers"   FALSE
)

(script-fu-menu-register "script-fu-vivid-light" "<Image>/Filters/Light and Shadow")


