This example (demoPrograms\engine\VC#\pointer or demoPrograms\engine\VB\pointer) demonstrates how to assign and retrieve data object pointers directly between WiT and a user application.

Direct Data Pointer Passing
In this example, when you click Run, image data allocated inside the .Net application is sent to WiT Engine and assigned to a WiT data object, and image data produced by WiT Engine is passed to user application and processed by the user application.
Data is passed from the user application to WiT Engine by using the readObj operator. Data is passed from WiT Engine to the user application by using the display operator.

Direct Data Pointer Passing Igraph
The SetInputHandleCallback2 is used to tell WiT Engine to call inputProc whenever any readObj operator with its filename parameter set to ‘sample’ is created. Inside the inputProc, the user can allocate memory for an image and assign this pointer to be used directly by WiT Engine by using SetObjMdData.
One of the display windows is mapped to the name ‘image’ as usual to display image in user application. Another display ‘stats’ is mapped by SetOutputHandleCallback to pass the WiT object handle to the outputProc callback. This object handle is used to retrieve the image data pointer such as its width, height and pixel type using GetObjMdData. Finally FreeObj is called to free this object data.
[C#]
WiT.engine.SetDisplayWnd("image", PictureBox1.Handle);
inHandleCallback = new WiT.engine.inputHandleCB2(inputProc);
freeDataCallback = new WiT.engine.freeInputDataCB(freetProc);
WiT.engine.SetInputHandleCallback2("sample", inHandleCallback, freeDataCallback);
outHandleCallback = new WiT.engine.outputHandleCB(outputProc);
WiT.engine.SetOutputHandleCallback("stats", outHandleCallback);
[Visual Basic]
WiT.engine.SetDisplayWnd("image", PictureBox1.Handle)
inHandleCallback = New WiT.engine.inputHandleCB2(AddressOf inputProc)
freeDataCallback = New WiT.engine.freeInputDataCB(AddressOf freeProc)
WiT.engine.SetInputHandleCallback2("sample", inHandleCallback, freeDataCallback)
outHandleCallback = New WiT.engine.outputHandleCB(AddressOf outputProc)
WiT.engine.SetOutputHandleCallback("stats", outHandleCallback)
The function inputProc processes the data handle it receives from WiT. The handle can be used to assign user allocated memory directly to data objects used by WiT Engine. The function freeProc is used to free the memory allocated in the user application:
[C#]
private void freeProc(IntPtr data)
{
Marshal.FreeHGlobal(data);
}
private int inputProc(string name, IntPtr handle)
{
if (name != "sample") return 1;
unsafe
{
int width = 256;
int height = 256;
byte * data = (byte *)Marshal.AllocHGlobal(width * height);
for (int i = 0;i < width;++i)
for (int j = 0;j < height;++j)
data[i *width + j] = (byte)j;
WiT.engine.SetObjMdData(handle,
(int)WiT.ObjectType.IMAGE, (int)WiT.ObjectType.UBYTE, 256, 256, (IntPtr)data);
}
return 1;
}
[Visual Basic]
Private Sub freeProc(ByVal data As IntPtr)
Marshal.FreeHGlobal(data)
End Sub
Public Function inputProc(ByVal name As String, ByVal handle As IntPtr) As Integer
If name <> "sample" Then
Return 1
End If
Dim width As Integer = 256
Dim height As Integer = 256
Dim size As Integer = width * height
Dim data As IntPtr = Marshal.AllocHGlobal(size)
Dim i As Integer
Dim j As Integer
Dim val(size) As Byte
Dim pos As Integer = 0
For i = 0 To width - 1 Step 1
For j = 0 To height - 1 Step 1
val(pos) = CType(j, Byte)
pos += 1
Next
Next
Marshal.Copy(val, 0, data, size)
WiT.engine.SetObjMdData(handle, _
CType(WiT.ObjectType.IMAGE, Integer), _
CType(WiT.ObjectType.UBYTE, Integer), width, height, data)
Return 1
End Function
The function outputProc processes the data handle it receives from WiT Engine. This handle can be used to retrieve WiT data memory pointer using GetObjMdData. FreeObj must be used to free the reference handle when it is no longer needed:
[C#]
private int outputProc(string name, IntPtr reference, IntPtr handle)
{
if (name != "stats")
{
WiT.engine.FreeObj(reference);
return 1;
}
int type = 0, dataType = 0, width = 0, height = 0;
IntPtr data = IntPtr.Zero;
if (WiT.engine.GetObjMdData(handle, ref type, ref dataType,
ref width, ref height, ref data) == 0)
{
statusBar1.Text = "Data is neither image nor vector";
WiT.engine.FreeObj(reference);
return 0;
}
string dType = "Unknown";
if (dataType == (int)WiT.ObjectType.UBYTE) dType = "Unsigned Byte";
if (type == (int)WiT.ObjectType.IMAGE)
{
statusBar1.Text =
string.Format("Image: {0}x{1}, Type: {2}", width, height, dType);
}
else if (type == (int)WiT.ObjectType.VECTOR)
{
statusBar1.Text = string.Format("Vector: {0}, Type: {1}", width, dType);
}
WiT.engine.FreeObj(reference);
return 1;
}
[Visual Basic]
Public Function outputProc(ByVal name As String, ByVal reference As IntPtr, _
ByVal handle As IntPtr) As Integer
If name <> "stats" Then
WiT.engine.FreeObj(reference)
Return 1
End If
Dim type As Integer = 0
Dim dataType As Integer = 0
Dim width As Integer = 0
Dim Height As Integer = 0
Dim data As IntPtr = IntPtr.Zero
If WiT.engine.GetObjMdData(handle, type, dataType, width, Height, data) _
= 0 Then
statusBar1.Text = "Data is neither image nor vector"
WiT.engine.FreeObj(reference)
Return 0
End If
Dim dType As String = "Unknown"
If dataType = CType(WiT.ObjectType.UBYTE, Integer) Then
dType = "Unsigned Byte"
End If
If type = CType(WiT.ObjectType.IMAGE, Integer) Then
statusBar1.Text = String.Format("Image: {0}x{1}, Type: {2}", _
width, Height, dType)
ElseIf type = CType(WiT.ObjectType.VECTOR, Integer) Then
statusBar1.Text = String.Format("Vector: {0}, Type: {1}", _
width, dType)
End If
WiT.engine.FreeObj(reference)
Return 1
End Function
A mapping for readObj can be unregistered at any time by calling SetInputHandleCallback2 with a value of IntPtr.Zero for the callback parameter.