#ifndef __RV_TYPES_H_INCLUDED_
#define __RV_TYPES_H_INCLUDED_

//多字节与双字节适应
 #include <tchar.h>

//ASSERT for RVB
#ifdef _DEBUG
   #include <assert.h>
   #define RV_ASSERT(expr) assert(expr) 
#else
   #define RV_ASSERT(expr)
#endif

//
//常量前缀命名： 1  如果是全局的常量: 'R'+两字母(模块或功能代号)+描述
//               2  如果为函数级的常量: 'RV'+类别缩写或函数头字母 + 描述
//
//

#define __RDT_8U      0
#define __RDT_8S      1 
#define __RDT_16U     2
#define __RDT_16S     3
#define __RDT_32S     4
#define __RDT_32F     5
#define __RDT_64F     6
#define __RDT_UD      7 // user defined

#define RDT_CN_MAX     64
#define RDT_CN_SHIFT   3
#define RDT_DEPTH_MAX  (1 << RDT_CN_SHIFT)
 
#define RDT_MAT_DEPTH_MASK       (RDT_DEPTH_MAX - 1)
#define RDT_MAT_DEPTH(flags)     ((flags) & RDT_MAT_DEPTH_MASK)

#define RDT_MAKETYPE(depth,cn)     (RDT_MAT_DEPTH(depth) + (((cn)-1) << RDT_CN_SHIFT))
#define RDT_MAKE_TYPE RDT_MAKETYPE

//Elemant  data type
#define RDT_8UC1 RDT_MAKETYPE(__RDT_8U,1)
#define RDT_8UC2 RDT_MAKETYPE(__RDT_8U,2)
#define RDT_8UC3 RDT_MAKETYPE(__RDT_8U,3)
#define RDT_8UC4 RDT_MAKETYPE(__RDT_8U,4)
#define RDT_8UC(n) RDT_MAKETYPE(__RDT_8U,(n))

#define RDT_8SC1 RDT_MAKETYPE(__RDT_8S,1)
#define RDT_8SC2 RDT_MAKETYPE(__RDT_8S,2)
#define RDT_8SC3 RDT_MAKETYPE(__RDT_8S,3)
#define RDT_8SC4 RDT_MAKETYPE(__RDT_8S,4)
#define RDT_8SC(n) RDT_MAKETYPE(__RDT_8S,(n))

#define RDT_16UC1 RDT_MAKETYPE(__RDT_16U,1)
#define RDT_16UC2 RDT_MAKETYPE(__RDT_16U,2)
#define RDT_16UC3 RDT_MAKETYPE(__RDT_16U,3)
#define RDT_16UC4 RDT_MAKETYPE(__RDT_16U,4)
#define RDT_16UC(n) RDT_MAKETYPE(__RDT_16U,(n))

#define RDT_16SC1 RDT_MAKETYPE(__RDT_16S,1)
#define RDT_16SC2 RDT_MAKETYPE(__RDT_16S,2)
#define RDT_16SC3 RDT_MAKETYPE(__RDT_16S,3)
#define RDT_16SC4 RDT_MAKETYPE(__RDT_16S,4)
#define RDT_16SC(n) RDT_MAKETYPE(__RDT_16S,(n))

#define RDT_32SC1 RDT_MAKETYPE(__RDT_32S,1)
#define RDT_32SC2 RDT_MAKETYPE(__RDT_32S,2)
#define RDT_32SC3 RDT_MAKETYPE(__RDT_32S,3)
#define RDT_32SC4 RDT_MAKETYPE(__RDT_32S,4)
#define RDT_32SC(n) RDT_MAKETYPE(__RDT_32S,(n))

#define RDT_32FC1 RDT_MAKETYPE(__RDT_32F,1)
#define RDT_32FC2 RDT_MAKETYPE(__RDT_32F,2)
#define RDT_32FC3 RDT_MAKETYPE(__RDT_32F,3)
#define RDT_32FC4 RDT_MAKETYPE(__RDT_32F,4)
#define RDT_32FC(n) RDT_MAKETYPE(__RDT_32F,(n))

#define RDT_64FC1 RDT_MAKETYPE(__RDT_64F,1)
#define RDT_64FC2 RDT_MAKETYPE(__RDT_64F,2)
#define RDT_64FC3 RDT_MAKETYPE(__RDT_64F,3)
#define RDT_64FC4 RDT_MAKETYPE(__RDT_64F,4)
#define RDT_64FC(n) RDT_MAKETYPE(__RDT_64F,(n))

//fundamental data type
#define RDT_CHAR      RDT_8SC1
#define RDT_BYTE      RDT_8UC1
#define RDT_INT       RDT_32SC1
#define RDT_SINGLE    RDT_32FC1
#define RDT_DOUBLE    RDT_64FC1

//compound data type
#define RDT_POINT     RDT_32SC2
#define RDT_POINTF    RDT_32FC2
#define RDT_SCALAR    RDT_64FC4
#define RDT_RGB       RDT_8UC3
#define RDT_RGBA      RDT_8UC4
#define RDT_COMPLEX   RDT_64FC2

#define RDT_HANDLE    RDT_INT

#define RDT_UD        RDT_MAKETYPE(__RDT_UD, 1)


#define RDT_CN_MASK          ((RDT_CN_MAX - 1) << RDT_CN_SHIFT)
#define RDT_CN(flags)        ((((flags) & RDT_CN_MASK) >> RDT_CN_SHIFT) + 1)

#define RDT_TYPE_MASK        (RDT_DEPTH_MAX*RDT_CN_MAX - 1)
#define RDT_TYPE(flags)      ((flags) & RDT_TYPE_MASK)
 


typedef enum __rv_bool_{
   RV_FUZZY=-1,
   RV_FALSE=0,
   RV_TRUE  =1,
}RV_BOOL;


#define  RvResult   int

typedef  float   RvFloat;
typedef  double  RvDouble;

typedef  int           RvInt;
typedef  unsigned int  RvUint;
typedef  unsigned char RvByte;
typedef  unsigned int  RvLabel;
typedef  unsigned char RvBit;
typedef  __int64       RvLarge;
 
typedef void*   RV_HANDLE;
typedef RV_HANDLE  RvImage;
typedef RV_HANDLE  RvMatrix;
typedef RV_HANDLE  RvRdg; //reading
typedef RV_HANDLE  RvMask;

typedef RV_HANDLE  RvBlob;
typedef RV_HANDLE  RvPick;
typedef RV_HANDLE  RvCut;
typedef RV_HANDLE  RvContour;

typedef RV_HANDLE  RvPath;

typedef RV_HANDLE  RvCollect; //handle of collect
typedef RV_HANDLE  RvDynamic; //该类型仅用于兼容，新版本尽量不要用
typedef RV_HANDLE  RvSequence;  //过时，使用RvQueue代替
typedef RV_HANDLE  RvQueue;
typedef RV_HANDLE  RvCluster;   //过时  
typedef RV_HANDLE  RvTree;  
typedef RV_HANDLE  RvList;  
//typedef RV_HANDLE  RvSafeList;  
typedef RV_HANDLE  RvGraph;  
 
 

#define  RvKernel  RvMask
#define  RvIntPtr  int 

#define  RvFloat32  float 
#define  RvFloat64  double 

typedef void*  RvMemoryStore;


typedef RV_HANDLE  HITEM;
typedef void*   RV_PTR;  
 
//graphical device context
typedef RV_HANDLE  HGDC;   
//typedef RV_HANDLE  HCANVAS;
typedef RV_HANDLE  HGOBJ;
//typedef RV_HANDLE  HCOOKIE;




#define  GByte         RvByte
#define  GColor        unsigned int 
#define  RvColor       GColor

//彩色像素颜色分量:BBGGRRAA
#define RV_RGB_TO_COLOR(rgb)     (( (rgb).red << 16) | ((rgb).green << 8) |  (rgb).blue | ( 255 << 24) )
#define RV_RGBA_TO_COLOR(rgba)    ( ( (rgba).red << 16) | ((rgba).green << 8) | (rgba).blue   | ((rgba).alpha << 24))
 
#define RV_RGB(red, green, blue)             (((red) << 16) | ((green) << 8) | ( blue ) | ( 255 << 24))
#define RV_RGBA(red, green, blue, alpha)     (((red) << 16) | ((green) << 8) | (blue) | ((alpha) << 24))

#define RV_GET_RED(color)       (((color) >> 16)   & 0xFF)
#define RV_GET_GREEN(color)     (((color) >> 8)   & 0xFF)
#define RV_GET_BLUE(color)      (((color)     )   & 0xFF)
#define RV_GET_ALPHA(color)     (((color) >> 24 )   & 0xFF)

#define RV_COLOR_TO_RGB(color, rgb)      { rgb.red = RV_GET_RED(color) ;  rgb.green =  RV_GET_GREEN(color) ;  rgb.blue = RV_GET_BLUE(color) ;}
#define RV_COLOR_TO_RGBA(color, rgba)      { rgba.red = RV_GET_RED(color) ;  rgba.green =  RV_GET_GREEN(color) ;  rgba.blue = RV_GET_BLUE(color) ; rgba.alpha = RV_GET_ALPHA(color) ;}

#define RV_COLOR_TO_GRAY(r,g,b)   ((r)*0.299 + (g)*0.587 + (b)*0.114)

//frequently used colors 
#define RV_COLOR_WHITE         ((0xFF << 16) |  (0xFF << 8) | (0xFF))
#define RV_COLOR_BLACK         ((0x00 << 16) |  (0x00 << 8) | (0x00))
#define RV_COLOR_GREY          ((0x80 << 16) |  (0x80 << 8) | (0x80)) 

#define RV_COLOR_BLUE          ((0x00 << 16) |  (0x00 << 8) | (0xFF))  
#define RV_COLOR_GREEN         ((0x00 << 16) |  (0xFF << 8) | (0x00)) 
#define RV_COLOR_CYAN          ((0x00 << 16) |  (0xFF << 8) | (0xFF))  // GRGB(0x00,  0xFF,0xFF )
#define RV_COLOR_RED            ((0xFF << 16) |  (0x00 << 8) | (0x00))  // GRGB(0xFF, 0x00, 0x00)
#define RV_COLOR_MAGENTA        ((0x8B << 16) |  (0x00 << 8) | (0x8B))  // GRGB(0x8B, 0x00, 0x8B)
#define RV_COLOR_BROWN          ((0xA5<< 16) |  (0x2A << 8) | (0x2A ))  //GRGB(0xA5, 0x2A,0x2A )
#define RV_COLOR_YELLOW         ((0xFF<< 16) |  (0xFF << 8) | (0x00 ))  //GRGB(0xFF, 0xFF, 0x00)

#define RV_COLOR_DARKGRAY       ((0x40 << 16) |  (0x40 << 8) | (0x40 ))  //GRGB(0x40, 0x40, 0x40)

#define RV_COLOR_LIGHTGREY      ((0xD3<< 16) |  (0xD3 << 8) | (0xD3 ))  //GRGB(0xD3, 0xD3, 0xD3)
#define RV_COLOR_LIGHTBLUE      ((0x80<< 16) |  (0x80 << 8) | (0xFF ))  // GRGB(0x80, 0x80, 0xFF)
#define RV_COLOR_LIGHTGREEN     ((0x80<< 16) |  (0xFF << 8) | (0x80 ))  //GRGB(0x80, 0xFF, 0x80)
#define RV_COLOR_LIGHTCYAN      ((0xFF<< 16) |  (0xFF << 8) | (0x80 ))  //GRGB(0xFF , 0xFF,0x80 )
#define RV_COLOR_LIGHTRED       ((0xFF<< 16) |  (0x80 << 8) | (0x80 ))  //GRGB(0xFF , 0x80,0x80 )
#define RV_COLOR_LIGHTMAGENTA   ((0xFF<< 16) |  (0x80 << 8) | (0xFF ))  //GRGB(0xFF, 0x80, 0xFF)

#define RV_COLOR_CH1     1
#define RV_COLOR_CH2     (1<<1)
#define RV_COLOR_CH3     (1<<2)
#define RV_COLOR_CH4     (1<<3)


#define RV_CHANNEL_BLUE       1
#define RV_CHANNEL_GREEN     (1<<1)
#define RV_CHANNEL_RED       (1<<2)
#define RV_CHANNEL_ALPHA     (1<<3)

//objct align type
#define RV_OA_NONE     0
#define RV_OA_HORI     1
#define RV_OA_VERT    (1<<1) 
#define RV_OA_ROTATE  (1<<2)
#define RV_OA_OFFSET    (RV_OA_HORI | RV_OA_VERT) 
#define RV_OA_EXTENDED  (RV_OA_OFFSET | RV_OA_ROTATE) 



typedef struct __rv_rgb_{
   RvByte red;
   RvByte green;
   RvByte blue;
}RvRgb;

typedef struct __rv_rgba_{
   RvByte red;
   RvByte green;
   RvByte blue;
   RvByte alpha;
}RvRgba;
 




typedef struct __rv_complex_{
	RvFloat real;
	RvFloat image;
}RvComplex;

#define RvReal32 float 
#define RvReal64 double 

typedef struct __rv_complex_32{
	float   re;
	float   im;
}RvComplex32;


typedef struct __rv_complex_64{
	double  re;
	double  im;
}RvComplex64;

typedef struct __rv_point_{
	int x, y ;
}RvPoint;

typedef struct __rv_point_i16_{
	short x, y ;
}RvPointI16;

typedef struct __rv_point_f_{
	float x, y ;
}RvPoint_f;

typedef struct __rv_point_f64_{
	double x, y ;
}RvPoint_f64;

typedef struct __rv_point_3d_{
	int x, y, z;
}RvPoint3D;

typedef struct __rv_point_3d_f_{
	float x, y, z;
}RvPoint3D_f;

typedef struct __rv_point_3d_f64_{
	double x, y, z;
}RvPoint3D_f64;


typedef struct __rv_scalar_{
	int val[4];
}RvScalar;

typedef struct __rv_scalar_f32_{
	float val[4];
}RvScalar_f;

typedef struct __rv_scalar64_{
	double val[4];
}RvScalar_f64 ;   

typedef struct __rv_range_{
    int lower, upper;
}RvRange;

typedef struct __rv_range_f32_{
    float lower, upper;
}RvRange_f;

typedef struct __rv_range_f64_{
    double lower, upper;
}RvRange_f64;

typedef struct _rv_size_{
	union{
		int sx;
		int cols;
		int w;
	};
	union{
		int sy ;
		int rows;
		int h;
	};
} RvSize;

typedef struct _rv_size_3d_{
	union{
		 int sx;
		 int cols;
		 int w;
	};
	union{
		 int sy ;
		 int rows;
		 int h;
	};
	union{
		 int sz;
		 int tiers; //层
		 int d; //深度
	};
} RvSize3D;

typedef struct _rv_size_f32_{
	union{
		float sx;
		float w;
	};
	union{
		float sy ;  
		float h;
	};
} RvSize_f ;
 
typedef struct _rv_size_f64_{
	union{
		double sx;
		double w;
	};
	union{
		double sy ;
		double h;
	};
} RvSize_f64 ;

typedef struct _rv_dim_{
	int dx, dy ;
}RvDim, RvDiff, RvOffset;
 
typedef struct _rv_dim_f32{
	float dx, dy ;
}RvDim_f, RvDiff_f, RvOffset_f  ;
 
typedef struct _rv_dim_f64{
	double dx, dy ;
} RvDim_f64, RvDiff_f64, RvOffset_f64;

typedef struct _rv_dim_3d_{
	int dx, dy, dz ;
}RvDim3D, RvDiff3D, RvOffset3D;
 

typedef struct __rv_rect
{
	//int  x, y,w, h ;
    int   left, top, right, bottom;
}RvRect;

typedef struct __rv_rect_f
{
	//int  x, y,w, h ;
    float   left, top, right, bottom;
}RvRect_f;

typedef struct __rv_rect_f64
{
	//int  x, y,w, h ;
    double   left, top, right, bottom;
}RvRect_f64;


typedef struct __rv_line_{
	RvPoint p0, p1;
}RvLine;

typedef struct __rv_line_f{
	RvPoint_f p0, p1;
}RvLine_f;


typedef struct __rv_line_f64{
	RvPoint_f64 p0, p1;
}RvLine_f64;



typedef struct __rv_ellipse_f{
	RvPoint_f center;
	float     rx, ry;
}RvEllipse_f;

typedef struct __rv_ellipse_f64{
	RvPoint_f64 center;
	double     rx, ry;
}RvEllipse_f64;



typedef struct __rv_string_
{
    int    size;
    TCHAR* data;
}RvString;

#define GRgb      RvRgba
#define GPoint    RvPoint
#define GSize     RvSize
#define GRect     RvRect
#define GPoint_f  RvPoint_f


//最大的图象宽度
//最大的图象高度
#define RV_MAX_IMAGE_WIDTH    (8192 << 1)  
#define RV_MAX_IMAGE_HEIGHT   (8192 << 1) 
 
//
#define RV_MAX_IMG_WID   RV_MAX_IMAGE_WIDTH 
#define RV_MAX_IMG_HEI   RV_MAX_IMAGE_HEIGHT

#define RV_MIN_CANVAS_WIDTH  8
#define RV_MIN_CANVAS_HEIGHT 2

#define RV_MIN_IMAGE_WIDTH   2
#define RV_MIN_IMAGE_HEIGHT  2


#define RV_MAX_CANVAS_WIDTH     RV_MAX_IMAGE_WIDTH
#define RV_MAX_CANVAS_HEIGHT    RV_MAX_IMAGE_HEIGHT




//GDC paint image  align
#define RV_PI_DEFAULT      0x00000001
#define RV_PI_CENTER       0x00000002
  
 
 
#define  RV_FC_MODE_MAX       0 //maximum
#define  RV_FC_MODE_LIST      1
#define  RV_FC_MODE_CCOMP     2
#define  RV_FC_MODE_TREE      3

 
/*Both the orders and the values of elements 
  must be not changed, new element can be appended 
  to the end if needed., never be inserted between
  any other two.
*/
typedef enum __rv_pick_shape_
{
	RPS_RECT = 0,
	RPS_CIRCLE ,
	RPS_ANNULUS,
	RPS_TORUS,
	RPS_POLYGON,
	RPS_ELLIPSE,//椭圆
	RPS_SECTOR, //扇形,新的形状需要添加到CUSTOM前面
	RPS_CUSTOM =101,
}RV_PICK_TYPE, RV_PICK_SHAPE;


#define RV_CT_ANGLE_ACCURACY      100
#define RV_CT_MAX_CHANNEL_COUNT   4


//curve item
typedef struct __rv_path_item_t
{
	double      x, y;
	double      vals[RV_CT_MAX_CHANNEL_COUNT];    
}RvPathItem;

//下面的定义顺序为固定，不能随意调换
typedef enum __rv_cut_type_
{
    RCT_POINT  = 0, 
	RCT_LINE,
	RCT_ARC,  
	RCT_CIRCLE,
	RCT_BULLEYE,
	RCT_RAKE,    //平行线
	RCT_SPOKE,   //放射线
	RCT_POLYLINE,
	RCT_ELLIPSE, //椭圆       added by hdy on 2016/05/17
	RCT_ANY_TWO, //任意两线段 added by hdy on 2016/05/17
	RCT_POLYGON, //封闭多边形 added by hdy on 2016/05/17
	RCT_ARCH,    //多条圆弧   added by hdy on 2018/01/08
	RCT_ANY_SEG, //任意数量线段 added by hdy on 2018/07/10
	RCT_SQUARE,  //方形（90度角4边形）, added by hdy on 2021/08/03
}RV_CUT_TYPE, RV_CUT_SHAPE;


#define RV_EXT_IMG_SHIFT    16
//#define RV_EXT_IMG_DATA_TYPE_MASK  ((1 << RV_EXT_IMG_SHIFT) - 1)

typedef enum __rv_image_type_
{	
	RIT_EXT     = (-1) ,//未知图像格式
    RIT_BIN     = 1 , 
    RIT_GRAY    = 8, 
    RIT_RGB     = 24 ,  
	RIT_RGBA    = 32 ,
	//其它颜色空间
	RIT_HSV     = 2025+9/*sizeof(float) + sizeof(float) + sizeof(RvByte)*/,
	RIT_YCBCR   = 2025+12/*sizeof(float) + sizeof(float) + sizeof(float)*/ ,
	RIT_YUV     = 2025+11,
	RIT_HSV_H     = 2025+9+1,
	RIT_HSV_S     = 2025+9+2,
	RIT_HSV_V     = 2025+9+3,
	RIT_YCBCR_Y       = 2025+12+1,
	RIT_YCBCR_CB      = 2025+12+2,
	RIT_YCBCR_CR      = 2025+12+3,
	RIT_YUV_Y   = 2025+11+1,
	RIT_YUV_U   = 2025+11+2,
	RIT_YUV_V   = 2025+11+3,
} RV_IMAGE_TYPE ;
 

typedef enum __rv_blob_type_
{	 
	RBT_DEF    =  0,  //default, 
	RBT_LAB    =  1,  //label,
	RBT_COMB  =  2,   //组合的 ,保留
}RV_BLOB_TYPE;

//原始blog的定义
// 1  组成BLOG的点集合
// 2  组成BLOG的边缘点的集合；
// 3  在包含点集合的最小矩形区内不属于该BLOB的其他BLOB点的集合。
typedef enum __rv_blob_encoder_
{	  
	RV_BEC_UNK    =-1,
	RV_BEC_RAW    =  0,  //原图
	RV_BEC_RLE    =  1,  //跑马编码,目前不支持
	RV_BEC_CNTR   =  2,  //轮廓线
	RV_BEC_CLUST  =  3,  //点簇
}RV_BLOB_ENCODER;


typedef struct __rv_box_2d_
{
    double    cx,cy ;            /* Center of the box.                          */
    double    width, height;     /* Box width and length.                       */
    double    angle;             /* Angle between the horizontal axis and the first side (i.e. length) in degrees */
}RvBox2D;


typedef struct __rv_box_3d_
{
    double      cx,cy,cz ;            /* Center of the box.                          */
    double      width, height, depth;     /* Box width and length.                       */
    double      angle;             /* Angle between the horizontal axis and the first side (i.e. length) in degrees */
}RvBox3D;

typedef struct __rv_criteria_{
	int       tag;   //property tag
	int       cop;   //comparison operator

	double  lower;
	double  upper;
}RvCriteria;


#define RV_DATA_CVT  union{ \
                      char*        ch; \
                      RvByte*      bt; \
                      RvInt*       i;	 \
                      RvFloat*     fl;  \
					  RvString*    str; \
		     		  /*HANDLE*      hdl;*/	\
					  void*        ptr;  \
					  RvComplex*   cmx;  \
					  RvPoint*     pnt;  \
					  RvPoint_f*   fpnt;  \
					  RvRgb*       rgb;  \
					  RvRgba*      rgba;  \
					  RvScalar*    scl; 	} 


#define RV_UNDEFINED_BASE   -(1<<16)
#define RV_MATRIX_BASE       1
#define RV_COLLECT_BASE      (1<<8)
#define RV_COMPOUND_BASE     (1<<16)


#define RV_IS_MAT_CLS(type)  ( ((type) >= RV_MATRIX_BASE) && ((type) < RV_COLLECT_BASE) )
#define RV_IS_DYN_CLS(type)  ( ((type) >= RV_COLLECT_BASE) && ((type) < RV_COMPOUND_BASE) )
#define RV_IS_CMP_CLS(type)  ( ((type) >= RV_COLLECT_BASE)   )

//class type
typedef enum __rv_class_type{
    RCT_MATRIX = RV_MATRIX_BASE,
	RCT_IMAGE    ,
	RCT_MASK     ,
	RCT_BLOB     ,
	RCT_READING,
	RCT_READING_BANK  ,
	RCT_SEQUENCE    = RV_COLLECT_BASE,
	RCT_LIST    ,
	RCT_CLUSTER ,
	RCT_GRAPH   ,
	RCT_TREE    ,
	RCT_CONTOUR,
	RCT_PICK   = RV_COMPOUND_BASE ,
	RCT_CUT      ,
	RCT_STROKE  ,
	RCT_COMPOSITE,
	RCT_COMP_DESCOR,
	
}RV_CLASS_TYPE;
  
 

//basic (ROI or BLOB) traits 
//
#define RV_BRT_PIXEL_SUM        1
#define RV_BRT_PIXEL_AVG       (1<<1)
#define RV_BRT_PIXEL_VAR       (1<<2)
#define RV_BRT_PIXEL_COUNT     (1<<3)
#define RV_BRT_AREA            (1<<4)
#define RV_BRT_ENCLOSED_AREA   (1<<5)

#define RV_BRT_CENTROID        (1<<6)
#define RV_BRT_COLOR           (1<<7)

#define RV_BRT_MAX_LEVEL       (1<<8)
#define RV_BRT_MIN_LEVEL       (1<<9)
#define RV_BRT_MAX_LEVEL_POS   (1<<10)
#define RV_BRT_MIN_LEVEL_POS   (1<<11)



//extended (ROI or BLOB) traits 
#define RV_ERT_CIRCULARITY        (1)
#define RV_ERT_BOUND_RECT         (1<<1)
#define RV_ERT_BOUND_BOX          (1<<2)
#define RV_ERT_OFFSET             (1<<3)

//#define RV_ERT_ANISOMETRY         (1<<4)
#define RV_ERT_GRAVITY            (1<<4)






//criteria 
#define RVC_BASE             0xC0000000
#define RVC_PIXEL_SUM        (RVC_BASE + 1)
#define RVC_PIXEL_AVG        (RVC_BASE + 2)
#define RVC_PIXEL_VAR        (RVC_BASE + 3)
#define RVC_PIXEL_COUNT      (RVC_BASE + 4)
#define RVC_AREA             (RVC_BASE + 5)
#define RVC_ENCLOSED_AREA    (RVC_BASE + 6)
#define RVC_CIRCULARITY      (RVC_BASE + 7)   
#define RVC_ANISOMETRY       (RVC_BASE + 8)          
#define RVC_GRAVITY          (RVC_BASE + 9)

#define RVC_DENSITY_NORTH    (RVC_BASE + 20)
#define RVC_DENSITY_EAST     (RVC_BASE + 21)
#define RVC_DENSITY_SOUTH    (RVC_BASE + 22)
#define RVC_DENSITY_WEST     (RVC_BASE + 23)
#define RVC_DENSITY_NW       (RVC_BASE + 24)
#define RVC_DENSITY_NE       (RVC_BASE + 25)
#define RVC_DENSITY_SW       (RVC_BASE + 26)
#define RVC_DENSITY_SE       (RVC_BASE + 27)


//坐标原点类型
enum RV_ORIGIN_TYPE
{
	RV_OT_LEFTTOP    = 0,
	RV_OT_LEFTBOTTOM,
};

//criteria Comparison oPerators 
//#define RCP_EQUAL_LOWER             0
//#define RCP_GREATER_LOWER           1
//#define RCP_LESS_LOWER              2
//#define RCP_EQUAL_UPPER             3
//#define RCP_GREATER_UPPER           4
//#define RCP_LESS_UPPER              5
//#define RCP_INSIDE_RANGE            6 // greater equal than upper and less than upper
//#define RCP_OUTSIDE_RANGE           7 // less than lower and greater equal than upper
//
////cretieria logic type
//#define RCL_ALL         1
//#define RCL_ANY         0
//#define RCL_NOT         -1



/*
//main roi traits 
//statistic
#define RV_MRT_STAT    1

//geometry
#define RV_MRT_GEOM    (1<<1)
 */



//sub roi traits



//graph sheet types
//#define RV_ST_HISTO       0
//#define RV_ST_STRENGTH    1
//#define RV_ST_PREVIEW     10 


//filter methods
#define RV_FM_NEAREST     1
#define RV_FM_BILINEAR    2
#define RV_FM_BOX         3
#define RV_FM_GAUSSIAN    4
#define RV_FM_HAMMING     5
#define RV_FM_BLACKMAN    6

#define RV_BIN_ONE   1
#define RV_BIN_ZERO  0

#define RV_MAX_PROP_LEN  (1024 * 4)

#define RV_DIR_UNK    0
#define RV_DIR_HORI   1
#define RV_DIR_VERT   (1<<1)
#define RV_DIR_BOTH   (RV_DIR_HORI | RV_DIR_VERT)






#define  RV_CONN_4   4
#define  RV_CONN_8   8


#define RV_MAX_CHANNEL_COUNT    4


/**********************************************************

  collect 

***********************************************************/

//collect_item used for serialized read/write  only
typedef struct __rv_collect_item_{
	struct __rv_collect_item_* prev, *next;
	 void* data;
	 //  char*  data;
}RvCollectItem;


typedef void ( WINAPI *RvDestroyFunc) (RV_HANDLE);
 /* a < b ? -1 : a > b ? 1 : 0 */
typedef int ( WINAPI *RvCompareFunc) (const void*  , const void*  ,   void*   );

//TRUE OF FALSE
typedef BOOL (WINAPI *RvConditionFunc) (const void*  , const void*  ,   void*   );



typedef struct _rv_slice_ 
{
    int  start, end;
}RvSlice;


/*
typedef struct __rv_tree_writer_
{
	int            count;
	int            elemSize;
	RvMemoryStore  nodeStore;      
	RvMemoryStore  dataStore;     
	RvCollectItem* current;
}RvTreeWriter;


typedef struct __rv_tree_reader_
{
	int               elemSize;
	RvCollectItem*    current;
}RvTreeReader;
*/








 //Contour definitions
typedef enum _rv_contour_type_
{        
	RV_CT_UNKNOWN    = -1,
	RV_CT_NOT_CLOSED = 0,
	RV_CT_HOLE = 1,             //洞
	RV_CT_NORMAL =(1<<1),       //常规轮廓(最外轮廓) 
}RV_CONTOUR_TYPE;

typedef enum _rv_contour_filter_
{  
	 
	RV_CF_EXTERNAL   =  RV_CT_NORMAL, //最外部的轮廓，可以一个图里面可以有多个
	RV_CF_HOLE_ONLY  =  RV_CT_HOLE,   //仅仅有效的洞轮廓
	RV_CF_ANY        =  (RV_CT_HOLE | RV_CT_NORMAL),          //所有轮廓,都是封闭的 
	RV_CF_MAX        =  (1<<16),          //最大轮廓
	RV_CF_COMPO      =  (1<<17)   ,        //BLOB
	RV_CF_TREE       =  (1<<18)   ,        //树形
}RV_CONTOUR_FILTER;

typedef enum _rv_contour_code_
{
	RV_CC_UNKNOWN=-1,
	RV_CC_DEFAULT  = 0, //不拟合，原始点坐标
	RV_CC_SEGMENT,      //简单线段，整个轮廓可能不连续
	RV_CC_APPROX,       //保留
	//RV_CC_APPROX2,    //保留
	//RV_CC_APPROX_L1=10,    //L1方法
	//RV_CC_APPROX_KCOS,    //保留
}RV_CONTOUR_CODE;

 //Contour Hierarchy
typedef enum _rv_contour_hierarchy_
{        
	RV_CH_UNKNOWN    =-1, 
	RV_CH_DEFAULT    =0,       //默认，无层次关系
	RV_CH_COMPO  ,             //两层BLOB
	RV_CH_TREE   ,             //树形
}RV_CONTOUR_HIERARCHY;
 

#define  RV_PI                3.1415926535897932384626433832795
#define  RV_PI_F              3.1415926535897932384626433832795f

#define RV_MAX(a,b)  ((a)>=(b)? (a):(b))
#define RV_MIN(a,b)  ((a)>=(b)? (b):(a))
#define RV_ABS(a,b)     ( (a)>(b) ? (a)-(b) : (b)-(a) )


#define RV_DIST(a,b)     sqrt(((a).x- (b).x)*((a).x- (b).x) *1.0  + ((a).y- (b).y) * ((a).y- (b).y))
#define RV_DIST_F(a,b)   sqrtf( float (((a).x- (b).x)*((a).x- (b).x))   + float(((a).y- (b).y) * ((a).y- (b).y) ) )

#define RV_DIST_F64(x0,y0,x1,y1)   sqrt( (double) (((x1)-(x0)) * ((x1)-(x0)))  + ((y1)-(y0)) * ((y1)-(y0)))

 #define RV_DEG_TO_RAD(n)   ((n) / 180. * RV_PI)

 #define RV_RAD_TO_DEG(n)   ((n) / RV_PI * 180.)

#define RV_MAX_RADIUS   ( 0x1FFFFF ) //

#define RV_CHECK_RANGE(lower, upper, pos)  ( (pos) < (lower)? (lower): ( (pos) > (upper)? (upper): (pos) ) )









#endif