using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MDigDoubleBuffering
{
public class UserDataObject
{
public MIL_INT NbGrabStart;
}
class Program
{
const int STRING_LENGTH_MAX = 20;
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[] MilImage = new MIL_ID[2];
MIL_ID MilImageDisp = MIL.M_NULL;
MIL_ID Default = MIL.M_DEFAULT;
int NbProc = 0;
int n = 0;
double Time = 0.0;
StringBuilder Text = new StringBuilder("0", STRING_LENGTH_MAX);
UserDataObject userObject = new UserDataObject();
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, ref MilDigitizer, ref MilImageDisp);
for (n = 0; n < 2; n++)
{
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 MilImage[n]);
}
userObject.NbGrabStart = 0;
GCHandle userObjectHandle = GCHandle.Alloc(userObject);
MIL_DIG_HOOK_FUNCTION_PTR grabStartDelegate = new MIL_DIG_HOOK_FUNCTION_PTR(GrabStart);
MIL.MdigHookFunction(MilDigitizer, MIL.M_GRAB_START, grabStartDelegate, GCHandle.ToIntPtr(userObjectHandle));
Console.WriteLine();
Console.WriteLine("DOUBLE BUFFERING ACQUISITION AND PROCESSING:");
Console.WriteLine("--------------------------------------------");
Console.WriteLine();
Console.WriteLine("Press <Enter> to stop.");
Console.WriteLine();
MIL.MdigControl(MilDigitizer, MIL.M_GRAB_MODE, MIL.M_ASYNCHRONOUS);
MIL.MdigGrab(MilDigitizer, MilImage[0]);
n = 0;
do
{
MIL.MdigGrab(MilDigitizer, MilImage[1 - n]);
if (NbProc == 0)
{
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, MIL.M_NULL);
}
MIL.MgraText(Default, MilImage[n], 32, 32, string.Format("{0}", NbProc + 1));
MIL.MimArith(MilImage[n], MIL.M_NULL, MilImageDisp, MIL.M_NOT);
NbProc++;
n = 1 - n;
} while (!Console.KeyAvailable);
MIL.MdigGrabWait(MilDigitizer, MIL.M_GRAB_END);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref Time);
Console.ReadKey();
Console.WriteLine("{0} frames grabbed, at a frame rate of {1:0.00} frames/sec ({2:0.00} ms/frame).", NbProc, NbProc / Time, 1000.0 * Time / NbProc);
Console.WriteLine("Press <Enter> to end.");
Console.WriteLine();
Console.ReadKey();
MIL.MdigHookFunction(MilDigitizer, MIL.M_GRAB_START + MIL.M_UNHOOK, grabStartDelegate, GCHandle.ToIntPtr(userObjectHandle));
userObjectHandle.Free();
for (n = 0; n < 2; n++)
{
MIL.MbufFree(MilImage[n]);
}
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImageDisp);
}
private static MIL_INT GrabStart(MIL_INT HookType, MIL_ID EventId, IntPtr UserObjectPtr)
{
if (UserObjectPtr != IntPtr.Zero)
{
GCHandle userObjectHandle = GCHandle.FromIntPtr(UserObjectPtr);
UserDataObject userData = userObjectHandle.Target as UserDataObject;
if (userData != null)
{
userData.NbGrabStart++;
Console.Write("#{0}\r", userData.NbGrabStart);
}
}
return(0);
}
}
}