#ifndef __IMG_H__
#define __IMG_H__

/* TYPES ************************************************************/

typedef struct {
  double *start;
  int stride, vdim, hdim;
} image_type;

#define MAX_DIMS 2
typedef struct {
  double v,h;           /* 2-D point coordinates */
  double d[MAX_DIMS];   /* N-D data vector at this point */
} point_type;

typedef struct {
  double vv,vh,hv,hh;
} covar_type;

/* simple image RENDERING functions *************************************/

/* set a pixel in an image */
extern void img_draw_pixel(image_type *img,
			  point_type *pnt,
			  double color);

/* draw a line into an image */
extern void img_draw_line(image_type *img,
			 point_type *p,
			 point_type *q,
			 double val);

/* draw a polygon into an image */
extern void img_draw_poly(image_type *img,
			 point_type *points,
			 int N,
			 double val);

/* point ANALYSIS and SYNTHESIS functions ********************************/

/* find the mean point of a given set of points */
extern void pnts_get_mean(point_type *points, /* input point list */
			  int L,              /* input #points */
			  point_type *u);     /* output mean point */

/* find the covariance of a given set of points */
extern void pnts_get_covariance(point_type *points, /* input point list */
				int L,              /* input #points */
				covar_type *K);     /* output covariance */

/* make a set of points with covariance K and mean u */
extern void pnts_make_normal_dist(covar_type *K,      /* input covariance */
				  point_type *u,      /* input mean point */
				  point_type *points, /* altered points */
				  int L);             /* # points */

/* make a contour that describes the K weighted distance of M from u */
extern void pnts_make_normal_contour(covar_type *K,      /* input covariance */
				     point_type *u,      /* input mean point */
				     double M,           /* input Mana dist */
				     point_type *points, /* altered points */
				     int L);             /* #points */

/* apply an affine transformation described by mean (translation) and 
 * covariance (Rotation) to a point */
extern void pnt_transform(point_type *input,
			  covar_type *Rotation,
			  point_type *translation,
			  point_type *output);

/* find a^(-1) given a */
void cov_inverse(covar_type *A, covar_type *A_inv);

/* find v and d given a such that a = v * d * v' */
void cov_eigen(covar_type *a, covar_type *v, covar_type *d);

/* out = vec * val * vec' */
void cov_synthesize(covar_type *eigvec, covar_type *eigval, covar_type *out );
/* At = A' */
void cov_transpose(covar_type *A, covar_type *At );

/* C = A * B */
void cov_mult(covar_type *A, covar_type *B, covar_type *C );

/* INTERPOLATION of IMAGE surface at a POINT ***************************/

extern double linear_interp(double x1,double y1,
			    double x2,double y2,
			    double x);

extern void bilinear_interp(point_type src_neigbhorhood[2][2],
			    point_type *src_pos,
			    int dims);

extern void bicubic_interp(point_type src_neighborhood[4][4],
			   point_type *pos,
			   int dims);


/* OTHER ***************************************************/

extern void img_histeq(unsigned char *data, long Y, long X);

extern void img_histogram(long *hist,unsigned char *data,long Y,long X);

extern long lvec_running_sum(long *vector,long L);

extern void img_remap(unsigned char *data,unsigned char *lut,long Y,long X);

#endif


