#include "MultiComponentGrab.h"
#define BUFFERING_SIZE 20
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n\n"));
MosPrintf(MIL_TEXT("MultiComponentGrab\n\n"));
MosPrintf(MIL_TEXT("[SYNOPSIS]\n\n"));
MosPrintf(
MIL_TEXT("This example demonstrates how to interface a multi-component\n")\
MIL_TEXT("device using MIL multi-component buffer containers.\n")\
MIL_TEXT("\nPress <Enter> to start.\n\n")\
);
}
MIL_INT MFTYPE ProcessingFunction(MIL_INT HookType, MIL_ID HookId, void* HookDataPtr);
void PrintComponentInfo(MIL_INT ComponentCount, MIL_INT ComponentNb, const ComponentData& ComponentInfo);
void FreeDisplayData(ComponentDataList& Components);
typedef struct
{
MIL_ID MilDigitizer;
ComponentDataList DisplayList;
MIL_INT ProcessedCount;
} HookDataStruct;
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDigitizer,
MilContainers[BUFFERING_SIZE] = { 0 };
MIL_INT MilContainerCount = 0,
ProcessFrameCount = 0,
GenICamSupport = M_FALSE;
MIL_DOUBLE ProcessFrameRate = 0;
HookDataStruct UserHookData;
PrintHeader();
MosGetch();
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL,
&MilDigitizer, M_NULL);
MsysInquire(MilSystem, M_GENICAM_AVAILABLE, &GenICamSupport);
if (GenICamSupport == M_TRUE)
{
MdigControl(MilDigitizer, M_GC_FEATURE_BROWSER, M_OPEN + M_ASYNCHRONOUS);
MosPrintf(MIL_TEXT("Use the MIL feature browser to setup your camera as required\n"));
MosPrintf(MIL_TEXT("and enable the desired components.\n"));
MosPrintf(MIL_TEXT("See mainly ComponentSelector and ComponentEnable features.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to start.\n"));
MosGetch();
}
MosPrintf(MIL_TEXT("\nMULTI-COMPONENT ACQUISITION IN PROGRESS..\n"));
MosPrintf(MIL_TEXT("-----------------------------------------\n\n"));
MappControl(M_DEFAULT, M_ERROR, M_PRINT_DISABLE);
for (MIL_UINT i = 0; i < BUFFERING_SIZE; i++)
{
MbufAllocContainer(MilSystem, M_GRAB + M_PROC, M_DEFAULT, &MilContainers[i]);
if (MilContainers[i] != M_NULL)
MilContainerCount++;
else
break;
}
MappControl(M_DEFAULT, M_ERROR, M_PRINT_ENABLE);
UserHookData.MilDigitizer = MilDigitizer;
UserHookData.ProcessedCount = 0;
MdigControl(MilDigitizer, M_GRAB_TIMEOUT, 10000);
MdigProcess(MilDigitizer, MilContainers, MilContainerCount,
M_START, M_DEFAULT, ProcessingFunction, &UserHookData);
MosPrintf(MIL_TEXT("Press <Enter> to stop. \n\n"));
MosGetch();
MdigProcess(MilDigitizer, MilContainers, MilContainerCount,
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%d frames grabbed at %.1f frames/sec (%.1f ms/frame).\n"),
(int)ProcessFrameCount, ProcessFrameRate, 1000.0 / ProcessFrameRate);
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
FreeDisplayData(UserHookData.DisplayList);
for (MIL_INT i = 0; i < MilContainerCount; i++)
{
MbufFree(MilContainers[i]);
}
MappFreeDefault(MilApplication, MilSystem, M_NULL, MilDigitizer, M_NULL);
return 0;
}
MIL_INT MFTYPE ProcessingFunction(MIL_INT HookType, MIL_ID HookId, void* HookDataPtr)
{
HookDataStruct *UserHookDataPtr = (HookDataStruct *)HookDataPtr;
ComponentDataList& DisplayList = UserHookDataPtr->DisplayList;
MIL_ID ModifiedContainerId = M_NULL;
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER + M_BUFFER_ID, &ModifiedContainerId);
std::vector<MIL_ID> Components;
MbufInquireContainer(ModifiedContainerId, M_CONTAINER, M_COMPONENT_LIST, Components);
for (size_t i = 0; i < Components.size(); i++)
{
MIL_INT64 ComponentType = 0, GroupId = 0, SourceId = 0, RegionId = 0;
MIL_STRING ComponentName;
MbufInquire(Components[i], M_COMPONENT_TYPE, &ComponentType);
MbufInquire(Components[i], M_COMPONENT_TYPE_NAME, ComponentName);
MbufInquire(Components[i], M_COMPONENT_GROUP_ID, &GroupId);
MbufInquire(Components[i], M_COMPONENT_SOURCE_ID, &SourceId);
MbufInquire(Components[i], M_COMPONENT_REGION_ID, &RegionId);
MIL_STRING_STREAM ConstructedName;
ConstructedName << ComponentName << MIL_TEXT("[") << GroupId << MIL_TEXT(":") << SourceId << MIL_TEXT(":") << RegionId << MIL_TEXT("]");
bool IsDisplayableComponent = ComponentType != M_COMPONENT_METADATA;
ComponentListIterator It = DisplayList.find(ConstructedName.str());
if (It == DisplayList.end())
{
MIL_STRING PfncName;
MbufInquire(Components[i], M_PFNC_NAME, PfncName);
ComponentData ComponentInfo(Components[i], IsDisplayableComponent, ConstructedName.str(), PfncName);
if (IsDisplayableComponent)
{
MbufCopy(Components[i], ComponentInfo.MilImageDisp);
}
PrintComponentInfo(Components.size(), i, ComponentInfo);
DisplayList[ConstructedName.str()] = ComponentInfo;
}
else
{
ComponentData& ComponentInfo = It->second;
if (IsDisplayableComponent)
{
MbufCopy(Components[i], ComponentInfo.MilImageDisp);
}
}
}
MosPrintf(MIL_TEXT("Containers processed: %d\r"), ++UserHookDataPtr->ProcessedCount);
return 0;
}
void FreeDisplayData(ComponentDataList& Components)
{
ComponentListIterator Iterator = Components.begin();
for (; Iterator != Components.end(); ++Iterator)
{
ComponentData& ComponentData = Iterator->second;
ComponentData.Free();
}
Components.clear();
}
void PrintComponentInfo(MIL_INT ComponentCount, MIL_INT ComponentNb, const ComponentData& ComponentInfo)
{
if (ComponentNb == 0)
{
MosPrintf(MIL_TEXT("+------------------------------------------------------------------------------+\n"));
MosPrintf(MIL_TEXT("| Container Component Count: %2d |\n"), (int)ComponentCount);
MosPrintf(MIL_TEXT("|------------------------------+------------------------+----------------------|\n"));
MosPrintf(MIL_TEXT("| Component Name | Size & Type | PFNC Format |\n"));
MosPrintf(MIL_TEXT("|------------------------------|------------------------|----------------------|\n"));
}
MosPrintf(MIL_TEXT("|%29.29s | %-23.23s| %-21.21s|\n"),
ComponentInfo.ComponentName.c_str(), ComponentInfo.ToString().c_str(),
ComponentInfo.PixelFormatName.c_str());
if (ComponentNb == ComponentCount - 1)
{
MosPrintf(MIL_TEXT("+------------------------------+------------------------+----------------------+\n"));
MosPrintf(MIL_TEXT("Note: [x:x:x] component name suffix encoded as [GroupId:SourceId:RegionId]\n\n"));
}
}