#!/bin/bash

# DEPRECATED as of July 2007 - use generate-xml-gal.py instead if possible

#  GNU command-line front-end for XML Photo Gallery
#  by Philip Guo, December 2005
#
#  Generate resized images, thumbnails, and skeleton XML
#  file for XML Photo Gallery
#
#     This file is part of the XML Photo Gallery Project
#     by Philip J. Guo (Created August 2005)
#     Copyright 2005 Philip J. Guo
#     http://alum.mit.edu/www/pgbovine/
#
#       based on the People Photo Gallery Project
#       by Philip J. Guo (Created January 2005)
#       Copyright 2005 Philip J. Guo
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.


# Input: Run in a directory with JPEG images (*.jpg and *.JPG)
# Params: $1 - XML output filename (copied into $xml_outfile)
#         $2 - Gallery folder name (copied into $gal_foldername)
#         $3 - Gallery name (use quotes if there are spaces)
#         $4 - (optional) Non-null to use EXIFTRAN to auto-rotate JPEGs
# Pre:    $gal_foldername sub-directory and $xml_outfile
#         (XML output filename) must not already exist
# Output: Creates a sub-directory '$gal_foldername/images' that contains
#         all images resized so that the longer side is
#         LONG_DIM (the other dimension scales appropriately)
#         a sub-directory '$gal_foldername/thumbnails' that contains
#         thumbnails resized to 130x100 (or 100x130)
#         with a drop shadow, and a skeleton XML file
#         that can be used with the XML Photo Gallery
# Requires: ImageMagick, GNU coreutils
# Optional: exiftran (auto-rotates JPEG images)

xml_outfile=$1
gal_foldername=$2
gal_name=$3
use_exiftran=$4

# Vertical images are ? x LONG_DIM pixels,
# Horizontal images are LONG_DIM x ? pixels,
# where ? is determined by the aspect ratio of the image
# (it's 600 for most 4:3 digital camera images)
LONG_DIM=800

# Same as LONG_DIM, except for thumbnails
# (will get padded with ~10 extra pixels for drop shadow)
THUMB_LONG_DIM=120

# Adjust JPEG compression ratio for quality vs. filesize
# tradeoff from 0 (worst but smallest file) to 100 (best but largest file)
# 85% seems to get rid of most compression artifacts while
# keeping a reasonable filesize
JPEG_QUALITY=85

# Add a drop shadow to a thumbnail image that's already been created
# (preferably with the 'convert -thumbnail' command)
# Parameters:
# $1 - thumbnail filename (relative path from pwd)
# $2 - width
# $3 - height
# adapted from Edmond Lau's eSlides (ported from Perl to BASH)
# http://web.mit.edu/edmond/www/eSlides.html
# ... which was adapted from Brendt Wohlberg's dropshadow script
# http://www.wohlberg.net/public/software/photo/dropshadow/
function add_drop_shadow() {
    thumb=$1
    img_width=$2
    img_height=$3
    border=6
    gap=5
    blur="2x1" #blur="1.7x1"
    shoulder=2

    # Simulate a drop shadow by drawing rectangles on the right and
    # bottom sides of the image and blurring those regions:
    RIGHT_RECT="rectangle $img_width,$shoulder $(($img_width+$border-$gap)),$(($img_height+$border-$gap))"
    BOTTOM_RECT="rectangle $shoulder,$img_height $(($img_width+$border-$gap)),$(($img_height+$border-$gap))"

    # Add a white border around the image
    mogrify -bordercolor white -border ${border}x${border} -chop ${border}x${border} -page ${border}x${border}+0+0 $thumb

    # Draw the rectangles
    mogrify -fill grey50 -draw "$RIGHT_RECT" -draw "$BOTTOM_RECT" $thumb

    # Blur them
    mogrify -region ${border}x$(($img_height+$border))+$img_width -blur $blur $thumb
    mogrify -region ${img_width}x${border}+0+${img_height} -blur $blur $thumb

    # Add some more of a border and chop stuff to center the image
    mogrify -bordercolor white -border 4x4 -gravity SouthEast -chop 4x4 $thumb
}


# Write a chunk of XML data for a particular image
# Parameters:
# $1 - image filename (without sub-directory path)
# $2 - width
# $3 - height
function write_image_xml() {
cat << END_OF_IMAGE >> $xml_outfile
<image>
<filename>$1</filename>
<title></title>
<description></description>
<people>
</people>
<keywords>
</keywords>
<date>
<month>0</month>
<year>0</year>
</date>
<physical>
<width>$2</width>
<height>$3</height>
</physical>
</image>

END_OF_IMAGE
}


# Beginning of the script:

if [ -d $gal_foldername ]
then
    echo "Error: '$gal_foldername' sub-directory exists. Please delete it before proceeding."
    exit
fi

if [ -f $xml_outfile ]
then
    echo "Error: '$xml_outfile' XML file exists. Please delete it before proceeding."
    exit
fi

mkdir $gal_foldername

images_dir=$gal_foldername/images
thumbs_dir=$gal_foldername/thumbnails

mkdir $images_dir
mkdir $thumbs_dir

touch $xml_outfile # Create XML file

# Write the header:
cat << END_OF_HEADER >> $xml_outfile
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE gallery SYSTEM "gallery.dtd">
<?xml-stylesheet type="text/xsl" href="xml_gal.xml"?>

<gallery>

<galleryName><![CDATA[$gal_name]]></galleryName>
<folderName><![CDATA[$gal_foldername]]></folderName>

<images>

END_OF_HEADER


# Iterate over all JPEG images in the current directory:
for img in *.jpg *.JPG
do

    # If there are no jpg's or JPG's in the directory,
    # then $img is set to '*.jpg' or '*.JPG', respectively.
    # Skip in those cases
    if [ $img == '*.jpg' ] || [ $img == '*.JPG' ]
    then
	continue
    fi

    echo "Working on $img ..."

    # If /usr/bin/exiftran exists, then invoke it to rotate JPEG
    # images based on the orientation in EXIF metadata
    if [ -n "$use_exiftran" ]
    then
	echo "Running exiftran to rotate image"
	exiftran -ai $img
    fi

    info=`identify $img`
    type=`echo $info | awk '{print $2}'`

    # We only support type == 'JPEG' right now
    # (This serves as a sanity check)
    if [ $type != 'JPEG' ]
    then
	continue
    fi

    dimensions=`echo $info | awk '{print $3}'`
    width=`echo $dimensions | cut -d'x' -f1`
    height=`echo $dimensions | cut -d'x' -f2`

    if [ $width -gt $height ]
    then
	# For horizontal images, specify the width
	convert -resize $LONG_DIM -quality $JPEG_QUALITY% $img $images_dir/$img
	# Create a thumbnail from the shrunken image for efficiency
	convert -thumbnail $THUMB_LONG_DIM $images_dir/$img $thumbs_dir/$img
	# To get the other dimension, we need to query for it:
	resized_height=`identify $images_dir/$img | awk '{print $3}' | cut -d'x' -f2`
	thumb_height=`identify $thumbs_dir/$img | awk '{print $3}' | cut -d'x' -f2`
	add_drop_shadow $thumbs_dir/$img $THUMB_LONG_DIM $thumb_height
	write_image_xml $img $LONG_DIM $resized_height
    else
	# For vertical images, specify the height
	convert -resize x$LONG_DIM -quality $JPEG_QUALITY% $img $images_dir/$img
	convert -thumbnail x$THUMB_LONG_DIM $images_dir/$img $thumbs_dir/$img
	resized_width=`identify $images_dir/$img | awk '{print $3}' | cut -d'x' -f1`
	thumb_width=`identify $thumbs_dir/$img | awk '{print $3}' | cut -d'x' -f1`
	add_drop_shadow $thumbs_dir/$img $thumb_width $THUMB_LONG_DIM
	write_image_xml $img $resized_width $LONG_DIM
    fi

done

# Write the footer:
cat << END_OF_FOOTER >> $xml_outfile
</images>

</gallery>
END_OF_FOOTER

echo "All done :)"
