Page 1 of 1

Deformer

Posted: Sat Jun 14, 2014 1:04 pm
by dobro
just a test
ImageImage

( at boot ... Wait a loadimage in Table )

order initially, was to make the pixels of an image, liquid :)


Click and Move Mouse :)

if you cancel the loading of an image,
is that on the Net that will be taken




Code: Select all

;***********************************************
;Titre  :*test_liquide4(deformer)
;Auteur  : Dobro
;Date  :14/06/2014
;Heure  :20:03:55
;Version Purebasic :  PureBasic 5.22 LTS (Windows - x86)
;Version de l'editeur :EPB V2.54
; Libairies necessaire : Aucune 
;***********************************************

UseJPEGImageDecoder()
Declare  dessin(taille_pinceau)
Declare  Action(oo)
Declare  mouse_Sens(vitesse.f)
Declare.l Loadimage_net(Adr$,nom_image$)
Declare.l event_palette_gadget(num) ; event_palette_gadget(numero du gadget)
Declare  Palette_gadget(num,x,y,larg,haut);Palette_gadget(numero du gadget,x,y,largeur,hauteur)
Declare  create_image(image)
Declare  mise_en_tableau(image,mode)

;
Enumeration
	#win
	#screen
Endenumeration
Enumeration
	#pinceau
EndEnumeration
; les gadgets
Enumeration
	#Text_pinceau
	#String_pinceau
	#Text_Vitesse
	#String_vitesse
	#Load_image
	#Save_image
	#Avertissement
	#Quit
	#palette
	#coche_dessin
	#efface
EndEnumeration

InitSprite()
InitMouse()
InitKeyboard()
;- les Structures
Structure element
	Id.l
	x.l
	y.l
	dep_x.f
	dep_y.f
	coul.l
	
EndStructure
global Dim element.element(800*600)
Global image=1
Global vit_y.f,vit_x.f
Global no_bouge=0
; ************ modifiable **********
Global taille_pinceau.f=30
Global vitesse.f=0.8; >= a 0.6 ; c'est la vitesse de deplacement du liquide
Global Frein.f=vitesse.f/4 ; le frein c'est le temp que va mettre le liquide a s'arreter plus le frein est petit par rapport a la vitesse , plus ça glisse
; *******************************
Global Bouton
global image_larg
global image_haut
global taille
Global x_pinceau.f
Global y_pinceau.f
Global total_i
Global Dessin.b=0
Global couleur_dessin.l
i=0
DisableDebugger
if OpenWindow(#win,10,10,1024,768,"Deformer.. Press ESC for Quit")
	;-Gadgets
	x_gadget=810
	y_gadget=50
	TextGadget(#Text_pinceau, x_gadget, y_gadget,110,35, "Taille_pinceau")
	StringGadget(#String_pinceau, x_gadget+115, y_gadget, 40, 25, "30")
	y_gadget+50
	TextGadget(#Text_Vitesse, x_gadget, y_gadget,110,35, "Vitesse (0.6<->2.0")
	StringGadget(#String_Vitesse, x_gadget+115, y_gadget, 40, 25, "0.8")
	y_gadget+50
	ButtonGadget(#Load_image, x_gadget, y_gadget, 120, 35, "Load image")
	y_gadget+40
	ButtonGadget(#Save_image, x_gadget, y_gadget, 120, 35, "Save image")
	;
	ButtonGadget(#quit, x_gadget+120, 10, 80, 35, "Quit")
	y_gadget+50
	ButtonGadget(#efface,x_gadget, y_gadget, 120, 35, "Clear image")
	
	;-screen
	if OpenWindowedScreen(WindowID(#win),1,1,800,600,0,1,1)
		CreateSprite(#pinceau,8,8,#PB_Sprite_PixelCollision)
		StartDrawing(SpriteOutput(#pinceau))
			circle(4,4,4,rgb(0,128,0))
		StopDrawing()
	Endif
	MouseLocate(810, 20)
Endif




;-boucle principale
Repeat
	ExamineKeyboard()
	ExamineMouse()
	;;Repeat ; beurk !
	Event =WaitWindowEvent(2)
	if MouseButton(#PB_MouseButton_Left)
		Bouton=1
		Else
		Bouton=0
	Endif
	x_pinceau.f=mousex()
	y_pinceau.f=mousey()
	;
	
	Select Event
		
		
		Case #PB_Event_Gadget
		select EventGadget() 
			Case #quit
			End
			Case #Load_image
			if isimage(image)
				FreeImage(image)
				Else
				;MessageRequester("erreur","pas d'image a virer ! ")
			endif
			image$=OpenFileRequester("open Jpg","c:\","*.jpg|*.JPG|*.jpeg|*.bmp",0)
			if image$=""
				Adr$="http://lapeniche.net/wp-content/uploads/2013/12/Chat.jpg" ; image a afficher qui viens du Net
				nom_image$="Chat.jpg"
				image=Loadimage_net(Adr$,nom_image$)
				Else
				LoadImage(image,image$)
			Endif
			ResizeImage(image,800,600) ; pour faire entrer dans la fenetre
			if IsImage(image)
				mise_en_tableau(image,0)
				
				Else
				MessageRequester("info","erreur pas d'image")
			Endif
			;- Affichage image
			StartDrawing(screenoutput())
				DrawImage(ImageID(image),0,0)
			StopDrawing()
			FlipBuffers()
			
			Case #Save_image
			file_image$=SaveFileRequester("Save JPG","c:\","*.jpg|*.JPG",0)
			SaveImage(image,file_image$,#PB_ImagePlugin_JPEG )
			Case #String_pinceau
			taille_pinceau.f=Val(GetGadgetText(#String_pinceau))
			if taille_pinceau.f<0:taille_pinceau.f=1:endif
			if taille_pinceau.f>800:taille_pinceau.f=800:endif
			Case #String_Vitesse
			vitesse.f=ValF(GetGadgetText(#String_Vitesse))
			if vitesse.f<0.6:vitesse.f=0.6:Endif
			If vitesse.f>2.0:vitesse.f=2.0:endif
			
			Case #efface
			ClearStructure(@Element, element)
			FreeImage(Image)
			ClearScreen($0)
			FlipBuffers()
		Endselect
		Case #PB_Event_CloseWindow
		End
	EndSelect
	;;;Until Event = 0
	;***********************
	;- Affichage image
	
	if IsImage(image)<>0
		Startdrawing(ImageOutput(image))
			;DrawingMode(#PB_2DDrawing_AlphaBlend )
			Action(oo)
		StopDrawing()
		Else
		
	Endif
	
	
	;- Affichage image
	StartDrawing(screenoutput())
		DrawImage(ImageID(image),0,0)
	StopDrawing()
	
	;- Affichage Pinceau
	mouse_Sens(vitesse.f) ; donne le sens de deplacement du pinceau , et place la vitesse constante dans vit_x.f  et vit_y.f
	if WindowMouseX(#win)<800 and WindowMouseY(#win)<600 ; on est dans l'ecran ??? 
		ReleaseMouse(0) ; oui
		DisplayTransparentSprite(#pinceau,MouseX(),MouseY())
		FlipBuffers()
		ClearScreen(RGB(0, 0, 0))
		Else ; non
		ReleaseMouse(1)  
	Endif
until  KeyboardPushed(#PB_Key_Escape)




Procedure Action(oo)
	; By Dobro
	;calldebugger
	;- Dessin_image
	For i=1 to total_i-1
		if element(i)\x>= x_pinceau-taille_pinceau and element(i)\x<=x_pinceau+taille_pinceau and element(i)\y>= y_pinceau-taille_pinceau and element(i)\y<=y_pinceau+taille_pinceau  and bouton=1  and no_bouge=0; on est sur le pixel
			;- Action
			
			;*********** Action bouton ***************
			; en fonction du sens du pinceau
			if Vit_x.f>0 ; vers la droite
				
				if element(i)\x>= x_pinceau-taille_pinceau and element(i)\x<=x_pinceau+taille_pinceau
					element(i)\dep_x.f=vit_x.f
				Endif
			Endif
			if Vit_x.f<0 ; vers la gauche
				if element(i)\x>= x_pinceau-taille_pinceau and element(i)\x<=x_pinceau+taille_pinceau
					element(i)\dep_x.f=vit_x.f
				Endif
			Endif
			if Vit_y.f>0 ; vers le bas
				if element(i)\y>= y_pinceau-taille_pinceau and element(i)\y<=y_pinceau+taille_pinceau
					element(i)\dep_y.f=vit_y.f
				Endif
			Endif
			if Vit_y.f<0 ; vers le haut
				if element(i)\y>= y_pinceau-taille_pinceau and element(i)\y<=y_pinceau+taille_pinceau
					element(i)\dep_y.f=vit_y.f
				Endif
			Endif
			
			; ***************************************
		Endif
		; ************************************
		
		element(i)\x=element(i)\x+element(i)\dep_x.f
		element(i)\y=element(i)\y+element(i)\dep_y.f
		
		if element(i)\dep_x.f>0
			element(i)\dep_x.f=element(i)\dep_x.f-Frein.f
		Endif
		If element(i)\dep_y.f>0
			element(i)\dep_y.f=element(i)\dep_y.f-Frein.f
		Endif
		
		if element(i)\dep_x.f<0
			element(i)\dep_x.f=element(i)\dep_x.f+Frein.f
		Endif
		If element(i)\dep_y.f<0
			element(i)\dep_y.f=element(i)\dep_y.f+Frein.f
		Endif
		
		
		if element(i)\dep_x.f<0:element(i)\dep_x.f=0:endif
		if element(i)\dep_y.f<0:element(i)\dep_y.f=0:endif
		;******** depassements *****
		if element(i)\x> image_larg-1
			element(i)\x=0
		Endif
		if element(i)\x< 0
			element(i)\x=image_larg-1
		Endif
		;
		if element(i)\y> image_haut-1
			element(i)\y=0
		Endif
		if element(i)\y< 0
			element(i)\y=image_haut-1
		Endif
		; ***************************
		plot(element(i)\x,element(i)\y,element(i)\coul.l)
		
	Next i
	bouton=0
	no_bouge=0
EndProcedure
;;***************
;-zone procedure
Procedure mouse_Sens(vitesse.f)
	; By Dobro
	Static mem_posx,mem_posy ,deltax,deltay
	posx=mousex()
	posy=mousey()
	deltax=MouseDeltaX()
	deltay=MouseDeltaY()
	if posx>mem_posx
		vit_x.f=vitesse.f
	Endif
	if posx<mem_posx
		vit_x.f=-vitesse.f
	Endif
	if posy>mem_posy
		vit_y.f=vitesse.f
	Endif
	if posy<mem_posy
		vit_y.f=-vitesse.f
	Endif
	if posy=mem_posy
		vit_y.f=0
	Endif
	if posx=mem_posx
		vit_x.f=0
	Endif
	
	if posx=mem_posx and posy=mem_posy
		no_bouge=1
		Else
		no_bouge=0
	Endif
	mem_posx=posx
	mem_posy=posy
	
	if (deltax<-2 or deltax>2) or (deltay<-2 or deltay>2)
		no_bouge=0
		Else
		no_bouge=1
	Endif
	
EndProcedure
Procedure.l Loadimage_net(Adr$,nom_image$)
	image=1
	; By Dobro
	;Adr$=adresse du fichier image a charger
	;nom_image$ = nom qu'on donne a cette image
	; la procedure renvoi l'Id de l'image recupéré
	InitNetwork()
	If ReceiveHTTPFile(Adr$, GetTemporaryDirectory()+nom_image$)
		if isimage(image)
			FreeImage(image)
		Endif
		LoadImage(image, GetTemporaryDirectory()+nom_image$)
		DeleteFile(GetTemporaryDirectory()+nom_image$)
		Debug "image récupérée !"
		ProcedureReturn image
		Else
		Debug "Erreur"
	EndIf
EndProcedure



Procedure mise_en_tableau(image,mode)
	
	image_larg=ImageWidth(image)
	image_haut=ImageHeight(image)
	taille=(image_larg * image_haut)
	;;Redim  element.element(taille)
	; mise en tableau
	i=0:zoo=1
	
	For y=1 to Image_haut-1
		Startdrawing(ImageOutput(image))
			For x=1 to image_larg-1
				if mode=0 ; mode loadimage
					coul=point(x,y)
					Else
					coul=rgbA(255,255,255,255)
				endif
				element(i)\id=i
				element(i)\x=x
				element(i)\y=y
				element(i)\dep_x.f=0
				element(i)\dep_y.f=0
				element(i)\coul.l=coul
				i=i+1
			Next x
		StopDrawing()
		StartDrawing(screenoutput())
			st$="/,/,/,/,/,/,/,/-,-,-,-,-,-,-,-,\,\,\,\,\,\,\,\,|,|,|,|,|,|,|,|" 
			DrawText(400,300,"in loading "+stringfield(st$,zoo,","))
			zoo=zoo+1 :if zoo=32:zoo=1:endif
		StopDrawing()
		FlipBuffers()
		ClearScreen($0)
	Next y
	total_i=i
Endprocedure

; Epb



Re: Deformer

Posted: Sat Jun 14, 2014 4:38 pm
by dobro
Code updated :)
Henceforth, I oblige to brush the image faster to have an effect
brush slowly, no effect :)


This allows better control of the deformation

Re: Deformer

Posted: Sun Jun 15, 2014 5:10 am
by VB6_to_PBx
Code updated :)
Henceforth, I oblige to brush the image faster to have an effect
brush slowly, no effect :)


This allows better control of the deformation
i like the changes you made !!!

if you could add more Code to SAVE the image once the User modified it
that would be great also !

Re: Deformer

Posted: Sun Jun 15, 2014 12:27 pm
by dobro
thanks
I plan to add gadgets :)

Re: Deformer

Posted: Mon Jun 16, 2014 2:38 pm
by dobro
@Fred , je suis bien obligé pour faire remonter le sujet entre 2 changements du code .... ;)

Code updated, adding Gadgets :)

if you cancel the loading of an image,
is that on the Net that will be taken (the Cat )