#ifndef __IPLIMAGE_DOT_H__
#define __IPLIMAGE_DOT_H__

#define IPL_DEPTH_SIGN 0x80000000

#define IPL_DEPTH_1U     1
#define IPL_DEPTH_8U     8
#define IPL_DEPTH_16U   16
#define IPL_DEPTH_32F   32

#define IPL_DEPTH_8S  (IPL_DEPTH_SIGN| 8)
#define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16)
#define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32)

#define IPL_DATA_ORDER_PIXEL  0
#define IPL_DATA_ORDER_PLANE  1

#define IPL_ORIGIN_TL 0
#define IPL_ORIGIN_BL 1

#define IPL_ALIGN_4BYTES   4
#define IPL_ALIGN_8BYTES   8
#define IPL_ALIGN_16BYTES 16
#define IPL_ALIGN_32BYTES 32

#define IPL_ALIGN_DWORD   IPL_ALIGN_4BYTES
#define IPL_ALIGN_QWORD   IPL_ALIGN_8BYTES

#include <string>

using namespace std;

#include "ChipMonkTypes.h"

typedef struct _IplImage
{
	int  nSize;         /* sizeof(IplImage) */
	int  ID;            /* version (=0)*/
	int  nChannels;     /* Most of OpenCV functions support 1,2,3 or 4 channels */
	int  alphaChannel;  /* ignored by OpenCV */
	int  depth;         /* pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
						IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported */
	char colorModel[4]; /* ignored by OpenCV */
	char channelSeq[4]; /* ditto */
	int  dataOrder;     /* 0 - interleaved color channels, 1 - separate color channels.
						cvCreateImage can only create interleaved images */
	int  origin;        /* 0 - top-left origin,
						1 - bottom-left origin (Windows bitmaps style) */
	int  align;         /* Alignment of image rows (4 or 8).
						OpenCV ignores it and uses widthStep instead */
	int  width;         /* image width in pixels */
	int  height;        /* image height in pixels */
	struct _IplROI *roi;/* image ROI. if NULL, the whole image is selected */
	struct _IplImage *maskROI; /* must be NULL */
	void  *imageId;     /* ditto */
	struct _IplTileInfo *tileInfo; /* ditto */
	int  imageSize;     /* image data size in bytes
						(==image->height*image->widthStep
						in case of interleaved data)*/
	char *imageData;  /* pointer to aligned image data */
	int  widthStep;   /* size of aligned image row in bytes */
	int  BorderMode[4]; /* ignored by OpenCV */
	int  BorderConst[4]; /* ditto */
	char *imageDataOrigin; /* pointer to very origin of image data
						   (not necessarily aligned) -
						   needed for correct deallocation */
}
IplImage;

typedef struct _IplROI
{
	int  coi; /* 0 - no COI (all channels are selected), 1 - 0th channel is selected ...*/
	int  xOffset;
	int  yOffset;
	int  width;
	int  height;
}
IplROI;

typedef struct
{
	int width;
	int height;
}
CvSize;

inline  CvSize  cvSize( int width, int height )
{
	CvSize s;

	s.width = width;
	s.height = height;

	return s;
}

typedef struct CvScalar
{
	double val[4];
}
CvScalar;

inline  CvScalar  cvScalar(int val0, int val1, int val2, int val3)
{
	CvScalar scalar;
	scalar.val[0] = val0; scalar.val[1] = val1;
	scalar.val[2] = val2; scalar.val[3] = val3;
	return scalar;
}

#define CV_RGB( r, g, b )  cvScalar( (b), (g), (r), 0 )

IplImage* cvCreateImage( CvSize size, int depth, int channels );
void cvReleaseImage(IplImage **ppImg);
bool cvSaveImage(const char *fileName, const IplImage *pImg);

typedef struct CvRect
{
	int x;
	int y;
	int width;
	int height;
}
CvRect;

inline  CvRect  cvRect( int x, int y, int width, int height )
{
	CvRect r;

	r.x = x;
	r.y = y;
	r.width = width;
	r.height = height;

	return r;
}

void  cvSetImageROI( IplImage* image, CvRect rect );
void cvResetImageROI( IplImage* image );

IplImage* cvCreateImageHeader( CvSize size, int depth, int channels );

inline RECT CvRectToRect( CvRect sr )
{
	RECT dr;
	dr.left = sr.x;
	dr.top = sr.y;
	dr.right = sr.x + sr.width;
	dr.bottom = sr.y + sr.height;

	return dr;
}

void cvReleaseImageHeader( IplImage** image );

typedef struct CvPoint
{
	int x;
	int y;
}
CvPoint;


inline  CvPoint  cvPoint( int x, int y )
{
	CvPoint p;

	p.x = x;
	p.y = y;

	return p;
}

void cvRectangle( IplImage *img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness = 1);

void cvLine(IplImage *img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness = 1);

inline int cvRound(float val) {
	if(val > 0) {
		return int(val + 0.5f);
	}
	return int(val - 0.5f);
}

#endif // __IPLIMAGE_DOT_H__
