#include <mil.h>
#include "InspectionTask.h"
CInspectionTask::CInspectionTask(MIL_INT ColorConversion , CInspectionTask* FixtureProvider , CInspectionTask* ImageProvider , CInspectionTask* RegionProvider )
: m_MilWorkImage (M_NULL),
m_MilOutputImage (M_NULL),
m_MilOutputRegionGraList (M_NULL),
m_ColorConversion (ColorConversion),
m_FixtureProvider (FixtureProvider),
m_ImageProvider (ImageProvider),
m_RegionProvider (RegionProvider)
{
}
CInspectionTask::~CInspectionTask()
{
CInspectionTask::Free();
}
void CInspectionTask::Init(MIL_ID MilSystem, MIL_INT ImageSizeX , MIL_INT ImageSizeY )
{
}
void CInspectionTask::Free()
{
if(m_MilWorkImage)
{
MbufFree(m_MilWorkImage);
m_MilWorkImage = M_NULL;
}
if(m_MilOutputImage)
{
MbufFree(m_MilOutputImage);
m_MilOutputImage = M_NULL;
}
if(m_MilOutputRegionGraList)
{
MgraFree(m_MilOutputRegionGraList);
m_MilOutputRegionGraList = M_NULL;
}
}
void CInspectionTask::InspectImage(MIL_ID MilImage)
{
if(m_MilWorkImage)
{
MbufFree(m_MilWorkImage);
m_MilWorkImage = M_NULL;
}
if((m_FixtureProvider && !m_FixtureProvider->IsResultValid()) ||
(m_ImageProvider && !m_ImageProvider->IsResultValid()) ||
(m_RegionProvider && !m_RegionProvider->IsResultValid()))
m_ResultStatus = eUnknown;
else
{
m_MilWorkImage = CreateConvertedImage(m_ImageProvider ? m_ImageProvider->GetOutputImage() : MilImage, m_ColorConversion);
SetImageFixture(m_MilWorkImage);
m_ResultStatus = Inspect(m_MilWorkImage);
}
}
void CInspectionTask::SetOutputFixture(MIL_INT LocationType, MIL_ID FixturingOffset, MIL_INT CalOrLocSourceId, MIL_DOUBLE Param1, MIL_DOUBLE Param2, MIL_DOUBLE Param3, MIL_DOUBLE Param4)
{
McalFixture(m_MilWorkImage, FixturingOffset, M_MOVE_RELATIVE, LocationType, CalOrLocSourceId, Param1, Param2, Param3, Param4);
}
void CInspectionTask::SetImageFixture(MIL_ID MilImage)
{
if(m_FixtureProvider)
{
McalAssociate(m_FixtureProvider->GetFixture(), MilImage, M_DEFAULT);
}
else
{
McalAssociate(M_NULL, MilImage, M_DEFAULT);
McalUniform(MilImage, 0.0, 0.0, 1.0, 1.0, 0.0, M_DEFAULT);
McalRelativeOrigin(MilImage, 0, 0, 0, 0, M_DEFAULT);
}
}
void CInspectionTask::AllocateOutputImage(MIL_ID MilSystem, MIL_INT NbBands, MIL_INT SizeX, MIL_INT SizeY, MIL_INT Type, MIL_INT64 BufAttribute)
{
if(m_MilOutputImage)
MbufFree(m_MilOutputImage);
MbufAllocColor(MilSystem, NbBands, SizeX, SizeY, Type, BufAttribute, &m_MilOutputImage);
}
void CInspectionTask::AllocateRegionGraList(MIL_ID MilSystem)
{
if(m_MilOutputRegionGraList)
MgraFree(m_MilOutputRegionGraList);
MgraAllocList(MilSystem, M_DEFAULT, &m_MilOutputRegionGraList);
}
void CInspectionTask::DrawInspectionGraphicalResult(MIL_ID MilGraContext, MIL_ID MilDest)
{
if(IsResultValid())
{
if(m_FixtureProvider)
{
MgraControl(MilGraContext, M_GRAPHIC_SOURCE_CALIBRATION, (MIL_DOUBLE)m_FixtureProvider->GetFixture());
MgraControl(MilGraContext, M_INPUT_UNITS, M_WORLD);
}
else
{
MgraControl(MilGraContext, M_GRAPHIC_SOURCE_CALIBRATION, M_NULL);
MgraControl(MilGraContext, M_INPUT_UNITS, M_PIXEL);
}
DrawGraphicalResult(MilGraContext, MilDest);
}
}
void CInspectionTask::DrawOutputFixture(MIL_ID MilGraContext, MIL_ID MilDest)
{
McalDraw(MilGraContext, m_MilWorkImage, MilDest, M_DRAW_RELATIVE_COORDINATE_SYSTEM + M_DRAW_FRAME, M_DEFAULT, M_DEFAULT);
}
void CInspectionTask::DrawInputRegion(MIL_ID MilGraContext, MIL_ID MilDest)
{
if(HasRegionProvider())
m_RegionProvider->DrawOutputRegion(MilGraContext, MilDest);
}
MIL_ID CInspectionTask::CreateConvertedImage(MIL_ID MilColorImage, MIL_INT ConversionType)
{
MIL_ID MilConvertedImage;
switch(ConversionType)
{
case M_RGB_TO_L:
case M_RGB_TO_Y:
MbufAlloc2d(MbufInquire(MilColorImage, M_OWNER_SYSTEM, M_NULL),
MbufInquire(MilColorImage, M_SIZE_X, M_NULL),
MbufInquire(MilColorImage, M_SIZE_Y, M_NULL),
8+M_UNSIGNED,
M_IMAGE+M_PROC,
&MilConvertedImage);
MimConvert(MilColorImage, MilConvertedImage, ConversionType);
break;
case M_RED:
case M_GREEN:
case M_BLUE:
MbufChildColor(MilColorImage, ConversionType, &MilConvertedImage);
break;
case M_NONE:
default:
MbufCreateColor(MbufInquire(MilColorImage, M_OWNER_SYSTEM, M_NULL),
M_DEFAULT,
M_DEFAULT,
M_DEFAULT,
M_DEFAULT,
M_IMAGE+M_PROC,
M_MIL_ID,
M_DEFAULT,
(void**)&MilColorImage,
&MilConvertedImage);
break;
}
return MilConvertedImage;
}