#include <mil.h>
#define BUFFERING_SIZE_MAX 22
MIL_INT MFTYPE ProcessingFunction(MIL_INT HookType, MIL_ID HookId,
void* HookDataPtr);
typedef struct
{
MIL_ID MilDigitizer;
MIL_ID MilImageDisp;
MIL_INT ProcessedImageCount;
MIL_TEXT_PTR ChunkDataName;
} HookDataStruct;
int MosMain(void)
{
MIL_ID MilApplication;
MIL_ID MilSystem ;
MIL_ID MilDigitizer ;
MIL_ID MilDisplay ;
MIL_ID MilImageDisp ;
MIL_ID MilGrabBufferList[BUFFERING_SIZE_MAX] = { 0 };
MIL_INT MilGrabBufferListSize;
MIL_INT ProcessFrameCount = 0;
MIL_INT NbFrames = 0, n=0;
MIL_INT BoardType;
MIL_DOUBLE ProcessFrameRate= 0;
MIL_BOOL ChunkModeActive = M_FALSE;
HookDataStruct UserHookData;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay,
&MilDigitizer, &MilImageDisp);
MsysInquire(MilSystem, M_BOARD_TYPE, &BoardType);
if(((BoardType & M_BOARD_TYPE_MASK) != M_GIGE_VISION)&&((BoardType & M_BOARD_TYPE_MASK) != M_USB3_VISION))
{
MosPrintf(MIL_TEXT("This example program can only be used with the Matrox Driver for ")
MIL_TEXT("GigE Vision or USB3 Vision.\n"));
MosPrintf(MIL_TEXT("Please ensure that the default system type is set accordingly in ")
MIL_TEXT("MIL Config.\n"));
MosPrintf(MIL_TEXT("-------------------------------------------------------------\n\n"));
MosPrintf(MIL_TEXT("Press <enter> to quit.\n"));
MosGetch();
MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImageDisp);
return 1;
}
MappControl(M_ERROR, M_PRINT_DISABLE);
MdigInquireFeature(MilDigitizer, M_FEATURE_VALUE, MIL_TEXT("ChunkModeActive"), M_TYPE_BOOLEAN, &ChunkModeActive);
if(MappGetError(M_CURRENT, M_NULL) != M_NULL_ERROR)
{
MosPrintf(MIL_TEXT("Your GigE/USB3 Vision device does not support chunk mode.\n\n"));
MosPrintf(MIL_TEXT("Press <enter> to quit.\n"));
MosGetch();
MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImageDisp);
return 1;
}
MappControl(M_ERROR, M_PRINT_ENABLE);
MappControl(M_ERROR, M_PRINT_DISABLE);
for(MilGrabBufferListSize = 0;
MilGrabBufferListSize<BUFFERING_SIZE_MAX; MilGrabBufferListSize++)
{
MbufAlloc2d(MilSystem,
MdigInquire(MilDigitizer, M_SIZE_X, M_NULL),
MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL),
8+M_UNSIGNED,
M_IMAGE+M_GRAB+M_PROC,
&MilGrabBufferList[MilGrabBufferListSize]);
if (MilGrabBufferList[MilGrabBufferListSize])
{
MbufClear(MilGrabBufferList[MilGrabBufferListSize], 0xFF);
}
else
break;
}
MappControl(M_ERROR, M_PRINT_ENABLE);
for (n=0; n<2 && MilGrabBufferListSize; n++)
{
MilGrabBufferListSize--;
MbufFree(MilGrabBufferList[MilGrabBufferListSize]);
}
UserHookData.MilDigitizer = MilDigitizer;
UserHookData.MilImageDisp = MilImageDisp;
UserHookData.ProcessedImageCount = 0;
UserHookData.ChunkDataName = M_NULL;
MosPrintf(MIL_TEXT("\nEnabling chunk mode.\n"));
MdigControlFeature(MilDigitizer, M_FEATURE_VALUE_AS_STRING, MIL_TEXT("ChunkModeActive"), M_DEFAULT, MIL_TEXT("1"));
MosPrintf(MIL_TEXT("Please select a chunk data to enable.\n"));
MIL_INT ChunkCount = 0;
MIL_TEXT_PTR* ChunkNames = 0;
MdigInquireFeature(MilDigitizer, M_FEATURE_ENUM_ENTRY_COUNT, MIL_TEXT("ChunkSelector"), M_DEFAULT, &ChunkCount);
if (ChunkCount)
{
ChunkNames = new MIL_TEXT_PTR[ChunkCount];
for (MIL_INT i = 0; i < ChunkCount; i++)
{
MIL_INT Len = 0;
MdigInquireFeature(MilDigitizer, M_FEATURE_ENUM_ENTRY_NAME + M_STRING_SIZE + i, MIL_TEXT("ChunkSelector"), M_DEFAULT, &Len);
ChunkNames[i] = new MIL_TEXT_CHAR[Len];
MdigInquireFeature(MilDigitizer, M_FEATURE_ENUM_ENTRY_NAME + i, MIL_TEXT("ChunkSelector"), M_DEFAULT, ChunkNames[i]);
MosPrintf(MIL_TEXT("%20s (%d) %s\n"), MIL_TEXT(""), i, ChunkNames[i]);
}
bool Done = false;
MIL_INT Selection = 0;
do
{
MosPrintf(MIL_TEXT("\nPlease select the event you wish to hook a function to: "));
#if M_MIL_USE_WINDOWS
scanf_s("%ld", &Selection);
#else
scanf("%ld", &Selection);
#endif
if ((Selection >= 0) && (Selection < ChunkCount))
Done = 1;
else
MosPrintf(MIL_TEXT("\nInvalid selection"));
} while (!Done);
MdigControlFeature(MilDigitizer, M_FEATURE_VALUE_AS_STRING, MIL_TEXT("ChunkSelector"), M_DEFAULT, ChunkNames[Selection]);
MdigControlFeature(MilDigitizer, M_FEATURE_VALUE_AS_STRING, MIL_TEXT("ChunkEnable"), M_DEFAULT, MIL_TEXT("1"));
size_t ChunkDataNameLen = MosStrlen(ChunkNames[Selection]) + 6;
UserHookData.ChunkDataName = new MIL_TEXT_CHAR[ChunkDataNameLen];
MosSprintf(UserHookData.ChunkDataName, ChunkDataNameLen,MIL_TEXT("Chunk%s"), ChunkNames[Selection]);
}
MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize,
M_START, M_DEFAULT, ProcessingFunction, &UserHookData);
MosPrintf(MIL_TEXT("Press <Enter> to stop.\n\n"));
MosGetch();
MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize,
M_STOP, M_DEFAULT, ProcessingFunction, &UserHookData);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_COUNT, &ProcessFrameCount);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_RATE, &ProcessFrameRate);
MosPrintf(MIL_TEXT("\n\n%ld frames grabbed at %.1f frames/sec (%.1f ms/frame).\n"),
ProcessFrameCount, ProcessFrameRate, 1000.0/ProcessFrameRate);
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
while(MilGrabBufferListSize > 0)
MbufFree(MilGrabBufferList[--MilGrabBufferListSize]);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImageDisp);
if (ChunkNames)
{
for (MIL_INT i = 0; i < ChunkCount; i++)
{
delete ChunkNames[i];
}
delete ChunkNames;
delete UserHookData.ChunkDataName;
}
return 0;
}
#define STRING_LENGTH_MAX 20
#define STRING_POS_X 20
#define STRING_POS_Y 20
MIL_INT MFTYPE ProcessingFunction(MIL_INT HookType, MIL_ID HookId,
void* HookDataPtr)
{
HookDataStruct *UserHookDataPtr = (HookDataStruct *)HookDataPtr;
MIL_ID ModifiedBufferId;
MIL_TEXT_CHAR Text[STRING_LENGTH_MAX]= {MIL_TEXT('\0'),};
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER+M_BUFFER_ID, &ModifiedBufferId);
MIL_INT64 ChunkData = 0;
MdigInquireFeature(UserHookDataPtr->MilDigitizer, M_FEATURE_VALUE, UserHookDataPtr->ChunkDataName, M_TYPE_INT64, &ChunkData);
MosPrintf(MIL_TEXT("Chunk data: %I64d \r"), ChunkData);
UserHookDataPtr->ProcessedImageCount++;
MosSprintf(Text, STRING_LENGTH_MAX, MIL_TEXT("%ld"),
UserHookDataPtr->ProcessedImageCount);
MgraText(M_DEFAULT, ModifiedBufferId, STRING_POS_X, STRING_POS_Y, Text);
MbufCopy(ModifiedBufferId, UserHookDataPtr->MilImageDisp);
return 0;
}