#ifndef  __RV_UCUT_H_INCLUDED_
#define  __RV_UCUT_H_INCLUDED_


#ifdef UCUT_EXPORTS
#define UCUT_API __declspec(dllexport)
#else
#define UCUT_API __declspec(dllimport)
#endif


#include <rvb\rvtypes.h>

#include <rvb\rdg.h>
#include <rvb\dsm.h>
#include <rvb\xgui.h>


class UCUT_API CUtColor
{
public:
	CUtColor();
	CUtColor(RvRgb& color);
	CUtColor(RvRgba& color);
	CUtColor(RvColor color);

	CUtColor(RvByte red, RvByte green, RvByte blue );
	CUtColor(RvByte red, RvByte green, RvByte blue, RvByte alpha );


	//是否与输入的size相等,功能类似==算符
	BOOL IsEqual(CUtColor& color, BOOL bAlphaExcluded);
	BOOL IsEqual(RvRgb& color);
	BOOL IsEqual(RvRgba& color, BOOL bAlphaExcluded);
	BOOL IsEqual(RvColor color, BOOL bAlphaExcluded);

	RvColor Color();

public:
	BOOL  operator==(CUtColor& color);
	BOOL  operator==(RvRgb& color);
	BOOL  operator==(RvRgba& color);
	BOOL  operator==(RvColor color);

	
	 
	CUtColor&  operator=(CUtColor& color);
	CUtColor&  operator=(RvRgb& color);
	CUtColor&  operator=(RvRgba& color);
	CUtColor&  operator=(RvColor color);

	operator RvColor();

public:
	RvByte red, green, blue, alpha;

};


class UCUT_API CUtPoint
{
public:
	CUtPoint();
	CUtPoint(RvPoint& point);
	CUtPoint(int x, int y );

	RvPoint Point();

	//是否与输入的size相等,功能类似==算符
	BOOL IsEqual(RvPoint& point);

public:
	BOOL  operator==(CUtPoint& point);
	BOOL  operator==(RvPoint& point);
	 
	CUtPoint&  operator=(CUtPoint& point);
	CUtPoint&  operator=(RvPoint& point);

	operator RvPoint();
public:
	int x, y;
};
 
 


class UCUT_API CUtSize
{
public:
	CUtSize();
	CUtSize(CUtSize& size);
	CUtSize(RvSize& size);

	CUtSize(int sx, int sy);
	CUtSize(UINT sx, UINT sy);
	
	RvSize Size();
	//是否与输入的size相等,功能类似==算符
	BOOL IsEqual(RvSize& size);
	  
public:
	CUtSize& operator=(CUtSize& size);
	CUtSize& operator=(RvSize& size);
	
	BOOL  operator==(CUtSize& size);
	BOOL  operator==(RvSize& size);

	operator RvSize();
public:
	UINT sx, sy;
};


class UCUT_API CUtOffset
{
public:
	CUtOffset();
	CUtOffset(RvOffset& offset);
	CUtOffset(int dx, int dy);
	CUtOffset(RvSize& size);
	CUtOffset(CUtSize& size);
	CUtOffset(RvPoint& point);

	RvOffset Offset();

	//是否与输入的size相等,功能类似==算符
	BOOL IsEqual(RvOffset& offset);
	BOOL IsEqual(CUtOffset& offset);

public:
	CUtOffset& operator=(CUtOffset& offset);
	CUtOffset& operator=(RvOffset& offset);
	
	BOOL  operator==(CUtOffset& offset);
	BOOL  operator==(RvOffset& offset);

	operator RvOffset();
public:
	int dx, dy;
};


class UCUT_API CUtRect
{
public:
	CUtRect();
	CUtRect(RvRect& rect);
	CUtRect(int left, int top, int right, int bottom);
	CUtRect(int left, int top, UINT width, UINT height);


	BOOL IsEqual(RvRect& rect);

	RvPoint    RightBottom();
	RvPoint    LeftTop();
	RvPoint    CenterPoint();
	int        Width();
	int        Height();
	RvSize     Size();
	RvRect     Rect();


	CUtRect&     Set( int left, int top, int right, int bottom);
	CUtRect&     Set(int left, int top, UINT width, UINT height);
	 
	CUtRect&     MoveToX( int x);
	CUtRect&     MoveToY( int y);
	CUtRect&     MoveToXY( int x, int y);

	//缩小
	CUtRect&    Deflate ( CUtSize& size);
	CUtRect&    Deflate(  int dx,   int dy );  
	CUtRect&    Deflate(CUtRect& rect );  
	CUtRect&    Deflate(int left, int top, int right, int bottom );  
		
	//扩展
	CUtRect&    Inflate ( CUtSize& size);
	CUtRect&    Inflate(  int dx,   int dy );  
	CUtRect&    Inflate(CUtRect& rect );  
	CUtRect&    Inflate(int left, int top, int right, int bottom );  

	BOOL      IsNormal();
	BOOL      IsEmpty();
	BOOL      IsNull();


	//CUtRect&    Substract(CUtRect& rect); 
	CUtRect&    Intersect(CUtRect& rect);
	CUtRect&    Intersect(RvRect rect);

	CUtRect&    Union( CUtRect& rect);
	CUtRect&    Union(RvRect  rect);


	CUtRect&    MulDiv( int nMultiplier, int nDivisor ) ;

	CUtRect&    Normalize();
	CUtRect&    Move(CUtSize& size);
	CUtRect&    Move(CUtOffset& offset);
	CUtRect&    Move(RvSize size);
	CUtRect&    Move(RvOffset  offset);
	CUtRect&    Move(int dx, int dy);

	CUtRect&    Rotate  ( float angle);
	RvRect_f    RotateF32( float angle);
	CUtRect&    Scale  ( float scaler); //缩放，基于原点
	CUtRect&    Scale  (RvPoint base, float scaler); //缩放,根据基准点缩放
	CUtRect&    ScaleLt  ( float scaler);  //以左上角缩放
	CUtRect&    ScaleCn  ( float scaler); //按中心点缩放


	BOOL     IsPointInside( CUtPoint& point);
	BOOL     IsPointInside( RvPoint  point);

	BOOL     IsSubRect(CUtRect& rect);
	BOOL     IsSubRect(RvRect rect);


	CUtRect&     SetEmpty();
	 
	//新:找出按照比例缩放的放在self矩形里面的最大矩形。
	RvRect    FitRect(int width, int height, float *ratio);
	 
	
public:
	CUtRect& operator=(CUtRect& rect);
	CUtRect& operator=(RvRect& rect);
	

	BOOL operator==(CUtRect& rect);
	BOOL operator==(RvRect& rect);

	operator RvRect();
public:
	int left,top, right, bottom;
};


 
 

#define UCUT_MAX_READING_TEXT_LEN  128

class UCUT_API CUtReading {
public:
	CUtReading();

	CUtReading(RvReadingBank bank);
	~CUtReading();
	
	CUtReading& operator= (CUtReading &r1);
	CUtReading& operator= (RvReadingBank bank);

	RvReadingBank GetHandle() {return m_readBank;};

	void Clear() ;
	int  GetCount() ;

	RV_READING_TYPE GetType(int index);

	void SetTag(int index, const TCHAR* strTag);
	const TCHAR* GetTag(int index);

	const TCHAR* ToString(int index);
	 

 	const TCHAR*   GetString(  int index=0);
	RvInt         GetInt(     int index=0);
	RvSize_f        GetSize(     int index=0);	
	RvDouble      GetDecimal(   int index=0);	
	RvPoint_f     GetPoint (   int index=0); 
	RvLine_f      GetLine (   int index=0); 
	RvRect_f      GetRect (   int index=0); 
	RvEllipse_f   GetEllipse (   int index=0); 
	RvRgb         GetRgb (   int index=0); 

 	void       Set(int index, int     val);
 	void       Set(int index, RvDouble  val);
	void       Set(int index, const TCHAR*  val);
	void       Set(int index, RvSize_f  val);
	void       Set(int index, RvRgb   val);
 	void       Set(int index, RvPoint_f  val);
	void       Set(int index, RvLine_f   val);
	void       Set(int index, RvRect_f   val);
	void       Set(int index, RvEllipse_f   val);
	
	RvInt         GetInt(     int index, int subidx);
	RvSize        GetSize(     int index, int subidx);	
	RvDouble      GetDecimal(   int index, int subidx);	
	RvPoint_f     GetPoint (   int index, int subidx); 
	RvLine_f      GetLine (   int index, int subidx); 
	RvRect_f      GetRect (   int index, int subidx); 
	RvEllipse_f   GetEllipse (   int index, int subidx); 
	RvRgb         GetRgb (   int index, int subidx); 

 	void       Set(int index, int subidx, int     val);
 	void       Set(int index, int subidx, RvDouble  val);
	void       Set(int index, int subidx, const TCHAR*  val);
	void       Set(int index, int subidx, RvSize  val);
	void       Set(int index, int subidx, RvRgb   val);
 	void       Set(int index, int subidx, RvPoint_f  val);
	void       Set(int index, int subidx, RvLine_f   val);
	void       Set(int index, int subidx, RvRect_f   val);
	void       Set(int index, int subidx, RvEllipse_f   val);

private:
	RvReadingBank m_readBank;

	TCHAR m_buffer[UCUT_MAX_READING_TEXT_LEN];
};

//#define CReading  CUtReading

//#define UCUT_RED_CHANNEL       1
//#define UCUT_GREEN_CHANNEL     (1<<1)
//#define UCUT_BLUE_CHANNEL      (1<<2)
//#define UCUT_ALL_CHANNEL       (UCUT_RED_CHANNEL | UCUT_GREEN_CHANNEL | UCUT_BLUE_CHANNEL)

class UCUT_API CUtImage
{
public:
	CUtImage(void);
	CUtImage(RV_IMAGE_TYPE type, int width, int height);
	CUtImage(const RvImage image);
	CUtImage(RV_IMAGE_TYPE type, int width, int height, const void* pImgData, size_t size);
 

	~CUtImage(void);

	CUtImage& operator=(  CUtImage &iw1);
	CUtImage& operator=( const RvImage image);

	HANDLE GetHandle() {return m_image;};

	BOOL Load(const TCHAR* strFilePath);
	BOOL LoadEx(const RvImage image);

	BOOL Save( const TCHAR* strFilePath);

	//IW_RED, IW_GREEN, IW_BLUE, 
	void ToGray(int channel);
	void ToBin(int nThreshold);
	void ToRgb();

	void Resize(int width, int height);
	void Resize(int width, int height, RvByte initVal);

	int GetWidth();
	int GetHeight();
	int GetPitch();

	RV_IMAGE_TYPE GetType();
	int  GetChannels();
	int  GetDepth();


	TCHAR* GetData();
	size_t GetSize();

	static RV_IMAGE_TYPE GetImageType(int depth);
	static int GetPixelDepth(RV_IMAGE_TYPE type);

	 
protected:

	RvImage m_image;
};


//CUtString字符串的最大长度为65535

class UCUT_API CUtString  
{
public:
	CUtString();
	CUtString(const TCHAR* strText );
//	CUtString(TCHAR* strText );
	CUtString(TCHAR  ch );
	CUtString(CUtString& strOther);
	 

	 

	virtual ~CUtString();

	//重载运算符和比较符
	//CUtString& operator=(const CUtString& strOther);
	CUtString& operator=(CUtString& strOther);
	//CUtString  operator=(CUtString  strOther);

	//CUtString& operator=(CUtString strOther);
	CUtString& operator=(const TCHAR* strContent);
	//CUtString operator=(TCHAR* strContent);
	CUtString& operator=(TCHAR ch);

	CUtString& operator+=(const TCHAR* strText);
	//CUtString operator+=(TCHAR* strText);
	CUtString& operator+=(CUtString& strText);
	CUtString& operator+=(TCHAR ch);

	CUtString operator+(const TCHAR* strText);
	CUtString operator+(CUtString& strText);
	CUtString operator+(TCHAR ch);
  	CUtString operator+(TCHAR* strText);


	BOOL operator==(const CUtString& strOther);
	BOOL operator==(const TCHAR* strContent);
	BOOL operator==(  TCHAR* strContent);
	BOOL operator==(  TCHAR ch);

	BOOL operator!=(const CUtString& strOther);
	BOOL operator!=(const TCHAR* strContent);
	BOOL operator!=(TCHAR* strContent);
	BOOL operator!=(TCHAR ch);

	
	//重载数据类型
	operator const TCHAR*();
	operator TCHAR*();

	const TCHAR* GetBuffer();
	TCHAR GetAt(int nIndex);

	unsigned int GetLength();//长度不包含null结束符

	int  Find(TCHAR ch,	BOOL bReverse=FALSE);
	int  Find(const TCHAR* sub,	BOOL bReverse=FALSE);
	int  Find(CUtString& sub,	BOOL bReverse=FALSE);
	int  FindEx(const TCHAR* strText );

	BOOL Extract(TCHAR ch, BOOL bReverse, CUtString& strOut);
	void Format(const TCHAR* strFormat, ...);

	int Remove(TCHAR ch);
	

	int Replace(TCHAR orich, TCHAR newch);
	int Replace(const TCHAR* strOri, const TCHAR* strNew);


	////排除首尾两段的空格，TAB，或回车字符
	void Trim();
	void Trim(TCHAR ch);

	//排除首尾两段的指定字符
	void Exclude(TCHAR ch);

	void Empty();
	BOOL IsEmpty();
	void Insert(int index, const TCHAR* strText);
	void Insert(int index, TCHAR ch);

	
	 
	CUtString Left(int count);
	CUtString Right(int count);
	CUtString Mid(int index, int count);

	void  ToUpper();
	void  ToLower();

private:
	TCHAR*          m_pData;
	unsigned int   m_nSize;

};


 





class UCUT_API CUtAutoLock{
public:
	CUtAutoLock(CRITICAL_SECTION* pCs);
	~CUtAutoLock();

private:
	CRITICAL_SECTION* m_pCs;
};




class UCUT_API CUtStopWatch  
{
public:
	CUtStopWatch();
	virtual ~CUtStopWatch();

	void   Start();

	double Stop(BOOL bStartAgain);

	double ElapsedTime();

	BOOL   IsStarted(); 

	void Reset();

private:

	double CalcElapsedTime(LARGE_INTEGER curtime);

    /// zero-point for time measurment
    LARGE_INTEGER m_startTime;

	BOOL m_bStarted;
    /// last time stamp
    //LARGE_INTEGER m_stopTime;

};

#define CStopWatch  CUtStopWatch

class UCUT_API CUtTickCounter{
public:
	CUtTickCounter();
	~CUtTickCounter();

	LONGLONG  Value();

private:
	DWORD m_nTick64Low, m_nTick64High;
};


#define CTickCounter  CUtTickCounter


#endif //__RV_UCUT_H_INCLUDED_
