#include <mil.h>
#define SEQUENCE_FILE M_TEMP_DIR MIL_TEXT("MilSequence.avi")
#define GRAB_SCALE 1.0
#define COMPRESSION_Q_FACTOR 50
#define FRAME_NUMBER_ANNOTATION M_YES
#define SAVE_SEQUENCE_TO_DISK M_YES
#define NB_GRAB_IMAGE_MAX 22
MIL_INT MFTYPE ArchiveFunction(MIL_INT HookType, MIL_ID HookId, void* HookDataPtr);
typedef struct
{
MIL_ID MilSystem;
MIL_ID MilDisplay;
MIL_ID MilImageDisp;
MIL_ID MilCompressedImage;
MIL_INT NbGrabbedFrames;
MIL_INT NbArchivedFrames;
MIL_INT SaveSequenceToDisk;
} HookDataStruct;
int MosMain(void)
{
MIL_ID MilApplication, MilRemoteApplication, MilSystem, MilDigitizer, MilDisplay, MilImageDisp;
MIL_ID MilGrabImages[NB_GRAB_IMAGE_MAX];
MIL_ID MilCompressedImage = M_NULL;
MIL_INT CompressAttribute=0;
MIL_INT NbFrames=0, Selection=1, LicenseModules=0, n=0;
MIL_INT FrameCount=0, FrameMissed=0, NbFramesReplayed=0, Exit=0;
MIL_DOUBLE FrameRate=0, TimeWait=0, TotalReplay=0;
MIL_DOUBLE GrabScale=GRAB_SCALE;
MIL_INT SaveSequenceToDisk = SAVE_SEQUENCE_TO_DISK;
HookDataStruct UserHookData;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay,
&MilDigitizer, M_NULL);
MbufAllocColor(MilSystem,
MdigInquire(MilDigitizer, M_SIZE_BAND, M_NULL),
(MIL_INT)(MdigInquire(MilDigitizer, M_SIZE_X, M_NULL)*GrabScale),
(MIL_INT)(MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL)*GrabScale),
8L+M_UNSIGNED,
M_IMAGE+M_GRAB+M_DISP, &MilImageDisp);
MbufClear(MilImageDisp, 0x0);
MdispSelect(MilDisplay, MilImageDisp);
MdigControl(MilDigitizer, M_GRAB_SCALE, GrabScale);
MdigGrabContinuous(MilDigitizer, MilImageDisp);
MosPrintf(MIL_TEXT("\nSEQUENCE ACQUISITION:\n"));
MosPrintf(MIL_TEXT("--------------------\n\n"));
MsysInquire(MilSystem, M_OWNER_APPLICATION, &MilRemoteApplication);
MappInquire(MilRemoteApplication, M_LICENSE_MODULES, &LicenseModules);
if (SaveSequenceToDisk && (LicenseModules & (M_LICENSE_JPEGSTD | M_LICENSE_JPEG2000)))
{
MosPrintf(MIL_TEXT("Choose the sequence format:\n"));
MosPrintf(MIL_TEXT("1) Uncompressed images.\n") );
if(LicenseModules & M_LICENSE_JPEGSTD)
MosPrintf(MIL_TEXT("2) Compressed images with a lossy JPEG algorithm.\n"));
if(LicenseModules & M_LICENSE_JPEG2000)
MosPrintf(MIL_TEXT("3) Compressed images with a lossy JPEG 2000 algorithm.\n"));
Selection = MosGetch();
}
else
{
MosPrintf(MIL_TEXT("Press <Enter> to record images.\n"));
Selection = '1';
MosGetch();
}
switch(Selection)
{
case '1':
case '\r':
MosPrintf(MIL_TEXT("\nRecording uncompressed images...\n\n"));
CompressAttribute = M_NULL;
break;
case '2':
MosPrintf(MIL_TEXT("\nRecording JPEG images...\n\n"));
CompressAttribute = M_COMPRESS + M_JPEG_LOSSY;
break;
case '3':
MosPrintf(MIL_TEXT("\nRecording JPEG 2000 images...\n\n"));
CompressAttribute = M_COMPRESS + M_JPEG2000_LOSSY;
break;
default:
MosPrintf(MIL_TEXT("\nInvalid selection !.\n\nUsing uncompressed images.\n\n"));
CompressAttribute = M_NULL;
while (MosKbhit()) MosGetch();
break;
}
if (CompressAttribute)
{
MbufAllocColor(MilSystem,
MdigInquire(MilDigitizer, M_SIZE_BAND, M_NULL),
(MIL_INT)(MdigInquire(MilDigitizer, M_SIZE_X, M_NULL)*GrabScale),
(MIL_INT)(MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL)*GrabScale),
8L+M_UNSIGNED, M_IMAGE+CompressAttribute, &MilCompressedImage);
MbufControl(MilCompressedImage, M_Q_FACTOR, COMPRESSION_Q_FACTOR);
}
MappControl(M_DEFAULT, M_ERROR, M_PRINT_DISABLE);
for (NbFrames=0, n=0; n<NB_GRAB_IMAGE_MAX; n++)
{
MbufAllocColor(MilSystem,
MdigInquire(MilDigitizer, M_SIZE_BAND, M_NULL),
(MIL_INT)(MdigInquire(MilDigitizer, M_SIZE_X, M_NULL)*GrabScale),
(MIL_INT)(MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL)*GrabScale),
8L+M_UNSIGNED, M_IMAGE+M_GRAB, &MilGrabImages[n]);
if (MilGrabImages[n])
{
NbFrames++;
MbufClear(MilGrabImages[n], 0xFF);
}
else
break;
}
MappControl(M_DEFAULT, M_ERROR, M_PRINT_ENABLE);
for (n=0; n<2 && NbFrames; n++)
{
NbFrames--;
MbufFree(MilGrabImages[NbFrames]);
}
MdigHalt(MilDigitizer);
if (SaveSequenceToDisk)
{
MosPrintf(MIL_TEXT("Saving the sequence to an AVI file...\n\n"));
MbufExportSequence(SEQUENCE_FILE, M_DEFAULT, M_NULL, M_NULL, M_DEFAULT, M_OPEN);
}
UserHookData.MilSystem = MilSystem;
UserHookData.MilDisplay = MilDisplay;
UserHookData.MilImageDisp = MilImageDisp;
UserHookData.MilCompressedImage = MilCompressedImage;
UserHookData.SaveSequenceToDisk = SaveSequenceToDisk;
UserHookData.NbGrabbedFrames = 0;
UserHookData.NbArchivedFrames = 0;
MdigProcess(MilDigitizer, MilGrabImages, NbFrames,
SaveSequenceToDisk ? M_START : M_SEQUENCE,
M_DEFAULT, ArchiveFunction, &UserHookData);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MdigProcess(MilDigitizer, MilGrabImages, NbFrames, M_STOP,
M_DEFAULT, ArchiveFunction, &UserHookData);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_COUNT, &FrameCount);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_RATE, &FrameRate);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_MISSED, &FrameMissed);
MosPrintf(MIL_TEXT("\n\n%ld frames archived (%ld missed), at %.1f frames/sec ")
MIL_TEXT("(%.1f ms/frame).\n"),UserHookData.NbArchivedFrames,
FrameMissed, FrameRate, 1000.0/FrameRate);
if (SaveSequenceToDisk)
MbufExportSequence(SEQUENCE_FILE, M_DEFAULT, M_NULL, M_NULL, FrameRate, M_CLOSE);
if (UserHookData.NbArchivedFrames > 0)
{
MIL_INT KeyPressed = 0;
do
{
if (SaveSequenceToDisk)
{
MosPrintf(MIL_TEXT("\nPlaying back sequence from the AVI file...\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MbufDiskInquire(SEQUENCE_FILE, M_NUMBER_OF_IMAGES, &FrameCount);
MbufDiskInquire(SEQUENCE_FILE, M_FRAME_RATE, &FrameRate);
MbufDiskInquire(SEQUENCE_FILE, M_COMPRESSION_TYPE, &CompressAttribute);
MbufImportSequence(SEQUENCE_FILE, M_DEFAULT, M_NULL,
M_NULL, M_NULL, M_NULL, M_NULL, M_OPEN);
}
TotalReplay = 0.0;
NbFramesReplayed = 0;
for (n=0; n<FrameCount; n++)
{
MappTimer(M_DEFAULT, M_TIMER_RESET, M_NULL);
if (SaveSequenceToDisk)
{
MbufImportSequence(SEQUENCE_FILE, M_DEFAULT, M_LOAD,
M_NULL, &MilImageDisp, n, 1, M_READ);
NbFramesReplayed++;
MosPrintf(MIL_TEXT("Frame #%d \r"), NbFramesReplayed);
}
else
{
MbufCopy(MilGrabImages[n], MilImageDisp);
NbFramesReplayed++;
MosPrintf(MIL_TEXT("Frame #%d \r"), NbFramesReplayed);
}
if (MosKbhit() && (n>=(NB_GRAB_IMAGE_MAX-1)))
{
MosGetch();
break;
}
MappTimer(M_DEFAULT, M_TIMER_READ, &TimeWait);
TotalReplay += TimeWait;
TimeWait = (1/FrameRate) - TimeWait;
MappTimer(M_DEFAULT, M_TIMER_WAIT, &TimeWait);
TotalReplay += (TimeWait > 0) ? TimeWait: 0.0;
}
if (SaveSequenceToDisk)
MbufImportSequence(SEQUENCE_FILE, M_DEFAULT, M_NULL,
M_NULL, M_NULL, M_NULL, M_NULL, M_CLOSE);
MosPrintf(MIL_TEXT("\n\n%ld frames replayed, at a frame rate of %.1f frames/sec ")
MIL_TEXT("(%.1f ms/frame).\n\n"), NbFramesReplayed, n/TotalReplay,
1000.0*TotalReplay/n);
MosPrintf(MIL_TEXT("Press <Enter> to end (or any other key to playback again).\n"));
KeyPressed = MosGetch();
}
while((KeyPressed != '\r') && (KeyPressed != '\n'));
}
MbufFree(MilImageDisp);
for (n=0; n<NbFrames; n++)
MbufFree(MilGrabImages[n]);
if (MilCompressedImage)
MbufFree(MilCompressedImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, M_NULL);
return 0;
}
#define STRING_LENGTH_MAX 20
#define STRING_POS_X 20
#define STRING_POS_Y 20
MIL_INT MFTYPE ArchiveFunction(MIL_INT HookType, MIL_ID HookId, void* HookDataPtr)
{
HookDataStruct *UserHookDataPtr = (HookDataStruct *)HookDataPtr;
MIL_ID ModifiedImage = 0;
MIL_INT n = 0;
MIL_TEXT_CHAR Text[STRING_LENGTH_MAX]= {MIL_TEXT('\0'),};
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER+M_BUFFER_ID, &ModifiedImage);
UserHookDataPtr->NbGrabbedFrames++;
if ((FRAME_NUMBER_ANNOTATION == M_YES))
{
MosSprintf(Text, STRING_LENGTH_MAX, MIL_TEXT(" %ld "),
UserHookDataPtr->NbGrabbedFrames);
MgraText(M_DEFAULT, ModifiedImage, STRING_POS_X, STRING_POS_Y, Text);
}
if (UserHookDataPtr->MilCompressedImage)
{
MbufCopy(ModifiedImage, UserHookDataPtr->MilCompressedImage);
}
if (UserHookDataPtr->SaveSequenceToDisk)
{
MbufExportSequence(SEQUENCE_FILE, M_DEFAULT, UserHookDataPtr->MilCompressedImage?
&UserHookDataPtr->MilCompressedImage: &ModifiedImage,
1, M_DEFAULT, M_WRITE);
UserHookDataPtr->NbArchivedFrames++;
MosPrintf(MIL_TEXT("Frame #%d \r"), UserHookDataPtr->NbArchivedFrames);
}
MbufCopy(ModifiedImage, UserHookDataPtr->MilImageDisp);
return 0;
}