#! /bin/sh

# DoublePrint.sh
# Reads PNM each of 2 specified files, writes PPM to standard output
# containing both inputs, the second either below or to the right of
# the first, of dimensions such that each input fits within
# 3.375"x2.531" when printed as 4"x6".
#
# Uses 'pnmfile', 'ppmmake', and 'pnmpaste'
#
# November 4, 2011
# For latest version, see gopher -p users/julianbr sdf.org

if [ -z "$2" ] || [ -n "$3" ]; then
       echo >&2 Usage ${0##*/} \(input pnm file \#1\) \(input pnm file \#2\)
       echo >&2 Writes PPM to standard output containing both inputs
       exit 0
       fi

InputDimensions=$(pnmfile "$1")
InputDimensions=${InputDimensions#*, }
InputDimensions=${InputDimensions# }
Input1Width=${InputDimensions%% *}
InputDimensions=${InputDimensions#* }
InputDimensions=${InputDimensions# }
InputDimensions=${InputDimensions#* }
InputDimensions=${InputDimensions# }
Input1Height=${InputDimensions%% *}

InputDimensions=$(pnmfile "$2")
InputDimensions=${InputDimensions#*, }
InputDimensions=${InputDimensions# }
Input2Width=${InputDimensions%% *}
InputDimensions=${InputDimensions#* }
InputDimensions=${InputDimensions# }
InputDimensions=${InputDimensions#* }
InputDimensions=${InputDimensions# }
Input2Height=${InputDimensions%% *}

WidestInput=$Input1Width
if [ $WidestInput -lt $Input2Width ]; then
       WidestInput=$Input2Width
       fi
HighestInput=$Input1Height
if [ $HighestInput -lt $Input2Height ]; then
       HighestInput=$Input2Height
       fi

if [ $WidestInput -gt $HighestInput ]; then
       OutputWidth=$(expr $WidestInput \* 32 / 27 + 2)
       OutputHeight=$(expr \( $Input1Height + $Input2Height \) \* 32 / 27 + 3)
       if [ $(expr $OutputWidth \* 3) -gt $(expr $OutputHeight \* 2) ]; then
               OutputHeight=$(expr \( $OutputWidth + 1 \) / 2 \* 3)
               OutputWidth=$(expr \( $OutputWidth + 1 \) / 2 \* 2)
       else
               OutputWidth=$(expr \( $OutputHeight + 2 \) / 3 \* 2)
               OutputHeight=$(expr \( $OutputHeight + 2 \) / 3 \* 3)
               fi
       ppmmake rgb:7F/7F/7F $OutputWidth $OutputHeight \
               | pnmpaste "$1" \
               $(expr \( $OutputWidth - $Input1Width \) / 2) \
               $(expr \( $OutputHeight - $Input1Height - $Input2Height \) \
                       / 3) \
               | pnmpaste "$2" \
               $(expr \( $OutputWidth - $Input2Width \) / 2) \
               $(expr $OutputHeight - $Input2Height \
                       - \( $OutputHeight - $Input1Height - $Input2Height \) \
                       / 3)
else
       OutputWidth=$(expr \( $Input1Width + $Input2Width \) \* 32 / 27 + 3)
       OutputHeight=$(expr $HighestInput \* 32 / 27 + 2)
       if [ $(expr $OutputWidth \* 2) -gt $(expr $OutputHeight \* 3) ]; then
               OutputHeight=$(expr \( $OutputWidth + 2 \) / 3 \* 2)
               OutputWidth=$(expr \( $OutputWidth + 2 \) / 3 \* 3)
       else
               OutputWidth=$(expr \( $OutputHeight + 1 \) / 2 \* 3)
               OutputHeight=$(expr \( $OutputHeight + 1 \) / 2 \* 2)
               fi
       ppmmake rgb:7F/7F/7F $OutputWidth $OutputHeight \
               | pnmpaste "$1" \
               $(expr \( $OutputWidth - $Input1Width - $Input2Width \) \
                       / 3) \
               $(expr \( $OutputHeight - $Input1Height \) / 2) \
               | pnmpaste "$2" \
               $(expr $OutputWidth - $Input2Width \
                       - \( $OutputWidth - $Input1Width - $Input2Width \) \
                       / 3) \
               $(expr \( $OutputHeight - $Input2Height \) / 2)
       fi

echo >&2 ${0##*/}: Writing ${OutputWidth}x${OutputHeight}