disappear !

Share your advanced PureBasic knowledge/code with the community.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

disappear !

Post by dobro »

a small code to simulate the corrector pencil of Photoshop
(or Affinity Photo:))

a demonstration video of this code is available here
https://www.youtube.com/watch?v=n2Wr3FB ... e=youtu.be


basically, we cover the part of the image we want to make disappear.
Pressing the H (horizontal) key will disappear using the left and right of the red zone.

Pressing the V (vertical) key will disappear using what is above and below the red zone

the code :

Code: Select all

;***********************************************
;Titre  :*disappear
;Auteur  : Zorro (Dobro )
;Date  :17/06/2016
;Heure  :17:18:16
;Version Purebasic :  PureBasic 5.42 LTS (Windows - x86)
;Version de l'editeur :EPB V2.62
; Libairies necessaire : Aucune 
;***********************************************
InitMouse()
InitKeyboard()
InitJoystick()
UseJPEGImageDecoder()
UsePNGImageDecoder()
Enumeration
		#fenetre
		#Police
		#curseur
		#image
		#event_L
		#event_H
EndEnumeration
Structure raster
		x.l
		Y.l
		sensx.l
		sensy.l
EndStructure
Dim raster.raster(100)
;***********Declaration Procedures *******
Declare disappear(Mode.s,img_num)
; ***********************************
Global Radius=16
Resultat = InitSprite()
FontID = LoadFont(#Police, "arial", 18, #PB_Font_Bold )
file_image$=OpenFileRequester("ouvre une image",GetCurrentDirectory() ,"*.bmp|*.jpg|*.png",1)
If file_image$<>""
		LoadImage(#image, file_image$)
Else
		End
Endif
ResizeImage(#image,1024,768)
;
WindowID = OpenWindow(#fenetre, 0, 0,1024,768,  "Disappear",#PB_Window_SystemMenu|#PB_Window_BorderLess |#PB_Window_ScreenCentered ) 
Result = OpenWindowedScreen(WindowID(#fenetre) ,0,0, 1027, 768, 1, 0,0)

Repeat
		ExamineMouse() :ExamineKeyboard()
		WaitWindowEvent(2)  
		xm=MouseX()
		ym=MouseY()
		
		;************ KEYBOARD *****************
		If MouseButton(#PB_MouseButton_Left)
				; dessin  
				FLag_mask=#True
				StartDrawing( ImageOutput(#Image))
						Circle(xm,ym, Radius,RGB(255,13,0)) 
				StopDrawing() 
		EndIf 
		if KeyboardPushed(#PB_Key_Escape)
				End
		Endif
		if KeyboardPushed(#PB_Key_H)
				Mode.s="H"
				Disappear(Mode,#image)
		Endif
		
		if KeyboardPushed(#PB_Key_V)
				Mode.s="V"
				Disappear(Mode,#image)
		Endif
		
		
		if KeyboardPushed(#PB_Key_Add)
				Radius=Radius+2
				If Radius>300:Radius=300:Endif
		Endif
		if KeyboardPushed(#PB_Key_Subtract)
				Radius=Radius-2
				If Radius<1:Radius=1:Endif
		Endif
		; ****************************************
		StartDrawing(ScreenOutput())
				DrawImage(ImageID(#Image), 0, 0) 
				Circle(xm,ym, Radius ,RGB(255,13,0)) 
		StopDrawing() 
		
		FlipBuffers():; affiche l'ecran
		ClearScreen(RGB(0, 0, 0)) :;efface l'ecran
Until Event=#PB_Event_CloseWindow 
;
Procedure Disappear(mode.s,img_num)
		;By Zorro (Dobro )
		; la procedure Magique !
		mem_posy_dep=0
		mem_posx_dep=0
		mem_posx_arr=0
		mem_posy_arr=0
		compteur2=0
		Haut_im=ImageHeight(img_num)
		Larg_im=ImageWidth(img_num)
		
		Select mode
				Case "V"
				StartDrawing( ImageOutput(img_num))
						; ************** Vertical *****************************************************
						For x=0 to Larg_im-1
								Flag=0:compteur1=0 :compteur2=0:finy=0:starty=0
								For y=0 to Haut_im-1
										coul=point(x,y)
										if coul=rgb(255,13,0)
												Starty=y ; retient la position de depart de la zone rouge sur la colone  en cours
												;calcul la hauteur de la zone mask de la ligne en cours
												While  coul=rgb(255,13,0)
														if Starty+compteur2<Haut_im-1
																coul=point(x,Starty+compteur2) ; verifie qu'on est toujours sur le mask
																compteur2=compteur2+1 ; << ça va etre notre largeur de travail
														Else
																Break
														Endif
												Wend ;<
												finy=Starty+compteur2; retient la position de fin de zone rouge pour la ligne en cours !
												compteur1=compteur2/2
												compteur2=0
												if Starty-compteur1>=0 ;
														For y1=Starty-compteur1 to Starty
																coul=point(x,y1); lit la couleur avant le debut de la zone rouge
																Plot(x,y1+compteur1,coul) ; dessine le point en cours avec la nouvelle couleur
														Next y1
												Endif
												For y1=finy to finy+compteur1
												If Y1<Haut_im
														coul=point(x,y1); lit la couleur apres  la zone rouge
														Plot(x,y1-compteur1,coul) ; dessine le point en cours avec la nouvelle couleur
														Endif
												Next y1
										Endif
								Next y
						Next x
				StopDrawing() 
				Case "H"
				StartDrawing( ImageOutput(img_num))
						; *************************************************************************************
						
						; ************** Horizontal *****************************************************
						For y=0 to Haut_im-1
								Flag=0:compteur1=0 :compteur2=0:finx=0:startx=0
								For x=0 to Larg_im-1
										coul=point(x,y)
										if coul=rgb(255,13,0)
												Startx=x ; retient la position de depart de la zone rouge sur la ligne en cours
												;calcul la largeur de la zone mask de la ligne en cours
												While  coul=rgb(255,13,0)
														if Startx+compteur2<Larg_im-1
																coul=point(Startx+compteur2,y) ; verifie qu'on est toujours sur le mask
																compteur2=compteur2+1 ; << ça va etre notre largeur de travail
														Else
																Break
														Endif
												Wend ;<
												finx=Startx+compteur2; retient la position de fin de zone rouge pour la ligne en cours !
												compteur1=compteur2/2
												compteur2=0
												if Startx-compteur1>=0 ;
														For x1=Startx-compteur1 to Startx
																coul=point(x1,y); lit la couleur avant le debut de la zone rouge
																Plot(x1+compteur1,y,coul) ; dessine le point en cours avec la nouvelle couleur
														Next x1
												Endif
												For x1=finx to finx+compteur1		
												If X1	<Larg_im-1
														coul=point(x1,y); lit la couleur  apres la zone rouge														
														Plot(x1-compteur1,y,coul) ; dessine le point en cours avec la nouvelle couleur
														Endif
												Next x1
										Endif
								Next x
						Next y
				StopDrawing() 
				; *************************************************************************************
		EndSelect
Endprocedure


; Epb


Last edited by dobro on Sun Nov 12, 2017 9:54 am, edited 9 times in total.
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: disappear !

Post by dobro »

I made a correction to the code
I limited it in plot and point, in principle it should not crash any more, if the red zone
is too important

to remove eventual artifacts, simply use several times.
the[V] or[H] key
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: disappear !

Post by davido »

@dobro,
Very good,
Works well on both my Macbook and Windows 10 PC.
Thank you for sharing :D
DE AA EB
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: disappear !

Post by dobro »

Thanks :)


I changed the system. (Code edited )
now it works much better, cleaner as a resul :D

ImageImage
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: disappear !

Post by kvitaliy »

@dobro
Cool!
Thank you for sharing :D
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: disappear !

Post by dobro »

Thanks :)

Hi. Hi,
Re-edited code
I added the keys[+] and[-] to change the brush size. :)

[Reedit]
Fixed 2 bugs , If you draw too close to the left or right edge of the image :oops:
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: disappear !

Post by Dude »

Very awesome code! :shock: Thank you for sharing! :D
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: disappear !

Post by Michael Vogel »

Cool idea, well done!

For notebooks without extra '+' and '-' keys:

Code: Select all

#Stepper=1; for fast machines

If KeyboardPushed(#PB_Key_Add) Or KeyboardPushed(#PB_Key_P)
	Radius=Radius+#Stepper
	If Radius>300:Radius=300:EndIf
EndIf
If KeyboardPushed(#PB_Key_Subtract) Or KeyboardPushed(#PB_Key_M)
	Radius=Radius-#Stepper
	If Radius<1:Radius=1:EndIf
EndIf
Another enhancement would be an automatic mode, here's a simple version...

Code: Select all

; at the top
Global xMax,xMin
Global yMax,yMin

Procedure resetMinMax()
	xMax=0 : xMin=9999
	yMax=0 : yMin=9999
EndProcedure

; keyboard routine
If MouseButton(#PB_MouseButton_Left)
		; dessin
		FLag_mask=#True
		StartDrawing( ImageOutput(#Image))
		Circle(xm,ym, Radius,RGB(255,13,0))
		StopDrawing()
		If xm>xMax : xMax=xm : EndIf
		If xm<xMin : xMin=xm : EndIf
		If ym>yMax : yMax=ym : EndIf
		If ym<yMin : yMin=ym : EndIf
	EndIf
	If KeyboardPushed(#PB_Key_Escape)
		End
	EndIf
	If KeyboardPushed(#PB_Key_H)
		Disappear("H",#image)
	EndIf
	If KeyboardPushed(#PB_Key_V)
		Disappear("V",#image)
	EndIf
	If KeyboardPushed(#PB_Key_A)
		Disappear("A",#image)
	EndIf

; main function (insert the lines before the original code)
Procedure Disappear(mode.s,img_num)

	If mode="A"
		If xMax-xMin>yMax-yMin
			Mode="V"
		Else
			Mode="H"
		EndIf
	EndIf
	resetMinMax()

	;By Zorro (Dobro )
	:
	:
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: disappear !

Post by RSBasic »

Image Image Image
Image
Image
Post Reply