#!/bin/ksh

# Color correct and view images
#
# The Color Management System software provides no way to set the
# profile for your monitor. (profmgr sets a default source profile
# not a default destination profile.) This script uses ${HOME}/.monitor.pf
# If you create a custom monitor profile using the CosmoColor web software,
# download that profile and save it in ${HOME}/.monitor.pf. Otherwise
# link one of the profiles in /var/cms/profiles to ${HOME}/.profile. 
#

MPROFILE=${HOME}/.monitor.pf
TMPDIR=${TMPDIR:-/var/tmp}
USAGE="Usage: $0 [-m monitor_profile] file ..."

# 
# The "-n" argument is a hack to allow this script to be used as an alternate
# viewer for gscan. It lets us quietly ignore the -noopen argument intended
# for vstiff that is passed by gscan.
#
# You can make gscan use this script as its STIFF viewer by putting a
# line like the following into the file ~/.desktop-*/GScan:
#
#       *viewStiff: <path>/cocoview
#
# Now when you view the scanned image in gscan, it will be color corrected.
# To make this change for the entire system, put the above line in the file
# /usr/lib/X11/app-defaults/GScan.
#
while getopts m:n: f
do
    case $f in
        m) MPROFILE=$OPTARG;;
        n) ;;
        -) break;;
        \?) echo $USAGE; exit 2;; 
    esac
done
shift `expr $OPTIND - 1`

if [ $# = "0" ]
then
    echo $USAGE
    exit 2
fi

for i in $*
do
    case `filetype $i` in
        *STIFF93aImage|TIFFImage)
            # Unfortunately, in all current IRIX releases vstiff doesn't properly
            # load its colormap. This is a problem on systems with 8-bit only
            # graphics. If you have this problem, clone the jpeg handling
            # code, change "jpeg" for "tiff", "jpg" for "tif" and replace
            # the following command.
            cocostiff -d ${MPROFILE} $i | /usr/sbin/vstiff -stdin -noopen &
            ;;
        *JPEGJFIFImage)
            cocofile=${i##*/}
            cocofile=${TMPDIR}/${cocofile%.*}.coco.jpg
            cocojpeg -d ${MPROFILE} -o $cocofile $i
            imgview $cocofile &
            sleep 2
            rm -f $cocofile
            ;;
        *GIF?*Image)
            cocofile=${i##*/}
            cocofile=${TMPDIR}/${cocofile%.*}.coco.gif
            cocogif -d ${MPROFILE} -o $cocofile $i
            imgview $cocofile &
            sleep 2
            rm -f $cocofile
            ;;
        *)
            echo "$0 only works with JPEG, TIFF/STIFF and GIF format images." 1>&2
            exit 1
            ;;
    esac
done
