#include "cakerfcheck_h.h"
#define KERG_SHAPE_OFFSET    0
#include "logic_local_def.h"
CAKerfCheck_H::CAKerfCheck_H(const QString &name, int32_t id)
    : CAlgorithm(name,id)
{
    m_Variants.add_Variant(Var_KerfCheck_H_Rangemax,
                           QObject::trUtf8("RangeMax"),int(0),false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_KerfCheck_H_Rangemin,
                           QObject::trUtf8("RangeMin"),int(0),false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_KerfCheck_H_Threshold,
                           QObject::trUtf8("Threshold"),int(0),false,
                           Variant::TOXML);
    m_Variants.add_Variant(Var_KerfCheck_H_Direct_x,
                           QObject::trUtf8("Direct_x"),false,false,
                           Variant::TOXML);

    m_Variants.add_Variant(Var_KerfCheck_H_Mount,
                           QObject::trUtf8("Mount"),int(0),false,
                           Variant::TOXML);

    m_Variants.add_Variant(Var_KerfCheck_H_Mount,
                           QObject::trUtf8("Mount"),int(0),false,
                           Variant::TOXML);

    m_Variants.add_Variant(Var_KerfCheck_H_Minlength,
                           QObject::trUtf8("Minlength"),int(0),false,
                           Variant::TOXML);

    m_Variants.add_Variant(Var_KerfCheck_H_Maxlength,
                           QObject::trUtf8("Maxlength"),int(0),false,
                           Variant::TOXML);
}

int32_t CAKerfCheck_H::exe(CMImage *pmimage)
{
    int direct_x=m_Variants.get_VData(Var_KerfCheck_H_Direct_x)->toBool();
    if(direct_x)
    {
        return exe_XDirect(pmimage);
    }
    return exe_YDirect(pmimage);
}


void CAKerfCheck_H::showresult_shape(CImageContainer *pContainer)
{
    CAlgorithm::showresult_shape( pContainer);
    int direct_x=m_Variants.get_VData(Var_KerfCheck_H_Direct_x)->toBool();
    if(direct_x)
    {
        showresult_XDirect(pContainer);
    }
    else
    {
        showresult_YDirect(pContainer);
    }
}

void CAKerfCheck_H::showragion_static(CImageContainer *pContainer)
{
    CAlgorithm::showragion_static( pContainer);
    int direct_x=m_Variants.get_VData(Var_KerfCheck_H_Direct_x)->toBool();
    if(direct_x)
    {
        showragion_static_XDirect(pContainer);
    }
    else
    {
        showragion_static_YDirect(pContainer);
    }
}

void CAKerfCheck_H::showresult_XDirect(CImageContainer *pContainer)
{
	//画找到的切割道的线段
    int isize=lcenters.size();
    CMImage *pmimage=&pContainer->m_Image;
    for(int i=0;i<isize;i++)
    {
        CShape* pShape=new CLine(CMPointd(0,lcenters[i].tY),
                                 CMPointd(pmimage->m_width,lcenters[i].tY));
        pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
        pShape->m_iConfig=shape_dash|Qt::cyan;
		if(i%2>0){
			CMPointd center;
			center.tX = lcenters[i].tX-300;
			center.tY = (lcenters[i].tY+lcenters[i-1].tY)/2;
			CCross* crossShape = new CCross(300, 20, center);
			crossShape->m_iConfig=shape_solid|Qt::cyan;
			pContainer->addShape(crossShape, CImageContainer::STATIC_SHAPE);
		}
    }

	//绘制统计的平均灰度值
	CMPointd Center=CMPointd(pmimage->m_width/2,pmimage->m_height/2);
	int iCenter=Center.tY;;
	int iRange=  m_Variants.get_VData(Var_KerfCheck_H_Rangemax)->toInt()/2.0;
	int iStart= iCenter-iRange, iEnd=iCenter+iRange;
	int iSize = iEnd-iStart+1;

    int iSizeAvg=m_Data.size();
    CXLD* pxld=new CXLD();
    //pxld->m_vpointfs.resize(iSizeAvg);
	pxld->m_vpointfs.resize(iSize);
    //for(int i=0;i<iSizeAvg;i++)
	//HACK:m_Data前没有按Range筛选，所以这里是整张图像里面的数据
	int startIndex = 0;
	for(int i=max(iStart, 0);i<min(iEnd+1, iSizeAvg);i++)
    {
        CMPointd pt=m_Data[i];
        pt.tX=pmimage->m_width/2+300+pt.tX;
        pxld->m_vpointfs[startIndex++]=pt;
    }
	pxld->m_iConfig = pxld->m_iConfig|Qt::blue;

    pContainer->addShape(pxld,CImageContainer::STATIC_SHAPE);
	//阈值的线
    int iThres=m_Variants.get_VData(Var_KerfCheck_H_Threshold)->toInt();
    CShape* pShape=new CLine(CMPointd(pmimage->m_width/2+300+iThres,0),
                     CMPointd(pmimage->m_width/2+300+iThres,pmimage->m_height));
	pShape->m_iConfig = pShape->m_iConfig|Qt::green;
    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
}

void CAKerfCheck_H::showresult_YDirect(CImageContainer *pContainer)
{
    int isize=lcenters.size();
    CMImage *pmimage=&pContainer->m_Image;
    for(int i=0;i<isize;i++)
    {
        CShape* pShape=new CLine(CMPointd(lcenters[i].tX,0),
                                 CMPointd(lcenters[i].tX,pmimage->m_height));
        pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
        pShape->m_iConfig=shape_dash|Qt::green;
    }

    int iSizeAvg=m_Data.size();
    CXLD* pxld=new CXLD();
    pxld->m_vpointfs.resize(iSizeAvg);
    for(int i=0;i<iSizeAvg;i++)
    {
        CMPointd pt=m_Data[i];
        pt.tY=pmimage->m_height/2+pt.tY;
        pxld->m_vpointfs[i]=pt;
    }
    pContainer->addShape(pxld,CImageContainer::STATIC_SHAPE);
    int iThres=m_Variants.get_VData(Var_KerfCheck_H_Threshold)->toInt();
    CShape* pShape=new CLine(CMPointd(0,pmimage->m_height/2+iThres),
                     CMPointd(pmimage->m_width,pmimage->m_height/2+iThres));
    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);


}

void CAKerfCheck_H::showragion_static_XDirect(CImageContainer *pContainer)
{
    CMImage *pmimage=&pContainer->m_Image;
//    int iSizeAvg=pmimage->m_height;
    int iRange=  m_Variants.get_VData(Var_KerfCheck_H_Rangemax)->toInt()/2.0;
    int iRangeMin= m_Variants.get_VData(Var_KerfCheck_H_Rangemin)->toInt()/2.0;
    CMPointd Center=CMPointd(pmimage->m_width/2,pmimage->m_height/2);
    int iCenter=Center.tY;
    int iStart= iCenter-iRange, iEnd=iCenter-iRangeMin;
    int iStart1= iCenter+iRangeMin, iEnd1=iCenter+iRange;
    //    iStart=max(1,iStart);
    //    iEnd=min(iSizeAvg-1,iEnd);
    //    iStart1=max(1,iStart1);
    //    iEnd1=min(iSizeAvg-1,iEnd1);

    CShape* pShape=new CRect(pmimage->m_width,iStart,
                             CMPointd(Center.tX,iStart/2));
    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
    pShape->m_iConfig=shape_fill|Qt::darkMagenta;

    if(iStart1!=iEnd)
    {
    pShape=new CRect(pmimage->m_width,(iStart1-iEnd),
                     CMPointd(Center.tX,(iStart1+iEnd)/2));
    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
    pShape->m_iConfig=shape_fill|Qt::darkMagenta;

    }
    pShape=new CRect(pmimage->m_width,(pmimage->m_height-iEnd1),
                     CMPointd(Center.tX,(pmimage->m_height+iEnd1)/2));
    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
    pShape->m_iConfig=shape_fill|Qt::darkMagenta;


//    int iThres=mparams[THRESHOLD]->getDataDirect();
//    pShape=new CLine(CMPointd(pmimage->m_width-iThres-KERG_SHAPE_OFFSET,0),
//                     CMPointd(pmimage->m_width-iThres-KERG_SHAPE_OFFSET,pmimage->m_height));
//    pShape->m_iConfig=shape_fill|Qt::green;
//    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
}

void CAKerfCheck_H::showragion_static_YDirect(CImageContainer *pContainer)
{
    CMImage *pmimage=&pContainer->m_Image;
//    int iSizeAvg=pmimage->m_width;
    int iRange=  m_Variants.get_VData(Var_KerfCheck_H_Rangemax)->toInt()/2.0;
    int iRangeMin= m_Variants.get_VData(Var_KerfCheck_H_Rangemin)->toInt()/2.0;
    CMPointd Center=CMPointd(pmimage->m_width/2,pmimage->m_height/2);
    int iCenter=Center.tX;
    int iStart= iCenter-iRange, iEnd=iCenter-iRangeMin;
    int iStart1= iCenter+iRangeMin, iEnd1=iCenter+iRange;
    //    iStart=max(1,iStart);
    //    iEnd=min(iSizeAvg-1,iEnd);
    //    iStart1=max(1,iStart1);
    //    iEnd1=min(iSizeAvg-1,iEnd1);

    CShape* pShape=new CRect((iStart),pmimage->m_height,
                             CMPointd(iStart/2,Center.tY));
    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
    pShape->m_iConfig=shape_fill|Qt::green;

    if(iStart1!=iEnd)
    {
        pShape=new CRect((iStart1-iEnd),pmimage->m_height,
                         CMPointd((iStart1+iEnd)/2,Center.tY));
        pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
        pShape->m_iConfig=shape_fill|Qt::green;
    }

    pShape=new CRect((pmimage->m_width-iEnd1),pmimage->m_height,
                     CMPointd((pmimage->m_width+iEnd1)/2,Center.tY));
    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
    pShape->m_iConfig=shape_fill|Qt::green;

//    int iThres=mparams[THRESHOLD]->getDataDirect();
//    pShape=new CLine(CMPointd(0,pmimage->m_height-iThres-KERG_SHAPE_OFFSET),
//                     CMPointd(pmimage->m_width,pmimage->m_height-iThres-KERG_SHAPE_OFFSET));
//    pShape->m_iConfig=shape_fill|Qt::green;
//    pContainer->addShape(pShape,CImageContainer::STATIC_SHAPE);
}

int32_t CAKerfCheck_H::exe_XDirect(CMImage *pmimage)
{
    timecost.StartTime();
    CAlgorithm::exe(pmimage);
    lcenters.clear();
    int iThreshold=m_Variants.get_VData(Var_KerfCheck_H_Threshold)->toInt() ;
    GetXDirectAvg(pmimage);
    int iSizeAvg=m_Data.size();
    MqDebug()<<QObject::trUtf8("iSizeAvg:%1").arg(iSizeAvg);
    int iRange=  m_Variants.get_VData(Var_KerfCheck_H_Rangemax)->toInt()/2.0;	//范围大
    int iRangeMin= m_Variants.get_VData(Var_KerfCheck_H_Rangemin)->toInt()/2.0; //范围小
    CMPointd Center=CMPointd(pmimage->m_width/2,pmimage->m_height/2);
    int iCenter=Center.tY;
    int iStart= iCenter-iRange, iEnd=iCenter-iRangeMin;	//上部分
    int iStart1= iCenter+iRangeMin, iEnd1=iCenter+iRange; //下部分
    iStart=max(1,iStart);		//iSizeAvg 数组的最大尺寸
    iEnd=min(iSizeAvg-1,iEnd);
    iStart1=max(1,iStart1);
    iEnd1=min(iSizeAvg-1,iEnd1);
//    int bmount=mparams[MOUNT]->getDataDirect();
//    if(!bmount)
    if(1)
    {
		GetXThroughSkipCenter(iThreshold, iStart,  iEnd1,  iStart1,  iEnd, Center);
        //GetXThrough(  iThreshold,  iStart,  iEnd,  iStart1,  iEnd1, Center);
    }
    else
    {
        GetXMount(  iThreshold,  iStart,  iEnd,  iStart1,  iEnd1, Center);
    }
    timecost.StopTime();
    return RTN_SUCCESS1;
}

int32_t CAKerfCheck_H::exe_YDirect(CMImage *pmimage)
{
    timecost.StartTime();
    CAlgorithm::exe(pmimage);
    lcenters.clear();
    int iThreshold=m_Variants.get_VData(Var_KerfCheck_H_Threshold)->toInt() ;
    GetYDirectAvg(pmimage);
    int iSizeAvg=m_Data.size();
    MqDebug()<<QObject::trUtf8("iSizeAvg:%1").arg(iSizeAvg);
    int iRange=  m_Variants.get_VData(Var_KerfCheck_H_Rangemax)->toInt()/2.0;
    int iRangeMin= m_Variants.get_VData(Var_KerfCheck_H_Rangemin)->toInt()/2.0;
    CMPointd Center=CMPointd(pmimage->m_width/2,pmimage->m_height/2);
    int iCenter=Center.tX;
    int iStart= iCenter-iRange, iEnd=iCenter-iRangeMin;
    int iStart1= iCenter+iRangeMin, iEnd1=iCenter+iRange;
    iStart=max(1,iStart);
    iEnd=min(iSizeAvg-1,iEnd);
    iStart1=max(1,iStart1);
    iEnd1=min(iSizeAvg-1,iEnd1);
//    int bmount=mparams[MOUNT]->getDataDirect();
//    if(!bmount)
    if(1)
    {
        GetYThrough(  iThreshold,  iStart,  iEnd,  iStart1,  iEnd1, Center);
    }
    else
    {
        GetYMount(  iThreshold,  iStart,  iEnd,  iStart1,  iEnd1, Center);
    }
    timecost.StopTime();
    return RTN_SUCCESS1;
}

void CAKerfCheck_H::GetYDirectAvg(CMImage* pmimage)
{
    Q_ASSERT(pmimage);
    int width=pmimage->m_width;
    int height=pmimage->m_height;
    m_Data.resize(width);
    uint8_t *ptr= pmimage->m_pRawImage;
    double * pData=new double[width];
    for (int i=0;i<width;i++)
    {
        double dAvgYdirect=0;
        for(int j=0;j<height;j++)
        {
            dAvgYdirect+=ptr[j*width+i];
        }
        dAvgYdirect/=height;
        m_Data[i].tX=i;
        pData[i]=dAvgYdirect;
    }
    for(int i=1;i<width-1;i++)
    {
       m_Data[i].tY=(pData[i-1]+pData[i+1])/2;
    }
    delete [] pData;
}

void CAKerfCheck_H::GetXDirectAvg(CMImage* pmimage)
{
    int width=pmimage->m_width;
    int height=pmimage->m_height;
    m_Data.resize(height);
    uint8_t *ptr= pmimage->m_pRawImage;
    double * pData=new double[height];
    for (int j=0;j<height;j++)
    {
        double dAvgXdirect=0;
        for (int i=0;i<width;i++)
        {
            dAvgXdirect+=ptr[j*width+i];
        }
        dAvgXdirect/=width;
        pData[j]=dAvgXdirect;
        m_Data[j].tY=j;
    }

    for (int j=1;j<height-1;j++)
    {
        m_Data[j].tX=(pData[j-1]+pData[j+1])/2;
		//MqDebug() << "Row:" << j << "Data:" << m_Data[j].tX;
    }
    delete [] pData;
}

void CAKerfCheck_H::Serialize(QDataStream &stream, bool bwrite, int iVersion)
{
    CAlgorithm::Serialize(stream,bwrite,iVersion);
}

void CAKerfCheck_H::GetYThrough(int iThreshold,int iStart,int iEnd,int iStart1,int iEnd1,CMPointd& Center)
{
/*    for (int i=iStart+1;i<=iEnd-1;i+=2)
    {
        int igrayValuePre=m_Data[i-1].tY;
        int igrayValueNext=m_Data[i+1].tY;
        if (igrayValuePre<=iThreshold&&igrayValueNext>iThreshold)
        {
            lcenters.push_back(CMPointd(i,Center.tY));
        }
        else if (igrayValuePre>=iThreshold&&igrayValueNext<iThreshold)
        {
            lcenters.push_back(CMPointd(i,Center.tY));
        }
    }
    for (int i=iStart1+1;i<=iEnd1-1;i+=2)
    {
        int igrayValuePre=m_Data[i-1].tY;
        int igrayValueNext=m_Data[i+1].tY;
        if (igrayValuePre<=iThreshold&&igrayValueNext>iThreshold)
        {
            lcenters.push_back(CMPointd(i,Center.tY));
        }
        else if (igrayValuePre>=iThreshold&&igrayValueNext<iThreshold)
        {
            lcenters.push_back(CMPointd(i,Center.tY));
        }
    }
*/
    if(iEnd==iStart1)
    {
        GetYThrough_single(iThreshold,iStart,iEnd1,Center);
    }
    else
    {
        GetYThrough_single(iThreshold,iStart,iEnd,Center);
        GetYThrough_single(iThreshold,iStart1,iEnd1,Center);
    }
}

void CAKerfCheck_H::GetYThrough_single(int iThreshold, int iStart, int iEnd, CMPointd &Center)
{
    int irealstart=iStart,irealend=iEnd;
    for(int i=iStart;i<=iEnd;i++)
    {
        if(m_Data[i].tY>iThreshold)
        {
          irealstart=i;
          break;
        }
    }
    for(int i=iEnd;i>=iStart;i--)
    {
        if(m_Data[i].tY>iThreshold)
        {
          irealend=i;
          break;
        }
    }
    int lLineStart=0,lLineEnd=0;
    bool blineexist=false;
    int iminlength=m_Variants.get_VData(Var_KerfCheck_H_Minlength)->toInt();
    int imaxlength=m_Variants.get_VData(Var_KerfCheck_H_Maxlength)->toInt();

    for(int i=irealstart;i<irealend;i++)
    {

        if(m_Data[i].tY<=iThreshold&&!blineexist)
        {
            blineexist=true;
            lLineStart=i;
        }
        else if(m_Data[i].tY>=iThreshold&&blineexist)
        {
            blineexist=false;
            lLineEnd=i;
            int ilength=abs(lLineEnd-lLineStart);
            if(ilength>=iminlength&&
               ilength<=imaxlength)
            {
                lcenters.push_back(CMPointd(lLineStart,Center.tY));
                lcenters.push_back(CMPointd(lLineEnd,Center.tY));
            }
        }
    }
}

void CAKerfCheck_H::GetXThrough(int iThreshold,int iStart,int iEnd,
                                int iStart1,int iEnd1,
                                CMPointd& Center)
{
 /*   for (int i=iStart+1;i<=iEnd-1;i+=2)
    {
        int igrayValue=m_Data[i-1].tX;
        int igrayValue1=m_Data[i+1].tX;
        if (igrayValue<iThreshold&&igrayValue1>iThreshold)
        {
            lcenters.push_back(CMPointd(Center.tX,i));
        }
        else if (igrayValue>iThreshold&&igrayValue1<iThreshold)
        {
            lcenters.push_back(CMPointd(Center.tX,i));
        }
    }
    for (int i=iStart1+1;i<=iEnd1-1;i+=2)
    {
        int igrayValue=m_Data[i-1].tX;
        int igrayValue1=m_Data[i+1].tX;
        if (igrayValue<iThreshold&&igrayValue1>iThreshold)
        {
            lcenters.push_back(CMPointd(Center.tX,i));
        }
        else if (igrayValue>iThreshold&&igrayValue1<iThreshold)
        {
            lcenters.push_back(CMPointd(Center.tX,i));
        }
    }
    */
    if(iEnd==iStart1)
    {
        GetXThrough_single(iThreshold,iStart,iEnd1,Center);
    }
    else
    {
        GetXThrough_single(iThreshold,iStart,iEnd,Center);
        GetXThrough_single(iThreshold,iStart1,iEnd1,Center);
    }
}

void CAKerfCheck_H::GetXThroughSkipCenter(int iThreshold,int iStart,int iEnd, int iSkipBegin, int iSkipEnd, CMPointd& Center)
{
	int irealstart=iStart,irealend=iEnd;
	for(int i=iStart;i<=iEnd;i++)
	{
		if(m_Data[i].tX>iThreshold)
		{
			irealstart=i;
			break;
		}
	}
	for(int i=iEnd;i>=iStart;i--)
	{
		if(m_Data[i].tX>iThreshold)
		{
			irealend=i;
			break;
		}
	}
	int lLineStart=0,lLineEnd=0;
	bool blineexist=false;
	int iminlength=m_Variants.get_VData(Var_KerfCheck_H_Minlength)->toInt();
	int imaxlength=m_Variants.get_VData(Var_KerfCheck_H_Maxlength)->toInt();

	for(int i=irealstart;i<irealend;i++)
	{

		if(iSkipBegin!=iSkipEnd&&iSkipBegin>=i&&iSkipEnd<=i)
			continue;
		if(m_Data[i].tX<=iThreshold&&!blineexist)
		{
			blineexist=true;
			lLineStart=i;
		}
		else if(m_Data[i].tX>=iThreshold&&blineexist)
		{
			blineexist=false;
			lLineEnd=i;
			int ilength=abs(lLineEnd-lLineStart);
			if(ilength>=iminlength&&
				ilength<=imaxlength)
			{
				lcenters.push_back(CMPointd(Center.tX,lLineStart));
				lcenters.push_back(CMPointd(Center.tX,lLineEnd));
			}
		}
	}
}

void CAKerfCheck_H::GetXThrough_single(int iThreshold, int iStart,
                                       int iEnd, CMPointd &Center)
{
    int irealstart=iStart,irealend=iEnd;
    for(int i=iStart;i<=iEnd;i++)
    {
        if(m_Data[i].tX>iThreshold)
        {
          irealstart=i;
          break;
        }
    }
    for(int i=iEnd;i>=iStart;i--)
    {
        if(m_Data[i].tX>iThreshold)
        {
          irealend=i;
          break;
        }
    }
    int lLineStart=0,lLineEnd=0;
    bool blineexist=false;
    int iminlength=m_Variants.get_VData(Var_KerfCheck_H_Minlength)->toInt();
    int imaxlength=m_Variants.get_VData(Var_KerfCheck_H_Maxlength)->toInt();

    for(int i=irealstart;i<irealend;i++)
    {

        if(m_Data[i].tX<=iThreshold&&!blineexist)
        {
            blineexist=true;
            lLineStart=i;
        }
        else if(m_Data[i].tX>=iThreshold&&blineexist)
        {
            blineexist=false;
            lLineEnd=i;
            int ilength=abs(lLineEnd-lLineStart);
            if(ilength>=iminlength&&
               ilength<=imaxlength)
            {
                lcenters.push_back(CMPointd(Center.tX,lLineStart));
                lcenters.push_back(CMPointd(Center.tX,lLineEnd));
            }
        }
    }
}

void CAKerfCheck_H::GetYMount(int iThreshold,int iStart,int iEnd,int iStart1,int iEnd1,CMPointd& Center)
{
    bool bstartMount=false;
    int istartMountPos=0;
    for (int i=iStart;i<=iEnd;i++)
    {
        int igrayValue=m_Data[i].tY;
        if (igrayValue>iThreshold)
        {
            bstartMount=true;
            istartMountPos=i;
        }
        else
        {
            if(bstartMount)
            {
                lcenters.push_back(CMPointd((istartMountPos+i-1)/2,
                                            Center.tY));
                bstartMount=false;
            }
        }
    }
    bstartMount=false;
    istartMountPos=0;
    for (int i=iStart1;i<=iEnd1;i++)
    {
        int igrayValue=m_Data[i].tY;
        if (igrayValue>iThreshold)
        {
            bstartMount=true;
            istartMountPos=i;
        }
        else
        {
            if(bstartMount)
            {
                lcenters.push_back(CMPointd((istartMountPos+i-1)/2,
                                            Center.tY));
                bstartMount=false;
            }
        }
    }
}

void CAKerfCheck_H::GetXMount(int iThreshold,int iStart,int iEnd,int iStart1,int iEnd1,CMPointd& Center)
{
    bool bstartMount=false;
    int istartMountPos=0;
    for (int i=iStart;i<=iEnd;i++)
    {
        int igrayValue=m_Data[i].tX;
        if (igrayValue>iThreshold)
        {
            bstartMount=true;
            istartMountPos=i;
        }
        else
        {
            if(bstartMount)
            {
                lcenters.push_back(CMPointd(Center.tX,
                                            (istartMountPos+i-1)/2));
                bstartMount=false;
            }
        }
    }
    for (int i=iStart1;i<=iEnd1;i++)
    {
        int igrayValue=m_Data[i].tX;
        if (igrayValue>iThreshold)
        {
            bstartMount=true;
            istartMountPos=i;
        }
        else
        {
            if(bstartMount)
            {
                lcenters.push_back(CMPointd(Center.tX,
                                            (istartMountPos+i-1)/2));
                bstartMount=false;
            }
        }
    }
}

