#!/usr/bin/perl
#
# Thumbnail and html page generator
# Copyright Arto Teräs <ajt@iki.fi> 1999-2000
#
# Some inspiration from thumbs.sh by Adam Kopacz / kLoGraFX
# http://www.klografx.de/ - Adam.K@klografx.de
#
# Version: 1.01
# Licence: GNU General Public License (version 2 or any later version)
#          http://www.gnu.org/copyleft/gpl.txt
#
# Changelog:
#
# 1.0  Initial version
# 1.01 Fixed a small error in command line utilities detection
#
# Latest version: http://www.iki.fi/ajt/software/thumbnailtools/
######################################################################
#
# modified by jra to remove html generation.
#
######################################################################
#
#
# Global defaults:
# 
$SCALEFACTOR = 7;    # the size of the original picture is divided
                     # by the SCALEFACTOR to get the thumbnail size,
                     # can be adjusted using the command line option -d

$THUMBQUALITY = 75;  # thumbnail image quality, can be adjusted using 
                     # the command line option -q

$TIMESTAMP_FROM_ORIGINAL = 1;   # file date picked from the 
                                # picture_orig.jpg, if it exists
                                # (not adjustable from command line)

#
######################################################################

use Getopt::Std;
use POSIX;

sub find_program {
    # Quick check if the given program can be found in the PATH
    # This will work only on Unix machines...
    my $program = shift;
    @system_dirs = split(':', $ENV{PATH});

    foreach (@system_dirs) {
	if (-x "$_/${program}") {
	    return 1;
	}
    }    
    # not found
    return 0;
}

# The main program
#
getopts('d:q:');

if (@ARGV) 
{
    if ($opt_d != 0) {
	$SCALEFACTOR = $opt_d;
    }
    if ($opt_q != 0) {
	$THUMBQUALITY = $opt_q;
    }

    # Check for necessary command line utilities
    $USE_IMAGEMAGICK = 0;
    if (! (find_program("cjpeg") == 1 
	   && find_program("djpeg") == 1
	   && find_program("pnmscale") == 1
	   && find_program("identify") == 1)) {
	# Ok, not all the programs available, but we can do the same a bit
	# slower with the convert utility from ImageMagick
	$USE_IMAGEMAGICK = 1;
	if (find_program("convert") == 0
	    || find_program("identify") == 0) {
	    print STDERR "Couldn't find all the necessary utilities.\n\n";
	    print STDERR "Please make sure that following programs can be found in the PATH:\n"; 
	    print STDERR "1. identify (ImageMagick package)\n";
	    print STDERR "2. convert (ImageMagick package)\n";
	    print STDERR "(works faster if you also have cjpeg, djpeg and pnmscale)\n";
	    exit(1);
	}
    }

    my $image;
    foreach $image (@ARGV) 
    {		
	if ($image =~ /(.*)\.jpg/)
	{	    
	    print "Processing image $image\n";
	    my $basename = $1;
	    my $thumbname = "$basename" . "_small.jpg";
	    my $origname = "$basename" . "_orig.jpg";

	    my @imagestats = stat($image);
	    my $imagesize = int ($imagestats[7] / 1024);  # in kilobytes
	    if (($TIMESTAMP_FROM_ORIGINAL) && (-f $origname))
	    {
		@imagestats = stat($origname);
	    }
	    my $imagedate = scalar localtime($imagestats[9]);
	    
	    # If the image name contains spaces, parsing the output of identify
	    # would fail without this trick:
	    my $parts_in_image_name = split(' ', $image);
	    my @imageinfo = split(' ', `identify -ping "$image"`);
	    my $dimensions = $imageinfo[$parts_in_image_name + 1];
	    my ($xsize, $ysize) = split('x', $dimensions);
	    $xsize /= $SCALEFACTOR;
	    $ysize /= $SCALEFACTOR;
	    
	    # Generate thumbnail
	    if ($USE_IMAGEMAGICK) {
		`convert -geometry ${xsize}x${ysize} -quality ${THUMBQUALITY} -interlace Line "$image" "$thumbname"`;
		}
		else {
		    # This is noticeably faster 
		    `djpeg -pnm "$image" | pnmscale -xsize $xsize | cjpeg -quality $THUMBQUALITY -optimize -progressive > "$thumbname"`;
		}
}
}
    exit(0);
}
else
{
    print "Usage: thumbgenerator.pl [OPTIONS] IMAGEFILES (in jpeg format)\n\n";
    print "Available options:\n";
    print "-d divider     the scaling factor for thumbnail size (default ${SCALEFACTOR})\n";
    print "-q quality     the output jpeg quality (default ${THUMBQUALITY}%)\n"; 
    exit(1);
}

