#!/bin/sh
#
# Script, that converts pictures saved in Canon's raw image file format
# (.crw) into a Photoshop image file with 16 bits per channel (.psd),
# an optimized TIFF with 8 bit per channel and a small downscaled
# JPEG version
# 
# Ensure, that you have both crw and ImageMagick installed on your
# system and that crw and convert are in your path.
#
# To get crw visit http://www2.primushost.com/~dcoffin/powershot/ and
# for ImageMagick go to http://www.imagemagick.org
#
if [ $# -eq 0 ]; then
        echo "no .crw files specified. Exiting"
        exit 1
fi
for file in $@; do
        imgpath=`dirname "${file}"`
        imgname=`basename "${file}" .CRW`
        # create PSD file
        crw -3 "${file}"
        # convert this to a normalized TIFF with 8 bits per channel
        echo "Optimize image, modify gamma and saturation, switch to 8 bit per channel"
        convert -normalize -gamma 1.2 -modulate 100,115 -depth 8 "${imgpath}/${imgname}.psd" "${imgpath}/${imgname}.tif"
        echo "Writing data to ${imgpath}/${imgname}.tif..."
        # create a small JPEG thumbnail
        echo "Create small thumbnail"
        convert -geometry 25%x25% -unsharp 1x10+2+0 "${imgpath}/${imgname}.tif" "${imgpath}/${imgname}.jpg"
        echo "Writing data to ${imgpath}/${imgname}.jpg..."
done
exit 0

