#!/usr/bin/perl -w

# Scan all files in current directory
open( INP, "/bin/ls |" ) or die( "Can't get files: $!" );

# Use gimp in batch mode as our output processor
# (needs (scheme (to drive it) (which is yucky)))
open( CMD, "| gimp --no-interface --no-data -b - " )
		or die( "Gimp failed: $!" );

sub do_image
{
	my $ix = shift;
	print STDERR "pic_${ix}.ppm ==> done_${ix}.ppm\n";
	print CMD "(process-image \"pic_${ix}.ppm\" \"done_${ix}.ppm\")\n";
}

# Here is the gimp processing function
print CMD <<EOF;
(define (process-image input output)
	(let* ( (image (car (file-pnm-load 1 input input)))
		(drawable (car (gimp-image-active-drawable image)))
		)
		(plug-in-gauss 1 image drawable 3 3 1)
		(plug-in-sel-gauss 1 image drawable 9 30)
		(plug-in-sel-gauss 1 image drawable 11 20)
		(file-pnm-save 1 image drawable output output 1)
		(gimp-image-delete image))
	;; Prints output while processing
	(string-append input " --> " output))
EOF

while(<INP>)
{
	if( m{^pic_([0-9]+)[.]ppm})
	{
		my $i = $1;
		if( $i < 100 )
		{
			do_image $i;
		}
		elsif( $i < 300 )
		{
			if(( $i & 1 ) == 0 ) { do_image $i; }
		}
		elsif( $i < 800 )
		{
			if(( $i & 3 ) == 0 ) { do_image $i; }
		}
		elsif( $i < 2000 )
		{
			if(( $i & 7 ) == 0 ) { do_image $i; }
		}
		else
		{
			if(( $i & 15 ) == 0 ) { do_image $i; }
		}
	}
}

print CMD "(gimp-quit 0)\n";

close CMD;
close INP;
