ESCAPI 2.0

Miscellaneous Forums/General Discussion/ESCAPI 2.0

I found this http://forums.indiegamer.com/showthread.php?t=9619
http://iki.fi/sol/code.html
and I think that it could be useful...
Now the interesting thing: as it is a .Dll, there is someone that can create a wrapper to it and *TEACH ME/US* how to make it? The DLL has a few commands so - I think - it should be quite easy to convert/create the wrapper.
Byez

have u test it ? how much fps ???

I have used only the supplied example and it works. I don't know how many frames I can do, I think it depends by the webcam hardware.
The problem is I cannot create a wrap for this library because I don't know how do it:D; the author claims his source as free under GPL license.

This works here... I've made the functions more Blitz-y but still need to wrap it up a little more nicely.

Thanks to Freak from the PureBasic forums!

(You need escapi20.zip from http://sol.gfxile.net/code.html and you should place this code in the same folder as escapi.dll.)

Type SimpleCapParams
	Field mTargetBuf:Byte Ptr ' Must be at least mWidth * mHeight * SizeOf(Int) of size! 
	Field mWidth
	Field mHeight
End Type

Global InitCOM () "C"
Global CountCaptureDevices () "C"
Global OpenCaptureDevice (device, scp:Byte Ptr) "C"
Global CloseCaptureDevice (device) "C"
Global GetCapture (device) "C"
Global CaptureDone (device) "C"
Global CaptureDeviceName (device, name:Byte Ptr, namelen) "C"
Global ESCAPIDLLVersion () "C"

Function SetupESCAPI ()

	esc = LoadLibraryA ("escapi.dll")
	
	If esc
	
		InitCOM					= GetProcAddress (esc, "initCOM")
		CountCaptureDevices		= GetProcAddress (esc, "countCaptureDevices")
		OpenCaptureDevice 		= GetProcAddress (esc, "initCapture")
		CloseCaptureDevice		= GetProcAddress (esc, "deinitCapture")
		GetCapture				= GetProcAddress (esc, "doCapture")
		CaptureDone				= GetProcAddress (esc, "isCaptureDone")
		CaptureDeviceName		= GetProcAddress (esc, "getCaptureDeviceName")
		ESCAPIDLLVersion		= GetProcAddress (esc, "ESCAPIDLLVersion")

		If InitCOM = Null Or CountCaptureDevices = Null Or OpenCaptureDevice = Null or..
			CloseCaptureDevice = Null Or GetCapture = Null Or CaptureDone = Null or..
				CaptureDeviceName = Null Or ESCAPIDLLVersion = Null
					DebugLog "Function missing!"
					Return 0
		EndIf

		InitCOM ()
		
		If ESCAPIDLLVersion () < $200
			DebugLog "Old DLL (needs version 2.0+)"
			Return 0
		EndIf
		
	Else
		DebugLog "Failed to open DLL"
		Return 0
	EndIf

	Return 1
	
End Function

Function CaptureDevice$ (device)
	Local cam:Byte [1024]
	CaptureDeviceName (device, cam, 1024)
	Return String.FromCString (cam)
End Function

If SetupESCAPI () = 0
	Notify "Error! Make sure escapi.dll is in same folder and capture device plugged in!"
	End
EndIf

For num = 0 Until CountCaptureDevices ()
	Print "Capture device [" + num + "] name: " + CaptureDevice (num)
Next

device = 0

' To list/select devices...

	'Repeat
	'	device = Int (Input ("Enter capture device number: "))
	'Until device > -1 And device < CountCaptureDevices ()

' Preferred target width/height (ESCAPI scales capture data to this)...

width = 320
height = 240

AppTitle = "Using " + CaptureDevice (device) + "..."

Graphics width, height', 32

' Data structure...

Local scp:SimpleCapParams = New SimpleCapParams

' A bank for captured data...

capture:TBank = CreateBank (width * height * 4)

' Stick bank memory pointer into data structure...

scp.mTargetBuf	= LockBank (capture)
scp.mWidth		= width
scp.mHeight		= height

' Store *integer* pointer to pixel data (for reading as integers)...

Local bufferstart:Int Ptr = Int Ptr (scp.mTargetBuf)

' This is used for 'current' RGB pointer in main loop...

Local rgbptr:Int Ptr

' Start capture process...

If OpenCaptureDevice (device, scp) = 0
	Print "Failed to initialise capture device!"
	End
EndIf

Repeat

	Cls

	GetCapture (device)
	
	Repeat
		If KeyHit (KEY_ESCAPE) Then quit = True
	Until CaptureDone (device)

	For y = 0 Until scp.mHeight
		For x = 0 Until scp.mWidth

			rgbptr = bufferstart + (y * scp.mWidth + x)	' Move pointer to data offset
			rgb = rgbptr [0]							' Read integer value

			' Split into RGB...
			
			r = (rgb Shr 16) & $FF
			g = (rgb Shr 8) & $FF
			b = rgb & $FF

			SetColor r, g, b
			Plot x, y
			
		Next
	Next

	Flip

Until quit = True

' Stop capture process...
	
CloseCaptureDevice (device)

' Unlock bank access...

UnlockBank (capture)

End


... and using PixMaps instead, which draw much more quickly (4-5 ms versus 90-100 ms for the above version, on Core 2 Duo E6300).

Try changing width and height to 640/480 or 1024/768, and the Graphics line to "Graphics width, height, 32" for full-screen fun.

Type SimpleCapParams
	Field mTargetBuf:Byte Ptr ' Must be at least mWidth * mHeight * SizeOf(Int) of size! 
	Field mWidth
	Field mHeight
End Type

Global InitCOM () "C"
Global CountCaptureDevices () "C"
Global OpenCaptureDevice (device, scp:Byte Ptr) "C"
Global CloseCaptureDevice (device) "C"
Global GetCapture (device) "C"
Global CaptureDone (device) "C"
Global CaptureDeviceName (device, name:Byte Ptr, namelen) "C"
Global ESCAPIDLLVersion () "C"

Function SetupESCAPI ()

	esc = LoadLibraryA ("escapi.dll")
	
	If esc
	
		InitCOM					= GetProcAddress (esc, "initCOM")
		CountCaptureDevices		= GetProcAddress (esc, "countCaptureDevices")
		OpenCaptureDevice 		= GetProcAddress (esc, "initCapture")
		CloseCaptureDevice		= GetProcAddress (esc, "deinitCapture")
		GetCapture				= GetProcAddress (esc, "doCapture")
		CaptureDone				= GetProcAddress (esc, "isCaptureDone")
		CaptureDeviceName		= GetProcAddress (esc, "getCaptureDeviceName")
		ESCAPIDLLVersion		= GetProcAddress (esc, "ESCAPIDLLVersion")

		If InitCOM = Null Or CountCaptureDevices = Null Or OpenCaptureDevice = Null or..
			CloseCaptureDevice = Null Or GetCapture = Null Or CaptureDone = Null or..
				CaptureDeviceName = Null Or ESCAPIDLLVersion = Null
					DebugLog "Function missing!"
					Return 0
		EndIf

		InitCOM ()
		
		If ESCAPIDLLVersion () < $200
			DebugLog "Old DLL (needs version 2.0+)"
			Return 0
		EndIf
		
	Else
		DebugLog "Failed to open DLL"
		Return 0
	EndIf

	Return 1
	
End Function

Function CaptureDevice$ (device)
	Local cam:Byte [1024]
	CaptureDeviceName (device, cam, 1024)
	Return String.FromCString (cam)
End Function

If SetupESCAPI () = 0
	Notify "Error! Make sure escapi.dll is in same folder and capture device plugged in!"
	End
EndIf

For num = 0 Until CountCaptureDevices ()
	Print "Capture device [" + num + "] name: " + CaptureDevice (num)
Next

device = 0

' To list/select devices...

	'Repeat
	'	device = Int (Input ("Enter capture device number: "))
	'Until device > -1 And device < CountCaptureDevices ()

' Preferred target width/height (ESCAPI scales capture data to this)...

width = 320
height = 240

AppTitle = "Using " + CaptureDevice (device) + "..."

Graphics width, height', 32

' Data structure...

Local scp:SimpleCapParams = New SimpleCapParams

' A PixMap for captured data...

pix:TPixmap = CreatePixmap (width, height, PF_BGRA8888)

' Stick pixmap memory pointer into data structure...

scp.mTargetBuf	= PixmapPixelPtr (pix)
scp.mWidth		= width
scp.mHeight		= height

' Start capture process...

If OpenCaptureDevice (device, scp) = 0
	Print "Failed to initialise capture device!"
	End
EndIf

Repeat

	Cls

	GetCapture (device)
	
	Repeat
		If KeyHit (KEY_ESCAPE) Then quit = True
	Until CaptureDone (device)

	DrawPixmap pix, 0, 0

	Flip

Until quit = True

' Stop capture process...
	
CloseCaptureDevice (device)

End


BTW The code isn't GPL'd -- the readme says:


Copyright (c)2007 Jari Komppa

The DLL is free for anyone to use for any purpose as long as you don't hold me responsible for anything. It would be nice if you'd toss me a mail if you play with this thing.



... so you don't have to release your own source code. He only quotes the LGPL because he distributes the SDL library with one of his examples.

Ohhhh you are a God!!!
Many thanks!
(sorry for the misunderunderstanding about the license)