#include "common.h"
static const MIL_TEXT_CHAR* const WARP_PROCESSING_IMAGE = EXAMPLE_IMAGE_PATH MIL_TEXT("LargeWafer.mim");
static const MIL_INT WARP_INCREMENT = 40;
static const MIL_INT MAX_INCREMENT = 20;
CMPWarpProcessing::CMPWarpProcessing(const MIL_TEXT_CHAR* Title, MIL_INT ProcessingIndex)
: CMPProcessing(Title,
MbufDiskInquire(WARP_PROCESSING_IMAGE, M_SIZE_X, M_NULL),
MbufDiskInquire(WARP_PROCESSING_IMAGE, M_SIZE_Y, M_NULL),
MbufDiskInquire(WARP_PROCESSING_IMAGE, M_TYPE, M_NULL),
MbufDiskInquire(WARP_PROCESSING_IMAGE, M_SIZE_BAND, M_NULL),
ProcessingIndex)
{
m_IncrementCount = 0;
m_Direction = enWarpRight;
m_FourCorners[0] = -(MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[1] = 0.0f;
m_FourCorners[2] = (MIL_FLOAT)GetBufferSizeX()-1-(MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[3] = 0.0f;
m_FourCorners[4] = (MIL_FLOAT)GetBufferSizeX()-1+(MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[5] = (MIL_FLOAT)GetBufferSizeY()-1;
m_FourCorners[6] = (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[7] = (MIL_FLOAT)GetBufferSizeY()-1;
m_FourCorners[0] += (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[1] += (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[2] -= (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[3] += (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[4] += (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[5] -= (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[6] -= (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[7] -= (MAX_INCREMENT*WARP_INCREMENT)/2;
m_FourCorners[8] = 0.0f;
m_FourCorners[9] = 0.0f;
m_FourCorners[10] = (MIL_FLOAT)GetBufferSizeX()-1;
m_FourCorners[11] = (MIL_FLOAT)GetBufferSizeY()-1;
m_ProcessingElements = new WarpProcessingStruct[GetNumMemoryBank()+1];
bool ValidBank = false;
MIL_INT64 MemoryBank = 0;
for (MIL_INT i=0; i<=GetNumMemoryBank(); i++)
{
MemoryBank = GetMemoryBank(i-1, ValidBank);
MbufAllocColor(GetSystemID(), GetBufferSizeBand(), GetBufferSizeX(), GetBufferSizeY(), GetBufferType(),
M_IMAGE+M_PROC+M_HOST_MEMORY+MemoryBank, &m_ProcessingElements[i].MilSourceBuffer);
MbufAllocColor(GetSystemID(), GetBufferSizeBand(), GetBufferSizeX(), GetBufferSizeY(), GetBufferType(),
M_IMAGE+M_PROC+M_HOST_MEMORY+MemoryBank, &m_ProcessingElements[i].MilDest1Buffer);
MbufAllocColor(GetSystemID(), GetBufferSizeBand(), GetBufferSizeX(), GetBufferSizeY(), GetBufferType(),
M_IMAGE+M_PROC+M_HOST_MEMORY+MemoryBank, &m_ProcessingElements[i].MilDest2Buffer);
MbufAllocColor(GetSystemID(), GetBufferSizeBand(), GetBufferSizeX(), GetBufferSizeY(), GetBufferType(),
M_IMAGE+M_PROC+M_HOST_MEMORY+MemoryBank, &m_ProcessingElements[i].MilDest3Buffer);
MbufAlloc2d(GetSystemID(), 3, 3, 32+M_FLOAT, M_STRUCT_ELEMENT+M_HOST_MEMORY+MemoryBank,
&m_ProcessingElements[i].MilStructElement);
MbufAlloc1d(GetSystemID(), 12, 32+M_FLOAT, M_ARRAY+M_HOST_MEMORY+MemoryBank,
&m_ProcessingElements[i].MilFourCornerBuffer);
MbufAlloc2d(GetSystemID(), 3, 3, 32+M_FLOAT, M_ARRAY+M_HOST_MEMORY+MemoryBank,
&m_ProcessingElements[i].MilCoefficientsBuffer);
MbufClear(m_ProcessingElements[i].MilSourceBuffer, 0.0);
MbufClear(m_ProcessingElements[i].MilDest1Buffer, 0.0);
MbufClear(m_ProcessingElements[i].MilDest2Buffer, 0.0);
MbufClear(m_ProcessingElements[i].MilDest3Buffer, 0.0);
MbufClear(m_ProcessingElements[i].MilStructElement, 0.0);
MbufClear(m_ProcessingElements[i].MilFourCornerBuffer, 0.0);
MbufClear(m_ProcessingElements[i].MilCoefficientsBuffer, 0.0);
MbufLoad(WARP_PROCESSING_IMAGE, m_ProcessingElements[i].MilSourceBuffer);
GenerateCoefficientsBuffer(i);
}
}
CMPWarpProcessing::~CMPWarpProcessing()
{
StopThread();
for (MIL_INT i=0; i<=GetNumMemoryBank(); i++)
{
MbufFree(m_ProcessingElements[i].MilSourceBuffer);
MbufFree(m_ProcessingElements[i].MilDest1Buffer);
MbufFree(m_ProcessingElements[i].MilDest2Buffer);
MbufFree(m_ProcessingElements[i].MilDest3Buffer);
MbufFree(m_ProcessingElements[i].MilStructElement);
MbufFree(m_ProcessingElements[i].MilFourCornerBuffer);
MbufFree(m_ProcessingElements[i].MilCoefficientsBuffer);
}
delete [] m_ProcessingElements;
}
void CMPWarpProcessing::Process(MIL_INT ProcessingObjectIndex)
{
MimMorphic(m_ProcessingElements[ProcessingObjectIndex].MilSourceBuffer, m_ProcessingElements[ProcessingObjectIndex].MilDest1Buffer,
m_ProcessingElements[ProcessingObjectIndex].MilStructElement, M_DILATE, 1, M_GRAYSCALE);
MimMorphic(m_ProcessingElements[ProcessingObjectIndex].MilSourceBuffer, m_ProcessingElements[ProcessingObjectIndex].MilDest2Buffer,
m_ProcessingElements[ProcessingObjectIndex].MilStructElement, M_ERODE, 1, M_GRAYSCALE);
MimArith(m_ProcessingElements[ProcessingObjectIndex].MilDest1Buffer, m_ProcessingElements[ProcessingObjectIndex].MilDest2Buffer,
m_ProcessingElements[ProcessingObjectIndex].MilDest3Buffer, M_SUB);
MimWarp( m_ProcessingElements[ProcessingObjectIndex].MilDest3Buffer, m_ProcessingElements[ProcessingObjectIndex].MilDest2Buffer,
m_ProcessingElements[ProcessingObjectIndex].MilCoefficientsBuffer,
M_NULL, M_WARP_POLYNOMIAL,
M_BILINEAR+M_OVERSCAN_CLEAR);
const MIL_INT PixelAdjustmentFactor = 3;
MimArith(m_ProcessingElements[ProcessingObjectIndex].MilDest2Buffer, PixelAdjustmentFactor,
m_ProcessingElements[ProcessingObjectIndex].MilDest3Buffer, M_MULT_CONST);
UpdateWarpCoefficients(ProcessingObjectIndex);
UpdateDisplay(m_ProcessingElements[ProcessingObjectIndex].MilDest3Buffer);
}
void CMPWarpProcessing::UpdateWarpCoefficients(MIL_INT ProcessingObjectIndex)
{
m_IncrementCount = (m_IncrementCount+1) % MAX_INCREMENT;
switch(m_Direction)
{
case enWarpRight:
m_FourCorners[0] += WARP_INCREMENT;
m_FourCorners[2] += WARP_INCREMENT;
m_FourCorners[4] -= WARP_INCREMENT;
m_FourCorners[6] -= WARP_INCREMENT;
m_Direction = (m_IncrementCount==0)?enWarpFront:enWarpRight;
break;
case enWarpLeft:
m_FourCorners[0] -= WARP_INCREMENT;
m_FourCorners[2] -= WARP_INCREMENT;
m_FourCorners[4] += WARP_INCREMENT;
m_FourCorners[6] += WARP_INCREMENT;
m_Direction = (m_IncrementCount==0)?enWarpBack:enWarpLeft;
break;
case enWarpFront:
m_FourCorners[0] -= WARP_INCREMENT;
if (m_IncrementCount<MAX_INCREMENT/2)
m_FourCorners[1] -= WARP_INCREMENT;
else
m_FourCorners[1] += WARP_INCREMENT;
m_FourCorners[2] += WARP_INCREMENT;
if (m_IncrementCount<MAX_INCREMENT/2)
m_FourCorners[3] -= WARP_INCREMENT;
else
m_FourCorners[3] += WARP_INCREMENT;
m_FourCorners[4] -= WARP_INCREMENT;
if (m_IncrementCount<MAX_INCREMENT/2)
m_FourCorners[5] += WARP_INCREMENT;
else
m_FourCorners[5] -= WARP_INCREMENT;
m_FourCorners[6] += WARP_INCREMENT;
if (m_IncrementCount<MAX_INCREMENT/2)
m_FourCorners[7] += WARP_INCREMENT;
else
m_FourCorners[7] -= WARP_INCREMENT;
m_Direction = (m_IncrementCount==0)?enWarpLeft:enWarpFront;
break;
case enWarpBack:
m_FourCorners[0] += WARP_INCREMENT;
if (m_IncrementCount<MAX_INCREMENT/2)
m_FourCorners[1] -= WARP_INCREMENT;
else
m_FourCorners[1] += WARP_INCREMENT;
m_FourCorners[2] -= WARP_INCREMENT;
if (m_IncrementCount<MAX_INCREMENT/2)
m_FourCorners[3] -= WARP_INCREMENT;
else
m_FourCorners[3] += WARP_INCREMENT;
m_FourCorners[4] += WARP_INCREMENT;
if (m_IncrementCount<MAX_INCREMENT/2)
m_FourCorners[5] += WARP_INCREMENT;
else
m_FourCorners[5] -= WARP_INCREMENT;
m_FourCorners[6] -= WARP_INCREMENT;
if (m_IncrementCount<MAX_INCREMENT/2)
m_FourCorners[7] += WARP_INCREMENT;
else
m_FourCorners[7] -= WARP_INCREMENT;
m_Direction = (m_IncrementCount==0)?enWarpRight:enWarpBack;
break;
default:
break;
}
GenerateCoefficientsBuffer(ProcessingObjectIndex);
}
void CMPWarpProcessing::GenerateCoefficientsBuffer(MIL_INT ProcessingObjectIndex)
{
MbufPut(m_ProcessingElements[ProcessingObjectIndex].MilFourCornerBuffer, m_FourCorners);
MgenWarpParameter(m_ProcessingElements[ProcessingObjectIndex].MilFourCornerBuffer,
m_ProcessingElements[ProcessingObjectIndex].MilCoefficientsBuffer, M_NULL,
M_WARP_4_CORNER_REVERSE, M_DEFAULT, M_NULL, M_NULL);
}