; Script-Fu butterfly_2.scm
; 
;
;	Copyright (C) 2013 samj ( http://aljacom.com/~gmic/ )
;
;	Licence GPL
;		Ce fichier est le Script-Fu "Butterfly Version 2..." pour Gimp. 
;		Ce Script-Fu "Butterfly Version 2..." 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 Version 2..." 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_2.scm est é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 
;
;	Accès sur l'image par :
;
;	Filtres > Distorsions > Butterfly Version 2...
;	Filters > Distorts > Butterfly Version 2...
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


		
(define	
	(
		butterfly_2-samj
		image
		drawable
		Parameter_1
		Parameter_2
		Parameter_3
		Parameter_4
		Use_cutoff
		r_The_cutoff_radius
		dr_The_falloff_radius
		My_Settings
		Display_Filter_Settings
		Add_an_alpha_channel
	)
	(let* 
		(
			(run_mode 1)
			(Parameter_5 0)
			(Parameter_6 0)
			(Parameter_7 0)
			(Parameter_8 0)
			(The_current_function 0)
			(operating_mode 0)

			;calque_tete
			(calque_tete)
			
			(message (car (gimp-message-get-handler)))
			(Fonction_En_Texte)
			
			(transparence (car (gimp-drawable-has-alpha drawable)))

		)

		(gimp-undo-push-group-start 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)))					
					)
				)

			
			)
		)
			

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

		(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 "\nPart 1\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"			
					)
				)
		)
	)

	

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



		; paramétrage complet du filtre (hors image et drawable)
		(set! run_mode 1)
		(set! Parameter_1 0.1)
		(set! Parameter_2 4)
		(set! Parameter_3 0.1)
		(set! Parameter_4 0)
		(set! Parameter_5 0)
		(set! Parameter_6 0)
		(set! Parameter_7 0)
		(set! Parameter_8 0)
		(set! Use_cutoff 1)
		(set! r_The_cutoff_radius 1)
		(set! dr_The_falloff_radius 0.2)
		(set! The_current_function 1)
		(set! operating_mode 0)




		(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 "\nPart 2\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)		

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

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


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


	(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 "\nPart 3\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)		

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













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




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


	)
)





(script-fu-register "butterfly_2-samj"
                    _"<Image>/Filters/Distorts/Butterfly Version 2..."
                    "Butterfly with twist plug-in."
                    "samj"
                    "samj"
                    "20130320"
                    "RGB*"
                    SF-IMAGE    "Image"     0
                    SF-DRAWABLE "Drawable"  0
					SF-ADJUSTMENT "Parameter 1 a1 float" '(0.32 -10 10 0.01 0.1 3 1)
					SF-ADJUSTMENT "Parameter 2 a2 float" '(0.74 -10 10 0.01 0.1 3 1)
					SF-ADJUSTMENT "Parameter 3 a3 float" '(-1.58 -10 10 0.01 0.1 3 1)
					SF-ADJUSTMENT "Parameter 4 a4 float" '(9.86 -10 10 0.01 0.1 3 1)
					SF-ADJUSTMENT "Use cutoff or not int" '(1 0 1 1 1 0 1)
					SF-ADJUSTMENT "r   The cutoff radius float" '(0.2 0 1 0.01 0.1 3 1)
					SF-ADJUSTMENT "dr  The falloff radius float" '(1 0.01 1 0.01 0.1 3 1)
					SF-STRING "My Settings" "My first filter :"
					SF-TOGGLE "TWIST Display Filter Settings" FALSE
					SF-TOGGLE "Add an alpha channel" TRUE


)

;; FIN






