Page 1 of 1

SuperPoint()

Posted: Tue Jan 04, 2011 5:23 pm
by dobro
in french:
Serai t'il envisageable d'avoir une fonction
superpoint(x_res,Y_res)

qui lise le contenue de la surface de dessin en cours
en une seule passe
et charge automatiquement un tableau

par exemple :

Code: Select all

dim toto(640,480) ; pour une image de cette résolution
StartDrawing(SpriteOutput(#sprite2))
@toto()=point(640,480) ; mettrai les pixels de l'image en cours (640,480) (La Matrice complete) direct dans le tableau !!!!
Stopdrawing()

Google translate
You it will be possible to have a function
Superpoint (x_res, Y_res)

that reads the contents of the current drawing surface
in one pass
and automatically loads a table

For example:

Code: Select all

dim toto(640,480) ; for an image of that resolution
StartDrawing(SpriteOutput(#sprite2))
@toto()=point(640,480) ; put the pixels in the current frame (640.480) (The Complete Matrix) directly in the table!!

?????? :)

Re: SuperPoint()

Posted: Tue Jan 04, 2011 7:31 pm
by Trond
Can't this be done with DrawingBuffer()?

Re: SuperPoint()

Posted: Wed Jan 05, 2011 12:43 am
by dobro
Thank you;

I will say that a table pre-filled Sun; is easier to handle than DrawingBuffer () ..
the address of a pixel is: *Line.Pixel = Buffer+Pitch*y
not very easy to manipulate is not it?

while the use of a array pixel(x, y) is conventional:)
for rotations, translations, color shifts, etc. ....

especially if these manipulations are done in real time has
a movie;)
(I work on a project to transform 2D film to film anaglyph) and
(2 films and left / right to movie anaglyph)

my algorithms are working already, but using point() / plot () is very slow
(one picture / 8 seconds!)

one can always find an alternative
This is what makes the strength of PureBasic

but an additional function, which facilitates this kind of work
will in my opinion, a better ..

in french
Merci ;

je dirai qu'un tableau Dim pré-remplis ; est plus facile a manipuler que DrawingBuffer()..
l'adresse d'un pixel est : *Line.Pixel = Buffer+Pitch*y
pas tres facile a manipuler n'est-ce pas ?

alors que l'utilisation d'un tableau pixel(x,y) est conventionnel :)
pour les rotations, les translations,les décalages de couleurs etc ....

surtout si ces manipulations sont a faire en temps réel
sur un Film par exemple ;)
(je travail sur un projet de transformation de film 2D en film anaglyphes)
(et 2 films gauche/droite en film anaglyphes )
mes algorithmes fonctionnent deja, mais en utilisant Point()//plot() c'est tres lent
(une image /8 secondes !!)

on peut toujours trouver une solution de remplacement
c'est ce qui fait la force de Purebasic

mais une fonction de plus , qui faciliterai ce genre de travail
serai a mon sens, un mieux ..

my project : Pure Anaglyphe only for image
2 modes :
mode1: image 2D left/right to Anaglyphe
mode2: one image 2D + Deph Mask to anaglyphe

http://michel.dobro.free.fr/download.php?view.10

Re: SuperPoint()

Posted: Wed Jan 05, 2011 4:00 am
by PureLust
Hi dobro,

the Proc "GrabArrayFromSprite()" in the following code grabs a sprite into an Array, as you've requested.
Because it's quite slippy, it might be of some use for you:

Image

(On my WinXP-System with a GForce 8600GTS I get around 330 FPS (including the BGR->RGB conversion (which is needed on my system) and without plotting the grabbed Data to the demo-image).)

Code: Select all

EnableExplicit

#SprWidth  = 320			; <= Just change the Sprite-Dimensions
#SprHeight = 240			;    to the size you want.

Dim myPixel.l(0,0)

InitSprite()
Define x, y, StartTime, Frames, Event

Procedure ConvertArrayPixelFormat(Array Points.l(2))
	
	Protected Width  = ArraySize(Points(),2)
	Protected Height = ArraySize(Points(),1)
	Protected x,y, col
	For x = 0 To Width
		For y = 0 To Height
			col = Points(y,x)
			Points(y,x) = (col & $FF00FF00) | ((col & $FF) << 16) | ((col & $FF0000) >> 16)
		Next
	Next
EndProcedure			
	
Procedure GrabArrayFromSprite(Sprite, Array Points.l(2), TargetFormat = #PB_PixelFormat_32Bits_RGB)
	
	Protected ReturnValue = #False
	Protected PixelFormat
	
	If IsSprite(Sprite)
		If StartDrawing(SpriteOutput(Sprite))
			PixelFormat = DrawingBufferPixelFormat()
			If PixelFormat = #PB_PixelFormat_32Bits_BGR Or PixelFormat = #PB_PixelFormat_32Bits_RGB
				
				Protected Width   = SpriteWidth(Sprite)
				Protected Height  = SpriteHeight(Sprite)
				Protected dbWidth = DrawingBufferPitch() >> 2
				
				If ArraySize(Points(),1) <> Height-1 Or ArraySize(Points(),2) <> dbWidth-1
					Dim Points(Height-1,dbWidth-1)
				EndIf
				
				CopyMemory(DrawingBuffer(), @Points(0,0), (dbWidth << 2) * Height)
				
				If TargetFormat <> PixelFormat
					ConvertArrayPixelFormat(Points())
				EndIf
				
				ReturnValue = #True
				
			Else
				
				Debug "Unsupported Pixelformat"
				
			EndIf
			
			StopDrawing()
			
		EndIf
	Else
		Debug "Sprite Nr. " + Str(Sprite) + " does not exist."
	EndIf
	
	ProcedureReturn ReturnValue
	
EndProcedure



If OpenWindow(0,0,0,#SprWidth*2+10,#SprHeight + 30,"", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	If OpenWindowedScreen(WindowID(0), 0, 0, #SprWidth, #SprHeight, 0, 0, 0 , #PB_Screen_NoSynchronization)
		
		If CreateImage(0, #SprWidth, #SprHeight) And CreateSprite(0,#SprWidth, #SprHeight); , #PB_Sprite_Memory)
			
			ImageGadget(0,#SprWidth + 10, 0, #SprWidth, #SprHeight, ImageID(0))
			CheckBoxGadget(1, #SprWidth - 70, #SprHeight + 5, 150, 20, "plot grabbed Array to Image")
			SetGadgetState(1,#True)
			
			StartTime = ElapsedMilliseconds()-1
			
			Repeat
				
				; ----- 1st, draw circles into the Demo-Sprite
				
				If StartDrawing(SpriteOutput(0))
					For x = 1 To 30
						Circle(Random(#SprWidth), Random(#SprHeight), Random(100)+10, Random($bbbbbb)+$222222)
					Next
					StopDrawing()
				EndIf
				
				; ----- 2nd, Grab the Sprite to the Array
				
				If Not GrabArrayFromSprite(0, myPixel())
					Debug "Cannot grab Array from Sprite."
					End
				EndIf
				
				; ----- 3rd, plot grabbed Data onto the Demo-Image, if CheckBox is checked
				
				If GetGadgetState(1)
					
					If StartDrawing(ImageOutput(0))
						For x = 0 To #SprWidth-1
							For y = 0 To #SprHeight-1
								Plot(x,y, myPixel(y,x))
							Next
						Next
						StopDrawing()
					EndIf
					
					SetGadgetState(0, ImageID(0))
					
				EndIf
			
				; ----- 4th, display FPS-Info in Window-Title
				
				Frames + 1
				SetWindowTitle(0, "GrabArrayFromSprite() Demo - "+Str(Frames * 1000 / (ElapsedMilliseconds() - StartTime))+" Frames/Sec")
				
				; ----- 5th, display Sprite on Screen to compare Original and Copy
				
				DisplaySprite(0,0,0)
				FlipBuffers()
				
				Repeat
					Event = WindowEvent()
					If Event = #PB_Event_Gadget And EventGadget() = 1 : StartTime = ElapsedMilliseconds()-1 : Frames = 0 : EndIf
					If Event = #PB_Event_CloseWindow : Break 2 : EndIf
				Until Not Event
			ForEver
		EndIf
	EndIf
EndIf
The only Drawback you have to deal with is, that the Array-Coordinates are Pixel(y,x) instead of Pixel(x,y).
I hope you can use it anyway.

And don't forget to turn of the debugger if you test it. :wink:

Greetz, PL.

Re: SuperPoint()

Posted: Wed Jan 05, 2011 9:05 am
by dobro
whaaaooooo!

awesome!

fred must include this in PureBasic native! : D

Many Thanks:)

Re: SuperPoint()

Posted: Wed Jan 05, 2011 1:30 pm
by Thorium
Actualy, you can use an array without copying the image data.
Just use pointers and adjust the width of the array to match the image pitch. For example: Width = Pitch / 4.
So you can easiely access all pixels, just ignore pixels outside the true image width.

That might only work on 32bit depth, because of alignment.

Re: SuperPoint()

Posted: Fri Jan 07, 2011 2:51 am
by PureLust
Thorium wrote:Actualy, you can use an array without copying the image data.
Just use pointers and adjust the width of the array to match the image pitch.
I also thought about to realize it with pointers.
But wouldn't you loose the the DC after StopDrawing(), which stops you from accessing the Sprite-Data via Pointers?

So, (relating to my example above) ... how would you realize the plotted copy to the Image (which needs to open a new DC by using StartDrawing()) with this Pointer-Method?

Re: SuperPoint()

Posted: Fri Jan 07, 2011 1:30 pm
by dobro
In your code,

Code: Select all

Protected Width   = SpriteWidth(Sprite)
Protected Height  = SpriteHeight(Sprite)
Protected dbWidth = DrawingBufferPitch() >> 2
I just added:
Protected buffer= DrawingBuffer()
Protected Width = SpriteWidth(Sprite)
Protected Height = SpriteHeight(Sprite)
Protected dbWidth = DrawingBufferPitch() >> 2

;)

Thank you again :)