env/bin/imgresize.sh
2019-09-11 17:44:49 +00:00

75 lines
1.4 KiB
Bash

#!/bin/sh
# env
# Written in 2019 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
usage() {
cat - <<. >&2
Usage:
${0##*/} size in-file out-file
"size" is WxH or Wx or xH.
.
exit 1
}
check_dimension_format() {
printf "%s\n" "$1" | grep -q "^[0-9]*x[0-9]*\$" \
&& [ "$1" != "x" ]
}
if [ $# -ne 3 ] || ! check_dimension_format "$1"; then
usage
fi
if [ -z "$2" ]; then
printf "%s: Input file name can't be empty.\n" \
"${0##*/}" >&2
exit 1
fi
if [ -z "$3" ]; then
printf "%s: Output file name can't be empty.\n" \
"${0##*/}" >&2
exit 1
fi
size=$1
w=${size#x*}
h=${size%*x}
infile=$2
outfile=$3
inprog=
case $infile in
*.jpg | *.jpeg)
inprog=jpegtopnm
;;
*.png) inprog=pngtopam
;;
*) printf "%s: Unknown input format.\n" "${0##*/}" >&2
exit 1
;;
esac
outprog=
case $outfile in
*.jpg | *.jpeg)
outprog=pnmtojpeg
;;
*.png) outprog=pamtopng
;;
*) printf "%s: Unknown output format.\n" "${0##*/}" >&2
exit 1
;;
esac
"$inprog" <"$infile" \
| pamscale ${w:+-width "$w"} ${h:+-height "$h"} \
| "$outprog" >"$outfile"