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

# This file is source'd by "linear-ppm-to-png" and
# "linear-ppm-pair-to-png" to parse the gamma and scaling options.  
#
# The client script should have defined the variables 
#
#     "$scriptname"  = the script name;
#     "$usagetail"   = the synopsis of any additional parameters;
#
# This script defines
# 
#   $usage        = the full usage example;
#   $gamma        = the gamma correction exponent (default "1.8");
#   $gammatag     = tag of corresponding colormap (default "gm18");
#   $ammag        = the inverse gamma correction exponent (1/$gamma);
#   $getimg       = the image-reading command, either "cat" or "pnmscale OPTS";

usage=\
  "${scriptname} [ SCALEOPTS ] [ -gamma N.N ] ${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.0 which should provide the correct
# appearance on the screen.
# 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.

gamma="1.0"
gammatag="gm10"
getimg=( "/bin/cat" )
opt=( )

bgcolor=0

while [[ ( $# -gt 0 ) && ( "/$1" =~ /-.* ) ]]; do
  if [[ ( $# -ge 2 ) && ( "/$1" == "/-gamma" ) ]]; then
    gamma="$2" 
    gammatag="`echo "gm$2" | tr -d '.\012'`";
    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 [[ ( $# -ge 3 ) && ( "/$1" == "/-xysize" ) ]]; then
    getimg=( "pnmscale" "$1" "$2" "$3" ); shift; shift; shift;
  else
    echo "** usage: ${usage}" 1>&2 ; exit 1
  fi
done

ammag=`gawk -v g=${gamma} 'BEGIN{printf "%7.5f", 1.0/g}'`
