#!/bin/bash

# Corrects a set of image files for lens distortion using a preprepared
# recombination matrix 

# usage:
#
#       example% batch_rubber_sheet matrix_file image1 image2 ...
#
# writes output images rsc_image1.v rsc_image2.v ...

# get name we were run as
name=`basename $0`

# check args
if [ $# -lt 2 ]; then
	echo "usage: $name matrix image1 image2 ..."
	echo "writes rsc_image1 rsc_image2 ..."
	echo
	echo "$name uses VIPS to correct a set of images for lens distortion"
	echo "using a matrix calculated by the 'resample' function."

        exit 1
fi

rec=$1
shift

# transform each argument
for i in $*; do
	path=`dirname $i`
	base=`basename $i`
	new=$path/rsc_$base

	echo "Transforming $i to $new ..."
	if [ -f $new ]; then
		echo "($new already exists, skipping)"
	else
		# bilinear interp., don't wrap edges
		$VIPSHOME/bin/vips im_transform $i $new $rec 1 0
	fi
done

