//@+leo-ver=4-thin
//@+node:aleator.20050908101148.2:@thin cvWrapLEO.h
//@@language c
#ifndef __CVWRAP__
#define __CVWRAP__

#ifndef M_PI
#define M_PI           3.14159265358979323846
#endif

#include <stdio.h>
#include <opencv2/core/core_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/legacy/compat.hpp>
#include <complex.h>

IplImage* wrapCreateImage32F(const int width, const int height, const int channels);
IplImage* wrapCreateImage64F(const int width, const int height, const int channels);

IplImage* wrapCreateImage8U(const int width, const int height, const int channels);

IplImage *wrapCopyMakeBorder(IplImage* src, const int top, const int bottom, const int left, const int right, const int borderType, const float value);

void wrapSubRS(const CvArr *src, double s,CvArr *dst);
void wrapSubS(const CvArr *src, double s,CvArr *dst);
void wrapAddS(const CvArr *src, double s, CvArr *dst);

double wrapAvg(const CvArr *src, const CvArr *mask);
double wrapStdDev(const CvArr *src);
double wrapStdDevMask(const CvArr *src,const CvArr *mask);
double wrapSum(const CvArr *src);
void wrapMinMax(const CvArr *src,const CvArr *mask
               ,double *minVal, double *maxVal);
void wrapAbsDiffS(const CvArr *src, double s, CvArr *dst);

void wrapSetImageROI(IplImage *i,int x, int y, int w, int h);

IplImage* wrapSobel(IplImage *src,int dx
                   ,int dy,int size);

IplImage* wrapLaplace(IplImage *src,int size);

IplImage* ensure8U(const IplImage *src);
IplImage* ensure32F(const IplImage *src);

void wrapSet32F2D(CvArr *arr, int x, int y, double value);
double wrapGet32F2D(CvArr *arr, int x, int y);
uint8_t wrapGet8U2DC(IplImage *arr, int x, int y,int c);

void wrapDrawCircle(CvArr *img, int x, int y, int radius, float r,float g,float b, int thickness);

void wrapDrawLine(CvArr *img, int x, int y, int x1, int y1, double r, double g, double b, int thickness);

void wrapFillPolygon(IplImage *img, int pc, int *xs, int *ys, float r, float g, float b);

void wrapDrawEllipse(IplImage *img, int x, int y, int r1, int r2, float a, float a1, float a2, float r, float g, float b, int thickness);

void wrapMatMul(int w, int h, double *mat
               , double *vec, double *t);

// Utils. Place them in another file
IplImage* rotateImage(IplImage* src,double scale,double angle);
CvHistogram* calculateHistogram(IplImage *img,int bins);
void wrapReleaseHist(CvHistogram *hist);
double getHistValue(CvHistogram *h,int bin);
void get_histogram(IplImage *img,IplImage *mask
                 ,float a, float b,int isCumulative
                 ,int binCount
                 ,double *values);

//void get_weighted_histogram(IplImage *img,IplImage *mask);
//                 ,float a, float b
//                 ,int binCount
//                 ,double *values);

IplImage* getSubImage(IplImage *img, int sx,int sy,int w,int h);
int getImageHeight(IplImage *img);
int getImageWidth(IplImage *img);


IplImage* susanSmooth(IplImage *src, int w, int h
                     ,double t, double sigma);

IplImage* susanEdge(IplImage *src,int w,int h,double t);
IplImage* getNthCentralMoment(IplImage *src, int n, int w, int h);
IplImage* getNthAbsCentralMoment(IplImage *src, int n, int w, int h);
IplImage* getNthMoment(IplImage *src, int n, int w, int h);

double calcGabor(double x, double y
                ,double stdX, double stdY
                ,double theta, double phase
                ,double cycles);

void gaborFilter(const CvArr *src, CvArr *dst
                ,int maskWidth, int maskHeight
                ,double stdX, double stdY
                ,double theta,double phase
                ,double cycles);

void radialGaborFilter(const CvArr *src, CvArr *dst
                ,int maskWidth, int maskHeight
                ,double sigma
                ,double phase,double center
                ,double cycles);

void renderRadialGabor(CvArr *dst,int width, int height
                ,double sigma
                ,double phase, double center
                ,double cycles);

void render_gaussian(IplImage *dst
                   ,double stdX, double stdY);

void renderGabor(CvArr *dst,int width, int height
                ,double dx, double dy
                ,double stdX, double stdY
                ,double theta, double phase
                ,double cycles);

void smb(IplImage *image,double t);
void smab(IplImage *image,int w, int h,double t);

IplImage* selectiveAvgFilter(IplImage *src,double t
                            ,int wwidth, int wheight);

IplImage* wrapFilter2D(IplImage *src, int ax,int ay,
                    int w, int h, double *kernel);
IplImage* wrapFilter2DImg(IplImage *src
                         ,IplImage *mask
                         ,int ax,int ay);

void wrapFloodFill(IplImage *i, int x, int y, double c
                  ,double low, double high,int fixed);

void sqrtImage(IplImage *src,IplImage *dst);

void weighted_localBinaryPattern(IplImage *src,int offsetX,int offsetXY
                                , IplImage* weights, double *LBP);

void localBinaryPattern(IplImage *src, int *LBP);
void localBinaryPattern3(IplImage *src, int *LBP);
void localBinaryPattern5(IplImage *src, int *LBP);
void localHorizontalBinaryPattern(IplImage *src, int *LBP);
void localVerticalBinaryPattern(IplImage *src, int *LBP);

void get_weighted_histogram(IplImage *src, IplImage *weights,
                       double start, double end,
                       int bins, double *histo);


void eigenValsViaSVD(double *A, int size, double *eVals
                    ,double *eVects);

IplImage* sizeFilter(const IplImage *src, double minSize, double maxSize, int mode);
int blobCount(const IplImage *src);


IplImage *acquireImage(int w, int h, double *d);

void wrapProbHoughLines(IplImage *img, double rho, double theta
                       , int threshold, double minLength
                       , double gapLength
                       , int *maxLines
                       , int *xs, int *ys
                       , int *xs1, int *ys1);


double average_of_line(int x0, int y0
                     ,int x1, int y1
                     ,IplImage *src);

IplImage* adaUpdateDistrImage(IplImage *target
                          ,IplImage *weigths
                          ,IplImage *test
                          ,double at);

double adaFitness1(IplImage *target
                 ,IplImage *weigths
                 ,IplImage *test);

CvMoments* getMoments(IplImage *src, int isBinary);

void freeCvMoments(CvMoments *x);

void getHuMoments(CvMoments *src,double *hu);

void freeCvHuMoments(CvHuMoments *x);

void haarFilter(IplImage *intImg,
                int a, int b, int c, int d,
                IplImage *target);

double haar_at(IplImage *intImg,
                int x1, int y1, int w, int h);

void wrapDrawRectangle(CvArr *img, int x1, int y1,
                       int x2, int y2, float r, float g, float b,
                       int thickness);

void calculateAtan(IplImage *src, IplImage *dst);
void calculateAtan2(IplImage *src1,IplImage *src2, IplImage *dst);
double calcSelectiveAvg(IplImage *img, double t , int x, int y , int wwidth, int wheight);

// Contours
typedef struct {
  CvMemStorage *storage;
  CvSeq *contour;
  CvSeq *start;
  int refCount;
} FoundContours;

typedef struct {
  FoundContours * fc;
  CvSeq *thisContour;
} FoundContour;

CvMoments* contour_moments(const FoundContour *c);
void contour_points(const FoundContour *f, int *xs, int *ys);
double contour_area(const FoundContour *c);
int cur_contour_size(const FoundContour *c);
double contour_perimeter(const FoundContour *c);
int more_contours(FoundContours *f);
int next_contour(FoundContours *f);
int reset_contour(FoundContours *f);
void free_found_contours(FoundContours *f);
void get_next_contour(FoundContours *fc);
void print_contour(const FoundContour *fc);
FoundContours* get_contours(const IplImage *src, int mode);
FoundContour* get_contour(FoundContours *);
void draw_contour( IplImage *src, FoundContour *con, int color
                 , int holeColor, int level, int thinkness, int linetype
                 , int dx, int dy);
void free_found_contour(FoundContour *f);

double juliaF(double a, double b,double x, double y);
void simpleMatchTemplate(const IplImage* target, const IplImage* template, int* x, int* y, double *val, int type);
IplImage* templateImage(const IplImage* target, const IplImage* template);
IplImage* simpleMergeImages(IplImage *a, IplImage *b,int offset_x, int offset_y);

void alphaBlit(IplImage *a, IplImage *aAlpha, IplImage *b, IplImage *bAlpha, int offset_x, int offset_y);
void blitImg(IplImage *a, IplImage *b,int offset_x, int offset_y);
IplImage* fadedEdges(int w, int h, int edgeW);
IplImage* rectangularDistance(int w, int h);
void radialRemap(IplImage *source, IplImage *dest, double k);
void plainBlit(IplImage *a, IplImage *b, int offset_y, int offset_x);
void wrapMinMaxLoc(const IplImage* target, int* minx, int* miny, int* maxx, int* maxy, double *minval, double *maxval);
void incrImageC(void);
IplImage* vignettingModelCos4(int w, int h) ;
IplImage* vignettingModelCos4XCyl(int w, int h) ;
IplImage* vignettingModelX2Cyl(int w, int h,double m, double s, double c);
void wrapDrawText(CvArr *img, char *text, float s, int x, int y,float r,float g,float b);

IplImage* vignettingModelB3(int w, int h,double b1, double b2, double b3);
IplImage* vignettingModelP(int w, int h,double scalex, double scaley, double max);
IplImage* wrapPerspective(IplImage* src, double a1, double a2, double a3
                                       , double a4, double a5, double a6
                                       , double a7, double a8, double a9);
IplImage* simplePerspective(double k,IplImage *src);
double bilinearInterp(IplImage *tex, double u, double v);
void findHomography(double* srcPts, double *dstPts, int noPts, double *homography);
void masked_merge(IplImage *src1, IplImage *mask, IplImage *src2, IplImage *dst);
IplImage* makeEvenUp(IplImage *src);
IplImage* padUp(IplImage *src,int right, int bottom);
IplImage* makeEvenDown(IplImage *src);
void vertical_average(IplImage *src1, IplImage *dst);

IplImage* composeMultiChannel(IplImage* img0
                             ,IplImage* img1
                             ,IplImage* img2
                             ,IplImage* img3
                             ,const int channels);

IplImage *acquireImageSlow(int w, int h, double *d);
IplImage *acquireImageSlowF(int w, int h, float *d);
void exportImageSlow(IplImage *img, double *d);
void exportImageSlowF(IplImage *img, float *d);

IplImage *acquireImageSlowComplex(int w, int h, complex double *d);
void exportImageSlowComplex(IplImage *img, complex double *d);
void subpixel_blit(IplImage *a, IplImage *b, double offset_y, double offset_x);
double bicubicInterp(IplImage *tex, double u, double v);

CvVideoWriter* wrapCreateVideoWriter(char *fn, char cc1, char cc2, char cc3, char cc4, double fps,int w, int h, int color);

double wrapGet32F2DC(CvArr *arr, int x, int y,int c);
void maximal_covering_circle(int ox,int oy, double or, IplImage *distmap
                            ,int *max_x, int *max_y, double *max_r);

int wrapFindChessBoardCorners(const void* image, int pw, int ph, CvPoint2D32f* corners, int* cornerCount, int flags);

int wrapDrawChessBoardCorners(void* image, int pw, int ph, CvPoint2D32f* corners, int cornerCount, int wasFound);

double wrapCalibrateCamera2(const CvMat* objectPoints, const CvMat* imagePoints, const CvMat* pointCounts, CvSize *imageSize, CvMat* cameraMatrix, CvMat* distCoeffs, CvMat* rvecs, CvMat* tvecs, int flags);

void wrapFitEllipse(CvArr* pts, CvBox2D *out);
int wrapCamShift(const CvArr* prob_image, CvRect *window, CvTermCriteria *criteria, CvConnectedComp* comp, CvBox2D* box);

void wrapExtractSURF(const CvArr* image, const CvArr* mask, CvSeq** keypoints, CvSeq** descriptors, CvMemStorage* storage, CvSURFParams *param, int useProvidedKeyPts){
return cvExtractSURF(image, mask, keypoints, descriptors, storage, *param, useProvidedKeyPts);
};

void wrapEllipseBox(CvArr* img, CvBox2D *box, CvScalar *color
                   ,int thickness, int lineType, int shift)
{
cvEllipseBox(img, *box, *color, thickness, lineType, shift);
};


void extractCVSeq(const CvSeq* seq,void *dest);

void printSeq(const CvSeq *seq) {
printf("Seq:\n flags %d\nheader_size %d\n total %d\n ptr %p",
       seq->flags, seq->header_size, seq->total, (void*)seq->ptr);
}

int wrapMeanShift(const CvArr* prob_image, CvRect *window, CvTermCriteria *criteria, CvConnectedComp* comp)
{
return cvMeanShift(prob_image, *window, *criteria, comp);
};

void wrapMinAreaRect2(const CvArr* points, CvMemStorage* storage, CvBox2D *r)
{ *r = cvMinAreaRect2(points, storage); }

void wrapBoundingRect(CvArr* points, int update, CvRect *r)
{ *r = cvBoundingRect(points, update); }

void wrapWarpPerspective(const CvArr* src, CvArr* dst, const CvMat* mapMatrix )
{ cvWarpPerspective( src,  dst,  mapMatrix, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); }

void blitShadow(IplImage *a, IplImage *b);

IplImage* ensure64F(const IplImage *src);

#endif
//@-node:aleator.20050908101148.2:@thin cvWrapLEO.h
//@-leo
