#include <pgm.h>
/* Fast (= paced for robot runtime)
   fisheye image rectifier, applying  calibration parameters
   from the "flatfish" wide angle image calibration program. 

   Fast interest operator, samples left member of stereo image pair,
   classifying regions suitable for stereo ranging.

   Fast correlator, for finding regions in right image corresponding to
   selected feastures in left image.

                              Created 4/96
                              Hans Moravec <hpm@cmu.edu>
                              Carnegie Mellon University
			      Pittsburgh, PA  15213
			      working at Daimler Benz research, Berlin */

/* Structure to hold raw calibration parameters and rectification tables */
typedef struct {Int32 Ph, Pw;		/* Height and Width of image */
		Float32 Yaim, Xaim, Distance, FOV;  /* Calibration setup */
		Float32 Pi, Pj, AS; /* pixel optical center and aspect ratio */
		Float32 Si, Sj;     /* optical image center in spot coords */
		Float32 AN;	     /* Roll angle of the grid image */
		Int32  PN;	    /* degree of radial distortion polynomials */
		Float32 *P;         /* Spot->Pixel radius polynomial */
		Float32 *S;         /* Pixel->Spot radius polynomial */
		signed Int8 *di, *dj; /* rectification offset tables */
		gray **pij;   /* rectification full pointer table */
	      }  Calib;

int FishHead(char *CalFile, Calib *Cal);
/* FishHead reads in flatfish calibration file named "CalFile",
   into a calibration structure Cal.  It does not expand the
   parameters into a pointer table in Cal (done by FishBody) */

int FishBody(Calib *Cal, gray **picin);
    /* FishBody calculates the per pixel rectification, and store in either the
       offset or the direct pointer tables, depending on compile switches */

int GoFish(char *CalFile, Calib *Cal, gray **picin);
/* GoFish reads in flatfish calibration file named "CalFile",
   expands the parameters into a pointer table into pgm picture
   "picin", whose parameters must match those given in CalFile,
   and stores the result in structure "Cal"  */
 
void FishTail(gray **picin, Calib *Cal, gray **picout);
/* FishTail applies the transformation stored in "Cal" to the pgm
   picture "picin" to produce the rectified image "picout" */


int Interest(gray **picin, int Pheight, int Pw, int *out);
/* Interest Operator, computes interest function on 8 x 8 windows.
   Image regions scored high by the interest operator contain enough
   brightness variations in all directions to be good candidates for
   finding by correlator in other views of the same scene
   picin is picin[Ph][Pw], out must be out[(Ph>>3)*(Pw>>3)]  */

int HInterest(gray **picin, int Pheight, int Pw, int *out);
/* Interest Operator, computes interest function on 8 x 8 windows.
   Image regions scored high by the interest operator contain enough
   brightness variations in horizontal direction to be good candidates for
   finding by correlator in other views of the same scene
   picin is picin[Ph][Pw], out must be out[(Ph>>3)*(Pw>>3)]  */

int Correl8(gray **left, int ileft, int jleft, int Pheight, int Pw,
	    gray **right, int irlo, int jrlo, int irhi, int jrhi,
	    int *correl, int *corri);
/* Scan image feature in "left" image centered on [ileft,jleft] across
   centers in "right" image rectangle bounded by [irlo,jrlo] [irhi,jrhi]
   inclusive.  The best correlation values for each j position, over all
   i positions are returned in indices jrlo to jrhi of array "correl".
   The array "corri" contains the i value at which the best match for
   each j was found. Correl8 is hard coded to use 8x8 windows around
   the selected locations. */

int Correl7(gray **left, int ileft, int jleft, int Pheight, int Pw,
	    gray **right, int irlo, int jrlo, int irhi, int jrhi,
	    int *correl, int *corri);
/* Correl7 is hard coded to use 7x7 windows around the selected locations. */


int Correl6(gray **left, int ileft, int jleft, int Pheight, int Pw,
	    gray **right, int irlo, int jrlo, int irhi, int jrhi,
	    int *correl, int *corri);
/* Correl6 is hard coded to use 6x6 windows around the selected locations. */

int FCorrel7(gray **leftmean, gray **rightmean,
	     gray **left, int ileft, int jleft, int Pheight, int Pw,
	    gray **right, int irlo, int jrlo, int irhi, int jrhi,
	    int *correl, int *corri);

int F2Correl7(unsigned Int16 *leftmean, unsigned Int16 *rightmean,
	     gray **left, int ileft, int jleft, int Pheight, int Pw,
	    gray **right, int irlo, int jrlo, int jrhi, int *correl);

/* Scan image feature in "left" image centered on [ileft,jleft] across
   centers in "right" image rectangle bounded by [irlo,jrlo] [irhi,jrhi]
   inclusive.  The best correlation values for each j position, over all
   i positions are returned in indices jrlo to jrhi of array "correl".
   The array "corri" contains the i value at which the best match for
   each j was found. Correl7 is hard coded to use 7x7 windows around
   the selected locations. FCorrel7 eliminates windows whose averages
   differ more than 20 from the prototype */

int CorrelHills(double wtscale, int *correl, int sjl, int sjh,
		int *ppos, double *wt);
/* find and weight the peaks in a correlation curve
   wtscale is the distance scale, in pixel gray levels
   correl[sjl to sjh] is the raw correlation curve array - gets wiped
   ppos gets the j positions of the peaks,
   wt gets the normalized weights, 1.0 = perfect  */
