# Last edited on 2024-04-01 16:59:36 by stolfi

# This file is source'd by "linear-ppm-to-gif" and
# "linear-ppm-pair-to-gif" to parse the scaling
# and transparency options.  
#
# The client script should have defined the variables 
#
#     "$scriptname"  = the script name;
#     "$usagetail"   = the synopsis of any additional parameters;
#     "$transparent" = should be set iff transparency is default.
#
# This script defines
# 
#   $usage        = the full usage example;
#   $gamma        = the gamma correction exponent (default "1.8");
#   $gammatag     = tag of corresponding colormap (default "gm18");
#   $getimg       = the image-reading command, either "cat" or "pnmscale OPTS";
#   $ppmtropts    = options for "ppmoquant", either ( ) or ( -transparent COLOR )
#   $giftropts    = options for "ppmtogif", either ( ) or ( -transparent COLOR ).

usage="${scriptname} [ SCALEOPTS ] [ -gamma N.N ] [ -transparent 'M:R,G,B' ] ${usagetail}"

# If SCALEOPTS are present, the input image is first scaled 
# with "pnmscale".  The SCALEOPTS are those of "pnmscale",
# except that a single number NN is not allowed --- instead,
# one must write "-scale NN".  The complete options are
#
#    -scale S        scales both X and Y by S
#    -xscale S       scales X only by S
#    -yscale S       scales Y only by S
#    -xsize W        scales both X and Y by W/width
#    -ysize H        scales both X and Y by H/height
#    -xysize W H     scales both X and Y by min(W/width, H/height)
#    -pixels N       scales both X and Y by sqrt(N/(width*height))
# 
# Separate scales may be specified for each axis.  Note that
# if only one "scale" is specified, the other axis is left unscaled;
# if only one "size" is specified, both axes are scaled by the same
# factor.  By the way, "-width" and "-hight" are alternative names
# for "-xsize" and "-ysize".
#
# The default gamma is 1.8 which should be OK for Sun XTerms.
# If specified, it must fit the format "[0-9]\.[0-9]*"; the
# "$gammatag" variable will be set to "g" followed by the value given
# with the period omitted.
#
# The transparent COLOR parameter should have the form "rgb:R/G/B", 
# where "M" is the input image's "maxval", and R, G, B
# is the input pixel value to make transparent.
# IMPORTANT: the input image(s) and the colormap are assumed to have 
# maxval = 255.
#
# The default transparent color is (64,64,64) before gamma correction,
# which is not in the current linear 6x6x6 colormap. It gets
# mapped to (118,118,118) =  by the default 1.8 gamma correction.

gamma="1.8"
gammatag="gm18"
itrpix=(  64  64  64 )
otrpix=( 118 118 118 )
recomputetr=0
getimg=( "/bin/cat" )
giftropts=( )
opt=( )

bgcolor=0

while [[ ( $# -gt 0 ) && ( "/$1" =~ /-.* ) ]]; do
  if [[ ( $# -ge 2 ) && ( "/$1" == "/-transparent" ) ]]; then
    transparent=1
    itrpix=( `echo "P2 1 1 255 255" | pgmtoppm "$2" | pnmdepth 255 | pnmnoraw | tail -1` )
    if [[ ( $status -ne 0 ) || ( ${#itrpix[@]} -ne 3 )  ]]; then
      echo "${scriptname}: error parsing color spec $2" 1>&2
      exit 1
    fi
    recomputetr=1
    shift; shift
  elif [[ ( $# -ge 2 ) && ( "/$1" == "/-gamma" )  ]]; then
    gamma="$2" 
    gammatag="`echo "gm$2" | tr -d '.\012'`";
    recomputetr=1
    shift; shift
    if [[ "/${gamma}" !~ /[0-9][.][0-9]*  ]]; then
      echo "${scriptname}: bad gamma spec, should be 'N.N'" 1>&2
      exit 1
    fi
  elif [[ ( $# -ge 2 ) && ( "/$1" == "/-scale" )  ]]; then
    getimg=( "pnmscale" "$2" ); shift; shift;
  elif [[ \
      ( $# -ge 2 ) && \
      ( ( "/$1" == "/-xsize" )  || \
        ( "/$1" == "/-ysize" )  || \
        ( "/$1" == "/-xscale" ) || \
        ( "/$1" == "/-yscale" ) || \
        ( "/$1" == "/-width" )  || \
        ( "/$1" == "/-height" ) || \
        ( "/$1" == "/-pixels" ) || \
      ) ) \
    ]]; then
    getimg=( "pnmscale" "$1" "$2" ); shift; shift;
  elif [[ ( $# -gt 3 ) && ( "/$1" == "/-xysize" ) ]]; then
    getimg=( "pnmscale" "$1" "$2" "$3" ); shift; shift; shift;
  else
    echo "** usage: ${usage}" 1>&2 ; exit 1
  fi
done

if [[  $transparent -ne 0 ]]; then
  if [[  $recomputetr -ne 0 ]]; then
    echo "recomputing gamma-corrected transparent color" 1>&2
    echo "warning: assuming maxval = 255 in input image and colormap" 1>&2
    otrpix=( \
      `echo "P3 1 1 255 ${itrpix}" | pnmgamma ${gamma} | pnmdepth 255 | pnmnoraw | tail -1` \
    )
    recomputetr=0
  fi

  itrcolor="`printf 'rgb:%02x/%02x/%02x' ${itrpix[@]}`"
  otrcolor="`printf 'rgb:%02x/%02x/%02x' ${otrpix[@]}`"
  echo "transparent color in  = '${itrpix}' = '${itrcolor}'" 1>&2
  echo "transparent color out = '${otrpix}' = '${otrcolor}'" 1>&2
  ppmtropts=( "-transparent" "${itrcolor}" )
  giftropts=( "-transparent" "${otrcolor}" )
fi
