using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Matrox.MatroxImagingLibrary;
namespace MDigGrabSequence
{
class Program
{
private const string SEQUENCE_FILE = MIL.M_TEMP_DIR + "MilSequence.avi";
private const double GRAB_SCALE = 1.0;
private const int COMPRESSION_Q_FACTOR = 50;
private static readonly MIL_INT FRAME_NUMBER_ANNOTATION = MIL.M_YES;
private static bool SAVE_SEQUENCE_TO_DISK = true;
private const int NB_GRAB_IMAGE_MAX = 22;
public class HookDataObject
{
public MIL_ID MilSystem;
public MIL_ID MilDisplay;
public MIL_ID MilImageDisp;
public MIL_ID MilCompressedImage;
public int NbGrabbedFrames;
public int NbArchivedFrames;
public bool SaveSequenceToDisk;
};
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilRemoteApplication = 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[] MilGrabImages = new MIL_ID[NB_GRAB_IMAGE_MAX];
MIL_ID MilCompressedImage = MIL.M_NULL;
ConsoleKeyInfo Selection = new ConsoleKeyInfo('1', ConsoleKey.D1, false, false, false);
int NbFrames = 0;
int n = 0;
int NbFramesReplayed = 0;
double FrameRate = 0;
double TimeWait = 0;
double TotalReplay = 0;
double GrabScale = GRAB_SCALE;
HookDataObject UserHookData = new HookDataObject();
MIL_INT LicenseModules = 0;
MIL_INT FrameCount = 0;
MIL_INT FrameMissed = 0;
MIL_INT CompressAttribute = 0;
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, ref MilDigitizer, MIL.M_NULL);
MIL.MbufAllocColor(MilSystem,
MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_BAND, MIL.M_NULL),
(MIL_INT)(MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X) * GrabScale),
(MIL_INT)(MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y) * GrabScale),
8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_GRAB + MIL.M_DISP,
ref MilImageDisp);
MIL.MbufClear(MilImageDisp, 0x0);
MIL.MdispSelect(MilDisplay, MilImageDisp);
MIL.MdigControl(MilDigitizer, MIL.M_GRAB_SCALE, GrabScale);
MIL.MdigGrabContinuous(MilDigitizer, MilImageDisp);
Console.WriteLine();
Console.WriteLine("SEQUENCE ACQUISITION:");
Console.WriteLine("--------------------");
Console.WriteLine();
MIL.MsysInquire(MilSystem, MIL.M_OWNER_APPLICATION, ref MilRemoteApplication);
MIL.MappInquire(MilRemoteApplication, MIL.M_LICENSE_MODULES, ref LicenseModules);
if (SAVE_SEQUENCE_TO_DISK && ((LicenseModules & (MIL.M_LICENSE_JPEGSTD | MIL.M_LICENSE_JPEG2000)) != 0))
{
Console.WriteLine("Choose the sequence format:");
Console.WriteLine("1) Uncompressed images.");
if ((LicenseModules & MIL.M_LICENSE_JPEGSTD) != 0)
{
Console.WriteLine("2) Compressed images with a lossy JPEG algorithm.");
}
if ((LicenseModules & MIL.M_LICENSE_JPEG2000) != 0)
{
Console.WriteLine("3) Compressed images with a lossy JPEG 2000 algorithm.");
}
Selection = Console.ReadKey();
}
else
{
Console.WriteLine("Press <Enter> to record images.");
Console.ReadKey();
}
switch (Selection.Key)
{
case ConsoleKey.NumPad1:
case ConsoleKey.D1:
case ConsoleKey.Enter:
Console.WriteLine();
Console.WriteLine("Recording uncompressed images...");
Console.WriteLine();
CompressAttribute = MIL.M_NULL;
break;
case ConsoleKey.NumPad2:
case ConsoleKey.D2:
Console.WriteLine();
Console.WriteLine("Recording JPEG images...");
Console.WriteLine();
CompressAttribute = MIL.M_COMPRESS + MIL.M_JPEG_LOSSY;
break;
case ConsoleKey.NumPad3:
case ConsoleKey.D3:
Console.WriteLine();
Console.WriteLine("Recording JPEG 2000 images...");
Console.WriteLine();
CompressAttribute = MIL.M_COMPRESS + MIL.M_JPEG2000_LOSSY;
break;
default:
Console.WriteLine();
Console.WriteLine("Invalid selection !.");
Console.WriteLine();
Console.WriteLine("Using uncompressed images.");
Console.WriteLine();
CompressAttribute = MIL.M_NULL;
while (Console.KeyAvailable)
{
Console.ReadKey();
}
break;
}
if (CompressAttribute != MIL.M_NULL)
{
MIL.MbufAllocColor(MilSystem,
MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_BAND, MIL.M_NULL),
(int)(MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, MIL.M_NULL) * GrabScale),
(int)(MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, MIL.M_NULL) * GrabScale),
8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + CompressAttribute,
ref MilCompressedImage);
MIL.MbufControl(MilCompressedImage, MIL.M_Q_FACTOR, COMPRESSION_Q_FACTOR);
}
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_DISABLE);
for (NbFrames = 0, n = 0; n < NB_GRAB_IMAGE_MAX; n++)
{
MIL.MbufAllocColor(MilSystem,
MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_BAND, MIL.M_NULL),
(int)(MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, MIL.M_NULL) * GrabScale),
(int)(MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, MIL.M_NULL) * GrabScale),
8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_GRAB,
ref MilGrabImages[n]);
if (MilGrabImages[n] != MIL.M_NULL)
{
NbFrames++;
MIL.MbufClear(MilGrabImages[n], 0xFF);
}
else
{
break;
}
}
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_ENABLE);
for (n = 0; n < 2 && NbFrames > 0; n++)
{
NbFrames--;
MIL.MbufFree(MilGrabImages[NbFrames]);
}
MIL.MdigHalt(MilDigitizer);
if (SAVE_SEQUENCE_TO_DISK)
{
Console.WriteLine("Saving the sequence to an AVI file...");
Console.WriteLine();
MIL.MbufExportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, MIL.M_DEFAULT, MIL.M_OPEN);
}
UserHookData.MilSystem = MilSystem;
UserHookData.MilDisplay = MilDisplay;
UserHookData.MilImageDisp = MilImageDisp;
UserHookData.MilCompressedImage = MilCompressedImage;
UserHookData.SaveSequenceToDisk = SAVE_SEQUENCE_TO_DISK;
UserHookData.NbGrabbedFrames = 0;
UserHookData.NbArchivedFrames = 0;
GCHandle UserHookDataHandle = GCHandle.Alloc(UserHookData);
MIL_DIG_HOOK_FUNCTION_PTR UserHookFunctionDelegate = new MIL_DIG_HOOK_FUNCTION_PTR(ArchiveFunction);
MIL.MdigProcess(MilDigitizer, MilGrabImages, NbFrames, SAVE_SEQUENCE_TO_DISK ? MIL.M_START : MIL.M_SEQUENCE, MIL.M_DEFAULT, UserHookFunctionDelegate, GCHandle.ToIntPtr(UserHookDataHandle));
Console.WriteLine("Press <Enter> to continue.");
Console.WriteLine();
Console.ReadKey(true);
MIL.MdigProcess(MilDigitizer, MilGrabImages, NbFrames, MIL.M_STOP, MIL.M_DEFAULT, UserHookFunctionDelegate, GCHandle.ToIntPtr(UserHookDataHandle));
UserHookDataHandle.Free();
MIL.MdigInquire(MilDigitizer, MIL.M_PROCESS_FRAME_COUNT, ref FrameCount);
MIL.MdigInquire(MilDigitizer, MIL.M_PROCESS_FRAME_RATE, ref FrameRate);
MIL.MdigInquire(MilDigitizer, MIL.M_PROCESS_FRAME_MISSED, ref FrameMissed);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("{0} frames archived ({1} missed), at {2:0.0} frames/sec ({3:0.0}ms/frame).", UserHookData.NbArchivedFrames, FrameMissed, FrameRate, 1000.0 / FrameRate);
if (SAVE_SEQUENCE_TO_DISK)
{
MIL.MbufExportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, FrameRate, MIL.M_CLOSE);
}
if (UserHookData.NbArchivedFrames > 0)
{
do
{
if (SAVE_SEQUENCE_TO_DISK)
{
Console.WriteLine();
Console.WriteLine("Playing back sequence from the AVI file...");
Console.WriteLine();
Console.WriteLine("Press <Enter> to end.");
Console.WriteLine();
Console.WriteLine();
MIL.MbufDiskInquire(SEQUENCE_FILE, MIL.M_NUMBER_OF_IMAGES, ref FrameCount);
MIL.MbufDiskInquire(SEQUENCE_FILE, MIL.M_FRAME_RATE, ref FrameRate);
MIL.MbufDiskInquire(SEQUENCE_FILE, MIL.M_COMPRESSION_TYPE, ref CompressAttribute);
MIL.MbufImportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_OPEN);
}
TotalReplay = 0.0;
NbFramesReplayed = 0;
for (n = 0; n < FrameCount; n++)
{
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET, MIL.M_NULL);
if (SAVE_SEQUENCE_TO_DISK)
{
MIL.MbufImportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_LOAD, MIL.M_NULL, ref MilImageDisp, n, 1, MIL.M_READ);
NbFramesReplayed++;
Console.Write("Frame #{0} \r", NbFramesReplayed);
}
else
{
MIL.MbufCopy(MilGrabImages[n], MilImageDisp);
NbFramesReplayed++;
Console.Write("Frame #{0} \r", NbFramesReplayed);
}
if (Console.KeyAvailable && (n >= (NB_GRAB_IMAGE_MAX - 1)))
{
Console.ReadKey(true);
break;
}
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref TimeWait);
TotalReplay += TimeWait;
TimeWait = (1 / FrameRate) - TimeWait;
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_WAIT, ref TimeWait);
TotalReplay += (TimeWait > 0) ? TimeWait : 0.0;
}
if (SAVE_SEQUENCE_TO_DISK)
{
MIL.MbufImportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_CLOSE);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("{0} frames replayed, at a frame rate of {1:0.0} frames/sec ({2:0.0} ms/frame).", NbFramesReplayed, n / TotalReplay, 1000.0 * TotalReplay / n);
Console.WriteLine();
Console.WriteLine("Press <Enter> to end (or any other key to playback again).");
Console.WriteLine();
}
while (Console.ReadKey(true).Key != ConsoleKey.Enter);
}
MIL.MbufFree(MilImageDisp);
for (n = 0; n < NbFrames; n++)
{
MIL.MbufFree(MilGrabImages[n]);
}
if (MilCompressedImage != MIL.M_NULL)
{
MIL.MbufFree(MilCompressedImage);
}
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MIL.M_NULL);
}
private const int STRING_LENGTH_MAX = 20;
private const int STRING_POS_X = 20;
private const int STRING_POS_Y = 20;
static MIL_INT ArchiveFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr)
{
GCHandle HookDataHandle = GCHandle.FromIntPtr(HookDataPtr);
HookDataObject UserHookDataPtr = HookDataHandle.Target as HookDataObject;
MIL_ID ModifiedImage = 0;
MIL.MdigGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ref ModifiedImage);
UserHookDataPtr.NbGrabbedFrames++;
if (FRAME_NUMBER_ANNOTATION == MIL.M_YES)
{
MIL.MgraText(MIL.M_DEFAULT, ModifiedImage, STRING_POS_X, STRING_POS_Y, UserHookDataPtr.NbGrabbedFrames.ToString());
}
if (UserHookDataPtr.MilCompressedImage != MIL.M_NULL)
{
MIL.MbufCopy(ModifiedImage, UserHookDataPtr.MilCompressedImage);
}
if (UserHookDataPtr.SaveSequenceToDisk)
{
MIL_ID ImageToExport;
if (UserHookDataPtr.MilCompressedImage != MIL.M_NULL)
{
ImageToExport = UserHookDataPtr.MilCompressedImage;
}
else
{
ImageToExport = ModifiedImage;
}
MIL.MbufExportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, ref ImageToExport, 1, MIL.M_DEFAULT, MIL.M_WRITE);
UserHookDataPtr.NbArchivedFrames++;
Console.Write("Frame #{0} \r", UserHookDataPtr.NbArchivedFrames);
}
MIL.MbufCopy(ModifiedImage, UserHookDataPtr.MilImageDisp);
return 0;
}
}
}