#pragma once


#include "areacal.h"

#define SD_MAX_COLOR_CHANNELS  3
//
//typedef struct __sd_surface_opts_
//{
//	int       binzType ;    //二值方法：简单，区间，最大,最小，平均（最小错误），
//	int       binzChannels; //通道:ALL, RED, GREEN, BLUE, RG,RB, GB 
//	RvColor   target;
//	RvColor   toler; 
//}SD_SURFACE_OPTS;
//
//typedef struct __sd_scratch_opts_
//{
//	int   lenThres;
//	int   thickThres;
//}SD_SCRATCH_OPTS;
// 
//typedef struct __sd_dirty_opts_
//{
//	int grayLower;
//	int grayUpper;
//	int sizeThres;	  
//}SD_DIRTY_OPTS;
//
//typedef struct __sd_deficiency_opts_
//{
//	int grayLower;
//	int grayUpper;
//	int sizeThres;	  
//}SD_DEFICIENCY_OPTS;
//
//typedef struct __sd_stone_opts_
//{
//	int grayLower;
//	int grayUpper;
//	int sizeThres;
//	float circularThres;
//}SD_STONE_OPTS;
//
//typedef struct __sd_bubble_opts_
//{
//	int grayLower;
//	int grayUpper;
//	int sizeThres;
//	float circularThres;
//}SD_BUBBLE_OPTS;
//
// typedef struct __sd_surface_
// {
//	 RvPoint leftTop;
//	 UINT    area;
//	 RvBox2D box;
//	 RvMask  mask;
// }SD_SURFACE;
//
//typedef struct __sd_blob_opts_
//{
//	int   binzType;
//	int   binzChannels;
//
//	union {
//		union{
//		   int grayLower;
//		   int redThres;
//		   float lowRate;//划痕占空比
//		};
//		union{ 
//		   int grayUpper;
//		   int greenThres;
//		   float uppRate;//划痕占空比
//		};
//
//		int blueThres;
//	};
//
//	float extra1, extra2;
//
//	float circular;
//	union{
//		int   minThick;
//		int   minWidth;
//	};
//	union{
//		int minLen;
//		int minHeight;
//	};	
//}SD_BLOB_OPTS;

//缺陷种类，该缺陷可以根据不同的缺陷类型进行重定义（类范围内）
#define SD_NORMAL        0      //没有缺陷 
#define SD_NONCIRCULAR   1      //圆度不够
#define SD_UNDER_SAMPLE  2      //采样不足
#define SD_RAGGED_EDGE   3      //边缘凹陷
#define SD_HOLES         4      //中间空洞
#define SD_UNKNOWN       59     //未知缺陷



typedef struct __sd_defect_desc_
{
	int     flags;                
	int     clscode;     //分类码: GSE_NORMAL, GSE_NONCIRCULAR
	double  reserved1; //area;    
	double  reserved2; //diameter;  
	double  reserved3; //circular;  
	double  reserved4; //maxHoleSize; 	
	RvPoint center;
	RvPoint vertex[4]; 
}SD_DEFECT_DESC;

class  CALIX_API CSurfaceDefect: public CAreaCal
{
public:
	//enum{
	//	 SD_CHECK_SCRATCH = (1<<0), //划痕
	//	 SD_CHECK_DIRTY  =  (1<<1),   //污渍
	//	 SD_CHECK_STONES  = (1<<2),   //结石
	//	 SD_CHECK_BUBBLES = (1<<3),   //气泡
	//	 SD_CHECK_COLOR  =  (1<<4),   //检测颜色
	//	 SD_CHECK_EDGE   =  (1<<5),   //边缘缺损
	//};
	//
	////RGB转BIN或GRAY, Reduction method
	enum{
		SD_RM_SIMPLE =0, //surface bin
		SD_RM_RANGE,     //surface bin
		SD_RM_MAX,       //BLOB GRAY,minerror
		SD_RM_AVG,       //BLOB GRAY,minerror
		SD_RM_MIN,       //BLOB GRAY,minerror
	};

	////RGB转BIN, CHANNELS
	enum{
	    SD_CHN_DEFAULT =0, //三通道颜色转换
		SD_CHN_RG,
		SD_CHN_RB,
		SD_CHN_BG,
		SD_CHN_RED,
		SD_CHN_GREEN,
		SD_CHN_BLUE,
	};

	CSurfaceDefect(void);
	virtual ~CSurfaceDefect(void);

	virtual BOOL Serialize(HANDLE device, BOOL bIn ) ;
    virtual CUCObj* Clone(  CUCObj *twin) ;

	virtual void Show(CDelegate* pDelegate,int mode, BOOL bForce) ;

	//virtual RV_BOOL Inspect(int* pErrCode=NULL)=0;

	//升级用
	//virtual BOOL LearnSurfaces(const RvImage image, int* pErrCode=NULL);
	const RvImage GetBinaryImage(){ return m_imBin; }; 
	const RvImage GetGrayImage(){ return m_imGray; }; 

	float GetMipScale(){ return m_nMipScale ; };

	//int GetSurfacesCount();

	//外部程序不可删除RvMask
	//const SD_SURFACE* GetSurfacesAt(int index);

	//缺陷数量
	int             GetDefectCount();
	const SD_DEFECT_DESC* GetDefectAt(int index); 
	void    ReleaseAllDetects();

	virtual void Reset();
	
	//void       ReleaseAllSurfaces();
	//void    ReleaseMarks(RvSequence seq);
	//void    ReleaseAllMarks();
	


public:
	//一旦发现缺陷，立马终止
	BOOL m_bHaltAsDetected;

	int m_nCheckItemFlags;   //检测项
	
	//产品宽度和高度范围
	//int       m_minSurfaceWidth, m_minSurfaceHeight;
	//int       m_maxSurfaceWidth, m_maxSurfaceHeight;

	//SD_SURFACE_OPTS  m_surface;  //产品区域颜色
	  
	//SD_BLOB_OPTS m_scratches; //划痕
	//SD_BLOB_OPTS m_stones;    //结石
	//SD_BLOB_OPTS m_bubbles;   //气泡
	//SD_BLOB_OPTS m_deficiencies; //缺角，边缘凹陷
	//SD_BLOB_OPTS m_dirty;     //污渍

protected:
	

	RV_HANDLE  DrawPosMarks(HGDC dc,      RV_HANDLE hStroke);
	
	 void  ConvertRgbToBin(const RvImage image, RvImage imBin , int channels,  int reduction,  RvColor target,RvScalar scalar/*, SD_SURFACE_OPTS* pSurfaceDesc*/ );
	 void  ConvertRgbToGray(const RvImage image, RvImage imGray , int grayChannels , int reduction/*, SD_SURFACE_OPTS* pBlobDesc*/ );
	 
	//检测有背景的表面
	//int   CheckMaskedSurface(RvSequence seqMask, RvImage image, RvImage imBin);

	// int  RetrieveSurfaces(RvImage image,  RvSequence seqOut);              //提取产品表面
	//int   CheckDirty(const RvImage imRoi, RvImage imBin, const RvSequence seqMask);     //提取污渍
	//int   CheckStones(const RvImage imRoi, RvImage imBin, const RvSequence seqMask);    //提取结石
	//int   CheckBubbles(const RvImage imRoi, RvImage imBin, const RvSequence seqMask);   //提取气泡
	//int   CheckScratches(const RvImage imRoi, RvImage imBin, const RvSequence seqMask); //提取划痕
	//int   CheckColor(const RvImage imRoi, RvImage imBin, const RvSequence seqMask);     //提取露铜(色彩差异)
	//int   CheckDeficiencies(const RvImage imRoi, RvImage imBin, const RvSequence seqMask); //提取缺角

protected:
	RvImage m_imBin;
	RvImage m_imGray;
	 
	float  m_nMipScale;

	//重要提醒:  新的缺陷描述空间使用MPM_ALLOC创建, 不要使用new 
	//           否则可能会系统错误.
	RvSequence m_seqDefects;  //缺陷列表 
 
		  
};

