#include "stdafx.h"
|
#include "ASGInspection.h"
|
|
#ifdef _DEBUG
|
#define new DEBUG_NEW
|
#undef THIS_FILE
|
static char THIS_FILE[] = __FILE__;
|
#endif
|
|
/////////////////////////////////////////////////////////////////////////////
|
// CASGInspection
|
|
CASGInspection::CASGInspection()
|
{
|
m_pPixelX = m_pPixelY = m_nPixelCnt = NULL;
|
m_pPixelType = m_pPixelValue = NULL;
|
m_pDefectParing = NULL;
|
m_nDefectParingCnt = 0;
|
|
InitializeCriticalSection(&m_csDefect);
|
}
|
|
CASGInspection::~CASGInspection()
|
{
|
DeleteCriticalSection(&m_csDefect);
|
}
|
|
BOOL CASGInspection::GetBufferCheck()
|
{
|
if(m_pPixelX == NULL || m_pPixelY == NULL || m_pPixelType == NULL || m_pPixelValue == NULL || m_nPixelCnt == NULL)
|
return FALSE;
|
|
return TRUE;
|
}
|
|
void CASGInspection::SetPixelData(int x, int y, int sub, int threshold)
|
{
|
m_pPixelX[*m_nPixelCnt] = x;
|
m_pPixelY[*m_nPixelCnt] = y;
|
|
if(sub > 0)
|
m_pPixelType[*m_nPixelCnt] = 0;
|
else
|
m_pPixelType[*m_nPixelCnt] = 1;
|
m_pPixelValue[*m_nPixelCnt] = abs(sub);
|
|
*m_nPixelCnt++;
|
}
|
|
BOOL CASGInspection::InsertPairing(int x, int y, int sub, int threshold, int graySrc, int grayRef)
|
{
|
EnterCriticalSection(&m_csDefect);
|
|
if(m_pDefectParing == NULL ||
|
m_nDefectParingCnt >= MAX_DEFECTPIX_NUM || m_nDefectParingCnt < 0)
|
{
|
LeaveCriticalSection(&m_csDefect);
|
return FALSE;
|
}
|
|
int nCnt = m_nDefectParingCnt;
|
m_pDefectParing[nCnt].s_DefectPair= DEFPAIR_PPAIR;
|
if(sub > 0)
|
m_pDefectParing[nCnt].s_DefectType= DEFTYPE_BLACK;
|
else
|
m_pDefectParing[nCnt].s_DefectType= DEFTYPE_WHITE;
|
|
m_pDefectParing[nCnt].s_fDefectPeak= abs(sub);
|
|
m_pDefectParing[nCnt].s_nDefectX= x;
|
m_pDefectParing[nCnt].s_nDefectY= y;
|
m_pDefectParing[nCnt].s_nGraySrc= graySrc;
|
m_pDefectParing[nCnt].s_nGrayRef= grayRef;
|
m_nDefectParingCnt++;
|
|
LeaveCriticalSection(&m_csDefect);
|
|
return TRUE;
|
}
|
|
void CASGInspection::VConvolutionConvC(CConvParam *pParam)
|
{
|
if(m_pDefectParing == NULL)
|
return;
|
|
m_nDefectParingCnt = 0;
|
|
int sx, ex, sy, ey;
|
sx= pParam->s_RectConv.left;
|
ex= pParam->s_RectConv.right;
|
sy= pParam->s_RectConv.top;
|
ey= pParam->s_RectConv.bottom;
|
|
int nPitch = (int)pParam->GetRealScanPitch();
|
int nPitch_p = nPitch+1;
|
int nConvWidth = pParam->s_nConvWidth;
|
int nConvHeight = pParam->s_nConvHeight;
|
int nHalfConvWidth = nConvWidth/2;
|
int nHalfConvHeight = nConvHeight/2;
|
|
ex -= nConvWidth;
|
ey -= nConvHeight;
|
|
#define CONV_PIXEL_DIVIDE 16
|
|
int Threshold = pParam->s_nThreshold;
|
int SubPixel = (int)((pParam->GetRealScanPitch()-nPitch)*CONV_PIXEL_DIVIDE+ 0.5);
|
int SubPixel_DIVIDE = CONV_PIXEL_DIVIDE - SubPixel;
|
int NegThres = Threshold*-1;
|
|
double f2pitch = pParam->GetRealScanPitch()*2;
|
int n2Pitch = (int)f2pitch;
|
int n2Pitch_p = n2Pitch+1;
|
int Sub2Pixel = (int)((f2pitch-n2Pitch)*CONV_PIXEL_DIVIDE+ 0.5);
|
int Sub2Pixel_DIVIDE = CONV_PIXEL_DIVIDE - Sub2Pixel;
|
|
int SrcValue = 0;
|
int DestValue = 0;
|
int DestValue1 = 0;
|
int DestValue2 = 0;
|
int SubValue = 0;
|
int i = 0;
|
|
int iLoopX,iLoopY;
|
int DestValue2Pitch,SubValue2Pitch;
|
int j;
|
int nImageWidth = pParam->s_nFrameWidth;
|
LPBYTE pSrc = pParam->s_lpBuffer;
|
int nRefVal;
|
|
//0~pitch °Å¸®±îÁö
|
for( j = sy; j < sy+ nPitch_p; j++)
|
{
|
for( i = sx; i < ex; i++)
|
{
|
SrcValue = DestValue = DestValue2Pitch = 0;
|
for(iLoopX=i;iLoopX<i+nConvWidth;iLoopX++)
|
{
|
for(iLoopY=j;iLoopY<j+nConvHeight;iLoopY++)
|
{
|
nRefVal = *(pSrc+iLoopX+iLoopY*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
SrcValue += nRefVal;
|
|
nRefVal = *(pSrc+iLoopX+(iLoopY+nPitch)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue1 = nRefVal;
|
nRefVal = *(pSrc+iLoopX+(iLoopY+nPitch_p)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue2 = nRefVal;
|
DestValue += (DestValue1*(SubPixel_DIVIDE) + DestValue2*(SubPixel))/CONV_PIXEL_DIVIDE;
|
|
nRefVal = *(pSrc+iLoopX+(iLoopY+n2Pitch)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue1 = nRefVal;
|
nRefVal = *(pSrc+iLoopX+(iLoopY+n2Pitch_p)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue2 = nRefVal;
|
DestValue2Pitch += (DestValue1*(Sub2Pixel_DIVIDE) + DestValue2*Sub2Pixel)/CONV_PIXEL_DIVIDE;
|
}
|
}
|
|
SubValue = DestValue - SrcValue;
|
SubValue2Pitch = DestValue2Pitch - SrcValue;
|
|
if ((SubValue > Threshold && SubValue2Pitch > Threshold) ||
|
(SubValue < NegThres && SubValue2Pitch < NegThres))
|
{
|
|
if(!InsertPairing(i+nHalfConvWidth,j+nHalfConvHeight,abs(SubValue)>abs(SubValue2Pitch)?SubValue:SubValue2Pitch,Threshold,SrcValue, DestValue2Pitch))
|
return;
|
}
|
}
|
}
|
|
//pitch~pitch °Å¸®±îÁö
|
for( j = sy+ nPitch+1; j < ey-nPitch_p; j++)
|
{
|
for( i = sx; i < ex; i++)
|
{
|
SrcValue = DestValue = DestValue2Pitch = 0;
|
for(iLoopX=i;iLoopX<i+nConvWidth;iLoopX++)
|
{
|
for(iLoopY=j;iLoopY<j+nConvHeight;iLoopY++)
|
{
|
nRefVal = *(pSrc+iLoopX+iLoopY*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
SrcValue += nRefVal;
|
|
nRefVal = *(pSrc+iLoopX+(iLoopY+nPitch)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue1 = nRefVal;
|
nRefVal = *(pSrc+iLoopX+(iLoopY+nPitch_p)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue2 = nRefVal;
|
DestValue += (DestValue1*(SubPixel_DIVIDE) + DestValue2*(SubPixel))/CONV_PIXEL_DIVIDE;
|
|
nRefVal = *(pSrc+iLoopX+(iLoopY-nPitch)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue1 = nRefVal;
|
nRefVal = *(pSrc+iLoopX+(iLoopY-nPitch_p)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue2 = nRefVal;
|
DestValue2Pitch += (DestValue1*(SubPixel_DIVIDE) + DestValue2*SubPixel)/CONV_PIXEL_DIVIDE;
|
}
|
}
|
|
SubValue = DestValue - SrcValue;
|
SubValue2Pitch = DestValue2Pitch - SrcValue;
|
|
if ((SubValue > Threshold && SubValue2Pitch > Threshold) ||
|
(SubValue < NegThres && SubValue2Pitch < NegThres))
|
{
|
if(!InsertPairing(i+nHalfConvWidth,j+nHalfConvHeight,abs(SubValue)>abs(SubValue2Pitch)?SubValue:SubValue2Pitch,Threshold,SrcValue, DestValue2Pitch))
|
return;
|
}
|
}
|
}
|
|
//0~pitch °Å¸®±îÁö
|
for( j = ey-nPitch_p; j < ey; j++)
|
{
|
for( i = sx; i < ex; i++)
|
{
|
SrcValue = DestValue = DestValue2Pitch = 0;
|
for(iLoopX=i;iLoopX<i+nConvWidth;iLoopX++)
|
{
|
for(iLoopY=j;iLoopY<j+nConvHeight;iLoopY++)
|
{
|
nRefVal = *(pSrc+iLoopX+iLoopY*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
SrcValue += nRefVal;
|
|
nRefVal = *(pSrc+iLoopX+(iLoopY-nPitch)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue1 = nRefVal;
|
nRefVal = *(pSrc+iLoopX+(iLoopY-nPitch_p)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue2 = nRefVal;
|
DestValue += (DestValue1*(SubPixel_DIVIDE) + DestValue2*(SubPixel))/CONV_PIXEL_DIVIDE;
|
|
nRefVal = *(pSrc+iLoopX+(iLoopY-n2Pitch)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue1 = nRefVal;
|
nRefVal = *(pSrc+iLoopX+(iLoopY-n2Pitch_p)*nImageWidth);
|
if(nRefVal >= pParam->s_nThresholdSupress)
|
nRefVal = pParam->s_nThresholdSupress;
|
DestValue2 = nRefVal;
|
DestValue2Pitch += (DestValue1*(Sub2Pixel_DIVIDE) + DestValue2*Sub2Pixel)/CONV_PIXEL_DIVIDE;
|
}
|
}
|
|
SubValue = DestValue - SrcValue;
|
SubValue2Pitch = DestValue2Pitch - SrcValue;
|
|
if ((SubValue > Threshold && SubValue2Pitch > Threshold) ||
|
(SubValue < NegThres && SubValue2Pitch < NegThres))
|
{
|
if(!InsertPairing(i+nHalfConvWidth,j+nHalfConvHeight,abs(SubValue)>abs(SubValue2Pitch)?SubValue:SubValue2Pitch,Threshold,SrcValue, DestValue2Pitch))
|
return;
|
}
|
}
|
}
|
}
|