env/bin/imgresize.sh

71 lines
1.3 KiB
Bash
Raw Normal View History

2019-09-11 04:11:40 +02:00
#!/bin/sh
2019-12-08 19:14:46 +01:00
# imgresize
2019-09-11 04:11:40 +02:00
# 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/>.
2019-12-08 19:14:46 +01:00
usage()
{
2019-09-11 04:11:40 +02:00
cat - <<. >&2
Usage:
${0##*/} size in-file out-file
"size" is WxH or Wx or xH.
.
exit 1
}
2019-12-08 19:14:46 +01:00
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
exit 1
}
check_dimension_format()
{
2019-09-11 04:11:40 +02:00
printf "%s\n" "$1" | grep -q "^[0-9]*x[0-9]*\$" \
&& [ "$1" != "x" ]
}
2019-12-08 19:14:46 +01:00
[ $# -eq 3 ] && check_dimension_format "$1" || usage
[ -n "$2" ] || err "Input file name can't be empty."
[ -n "$3" ] || err "Output file name can't be empty."
2019-09-11 04:11:40 +02:00
size=$1
2019-12-08 17:17:45 +01:00
w=${size%x*}
h=${size#*x}
2019-09-11 04:11:40 +02:00
infile=$2
outfile=$3
inprog=
case $infile in
*.jpg | *.jpeg)
inprog=jpegtopnm
;;
*.png) inprog=pngtopam
;;
2019-12-08 19:14:46 +01:00
*) err "Unknown input format."
2019-09-11 04:11:40 +02:00
;;
esac
outprog=
case $outfile in
*.jpg | *.jpeg)
outprog=pnmtojpeg
;;
*.png) outprog=pamtopng
;;
2019-12-08 19:14:46 +01:00
*) err "Unknown output format."
2019-09-11 04:11:40 +02:00
;;
esac
"$inprog" <"$infile" \
| pamscale ${w:+-width "$w"} ${h:+-height "$h"} \
| "$outprog" >"$outfile"