from __future__ import print_function
import sys
import ctypes
import mil as MIL
if sys.hexversion >= 0x03000000:
get_input = input
else:
get_input = raw_input
class HookDataStruct(ctypes.Structure):
_fields_ = [
("MilDigitizer", MIL.MIL_ID),
("MilImageDisp", MIL.MIL_ID),
("ProcessedImageCount", MIL.MIL_INT)]
BUFFERING_SIZE_MAX = 20
STRING_POS_X = 20
STRING_POS_Y = 20
def ProcessingFunction(HookType, HookId, HookDataPtr):
ModifiedBufferId = MIL.MIL_ID(0)
MIL.MdigGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ctypes.byref(ModifiedBufferId))
UserData = ctypes.cast(ctypes.c_void_p(HookDataPtr), ctypes.POINTER(HookDataStruct)).contents
UserData.ProcessedImageCount += 1
print("Processing frame
MIL.MgraText(MIL.M_DEFAULT, ModifiedBufferId, STRING_POS_X, STRING_POS_Y, MIL.MIL_TEXT("{:d}".format(UserData.ProcessedImageCount)))
MIL.MimArith(ctypes.c_double(ModifiedBufferId.value), 0.0, UserData.MilImageDisp, MIL.M_NOT)
return 0
def MdigProcessExample():
MilApplication = MIL.MappAlloc(MIL.MIL_TEXT("M_DEFAULT"), MIL.M_DEFAULT, None)
MilSystem = MIL.MsysAlloc(MIL.M_DEFAULT, MIL.M_SYSTEM_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, None)
MilDisplay = MIL.MdispAlloc(MilSystem, MIL.M_DEFAULT, MIL.MIL_TEXT("M_DEFAULT"), MIL.M_DEFAULT, None)
MilDigitizer = MIL.MdigAlloc(MilSystem, MIL.M_DEFAULT, MIL.MIL_TEXT("M_DEFAULT"), MIL.M_DEFAULT, None)
SizeX = MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, None)
SizeY = MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, None)
MilImageDisp = MIL.MbufAlloc2d(MilSystem,
SizeX,
SizeY,
8 + MIL.M_UNSIGNED,
MIL.M_IMAGE +
MIL.M_PROC + MIL.M_DISP + MIL.M_GRAB,
None)
MIL.MbufClear(MilImageDisp, MIL.M_COLOR_BLACK)
MIL.MdispSelect(MilDisplay, MilImageDisp)
print("\nMULTIPLE BUFFERED PROCESSING.")
print("-----------------------------\n")
MIL.MdigGrabContinuous(MilDigitizer, MilImageDisp)
get_input("Press <Enter> to start processing.\n")
MIL.MdigHalt(MilDigitizer)
MilGrabBufferList = (MIL.MIL_ID * BUFFERING_SIZE_MAX)()
MilGrabBufferListSize = 0
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_DISABLE)
for n in range(0, BUFFERING_SIZE_MAX):
MilGrabBufferList[n] = (MIL.MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, None))
if (MilGrabBufferList[n] != MIL.M_NULL):
MIL.MbufClear(MilGrabBufferList[n], 0xFF)
MilGrabBufferListSize += 1
else:
break
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_ENABLE)
UserHookData = HookDataStruct(MilDigitizer, MilImageDisp, 0)
ProcessingFunctionPtr = MIL.MIL_DIG_HOOK_FUNCTION_PTR(ProcessingFunction)
MIL.MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize, MIL.M_START, MIL.M_DEFAULT, ProcessingFunctionPtr, ctypes.byref(UserHookData))
get_input("Press <Enter> to stop. \n\n")
MIL.MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize, MIL.M_STOP, MIL.M_DEFAULT, ProcessingFunctionPtr, ctypes.byref(UserHookData))
ProcessFrameCount = MIL.MdigInquire(MilDigitizer, MIL.M_PROCESS_FRAME_COUNT, None)
ProcessFrameRate = MIL.MdigInquire(MilDigitizer, MIL.M_PROCESS_FRAME_RATE, None)
print("\n{:d} frames grabbed at {:.1f} frames/sec ({:.1f} ms/frame)".format(ProcessFrameCount, ProcessFrameRate, 1000.0/ProcessFrameRate))
get_input("Press <Enter> to end.\n")
for id in range(0, MilGrabBufferListSize):
MIL.MbufFree(MilGrabBufferList[id])
MIL.MbufFree(MilImageDisp)
MIL.MdispFree(MilDisplay)
MIL.MdigFree(MilDigitizer)
MIL.MsysFree(MilSystem)
MIL.MappFree(MilApplication)
return
if __name__ == "__main__":
MdigProcessExample()