using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Matrox.MatroxImagingLibrary;
namespace MDigProcess
{
class Program
{
private const int BUFFERING_SIZE_MAX = 20;
public class HookDataStruct
{
public MIL_ID MilDigitizer;
public MIL_ID MilImageDisp;
public int ProcessedImageCount;
};
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDigitizer = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL_ID MilImageDisp = MIL.M_NULL;
MIL_ID[] MilGrabBufferList = new MIL_ID[BUFFERING_SIZE_MAX];
int MilGrabBufferListSize = 0;
MIL_INT ProcessFrameCount = 0;
double ProcessFrameRate = 0;
HookDataStruct UserHookData = new HookDataStruct();
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay,
ref MilDigitizer, ref MilImageDisp);
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_DISABLE);
for (MilGrabBufferListSize = 0; MilGrabBufferListSize < BUFFERING_SIZE_MAX; MilGrabBufferListSize++)
{
MIL.MbufAlloc2d(MilSystem,
MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, MIL.M_NULL),
MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, MIL.M_NULL),
8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC,
ref MilGrabBufferList[MilGrabBufferListSize]);
if (MilGrabBufferList[MilGrabBufferListSize] != MIL.M_NULL)
{
MIL.MbufClear(MilGrabBufferList[MilGrabBufferListSize], 0xFF);
}
else
{
break;
}
}
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_ENABLE);
for (int n = 0; n < 2 && MilGrabBufferListSize > 0; n++)
{
MilGrabBufferListSize--;
MIL.MbufFree(MilGrabBufferList[MilGrabBufferListSize]);
}
Console.WriteLine();
Console.WriteLine("MULTIPLE BUFFERED PROCESSING.");
Console.WriteLine("-----------------------------");
Console.WriteLine();
Console.Write("Press <Enter> to start processing.\r");
MIL.MdigGrabContinuous(MilDigitizer, MilImageDisp);
Console.ReadKey();
MIL.MdigHalt(MilDigitizer);
UserHookData.MilDigitizer = MilDigitizer;
UserHookData.MilImageDisp = MilImageDisp;
UserHookData.ProcessedImageCount = 0;
GCHandle hUserData = GCHandle.Alloc(UserHookData);
MIL_DIG_HOOK_FUNCTION_PTR ProcessingFunctionPtr = new MIL_DIG_HOOK_FUNCTION_PTR(ProcessingFunction);
MIL.MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize, MIL.M_START, MIL.M_DEFAULT, ProcessingFunctionPtr, GCHandle.ToIntPtr(hUserData));
Console.WriteLine("Press <Enter> to stop. ");
Console.WriteLine();
Console.ReadKey();
MIL.MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize, MIL.M_STOP, MIL.M_DEFAULT, ProcessingFunctionPtr, GCHandle.ToIntPtr(hUserData));
hUserData.Free();
MIL.MdigInquire(MilDigitizer, MIL.M_PROCESS_FRAME_COUNT, ref ProcessFrameCount);
MIL.MdigInquire(MilDigitizer, MIL.M_PROCESS_FRAME_RATE, ref ProcessFrameRate);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("{0} frames grabbed at {1:0.0} frames/sec ({2:0.0} ms/frame).", ProcessFrameCount, ProcessFrameRate, 1000.0 / ProcessFrameRate);
Console.WriteLine("Press <Enter> to end.");
Console.WriteLine();
Console.ReadKey();
while (MilGrabBufferListSize > 0)
{
MIL.MbufFree(MilGrabBufferList[--MilGrabBufferListSize]);
}
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImageDisp);
}
private const int STRING_LENGTH_MAX = 20;
private const int STRING_POS_X = 20;
private const int STRING_POS_Y = 20;
static MIL_INT ProcessingFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr)
{
MIL_ID ModifiedBufferId = MIL.M_NULL;
if (!IntPtr.Zero.Equals(HookDataPtr))
{
GCHandle hUserData = GCHandle.FromIntPtr(HookDataPtr);
HookDataStruct UserData = hUserData.Target as HookDataStruct;
MIL.MdigGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ref ModifiedBufferId);
UserData.ProcessedImageCount++;
Console.Write("Processing frame #{0}.\r", UserData.ProcessedImageCount);
MIL.MgraText(MIL.M_DEFAULT, ModifiedBufferId, STRING_POS_X, STRING_POS_Y, String.Format("{0}", UserData.ProcessedImageCount));
MIL.MimArith(ModifiedBufferId, MIL.M_NULL, UserData.MilImageDisp, MIL.M_NOT);
}
return 0;
}
}
}