#!/bin/sh
#
# rletojpeg [-o outfile] [infile]
#
# Shell script for RLE => JPEG conversion.
#
# Uses Tom Lane's cjpeg program and Jef Poskanzer's PBM+ tiftopnm program.
#
# (I go through the intermediate TIFF format because the cjpeg program
#  apparently doesn't recognize the magic number for a PPM plain file
#  and I didn't feel like diddling with the source code until I am sure
#  what the problem is.)
#
# Rich Thomson
# November 2nd, 1991
#
#	   Copyright 1991 by Evans & Sutherland Computer Corporation,
#			      Salt Lake City, Utah
#
#			      All Rights Reserved
#
#     Permission to use, copy, modify, and distribute this software and its
#	documentation for any purpose and without fee is hereby granted,
#     provided that the above copyright notice appear in all copies and that
#	both that copyright notice and this permission notice appear in
#   supporting documentation, and that the names of Evans & Sutherland not be
#       used in advertising or publicity pertaining to distribution of the
#	      software without specific, written prior permission.
#
#   EVANS & SUTHERLAND  DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
#     INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
#     EVENT SHALL EVANS & SUTHERLAND BE LIABLE FOR ANY SPECIAL, INDIRECT OR
#  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
#     DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
#	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
#			 PERFORMANCE OF THIS SOFTWARE.
#
usage()
{
    echo "rletojpeg [-o outfile] [infile]"
    exit 1
}

convert()
{
    rleflip -v $file | rletotiff -o $tiff; tifftopnm $tiff 2>&- | cjpeg
}
ofile="-"

if [ $# -gt 3 ]; then
    usage
fi

if [ "X$1" = "X-o" ]; then
    shift
    ofile=$1
    shift
fi

if [ $# -gt 1 ]; then
    usage
fi

file=$1
if [ $# -eq 0 ]; then
    file=""
    tiff=/tmp/stdin.tiff
else
    if [ "X$file" = "-" ]; then
	file=""
	tiff=/tmp/stdin.tiff
    else
	f=`basename $file .Z`
	f=`basename $f .rle`
	tiff=/tmp/${f}.tiff
    fi
fi

if [ $ofile = "-" ]; then
    convert
else
    convert > $ofile
fi
rm $tiff
