{ RCSid $Id: fisheye_corr.cal,v 1.5 2016/09/14 20:39:08 greg Exp $ }
{
	Correct fisheye image using a distortion function:

		rad(r) =	function of r in [0,.5] range, returing same

	This function takes the pixel distance from the image center, where
	1.0 is the full width of the image constituting 180-degrees of view.
	The output is the corrected distance from the center for the output.
	(If your function is the inverse of this, set map_inverse=1.)

		pcomb -f fisheye_corr.cal -e 'rad(r)=my_function(r)' \
			-o fisheye.hdr > corrected.hdr

	We also clear the region outside the r=[0,.5] circle to black.
	If this is all you want to do, use "rad(r)=r".

	If you know you have a solid-angle preserving distortion, use
	"rad(r)=mapsolid(r)" or leave off definition, as this is the default.

	Note that you will need to add back the VIEW= line to the header, e.g.:

		echo "VIEW= -vta -vh 180 -vv 180" > corrected.hdr
		(above_command) >> corrected.hdr

	Or:

		(above_command) | getinfo -a "VIEW= -vta -vh 180 -vv 180" \
			> corrected.hdr
}
xc : xres/2;
yc : yres/2;
sq(x) : x*x;
map_inverse = -1;		{ change to 1 if rad(r) is inverse mapping }
inp_r = sqrt(sq((x-xc)/xres) + sq((y-yc)/yres));
mapped_r = rad(inp_r);
rmult = if(map_inverse, inp_r/(mapped_r+1e-7), mapped_r/(inp_r+1e-7));
xoff = (x-xc)*(1-rmult);
yoff = (y-yc)*(1-rmult);
ro = if(.5-inp_r, ri(1,xoff,yoff), 0);
go = if(.5-inp_r, gi(1,xoff,yoff), 0);
bo = if(.5-inp_r, bi(1,xoff,yoff), 0);

{ Radius adjustment for equisolid-angle projection to equidistant (-vta) }
mapsolid(r) : 2/PI*asin(sqrt(2)*r);

{ Default correction function }
rad(r) = mapsolid(r);
