#!/bin/bash

# crop a set of image files
#
# usage:
#
#       example% batch_crop <left> <top> <width> <height> image1 image2 etc
#
# writes output images cr_image1 cr_image2, etc.

name=`basename $0`

# check args
if [ $# -lt 5 ]; then
	echo "usage: $name <left> <top> <width> <height> image1 image2 ..."
	echo "writes: cr_image1 cr_image2 ..."
	echo 
	echo "$name crops a group of image files"

	exit 1
fi

left=$1
top=$2
width=$3
height=$4
shift 4

for i in $*; do
	path=`dirname $i`
	base=`basename $i`
	new=$path/cr_$base

	echo "Cropping $i to $new ..."
	if [ -f $new ]; then
		echo "($new already exists, skipping)"
	else
		$VIPSHOME/bin/vips im_extract_area $i $new \
			$left $top $width $height
	fi
done
