#include <mil.h>
#include "exampleinterface.h"
#define ENABLE_IMAGES_IN_OVERLAY 1
static const MIL_TEXT_CHAR* const OVERLAY_IMAGE_FILES[] =
{
EXAMPLE_IMAGE_PATH MIL_TEXT("CalibrateCameraImage.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("AdjustMinIntensityImage.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("CalibrateLaserImage.mim")
};
static const MIL_TEXT_CHAR* const FRAME_IMAGE_FILE =
EXAMPLE_IMAGE_PATH MIL_TEXT("frame.mim");
static const MIL_DOUBLE DEFAULT_TRANSPARENT_COLOR = M_RGB888(16,0,0);
static const MIL_DOUBLE FRAME_TRANSPARENT_COLOR = M_RGB888(227,0,227);
static const MIL_INT FRAME_BORDER = 5;
static const MIL_DOUBLE MAX_PICTURE_RATIO = 0.25;
CExampleInterface::CExampleInterface(MIL_CONST_TEXT_PTR SystemDescriptor, MIL_INT64 BufAttr)
: m_BufferAttribute(BufAttr),
m_SizeX ( 0 ),
m_SizeY ( 0 ),
m_OverlayOffsetX ( 0 ),
m_OverlayOffsetY ( 0 ),
m_CalibrationMode( 0 ),
m_MilApplication (M_NULL ),
m_MilSystem (M_NULL ),
m_MilDisplay (M_NULL ),
m_MilDisplayImage(M_NULL ),
m_MilOverlayImage(M_NULL )
{
MappAlloc(M_NULL, M_DEFAULT, &m_MilApplication);
MsysAlloc(M_DEFAULT, SystemDescriptor, M_DEFAULT, M_DEFAULT, &m_MilSystem);
MdispAlloc(m_MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &m_MilDisplay);
for (MIL_INT i = 0 ; i < eNbOverlayImages; i++)
{
m_MilOverlayPictures[i] = M_NULL;
}
}
CExampleInterface::~CExampleInterface()
{
for (MIL_INT i = 0; i < eNbOverlayImages; ++i)
{
if (m_MilOverlayPictures[i] != M_NULL)
MbufFree(m_MilOverlayPictures[i]);
}
if (m_MilDisplayImage != M_NULL)
MbufFree(m_MilDisplayImage);
if (m_MilDisplay != M_NULL)
MdispFree(m_MilDisplay);
if (m_MilSystem != M_NULL)
MsysFree(m_MilSystem);
if (m_MilApplication != M_NULL)
MappFree(m_MilApplication);
}
bool CExampleInterface::AskInteractiveExample()
{
MosPrintf(MIL_TEXT("You can run this example with images:\n")
MIL_TEXT(" 1. Reloaded from disk (STAND-ALONE)\n")
MIL_TEXT(" 2. Grabbed using a camera (INTERACTIVE)\n\n")
MIL_TEXT("Your choice (1 or 2): "));
MIL_INT Choice = 0;
while (Choice == 0)
{
switch (MosGetch())
{
case MIL_TEXT('1'):
Choice = 1;
MosPrintf(MIL_TEXT("1. STAND-ALONE\n\n"));
break;
case MIL_TEXT('2'):
Choice = 2;
MosPrintf(MIL_TEXT("2. INTERACTIVE\n\n"));
break;
}
}
bool IsInteractive = (Choice == 2);
return IsInteractive;
}
MIL_INT CExampleInterface::AskCalibrationMode()
{
MosPrintf(MIL_TEXT("Choose a calibration mode:\n")
MIL_TEXT(" 1. M_DEPTH_CORRECTION\n")
MIL_TEXT(" 2. M_CALIBRATED_CAMERA_LINEAR_MOTION\n\n")
MIL_TEXT("Your choice (1 or 2): "));
MIL_INT CalibrationMode = M_NULL;
while (CalibrationMode == M_NULL)
{
switch (MosGetch())
{
case MIL_TEXT('1'):
CalibrationMode = M_DEPTH_CORRECTION;
MosPrintf(MIL_TEXT("1. M_DEPTH_CORRECTION\n\n")
MIL_TEXT("For this calibration mode, the following steps will be executed:\n")
MIL_TEXT(" - Setting up the laser line extraction parameters.\n")
MIL_TEXT(" - Calibrating the 3d reconstruction setup.\n\n"));
break;
case MIL_TEXT('2'):
CalibrationMode = M_CALIBRATED_CAMERA_LINEAR_MOTION;
MosPrintf(MIL_TEXT("2. M_CALIBRATED_CAMERA_LINEAR_MOTION\n\n")
MIL_TEXT("For this calibration mode, the following steps will be executed:\n")
MIL_TEXT(" - Calibrating the camera.\n")
MIL_TEXT(" - Setting up the laser line extraction parameters.\n")
MIL_TEXT(" - Calibrating the 3d reconstruction setup.\n\n"));
break;
}
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
m_CalibrationMode = CalibrationMode;
return CalibrationMode;
}
void CExampleInterface::ShowDisplay()
{
if (m_MilDisplayImage == M_NULL)
{
MbufAlloc2d(m_MilSystem, m_SizeX, m_SizeY, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP+m_BufferAttribute, &m_MilDisplayImage);
MbufClear(m_MilDisplayImage, 0.0);
MdispControl(m_MilDisplay, M_TRANSPARENT_COLOR, DEFAULT_TRANSPARENT_COLOR);
MIL_INT PictureSizeX, PictureSizeY;
DeterminePictureSize(&PictureSizeX, &PictureSizeY);
MIL_ID MilFrameOriginalImage = MbufRestore(FRAME_IMAGE_FILE, m_MilSystem, M_NULL);
MIL_ID MilFrameImage = MbufAllocColor(m_MilSystem, 3, PictureSizeX, PictureSizeY, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, M_NULL);
MimResize(MilFrameOriginalImage, MilFrameImage, M_FILL_DESTINATION, M_FILL_DESTINATION, M_NEAREST_NEIGHBOR);
MbufFree(MilFrameOriginalImage);
for (MIL_INT i = 0; i < eNbOverlayImages; ++i)
{
MIL_ID MilOriginalImage = MbufRestore(OVERLAY_IMAGE_FILES[i], m_MilSystem, M_NULL);
MbufAllocColor(m_MilSystem, 3, PictureSizeX, PictureSizeY, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, &m_MilOverlayPictures[i]);
MimResize(MilOriginalImage, m_MilOverlayPictures[i], M_FILL_DESTINATION, M_FILL_DESTINATION, M_BICUBIC+M_REGULAR);
MbufFree(MilOriginalImage);
MbufCopyCond(MilFrameImage, m_MilOverlayPictures[i], MilFrameImage, M_NOT_EQUAL, FRAME_TRANSPARENT_COLOR);
}
MbufFree(MilFrameImage);
m_OverlayOffsetX = m_SizeX - PictureSizeX - FRAME_BORDER;
m_OverlayOffsetY = FRAME_BORDER;
}
MdispSelect(m_MilDisplay, m_MilDisplayImage);
MdispControl(m_MilDisplay, M_OVERLAY, M_ENABLE);
MdispInquire(m_MilDisplay, M_OVERLAY_ID, &m_MilOverlayImage);
}
void CExampleInterface::HideDisplay()
{
m_MilOverlayImage = M_NULL;
MdispControl(m_MilDisplay, M_OVERLAY, M_DISABLE);
MdispSelect(m_MilDisplay, M_NULL);
}
void CExampleInterface::CopyInOverlay(EOverlayImage eOverlayImage)
{
#if ENABLE_IMAGES_IN_OVERLAY
MbufCopyClip(m_MilOverlayPictures[eOverlayImage], m_MilOverlayImage, m_OverlayOffsetX, m_OverlayOffsetY);
#endif
}
void CExampleInterface::SetImageSize(MIL_INT SizeX, MIL_INT SizeY)
{
m_SizeX = SizeX;
m_SizeY = SizeY;
}
bool CExampleInterface::IsValidBase() const
{
bool IsValid = ( m_MilApplication != M_NULL &&
m_MilSystem != M_NULL &&
m_MilDisplay != M_NULL );
return IsValid;
}
void CExampleInterface::DeterminePictureSize(MIL_INT* pPictureSizeX, MIL_INT* pPictureSizeY) const
{
MIL_INT PictureSizeX = MbufDiskInquire(FRAME_IMAGE_FILE, M_SIZE_X, M_NULL);
MIL_INT PictureSizeY = MbufDiskInquire(FRAME_IMAGE_FILE, M_SIZE_Y, M_NULL);
MIL_DOUBLE ResizeFactor = 1.0;
if (PictureSizeX > m_SizeX * MAX_PICTURE_RATIO)
{
ResizeFactor = m_SizeX * MAX_PICTURE_RATIO / PictureSizeX;
}
if (PictureSizeY > m_SizeY * MAX_PICTURE_RATIO)
{
MIL_DOUBLE ResizeFactorY = m_SizeY * MAX_PICTURE_RATIO / PictureSizeY;
if (ResizeFactorY < ResizeFactor)
ResizeFactor = ResizeFactorY;
}
*pPictureSizeX = static_cast<MIL_INT>( PictureSizeX * ResizeFactor );
*pPictureSizeY = static_cast<MIL_INT>( PictureSizeY * ResizeFactor );
}