; Script-Fu Butterfly_3.scm
;
; Téléchargement - Download :     http://gimpchat.com/viewtopic.php?f=23&t=2519&start=60#p84719
;
; Ce filtre est une adaptation du didacticiel "Butterfly Wings" http://gimpchat.com/viewtopic.php?f=23&t=2519  créé par Lylejk
; des effets de couleur, de distorsion, de'inclinaison sont ajoutés à ce didacticiel.
; Ce script comporte du code issu du script-fu "script-fu-amazing-circles"  créé par Harry Phillips Mar. 23 2007 (GPL 3) et "Landscape Illustrator" créé par Mark Lowry Created on 5/31/2006 for v.2.2.8 (GPL)
;
;	Copyright (C) 2013 samj ( http://aljacom.com/~gmic/ )
;
;	Licence GPL
;		Ce fichier est le Script-Fu "Butterfly_3.scm" pour Gimp. 
;		Ce Script-Fu "Butterfly_3.scm" est un logiciel libre ; vous pouvez le redistribuer ou le modifier suivant les termes de la GNU General Public License
;		telle que publiée par la Free Software Foundation ; soit la version 3 de la licence, soit (à votre gré) toute version ultérieure.
;		Ce Script-Fu "Butterfly_3.scm" est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE ; 
;		pas même la garantie implicite de COMMERCIABILISABILITÉ ni d'ADÉQUATION à UN OBJECTIF PARTICULIER.
;		Consultez la GNU General Public License pour plus de détails.
;		Vous devez avoir reçu une copie de la GNU General Public License en même temps que GIMP ; si ce n'est pas le cas, consultez <http://www.gnu.org/licenses>
;
;	Ce fichier Butterfly_3.scmest édité avec Notepad++    http://notepad-plus-plus.org/
;
;
;	Version 20130323 ajout Creativity
;	Version 20130322 ajout Translucent Lines, correction Random
;	Version 20130320 (origine) C'est le printemps !  
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;	Ce script est un exemple employant le filtre "Twist" version 2.1 Download http://gimpchat.com/viewtopic.php?f=7&t=6732
;
;	Accès sur l'image par :
;
;	Filtres > Distorsions > Butterfly Wings V2 ...
;	Filters > Distorts > Butterfly Wings V2 ...
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Merci à Lylejk , Harry Phillips et Mark Lowry :o)
;

		
(define	
	(
		Butterfly_3_Lylejk_samj
		image
		drawable
		Effects
		Pattern_Resize_Crop
		Creativity
		Parameter_2_S
		Parameter_5_S
		Parameter_7_S
		Parameter_1_corps
		operating_mode
		Display_Filter_Settings
		Add_an_alpha_channel
		Inclination_of_the_Y_axis
		Inclination_of_the_Y_axis_0_1
		Inclination_of_the_Y_axis_1_1
		Angle
		Pourcentage_Perspective
		Flatten
		Back_Ground_Color
		Aleatoire


	)
	(let* 
		(
			(run_mode 1)
			(Parameter_1 0)
			(Parameter_2 0)
			(Parameter_3 0)
			(Parameter_4 0)
			(Parameter_5 0)
			(Parameter_6 0)
			(Parameter_7 0)
			(Parameter_8 0)
			(Use_cutoff 0)
			(r_The_cutoff_radius 0)
			(dr_The_falloff_radius 0)
			(The_current_function 0)

			(message (car (gimp-message-get-handler)))
			(Fonction_En_Texte)
			( My_Settings "Butterfly Wings V3 Settings")
			;calque_tete
			(calque_tete)
			
			(transparence (car (gimp-drawable-has-alpha drawable)))
			
			(width (car (gimp-image-width image)))
			(height (car (gimp-image-height image)))
			
			(width_1) ; fond
			(height_1)
			(width_2) ; ailes
			(height_2)
			(width_3) ; corps
			(height_3)
			
			(buffer-name "copier_visible")
			(copier_visible)
			(calque_sym)
			(selection_en_cours)

			(calque_corps)
			(Perspective 0)
			
			(old_context_transform_direction (car (gimp-context-get-transform-direction)))
			(old_context_interpolation (car (gimp-context-get-interpolation)))
			(old_context_recursion (car (gimp-context-get-transform-recursion)))
			(old_context_transform_resize (car (gimp-context-get-transform-resize)))
			(old-fg (car (gimp-context-get-foreground)))
			(old-bg (car (gimp-context-get-background)))


			(matrix 0)
			(matrix-list 0)
			(channels 0)
			

		)

		

		
		(gimp-context-set-transform-direction TRANSFORM-FORWARD)
		(gimp-context-set-interpolation 2)
		(gimp-context-set-transform-recursion 3)
		(gimp-context-set-transform-resize TRANSFORM-RESIZE-ADJUST)
		
		(gimp-undo-push-group-start image)

		(gimp-selection-none image)
		
		
		(if (= Aleatoire 1)
			(begin
				(set! Effects  (round (random 8)) ) ; '("A" "B" "C Mosaic" "D Mosaic" "E Whirling & Pinching" "F Pixelize" "G")
				(set! Pattern_Resize_Crop  (round (random 3)) ) ;  '("Resize" "Crop" "Translucent Lines")
				(set! Parameter_2_S  (- (random 8) 10) ) ; '(-6.5 -10 -2 0.01 0.1 3 1)
				(set! Parameter_5_S (- (/ (random 44) 100) 0.22)  ) ; '(0.2 -0.22 0.22 0.01 0.1 3 1) 
				(set! Parameter_7_S  (- (/ (random 5) 10) 0.6) ) ; '(-0.05 -0.6 -0.02 0.01 0.1 3 1) 
				;  (set! Parameter_1_corps (round (random 2))  ) ;  '("Normal" "Storing The Parameters")
				(set! operating_mode  (round (random 2)) ) ; FALSE
				;  (set! Display_Filter_Settings  (round (random 2)) ) ; TRUE
				(set! Add_an_alpha_channel  (round (random 2)) ) ; FALSE
				(set! Inclination_of_the_Y_axis  (- (/ (random 2000) 1000) 1) ) ;  '(-0.09 -1 1 0.00001 0.001 5 1)
				(set! Inclination_of_the_Y_axis_0_1  (- (/ (random 2000) 1000) 1) ) ; '(0.77 -1 1 0.00001 0.001 5 1)
				(set! Inclination_of_the_Y_axis_1_1  (- (random 90) 45)  ) ;  '(-30 -45 45 1 10 2 1)
				(set! Angle  (- (random 90) 45) ) ;  '(-30 -45 45 1 10 2 1)
				(set! Pourcentage_Perspective  (- (random 200) 100) ) ; '(0 -100 +100 1 10 2 1)	
				(set! Flatten (round (random 2)) )
			)
		)
		
		(if (= Add_an_alpha_channel 1)
			(begin

				(if (= transparence 0) ; pas de canal alpha
					(begin
						(set! transparence 1)
						(gimp-layer-add-alpha (car (gimp-image-get-active-drawable image)))					
					)
				)

			
			)
		)
			

;******************************************************************************************************



		(if (= Pattern_Resize_Crop 2) ; c'est Translucent Lines qui est choisi
			(begin
			
						; forcer transparence
						(set! transparence (car (gimp-drawable-has-alpha drawable)))
						
						(if (= transparence 0) ; pas de canal alpha
							(begin
								(set! transparence 1)
								(gimp-layer-add-alpha (car (gimp-image-get-active-drawable image)))					
							)
						)
						
						
						
						
						; paramétrage complet du filtre (hors image et drawable)
						(set! run_mode 1)
						(set! Parameter_1 1)
						(set! Parameter_2 10)
						(set! Parameter_3 0)
						(set! Parameter_4 0)
						(set! Parameter_5 0)
						(set! Parameter_6 0)
						(set! Parameter_7 0)
						(set! Parameter_8 0)
						(set! Use_cutoff 0)
						(set! r_The_cutoff_radius 0.5)
						(set! dr_The_falloff_radius 0.2)
						(set! The_current_function 3)


						(plug-in-Twist 
							run_mode				;      GIMP_PDB_INT32, run_mode, Interactive, non-interactive
							image					;      GIMP_PDB_IMAGE, image, Input image
							drawable				;      GIMP_PDB_DRAWABLE, drawable, Input drawable
							Parameter_1				;      GIMP_PDB_FLOAT, a1, float parameter #1
							Parameter_2				;      GIMP_PDB_FLOAT, a2, float parameter #2
							Parameter_3				;      GIMP_PDB_FLOAT, a3, float parameter #3
							Parameter_4				;      GIMP_PDB_FLOAT, a4, float parameter #4
							Parameter_5				;      GIMP_PDB_FLOAT, a5, float parameter #5
							Parameter_6				;      GIMP_PDB_FLOAT, a6, float parameter #6
							Parameter_7				;      GIMP_PDB_FLOAT, a7, float parameter #7
							Parameter_8				;      GIMP_PDB_FLOAT, a8, float parameter #8
							"unused"				;      GIMP_PDB_STRING , lettering01, lettering #1
							"unused"				;      GIMP_PDB_STRING , lettering02, lettering #2
							"unused"				;      GIMP_PDB_STRING , lettering03, lettering #3
							"unused"				;      GIMP_PDB_STRING , lettering04, lettering #4
							"unused"				;      GIMP_PDB_STRING , lettering05, lettering #5
							"unused"				;      GIMP_PDB_STRING , lettering06, lettering #6
							"unused"				;      GIMP_PDB_STRING , lettering07, lettering #7
							"unused"				;      GIMP_PDB_STRING , lettering08, lettering #8
							Use_cutoff				;      GIMP_PDB_INT32, use_cutoff, Use cutoff or not
							r_The_cutoff_radius		;      GIMP_PDB_FLOAT, r, The cutoff radius
							dr_The_falloff_radius	;      GIMP_PDB_FLOAT, dr, The falloff radius
							The_current_function	;      GIMP_PDB_INT32, current_function, The current function
																; "R(r) - 4th order poly"	0
																; "R(phi) - cosine"			1
																; "Phi(r) - 4th order poly"	2
																; "Phi(phi) - cosine"		3
																; "R(r)Phi(phi)"			4
																; "R(r)R(phi)"				5
																; "Cosines"					6
																; "Gauss"					7
																; "XY 4th Order"			8
							operating_mode			;      GIMP_PDB_INT32, operating_mode ( 0=normal 1=mémorisation des valeurs )
						)


					(if (= Display_Filter_Settings 1) ; effets standard , prépositionner les valeurs si les curseurs sont à 0
						(begin
								;; sélectionner la console erreurs pour envoyer un message
								(gimp-message-set-handler 2)

								(if (= The_current_function 0) (set! Fonction_En_Texte "R(r) - 4th order poly" ) )
								(if (= The_current_function 1) (set! Fonction_En_Texte "R(phi) - cosine" ) )
								(if (= The_current_function 2) (set! Fonction_En_Texte "Phi(r) - 4th order poly" ) )
								(if (= The_current_function 3) (set! Fonction_En_Texte "Phi(phi) - cosine" ) )
								(if (= The_current_function 4) (set! Fonction_En_Texte "R(r)Phi(phi)" ) )
								(if (= The_current_function 5) (set! Fonction_En_Texte "R(r)R(phi)" ) )
								(if (= The_current_function 6) (set! Fonction_En_Texte "Cosines" ) )
								(if (= The_current_function 7) (set! Fonction_En_Texte "Gauss" ) )
								(if (= The_current_function 8) (set! Fonction_En_Texte "XY 4th Order" ) )

								(gimp-message 
									(string-append My_Settings "\nParameter 1...8 =\n" 
										(number->string Parameter_1) "\n" 
										(number->string Parameter_2) "\n" 
										(number->string Parameter_3) "\n" 
										(number->string Parameter_4) "\n" 
										(number->string Parameter_5) "\n" 
										(number->string Parameter_6) "\n" 
										(number->string Parameter_7) "\n" 
										(number->string Parameter_8) 
										"\ncutoff r dr function =\n"
										(number->string Use_cutoff) "\n"
										(number->string r_The_cutoff_radius) "\n"
										(number->string dr_The_falloff_radius) "\n"
										Fonction_En_Texte
										"\n--------------------\n"			
									)
								)
						)
					)

			)
		)

;******************************************************************************************************

		
		; créer une image carrée
		(if (= width height)
			()
			;else
			(begin
				(if (= Pattern_Resize_Crop 1) ; c'est crop qui est choisi
					(begin
						;Check which is longer
						(if (> width height)
							(gimp-image-crop image height height (/ (- width height) 2) 0)
							;else
							(gimp-image-crop image width width 0 (/ (- height width) 2))
						)
					)
					;else
					(begin
						;Check which is longer
						(if (> width height)
							(gimp-layer-scale (car (gimp-image-get-active-layer image))  height height 0)
							;else
							(gimp-layer-scale (car (gimp-image-get-active-layer image))  width width 0)
						)
						; ajuster taille image à la taille des calques
						(gimp-image-resize-to-layers image)
					)
				)
			)
		)





		(if (= Effects 0)
			(begin
				;Second polar co-ord with "To Polar = on"
				(plug-in-polar-coords 1 image (car (gimp-image-get-active-layer image)) 100 0 1 0 1)
			)
		)

		(if (= Effects 1)
			(begin
				;First polar co-ord with "To Polar = off"
				(plug-in-polar-coords 1 image (car (gimp-image-get-active-layer image)) 100 0 1 0 0)

				;Flip vertically
				(gimp-image-flip image 1)

				;Second polar co-ord with "To Polar = on"
				(plug-in-polar-coords 1 image (car (gimp-image-get-active-layer image)) 100 0 1 0 1)
			)
		)

		(if (= Effects 2)
			(begin
				;Second polar co-ord with "To Polar = on"
				(plug-in-polar-coords 1 image (car (gimp-image-get-active-layer image)) 100 0 1 0 1)
				(plug-in-mosaic 1 image drawable (/ width 20) (/ height 20) 5 1 1 0 1 1 1 1 0 0)
				; (gimp-layer-flatten drawable)
				; (gimp-layer-add-alpha drawable)
				
			)
		)

		(if (= Effects 3)
			(begin
				;First polar co-ord with "To Polar = off"
				(plug-in-polar-coords 1 image (car (gimp-image-get-active-layer image)) 100 0 1 0 0)

				;Flip vertically
				(gimp-image-flip image 1)
				(plug-in-mosaic 1 image drawable (/ width 10) (/ height 10) 5 1 1 0 1 1 1 3 0 0)
				; (gimp-layer-flatten drawable)
				; (gimp-layer-add-alpha drawable)
			)
		)
		
		(if (= Effects 4)
			(begin
				;Second polar co-ord with "To Polar = on"
				(plug-in-polar-coords 1 image (car (gimp-image-get-active-layer image)) 100 0 1 0 1)
				(plug-in-whirl-pinch 1 image drawable 720 -1 2)
				; (gimp-layer-flatten drawable)
				; (gimp-layer-add-alpha drawable)
				
			)
		)

		(if (= Effects 5)
			(begin
				;Second polar co-ord with "To Polar = on"
				(plug-in-polar-coords 1 image (car (gimp-image-get-active-layer image)) 100 0 1 0 1)
				(plug-in-pixelize 1 image drawable (/ width 11))
				; (gimp-layer-flatten drawable)
				; (gimp-layer-add-alpha drawable)
				
			)
		)









;******************************************************************************************************


		

		; paramétrage complet du filtre (hors image et drawable)
		(set! run_mode 1)
		(set! Parameter_1 0.04)
		(set! Parameter_2 -10)
		(set! Parameter_3 10)
		(set! Parameter_4 -10)
		(set! Parameter_5 0)
		(set! Parameter_6 6.7)
		(set! Parameter_7 0)
		(set! Parameter_8 0)
		(set! Use_cutoff 1)
		(set! r_The_cutoff_radius 1)
		(set! dr_The_falloff_radius 1)
		(set! The_current_function 8)





		(plug-in-Twist 
			run_mode				;      GIMP_PDB_INT32, run_mode, Interactive, non-interactive
			image					;      GIMP_PDB_IMAGE, image, Input image
			drawable				;      GIMP_PDB_DRAWABLE, drawable, Input drawable
			Parameter_1				;      GIMP_PDB_FLOAT, a1, float parameter #1
			Parameter_2				;      GIMP_PDB_FLOAT, a2, float parameter #2
			Parameter_3				;      GIMP_PDB_FLOAT, a3, float parameter #3
			Parameter_4				;      GIMP_PDB_FLOAT, a4, float parameter #4
			Parameter_5				;      GIMP_PDB_FLOAT, a5, float parameter #5
			Parameter_6				;      GIMP_PDB_FLOAT, a6, float parameter #6
			Parameter_7				;      GIMP_PDB_FLOAT, a7, float parameter #7
			Parameter_8				;      GIMP_PDB_FLOAT, a8, float parameter #8
			"unused"				;      GIMP_PDB_STRING , lettering01, lettering #1
			"unused"				;      GIMP_PDB_STRING , lettering02, lettering #2
			"unused"				;      GIMP_PDB_STRING , lettering03, lettering #3
			"unused"				;      GIMP_PDB_STRING , lettering04, lettering #4
			"unused"				;      GIMP_PDB_STRING , lettering05, lettering #5
			"unused"				;      GIMP_PDB_STRING , lettering06, lettering #6
			"unused"				;      GIMP_PDB_STRING , lettering07, lettering #7
			"unused"				;      GIMP_PDB_STRING , lettering08, lettering #8
			Use_cutoff				;      GIMP_PDB_INT32, use_cutoff, Use cutoff or not
			r_The_cutoff_radius		;      GIMP_PDB_FLOAT, r, The cutoff radius
			dr_The_falloff_radius	;      GIMP_PDB_FLOAT, dr, The falloff radius
			The_current_function	;      GIMP_PDB_INT32, current_function, The current function
												; "R(r) - 4th order poly"	0
												; "R(phi) - cosine"			1
												; "Phi(r) - 4th order poly"	2
												; "Phi(phi) - cosine"		3
												; "R(r)Phi(phi)"			4
												; "R(r)R(phi)"				5
												; "Cosines"					6
												; "Gauss"					7
												; "XY 4th Order"			8
			operating_mode			;      GIMP_PDB_INT32, operating_mode ( 0=normal 1=mémorisation des valeurs )
		)


;******************************************************************************************************

	(if (= Display_Filter_Settings 1) ; effets standard , prépositionner les valeurs si les curseurs sont à 0
		(begin
				;; sélectionner la console erreurs pour envoyer un message
				(gimp-message-set-handler 2)

				(if (= The_current_function 0) (set! Fonction_En_Texte "R(r) - 4th order poly" ) )
				(if (= The_current_function 1) (set! Fonction_En_Texte "R(phi) - cosine" ) )
				(if (= The_current_function 2) (set! Fonction_En_Texte "Phi(r) - 4th order poly" ) )
				(if (= The_current_function 3) (set! Fonction_En_Texte "Phi(phi) - cosine" ) )
				(if (= The_current_function 4) (set! Fonction_En_Texte "R(r)Phi(phi)" ) )
				(if (= The_current_function 5) (set! Fonction_En_Texte "R(r)R(phi)" ) )
				(if (= The_current_function 6) (set! Fonction_En_Texte "Cosines" ) )
				(if (= The_current_function 7) (set! Fonction_En_Texte "Gauss" ) )
				(if (= The_current_function 8) (set! Fonction_En_Texte "XY 4th Order" ) )

				(gimp-message 
					(string-append My_Settings "\nParameter 1...8 =\n" 
						(number->string Parameter_1) "\n" 
						(number->string Parameter_2) "\n" 
						(number->string Parameter_3) "\n" 
						(number->string Parameter_4) "\n" 
						(number->string Parameter_5) "\n" 
						(number->string Parameter_6) "\n" 
						(number->string Parameter_7) "\n" 
						(number->string Parameter_8) 
						"\ncutoff r dr function =\n"
						(number->string Use_cutoff) "\n"
						(number->string r_The_cutoff_radius) "\n"
						(number->string dr_The_falloff_radius) "\n"
						Fonction_En_Texte
						"\n--------------------\n"			
					)
				)
		)
	)



;******************************************************************************************************


		(plug-in-autocrop-layer 1 image drawable)
		
		; ajuster taille image à la taille des calques
		(gimp-image-resize-to-layers image)



;******************************************************************************************************
	; Creativity

	(if (= Creativity 1) ; iwarp
		(begin
		
			(plug-in-iwarp 0 image drawable)
		
		
		)
	)

	(if (= Creativity 2) ; convolution matrix
		(begin
		
			(set! matrix-list
				'(
					0		0		0		0		0
					0		0		0		0		0
					0		0		1		0		0
					0		0		0		0		0
					0		0		0		0		0
				 )
			)
			
			(set! channels (make-vector 5 'long ))
			
			(vector-set! channels 0 1 )
			(vector-set! channels 1 1 )
			(vector-set! channels 2 1 )
			(vector-set! channels 3 1 )
			(vector-set! channels 4 1 )
		
			; Convert maxtrix list (25) into matrix array (5x5)
			(define (get-matrix matrix-list)
			  (let* (
					(n 0)
					)
			  (set! n 25 )
			  (define (list-ref l n) (nth n l))
				(let* ((count 0)
					   (matrix (cons-array 25 'double)))
				  (while (< count 25 )
				  (aset matrix count (list-ref matrix-list count))
				  (set! count (+ count 1)))
				  matrix ; Return the matrix array
				)
			  )
			)

			(plug-in-convmatrix 0 image drawable 25 (set! matrix (get-matrix matrix-list)) 1 1 0 5 channels 0)

		)
	)
 
	(if (= Creativity 3) ; iwarp + convolution matrix
		(begin
			
			(plug-in-iwarp 0 image drawable)
			
			(set! matrix-list
				'(
					0		0		0		0		0
					0		0		0		0		0
					0		0		1		0		0
					0		0		0		0		0
					0		0		0		0		0
				 )
			)
			
			(set! channels (make-vector 5 'long ))
			
			(vector-set! channels 0 1 )
			(vector-set! channels 1 1 )
			(vector-set! channels 2 1 )
			(vector-set! channels 3 1 )
			(vector-set! channels 4 1 )
		
			; Convert maxtrix list (25) into matrix array (5x5)
			(define (get-matrix matrix-list)
			  (let* (
					(n 0)
					)
			  (set! n 25 )
			  (define (list-ref l n) (nth n l))
				(let* ((count 0)
					   (matrix (cons-array 25 'double)))
				  (while (< count 25 )
				  (aset matrix count (list-ref matrix-list count))
				  (set! count (+ count 1)))
				  matrix ; Return the matrix array
				)
			  )
			)

			(plug-in-convmatrix 0 image drawable 25 (set! matrix (get-matrix matrix-list)) 1 1 0 5 channels 0)

		)
	)

;******************************************************************************************************


		(set! drawable (car (gimp-image-get-active-layer image)))
		
		(set! copier_visible (car (gimp-edit-named-copy-visible image buffer-name)))

		(set! width_1 (car (gimp-drawable-width drawable)))
		(set! height_1 (car (gimp-drawable-height drawable)))

		(set! calque_sym (car (gimp-layer-new image (+ width_1 width_1) (+ height_1 height_1) 0 "calque_sym" 100 0)))
		
		(gimp-image-insert-layer image calque_sym -1 0)
		
		(gimp-layer-add-alpha calque_sym)
		
		(gimp-item-set-visible calque_sym TRUE)

		; ajuster taille image à la taille des calques
		(gimp-image-resize-to-layers image)
		
		; tests
		; appliquer blanc sur PP
		;(gimp-context-set-foreground '(255 255 255))
		
		; remplir de PP	
		(gimp-drawable-fill calque_sym 0)
		
		(gimp-edit-clear calque_sym)

		; bas droite
		(gimp-image-select-rectangle image 0  width_1  height_1  (- (+ width_1 width_1) 1)   (- (+ height_1 height_1) 1 )  )
		
		(set! selection_en_cours (car (gimp-edit-named-paste calque_sym copier_visible 0)))
		
		(gimp-floating-sel-anchor selection_en_cours)
		
		(gimp-selection-none image)
		
		; haut droite
		(gimp-image-select-rectangle image 0  width_1  0  (- (+ width_1 width_1) 1)   (- height_1 1 )  )
		
		(set! selection_en_cours (car (gimp-edit-named-paste calque_sym copier_visible 0)))
		
		(gimp-flip selection_en_cours 1)

		(gimp-floating-sel-anchor selection_en_cours)
		
		(gimp-selection-none image)


		; bas gauche
		(gimp-image-select-rectangle image 0  0  height_1  (- width_1  1)   (- (+ height_1 height_1) 1 )  )
		
		(set! selection_en_cours (car (gimp-edit-named-paste calque_sym copier_visible 0)))
		
		(gimp-flip selection_en_cours 0)

		(gimp-floating-sel-anchor selection_en_cours)
		
		(gimp-selection-none image)

		; haut gauche
		(gimp-image-select-rectangle image 0  0  0  (- width_1  1)   (- height_1 1 )   )
		
		(set! selection_en_cours (car (gimp-edit-named-paste calque_sym copier_visible 0)))
		
		(gimp-flip selection_en_cours 0)
		(gimp-flip selection_en_cours 1)

		(gimp-floating-sel-anchor selection_en_cours)
		
		(gimp-selection-none image)

		(plug-in-autocrop-layer 1 image calque_sym)

		; ajuster taille image à la taille des calques
		(gimp-image-resize-to-layers image)
		
		
		
;******************************************************************************************************		
	; calque calque_sym rempli des 4 ailes, les dimensions sont ajustées
;******************************************************************************************************	


	
	
;******************************************************************************************************

		(set! width_2 (car (gimp-drawable-width calque_sym)))
		(set! height_2 (car (gimp-drawable-height calque_sym)))

		(gimp-layer-resize calque_sym (* width_2 2)  (* height_2 2)   (- (/ width_2 2)  1)    (- (/ height_2 2) 1 )  )
		
		; ajuster taille image à la taille des calques
		(gimp-image-resize-to-layers image)

		(gimp-selection-none image)
		


;******************************************************************************************************

			; ailes

			(set! run_mode 1)
			(set! Parameter_1 0.75)
			(set! Parameter_3 -2.6)
			(set! Parameter_4 5.6)
			(set! Parameter_6 6)
			(set! Parameter_8 0)
			(set! Use_cutoff 0)
			(set! r_The_cutoff_radius 0.5)
			(set! dr_The_falloff_radius 0.2)
			(set! The_current_function 5)



		(plug-in-Twist 
			run_mode				;      GIMP_PDB_INT32, run_mode, Interactive, non-interactive
			image					;      GIMP_PDB_IMAGE, image, Input image
			calque_sym				;      GIMP_PDB_DRAWABLE, drawable, Input drawable
			Parameter_1				;      GIMP_PDB_FLOAT, a1, float parameter #1
			Parameter_2_S				;      GIMP_PDB_FLOAT, a2, float parameter #2
			Parameter_3				;      GIMP_PDB_FLOAT, a3, float parameter #3
			Parameter_4				;      GIMP_PDB_FLOAT, a4, float parameter #4
			Parameter_5_S				;      GIMP_PDB_FLOAT, a5, float parameter #5
			Parameter_6				;      GIMP_PDB_FLOAT, a6, float parameter #6
			Parameter_7_S				;      GIMP_PDB_FLOAT, a7, float parameter #7
			Parameter_8				;      GIMP_PDB_FLOAT, a8, float parameter #8
			"unused"				;      GIMP_PDB_STRING , lettering01, lettering #1
			"unused"				;      GIMP_PDB_STRING , lettering02, lettering #2
			"unused"				;      GIMP_PDB_STRING , lettering03, lettering #3
			"unused"				;      GIMP_PDB_STRING , lettering04, lettering #4
			"unused"				;      GIMP_PDB_STRING , lettering05, lettering #5
			"unused"				;      GIMP_PDB_STRING , lettering06, lettering #6
			"unused"				;      GIMP_PDB_STRING , lettering07, lettering #7
			"unused"				;      GIMP_PDB_STRING , lettering08, lettering #8
			Use_cutoff				;      GIMP_PDB_INT32, use_cutoff, Use cutoff or not
			r_The_cutoff_radius		;      GIMP_PDB_FLOAT, r, The cutoff radius
			dr_The_falloff_radius	;      GIMP_PDB_FLOAT, dr, The falloff radius
			The_current_function	;      GIMP_PDB_INT32, current_function, The current function
												; "R(r) - 4th order poly"	0
												; "R(phi) - cosine"			1
												; "Phi(r) - 4th order poly"	2
												; "Phi(phi) - cosine"		3
												; "R(r)Phi(phi)"			4
												; "R(r)R(phi)"				5
												; "Cosines"					6
												; "Gauss"					7
												; "XY 4th Order"			8
			operating_mode			;      GIMP_PDB_INT32, operating_mode ( 0=normal 1=mémorisation des valeurs )
		)


;******************************************************************************************************

	(if (= Display_Filter_Settings 1) ; effets standard , prépositionner les valeurs si les curseurs sont à 0
		(begin
				;; sélectionner la console erreurs pour envoyer un message
				(gimp-message-set-handler 2)

				(if (= The_current_function 0) (set! Fonction_En_Texte "R(r) - 4th order poly" ) )
				(if (= The_current_function 1) (set! Fonction_En_Texte "R(phi) - cosine" ) )
				(if (= The_current_function 2) (set! Fonction_En_Texte "Phi(r) - 4th order poly" ) )
				(if (= The_current_function 3) (set! Fonction_En_Texte "Phi(phi) - cosine" ) )
				(if (= The_current_function 4) (set! Fonction_En_Texte "R(r)Phi(phi)" ) )
				(if (= The_current_function 5) (set! Fonction_En_Texte "R(r)R(phi)" ) )
				(if (= The_current_function 6) (set! Fonction_En_Texte "Cosines" ) )
				(if (= The_current_function 7) (set! Fonction_En_Texte "Gauss" ) )
				(if (= The_current_function 8) (set! Fonction_En_Texte "XY 4th Order" ) )

				(gimp-message 
					(string-append My_Settings "\nParameter 1...8 =\n" 
						(number->string Parameter_1) "\n" 
										(number->string Parameter_2_S) "\n" 
						(number->string Parameter_3) "\n" 
						(number->string Parameter_4) "\n" 
										(number->string Parameter_5_S) "\n" 
						(number->string Parameter_6) "\n" 
										(number->string Parameter_7_S) "\n" 
						(number->string Parameter_8) 
						"\ncutoff r dr function =\n"
						(number->string Use_cutoff) "\n"
						(number->string r_The_cutoff_radius) "\n"
						(number->string dr_The_falloff_radius) "\n"
						Fonction_En_Texte
						"\n--------------------\n"			
					)
				)
		)
	)

	


;******************************************************************************************************

		(gimp-image-remove-layer image drawable)
		
		(set! calque_corps (car (gimp-layer-new-from-visible image image "corps")))
		
		(gimp-image-insert-layer image calque_corps -1 0)
		
		(gimp-layer-add-alpha calque_corps)
		
		(gimp-item-set-visible calque_corps TRUE)
		
		

		; paramétrage complet du filtre (hors image et drawable)
		(set! run_mode 1)

		(set! Parameter_2 -3)
		(set! Parameter_3 1)
		(set! Parameter_4 1)
		(set! Parameter_5 0)
		(set! Parameter_6 0)
		(set! Parameter_7 0)
		(set! Parameter_8 0)
		(set! Use_cutoff 0)
		(set! r_The_cutoff_radius 0.5)
		(set! dr_The_falloff_radius 0.2)
		(set! The_current_function 3)





		(plug-in-Twist 
			run_mode				;      GIMP_PDB_INT32, run_mode, Interactive, non-interactive
			image					;      GIMP_PDB_IMAGE, image, Input image
			calque_corps			;      GIMP_PDB_DRAWABLE, drawable, Input drawable
			Parameter_1_corps						;      GIMP_PDB_FLOAT, a1, float parameter #1
			Parameter_2				;      GIMP_PDB_FLOAT, a2, float parameter #2
			Parameter_3				;      GIMP_PDB_FLOAT, a3, float parameter #3
			Parameter_4				;      GIMP_PDB_FLOAT, a4, float parameter #4
			Parameter_5				;      GIMP_PDB_FLOAT, a5, float parameter #5
			Parameter_6				;      GIMP_PDB_FLOAT, a6, float parameter #6
			Parameter_7				;      GIMP_PDB_FLOAT, a7, float parameter #7
			Parameter_8				;      GIMP_PDB_FLOAT, a8, float parameter #8
			"unused"				;      GIMP_PDB_STRING , lettering01, lettering #1
			"unused"				;      GIMP_PDB_STRING , lettering02, lettering #2
			"unused"				;      GIMP_PDB_STRING , lettering03, lettering #3
			"unused"				;      GIMP_PDB_STRING , lettering04, lettering #4
			"unused"				;      GIMP_PDB_STRING , lettering05, lettering #5
			"unused"				;      GIMP_PDB_STRING , lettering06, lettering #6
			"unused"				;      GIMP_PDB_STRING , lettering07, lettering #7
			"unused"				;      GIMP_PDB_STRING , lettering08, lettering #8
			Use_cutoff				;      GIMP_PDB_INT32, use_cutoff, Use cutoff or not
			r_The_cutoff_radius		;      GIMP_PDB_FLOAT, r, The cutoff radius
			dr_The_falloff_radius	;      GIMP_PDB_FLOAT, dr, The falloff radius
			The_current_function	;      GIMP_PDB_INT32, current_function, The current function
												; "R(r) - 4th order poly"	0
												; "R(phi) - cosine"			1
												; "Phi(r) - 4th order poly"	2
												; "Phi(phi) - cosine"		3
												; "R(r)Phi(phi)"			4
												; "R(r)R(phi)"				5
												; "Cosines"					6
												; "Gauss"					7
												; "XY 4th Order"			8
			operating_mode			;      GIMP_PDB_INT32, operating_mode ( 0=normal 1=mémorisation des valeurs )
		)

;******************************************************************************************************



	(if (= Display_Filter_Settings 1) ; effets standard , prépositionner les valeurs si les curseurs sont à 0
		(begin
				;; sélectionner la console erreurs pour envoyer un message
				(gimp-message-set-handler 2)

				(if (= The_current_function 0) (set! Fonction_En_Texte "R(r) - 4th order poly" ) )
				(if (= The_current_function 1) (set! Fonction_En_Texte "R(phi) - cosine" ) )
				(if (= The_current_function 2) (set! Fonction_En_Texte "Phi(r) - 4th order poly" ) )
				(if (= The_current_function 3) (set! Fonction_En_Texte "Phi(phi) - cosine" ) )
				(if (= The_current_function 4) (set! Fonction_En_Texte "R(r)Phi(phi)" ) )
				(if (= The_current_function 5) (set! Fonction_En_Texte "R(r)R(phi)" ) )
				(if (= The_current_function 6) (set! Fonction_En_Texte "Cosines" ) )
				(if (= The_current_function 7) (set! Fonction_En_Texte "Gauss" ) )
				(if (= The_current_function 8) (set! Fonction_En_Texte "XY 4th Order" ) )

				(gimp-message 
					(string-append My_Settings "\nParameter 1...8 =\n" 
										(number->string Parameter_1_corps) "\n" 
						(number->string Parameter_2) "\n" 
						(number->string Parameter_3) "\n" 
						(number->string Parameter_4) "\n" 
						(number->string Parameter_5) "\n" 
						(number->string Parameter_6) "\n" 
						(number->string Parameter_7) "\n" 
						(number->string Parameter_8) 
						"\ncutoff r dr function =\n"
						(number->string Use_cutoff) "\n"
						(number->string r_The_cutoff_radius) "\n"
						(number->string dr_The_falloff_radius) "\n"
						Fonction_En_Texte
						"\n--------------------\n"			
					)
				)
		)
	)





;******************************************************************************************************

		(if (= Pourcentage_Perspective 0)
			(begin
				;(gimp-message (number->string Pourcentage_Perspective))
			)
			;else
			(begin
			
				(set! width_2 (car (gimp-drawable-width calque_sym)))
				(set! height_2 (car (gimp-drawable-height calque_sym)))

				(set! Perspective ( * (* width_2 0.25) (/ Pourcentage_Perspective 100) ) )
				
				(gimp-item-transform-perspective
					calque_sym ; item				
					(* width_2 0.25) ; x0 UL
					0 ; y0 UL
					(* width_2 0.75) ; x1 UR
					0 ; y1 UR
					(- (* width_2 0.25) Perspective)   ; x2 LL
					height_2 ; y2 LL
					(+ (* width_2 0.75) Perspective)  ; x3 LR
					height_2  ; y3 LR
				)

				(set! width_3 (car (gimp-drawable-width calque_corps)))
				(set! height_3 (car (gimp-drawable-height calque_corps)))

				(set! Perspective ( * (* width_3 0.25) (/ Pourcentage_Perspective 100) ) )
				
				(gimp-item-transform-perspective
					calque_corps ; item				
					(* width_3 0.25) ; x0 UL
					0 ; y0 UL
					(* width_3 0.75) ; x1 UR
					0 ; y1 UR
					(- (* width_3 0.25) Perspective)   ; x2 LL
					height_3 ; y2 LL
					(+ (* width_3 0.75) Perspective)  ; x3 LR
					height_3  ; y3 LR
				)
				
			)
		)




;******************************************************************************************************



		; angle en radians
		(set! Angle ( / (* Angle (* 2 3.14159265358979323846)) 360))
		
		(if (= Inclination_of_the_Y_axis 1)
			(begin

				(gimp-item-transform-matrix 
					calque_corps	; item 
					1	; coeff-0-0 
					Inclination_of_the_Y_axis_0_1	; coeff-0-1
					0	; coeff-0-2 
					0	; coeff-1-0 
					Inclination_of_the_Y_axis_1_1	; 		coeff-1-1 
					0	; coeff-1-2 
					0	; coeff-2-0 
					0	; coeff-2-1 
					1	; coeff-2-2
				)
				
				
				(gimp-item-transform-rotate calque_corps Angle 1 0 0)
			
			)
		)		

		
		
;******************************************************************************************************		


		(if (= Inclination_of_the_Y_axis 1)
			(begin

				(gimp-item-transform-matrix 
					calque_sym	; item 
					1	; coeff-0-0 
					Inclination_of_the_Y_axis_0_1	; coeff-0-1
					0	; coeff-0-2 
					0	; coeff-1-0 
					Inclination_of_the_Y_axis_1_1	; 		coeff-1-1 
					0	; coeff-1-2 
					0	; coeff-2-0 
					0	; coeff-2-1 
					1	; coeff-2-2
				)
				

				
				(gimp-item-transform-rotate calque_sym Angle 1 0 0)
			
			)
		)


		;(plug-in-autocrop-layer 1 image calque_sym)
		;(plug-in-autocrop-layer 1 image calque_corps)

		
		; ajuster taille image à la taille des calques
		(gimp-image-resize-to-layers image)
		


;******************************************************************************************************

		(if (= Flatten 1)
			(begin
			
				(gimp-context-set-background Back_Ground_Color)
				(gimp-image-flatten image)
				(plug-in-autocrop 1 image (car (gimp-image-get-active-layer image)))
			
			)
		)

		
;******************************************************************************************************


		;; rétablir le mode message
		(gimp-message-set-handler message)

		(gimp-context-set-transform-direction old_context_transform_direction )
		(gimp-context-set-interpolation old_context_interpolation)
		(gimp-context-set-transform-recursion old_context_recursion)
		(gimp-context-set-transform-resize old_context_transform_resize)
		(gimp-context-set-background old-bg)
		(gimp-context-set-foreground old-fg)

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


	)
)





(script-fu-register "Butterfly_3_Lylejk_samj"
                    _"<Image>/Filters/Distorts/Butterfly Wings V2 ..."
                    "/Butterfly Wings V2 with twist plug-in."
                    "Lylejk"
                    "Lylejk"
                    "20130323"
                    "RGB*"
                    SF-IMAGE    "Image"     0
                    SF-DRAWABLE "Drawable"  0
					SF-OPTION "Effects" '("A" "B" "C Mosaic" "D Mosaic" "E Whirling & Pinching" "F Pixelize" "G")
					SF-OPTION "Pattern" '("Resize" "Crop" "Translucent Lines")
					SF-OPTION "Creativity" '("Script-Fu" "Interactive Warping" "Convolution Matrix" "Interactive Warping + Convolution Matrix")
					SF-ADJUSTMENT "Parameter 2 a2 float " '(-6.5 -10 -2 0.01 0.1 3 1)
					SF-ADJUSTMENT "Parameter 5 a5 float " '(0.2 -0.22 0.22 0.01 0.1 3 1)
					SF-ADJUSTMENT "Parameter 7 a7 float " '(-0.05 -0.6 -0.02 0.01 0.1 3 1)
					SF-ADJUSTMENT "Parameter Body 1 a1 float " '(-0.45 -1 0 0.01 0.1 3 1)
					SF-OPTION "Operating Mode" '("Normal" "Storing The Parameters")
					SF-TOGGLE "TWIST Display Filter Settings" FALSE
					SF-TOGGLE "Add an alpha channel" TRUE
					SF-TOGGLE "Inclination of the Y-axis" FALSE
					SF-ADJUSTMENT "Inclination of the Y-axis coeff-0-1 " '(-0.09 -1 1 0.00001 0.001 5 1)
					SF-ADJUSTMENT "Inclination of the Y-axis coeff-1-1 " '(0.77 -1 1 0.00001 0.001 5 1)
					SF-ADJUSTMENT "Inclination Angle " '(-30 -45 45 1 10 2 1)
					SF-ADJUSTMENT "Perspective % " '(0 -100 +100 1 10 2 1)
					SF-TOGGLE "Flatten" FALSE
					SF-COLOR "Back Ground Color" '(0 0 0)
					SF-TOGGLE "Random" FALSE
					



)

;; FIN






