#! /bin/csh -f
# Last edited on 2009-12-23 17:40:19 by stolfi

set usage = "$0 [ -crop DX DY NX NY ] [ -portrait | -logo | -landscape | -size NX NY | -aspect AX AY ] FILE..."

# Converts a bunch of original images 
# to "p-raw.ppm.gz" files of a specified aspect ratio, and puts them 
# in the same directory as the ".tiff" files.

# The pictures are automatically cropped and scaled to the
# specified size and shape.

# For correct scaling, the input images should be scanned 
# with the linear ("raw monitor") calibration.

# !!! TO DO: use ".png" as the intermediate format instead of ".ppm.gz".

# Set program paths:

set tooldir = "$0"; set tooldir = "${tooldir:h}"

set getdims = "${STOLFIHOME}/bin/get-pnm-dimensions"
# set pbmbin = "${STOLFIHOME}/pkg/netpbm-1mar1994-1/PUB/${PLATFORM}/bin"
set pbmbin = "/usr/bin"

set aspx = 5
set aspy = 7
unset nx
unset ny

unset cropdx
unset cropdy
unset cropnx
unset cropny

while ( ( $#argv >= 1 ) && ( "/$1" =~ /-* ) )
  if ( "/$1" == "/-portrait" ) then
    set aspx = 5; set aspy = 7;
    set nx = 280; set ny = 392;
    shift;
  else if ( "/$1" == "/-logo" ) then
    set aspx = 1; set aspy = 1;
    set nx = 400; set ny = 400;
    shift;
  else if ( "/$1" == "/-landscape" ) then
    set aspx = 7; set aspy = 5;
    set nx = 630; set ny = 450;
    shift;
  else if ( ( "/$1" == "/-crop" ) && ( $#argv > 5 ) ) then
    set cropdx = "$2"; set cropdy = "$3";
    set cropnx = "$4"; set cropny = "$5";
    shift; shift; shift; shift; shift;
  else if ( ( "/$1" == "/-size" ) && ( $#argv > 3 ) ) then
    set nx = "$2";    set ny = "$3";
    set aspx = "$nx"; set aspy = "$ny";
    # Reduce $nx, $ny to relatively prime ratio:
    @ tx = $nx; 
    @ ty = $ny;
    while ( $ty > 0 )
      @ tr = $tx - ( ( $tx / $ty ) * $ty )
      @ tx = $ty
      @ ty = $tr
    end
    @ gcd = $tx
    echo "gcd($nx,$ny) = $gcd"
    @ aspx = $nx / $gcd
    @ aspy = $ny / $gcd
    echo "aspect ratio ${aspx}:${aspy}"
    shift; shift; shift
  else if ( ( "/$1" == "/-aspect" ) && ( $#argv > 3 ) ) then
    set aspx = "$2"; set aspy = "$3";
    shift; shift; shift
  else
    echo "usage: $usage"
    exit 1
  endif
end

if ( $#argv < 1 ) then
  echo "usage: $usage"
  exit 1
endif

set files = ( $* )

foreach forig ( ${files} )
  set name = ${forig:r}
  set fraw = ${forig:h}/p-raw.ppm
  if ( -e ${forig} ) then
    if ( -e ${fraw}.gz ) then
      /bin/mv ${fraw}.gz ${fraw}.gz~
      echo "renamed ${fraw}.gz to ${fraw}.gz~"
    endif
    if ( ".${forig:e}" == ".ppm" ) then
      set fppm = "${forig}"
    else
      set fppm = "/tmp/$$.ppm"
      echo "converting ${forig} to ${fppm}..." 
      convert ${forig} ${fppm}
    endif

    # echo "determining the cropping and scaling parameters..."
    set fxy = ( `cat ${fppm} | ${getdims}` )
    if ( ?cropnx ) then
      # Start with the user-specified cropping:
      @ skipx = $cropdx
      @ skipy = $cropdy
      @ bignx = $cropnx
      @ bigny = $cropny
    else
      # No user-specified cropping:
      @ skipx = 0
      @ skipy = 0
      @ bignx = $fxy[1]
      @ bigny = $fxy[2]
    endif
    
    # Adjust cropping to specified aspect ratio:
    @ rx = $bignx * $aspy
    @ ry = $bigny * $aspx 
    if ( $rx < $ry ) then
      @ nrel = $bignx / $aspx
    else
      @ nrel = $bigny / $aspy
    endif
    @ cutx = $nrel * $aspx
    @ cuty = $nrel * $aspy
    @ skipx =  ( ( $bignx - $cutx ) / 2 ) + $skipx
    @ skipy =  ( ( $bigny - $cuty ) / 2 ) + $skipy

    echo "converting ${fppm} to ${fraw}.gz..."
    if ( $?nx ) then
      set nxy = "${nx}x${ny}"
      echo "cropping from ${fxy[1]}x${fxy[2]} to ${cutx}x${cuty} and scaling to ${nxy}" 
      set scalecmd = ( ${pbmbin}/pnmscale -xsize ${nx} )
    else
      unset nxy
      echo "cropping from ${fxy[1]}x${fxy[2]} to ${cutx}x${cuty}" 
      set scalecmd = ( /bin/cat )
    endif

    cat ${fppm} \
    | ${pbmbin}/pnmcut $skipx $skipy $cutx $cuty \
    | ${scalecmd} \
    | gzip > ${fraw}.gz

    if ( -r /tmp/$$.ppm ) /bin/rm -f /tmp/$$.ppm
  else
    echo "${forig} not found"'\!'
  endif
end
