; Script-Fu Butterfly_Wings_by_Lylejk.scm
; Ce filtre est une adaptation du didacticiel "Butterfly Wings" http://gimpchat.com/viewtopic.php?f=23&t=2519  créé par Lylejk
; Ce script comporte du code issu du script-fu "script-fu-amazing-circles"  créé par Harry Phillips Mar. 23 2007 (GPL 3)
;
;	Copyright (C) 2013 samj ( http://aljacom.com/~gmic/ )
;
;	Licence GPL
;		Ce fichier est le Script-Fu "Butterfly Wings by Lylejk...." pour Gimp. 
;		Ce Script-Fu "Butterfly Wings by Lylejk...." 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 Wings by Lylejk...." 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_Wings_by_Lylejk.scmest édité avec Notepad++    http://notepad-plus-plus.org/
;
;	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 by Lylejk....
;	Filters > Distorts > Butterfly Wings by Lylejk....
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Merci à Lylejk et Harry Phillips  :o)
;

		
(define	
	(
		Butterfly_Wings_by_Lylejk
		image
		drawable
		Pattern_Crop_Resize
		Parameter_2
		Parameter_5
		Parameter_7
		operating_mode
		Display_Filter_Settings
		Add_an_alpha_channel
		Resize_Butterfly_Wings
	)
	(let* 
		(
			(run_mode 1)
			(Parameter_1 0.75)
			(Parameter_3 -2.6)
			(Parameter_4 5.6)
			(Parameter_6 6)
			(Parameter_8 0)
			(Use_cutoff 0)
			(r_The_cutoff_radius 0.5)
			(dr_The_falloff_radius 0.2)
			(The_current_function 5)

			(message (car (gimp-message-get-handler)))
			(Fonction_En_Texte)
			( My_Settings "Butterfly Wings Settings")
			;calque_tete
			(calque_tete)
			
			(transparence (car (gimp-drawable-has-alpha drawable)))
			
			(width (car (gimp-image-width image)))
			(height (car (gimp-image-height image)))
			
			(dimension_offset)

		)

		(gimp-undo-push-group-start image)

		(gimp-selection-none image)
		
		(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)))					
					)
				)

			
			)
		)
			

;******************************************************************************************************
		
		; créer une image carrée
		(if (= width height)
			()
			;else
			(begin
				(if (= Pattern_Crop_Resize 0)
					(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)
					)
				)
			)
		)



		;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)

		
		(set! dimension_offset  ( / (car (gimp-image-width image)) 2 )  )
		
		(gimp-drawable-offset drawable 1 1 dimension_offset dimension_offset)

		;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 (= 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"			
					)
				)
		)
	)

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


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





		(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 (= transparence 1) ; canal alpha
			(begin
				(plug-in-autocrop-layer 1 image drawable)				
			)
		)


		(if (= Resize_Butterfly_Wings 1) ; resize
			(begin
				(gimp-layer-scale (car (gimp-image-get-active-layer image))  width height 0)			
			)
		)		
	
		
		; ajuster taille image à la taille des calques
		(gimp-image-resize-to-layers image)





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


					
		; copier le calque calque_fond
		(set! calque_tete (car (gimp-layer-new-from-visible image image "calque_tete")))
					
		; ajouter le calque calque_fond_2
		(gimp-image-insert-layer image calque_tete 0 -1)		

		
		
		; mettre le calque en mode addition si pas de canal alpha
		(if (= transparence 0) (gimp-layer-set-mode calque_tete 7) )
		

		; paramétrage complet du filtre (hors image et drawable)
		(set! run_mode 1)
		(set! Parameter_1 0.8)
		(set! Parameter_2 1)
		(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)
		(set! operating_mode 0)




		(plug-in-Twist 
			run_mode				;      GIMP_PDB_INT32, run_mode, Interactive, non-interactive
			image					;      GIMP_PDB_IMAGE, image, Input image
			calque_tete				;      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 )
		)





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








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


	)
)





(script-fu-register "Butterfly_Wings_by_Lylejk"
                    _"<Image>/Filters/Distorts/Butterfly Wings by Lylejk...."
                    "Butterfly with twist plug-in."
                    "Lylejk"
                    "Lylejk"
                    "20130320"
                    "RGB*"
                    SF-IMAGE    "Image"     0
                    SF-DRAWABLE "Drawable"  0
					SF-OPTION "Pattern" '("Crop" "Resize")
					SF-ADJUSTMENT "Parameter 2 a1 float" '(-6.5 -10 -2 0.01 0.1 3 1)
					SF-ADJUSTMENT "Parameter 5 a2 float" '(0.2 -0.22 0.22 0.01 0.1 3 1)
					SF-ADJUSTMENT "Parameter 7 a3 float" '(-0.05 -0.6 -0.02 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 "Resize Butterfly Wings" FALSE


)

;; FIN






