; GIMP - The GNU Image Manipulation Program
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
;
; 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 2 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.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
;
; 
; The script can be found on an open image under: Filters > Light and Shadow > Plastic Wrap All Layers...

; 
;January 18, 2014 Update: This script was previously designed to work on all layers. 
;This update adds the option to work on the active layer, all visible layers, and all linked layers.


; Define the Function

(define (script-fu-plasti-wrap-all-layers 	image		 
				drawable
				actLayer
				radius
				)

; Declare the Variables

	(let* (
		    	(activeLayer nil)
			(counter nil)
			(numLayers nil)
			(allLayersRef nil)
			(baseType nil)
			(origSelect nil)
			(waxLayer1 nil)
			(waxLayer2 nil)
			(copyLayer nil)	
			(visible nil)
			(linked nil)	
)

(gimp-context-push)
(gimp-image-undo-group-start image)


(set! origSelect (car (gimp-selection-save image)))
(gimp-selection-none image) 


(if (= actLayer 0)

(begin

(set! counter (car (gimp-image-get-layers image)))
(set! numLayers (car (gimp-image-get-layers image)))
(set! allLayersRef (cadr (gimp-image-get-layers image)))

	(while (> numLayers 0)
	(set! activeLayer (car (gimp-image-set-active-layer image (aref allLayersRef (- numLayers 1)))))
	(set! activeLayer (car (gimp-image-get-active-layer image))) 
	(gimp-drawable-set-visible activeLayer TRUE)
	(set! waxLayer1 (car (gimp-layer-copy activeLayer TRUE)))
	(set! waxLayer2 (car (gimp-layer-copy activeLayer TRUE)))
	(gimp-image-add-layer image waxLayer1 -1)	
	(gimp-image-add-layer image waxLayer2 -1)
;	(gimp-desaturate waxLayer1)
;	(gimp-desaturate waxLayer2)
	(gimp-drawable-desaturate waxLayer1 DESATURATE-LUMINANCE)
	(gimp-drawable-desaturate waxLayer2 DESATURATE-LUMINANCE)
	(gimp-invert waxLayer2)
	(plug-in-neon RUN-NONINTERACTIVE image waxLayer1 radius 0)
	(plug-in-neon RUN-NONINTERACTIVE image waxLayer2 radius 0)
	(gimp-layer-set-mode waxLayer2 LAYER-MODE-SCREEN)
	(gimp-image-merge-down image waxLayer2 CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(set! copyLayer (car (gimp-layer-copy activeLayer TRUE)))
	(gimp-image-add-layer image copyLayer -1)
	(gimp-layer-set-mode copyLayer LAYER-MODE-DIVIDE-LEGACY)
	(gimp-image-merge-down image copyLayer CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(gimp-invert activeLayer)
	(gimp-layer-set-mode activeLayer LAYER-MODE-SCREEN)
	(gimp-image-merge-down image activeLayer CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))	
	(set! numLayers (- numLayers 1))
	)
)
)


(if (= actLayer 1)

(begin

	(gimp-image-set-active-layer image drawable)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(gimp-drawable-set-visible activeLayer TRUE)
	(set! waxLayer1 (car (gimp-layer-copy activeLayer TRUE)))
	(set! waxLayer2 (car (gimp-layer-copy activeLayer TRUE)))
	(gimp-image-add-layer image waxLayer1 -1)	
	(gimp-image-add-layer image waxLayer2 -1)
	(gimp-desaturate waxLayer1)
	(gimp-desaturate waxLayer2)
	(gimp-invert waxLayer2)
	(plug-in-neon RUN-NONINTERACTIVE image waxLayer1 radius 0)
	(plug-in-neon RUN-NONINTERACTIVE image waxLayer2 radius 0)
	(gimp-layer-set-mode waxLayer2 LAYER-MODE-SCREEN)
	(gimp-image-merge-down image waxLayer2 CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(set! copyLayer (car (gimp-layer-copy activeLayer TRUE)))
	(gimp-image-add-layer image copyLayer -1)
	(gimp-layer-set-mode copyLayer LAYER-MODE-DIVIDE-LEGACY)
	(gimp-image-merge-down image copyLayer CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(gimp-invert activeLayer)
	(gimp-layer-set-mode activeLayer LAYER-MODE-SCREEN)
	(gimp-image-merge-down image activeLayer CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))	
	)
)


(if (= actLayer 2)

(begin

(set! counter (car (gimp-image-get-layers image)))
(set! numLayers (car (gimp-image-get-layers image)))
(set! allLayersRef (cadr (gimp-image-get-layers image)))

	(while (> numLayers 0)
	(set! activeLayer (car (gimp-image-set-active-layer image (aref allLayersRef (- numLayers 1)))))
	(set! activeLayer (car (gimp-image-get-active-layer image))) 
	(set! visible (car (gimp-item-get-visible activeLayer)))
	(if (= visible  TRUE)
	(begin
	(gimp-drawable-set-visible activeLayer TRUE)
	(set! waxLayer1 (car (gimp-layer-copy activeLayer TRUE)))
	(set! waxLayer2 (car (gimp-layer-copy activeLayer TRUE)))
	(gimp-image-add-layer image waxLayer1 -1)	
	(gimp-image-add-layer image waxLayer2 -1)
	(gimp-desaturate waxLayer1)
	(gimp-desaturate waxLayer2)
	(gimp-invert waxLayer2)
	(plug-in-neon RUN-NONINTERACTIVE image waxLayer1 radius 0)
	(plug-in-neon RUN-NONINTERACTIVE image waxLayer2 radius 0)
	(gimp-layer-set-mode waxLayer2 LAYER-MODE-SCREEN)
	(gimp-image-merge-down image waxLayer2 CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(set! copyLayer (car (gimp-layer-copy activeLayer TRUE)))
	(gimp-image-add-layer image copyLayer -1)
	(gimp-layer-set-mode copyLayer LAYER-MODE-DIVIDE-LEGACY)
	(gimp-image-merge-down image copyLayer CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(gimp-invert activeLayer)
	(gimp-layer-set-mode activeLayer LAYER-MODE-SCREEN)
	(gimp-image-merge-down image activeLayer CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))	
	(set! numLayers (- numLayers 1))
	)
	(set! numLayers (- numLayers 1))
	)

)
); first begin
); if actLayer=2

(if (= actLayer 3)

(begin

(set! counter (car (gimp-image-get-layers image)))
(set! numLayers (car (gimp-image-get-layers image)))
(set! allLayersRef (cadr (gimp-image-get-layers image)))

	(while (> numLayers 0)
	(set! activeLayer (car (gimp-image-set-active-layer image (aref allLayersRef (- numLayers 1)))))
	(set! activeLayer (car (gimp-image-get-active-layer image))) 
	(set! linked (car (gimp-item-get-linked activeLayer)))
	(if (= linked  TRUE)
	(begin
	(gimp-drawable-set-visible activeLayer TRUE)
	(set! waxLayer1 (car (gimp-layer-copy activeLayer TRUE)))
	(set! waxLayer2 (car (gimp-layer-copy activeLayer TRUE)))
	(gimp-image-add-layer image waxLayer1 -1)	
	(gimp-image-add-layer image waxLayer2 -1)
	(gimp-desaturate waxLayer1)
	(gimp-desaturate waxLayer2)
	(gimp-invert waxLayer2)
	(plug-in-neon RUN-NONINTERACTIVE image waxLayer1 radius 0)
	(plug-in-neon RUN-NONINTERACTIVE image waxLayer2 radius 0)
	(gimp-layer-set-mode waxLayer2 LAYER-MODE-SCREEN)
	(gimp-image-merge-down image waxLayer2 CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(set! copyLayer (car (gimp-layer-copy activeLayer TRUE)))
	(gimp-image-add-layer image copyLayer -1)
	(gimp-layer-set-mode copyLayer LAYER-MODE-DIVIDE-LEGACY)
	(gimp-image-merge-down image copyLayer CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))
	(gimp-invert activeLayer)
	(gimp-layer-set-mode activeLayer LAYER-MODE-SCREEN)
	(gimp-image-merge-down image activeLayer CLIP-TO-BOTTOM-LAYER)
	(set! activeLayer (car (gimp-image-get-active-layer image)))	
	(gimp-item-set-linked activeLayer TRUE)
	(set! numLayers (- numLayers 1))
	)
	(set! numLayers (- numLayers 1))
	)

)
); first begin
); if actLayer=3


(gimp-selection-load origSelect)
(gimp-image-remove-channel image origSelect) 	

(gimp-image-undo-group-end image)
(gimp-context-pop)
(gimp-displays-flush)


  )
)

(script-fu-register "script-fu-plasti-wrap-all-layers"
  "<Image>/Filters/Light and Shadow/Plastic Wrap All Layers..."
  "Apply a Plastic Wrap effect to all layers of an image."
  "Art Wade"
  "Art Wade"
  "January 18, 2014"
  "RGB*"
SF-IMAGE       	"Image" 0
SF-DRAWABLE    	"Drawable" 0
SF-OPTION		"Layers to work on" 		'("All"
							"Active"
							"Visible"
							"Linked")
SF-ADJUSTMENT "Plastic Amount (Lower values means more plastic)" '(5 1 100 1 2 0 1)
)


                         

