#include <mil.h>
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("BaboonMono.mim")
#define FOCUS_MAX_NB_POSITIONS 100
#define FOCUS_MIN_POSITION 0
#define FOCUS_MAX_POSITION (FOCUS_MAX_NB_POSITIONS - 1)
#define FOCUS_START_POSITION 10
#define FOCUS_MAX_POSITION_VARIATION M_DEFAULT
#define FOCUS_MODE M_SMART_SCAN
#define FOCUS_SENSITIVITY 1
typedef struct
{ MIL_ID SourceImage;
MIL_ID FocusImage;
MIL_ID Display;
long Iteration;
} DigHookUserData;
MIL_INT MFTYPE MoveLensHookFunction(MIL_INT HookType,
MIL_INT Position,
void* UserDataHookPtr);
void SimulateGrabFromCamera(MIL_ID SourceImage,
MIL_ID FocusImage,
MIL_INT Iteration,
MIL_ID AnnotationDisplay);
void DrawCursor(MIL_ID AnnotationImage, MIL_INT Position);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilSource,
MilCameraFocus;
MIL_INT FocusPos;
DigHookUserData UserData;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MbufRestore(IMAGE_FILE, MilSystem, &MilSource);
MbufRestore(IMAGE_FILE, MilSystem, &MilCameraFocus);
MbufClear(MilCameraFocus, 0);
MdispSelect(MilDisplay, MilCameraFocus);
SimulateGrabFromCamera(MilSource, MilCameraFocus, FOCUS_START_POSITION, MilDisplay);
UserData.SourceImage = MilSource;
UserData.FocusImage = MilCameraFocus;
UserData.Iteration = 0L;
UserData.Display = MilDisplay;
MosPrintf(MIL_TEXT("\nAUTOFOCUS:\n"));
MosPrintf(MIL_TEXT("----------\n\n"));
MosPrintf(MIL_TEXT("Automatic focusing operation will be done on this image.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MosPrintf(MIL_TEXT("Autofocusing...\n\n"));
MdigFocus(M_NULL,
MilCameraFocus,
M_DEFAULT,
MoveLensHookFunction,
&UserData,
FOCUS_MIN_POSITION,
FOCUS_START_POSITION,
FOCUS_MAX_POSITION,
FOCUS_MAX_POSITION_VARIATION,
FOCUS_MODE + FOCUS_SENSITIVITY,
&FocusPos);
MosPrintf(MIL_TEXT("The best focus position is %d.\n"), FocusPos);
MosPrintf(MIL_TEXT("The best focus position found in %d iterations.\n\n"),
UserData.Iteration);
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
MbufFree(MilSource);
MbufFree(MilCameraFocus);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
MIL_INT MFTYPE MoveLensHookFunction(MIL_INT HookType,
MIL_INT Position,
void* UserDataHookPtr)
{
DigHookUserData *UserData = (DigHookUserData *)UserDataHookPtr;
if(HookType == M_CHANGE || HookType == M_ON_FOCUS)
{
SimulateGrabFromCamera( UserData->SourceImage,
UserData->FocusImage,
Position,
UserData->Display
);
UserData->Iteration++;
}
return 0;
}
#define FOCUS_BEST_POSITION (FOCUS_MAX_NB_POSITIONS/2)
void SimulateGrabFromCamera(MIL_ID SourceImage, MIL_ID FocusImage,
MIL_INT Iteration, MIL_ID AnnotationDisplay)
{
MIL_INT NbSmoothNeeded;
MIL_INT BufType;
MIL_INT BufSizeX;
MIL_INT BufSizeY;
MIL_INT Smooth;
MIL_ID TempBuffer;
MIL_ID SourceOwnerSystem;
#if (M_MIL_LITE)
#error "Replace the SimulateGrabFromCamera()function with a true image grab."
#endif
NbSmoothNeeded = MosAbs(Iteration - FOCUS_BEST_POSITION);
BufType = MbufInquire(FocusImage, M_TYPE, M_NULL);
BufSizeX = MbufInquire(FocusImage, M_SIZE_X, M_NULL);
BufSizeY = MbufInquire(FocusImage, M_SIZE_Y, M_NULL);
if(NbSmoothNeeded == 0)
{
MbufCopy(SourceImage, FocusImage);
}
else if(NbSmoothNeeded == 1)
{
MimConvolve(SourceImage, FocusImage, M_SMOOTH);
}
else
{
SourceOwnerSystem = (MIL_ID)MbufInquire(SourceImage, M_OWNER_SYSTEM, M_NULL);
MbufAlloc2d(SourceOwnerSystem, BufSizeX, BufSizeY,
BufType, M_IMAGE+M_PROC, &TempBuffer);
MimConvolve(SourceImage, TempBuffer, M_SMOOTH);
for(Smooth=1;Smooth<NbSmoothNeeded-1;Smooth++)
{
MimConvolve(TempBuffer, TempBuffer, M_SMOOTH);
}
MimConvolve(TempBuffer, FocusImage, M_SMOOTH);
MbufFree(TempBuffer);
}
DrawCursor(AnnotationDisplay, Iteration);
}
#define CURSOR_POSITION ((BufSizeY*7)/8)
#define CURSOR_SIZE 14
#define CURSOR_COLOR M_COLOR_GREEN
void DrawCursor(MIL_ID AnnotationDisplay, MIL_INT Position)
{
MIL_ID AnnotationImage;
MIL_INT BufSizeX, BufSizeY, n;
MIL_DOUBLE CursorColor;
MdispControl(AnnotationDisplay, M_OVERLAY, M_ENABLE);
MdispControl(AnnotationDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
MdispInquire(AnnotationDisplay, M_OVERLAY_ID, &AnnotationImage);
MbufInquire(AnnotationImage, M_SIZE_X, &BufSizeX);
MbufInquire(AnnotationImage, M_SIZE_Y, &BufSizeY);
CursorColor = CURSOR_COLOR;
MgraColor(M_DEFAULT, CursorColor);
n = (BufSizeX/FOCUS_MAX_NB_POSITIONS);
MgraLine(M_DEFAULT, AnnotationImage, 0, CURSOR_POSITION+CURSOR_SIZE,
BufSizeX-1, CURSOR_POSITION+CURSOR_SIZE);
MgraLine(M_DEFAULT, AnnotationImage, Position*n, CURSOR_POSITION+CURSOR_SIZE,
Position*n-CURSOR_SIZE, CURSOR_POSITION);
MgraLine(M_DEFAULT, AnnotationImage, Position*n, CURSOR_POSITION+CURSOR_SIZE,
Position*n+CURSOR_SIZE, CURSOR_POSITION);
MgraLine(M_DEFAULT, AnnotationImage, Position*n-CURSOR_SIZE, CURSOR_POSITION,
Position*n+CURSOR_SIZE, CURSOR_POSITION);
}