Debian Codegraph @

editor/pamstretch-gen

raw
1 #!/bin/sh
2 ###############################################################################
3 # pamstretch-gen
4 ###############################################################################
5 # A generalized version of pamstretch that can do non-integer scale factors.
6 #
7 # It works by scaling up with pamstretch then scaling down with pamscale.
8 #
9 # it also copes with N<1, but then it just uses pamscale. :-)
10 #
11 # Formerly named 'pnminterp-gen' and 'pnmstretch-gen'.
12 #
13 ###############################################################################
14
15 # Scan command line arguments
16
17 while true ; do
18 case "$1" in
19 -version|--version )
20 pamstretch --version; exit $?;
21 ;;
22 -p|-pl|-pla|-plai|-plain|--p|--pl|--pla|--plai|--plain )
23 plainopt="-plain"
24 shift
25 ;;
26 -q|-qu|-qui|-quie|-quiet|--q|--qu|--qui|--quie|--quiet )
27 quietopt="-quiet"
28 shift
29 ;;
30 -verb|-verbo|-verbos|-verbose|--verb|--verbo|--verbos|--verbose )
31 verboseopt="-verbose"
32 shift
33 ;;
34 -* )
35 echo 'usage: pamstretch-gen N [pnmfile]' 1>&2
36 exit 1
37 ;;
38 * )
39 break
40 ;;
41 esac
42 done
43
44 tempfile=$(mktemp "${TMPDIR:-/tmp}/netpbm.XXXXXXXX")
45 if [ $? -ne 0 -o ! -e $tempfile ]; then
46 echo "Could not create temporary file. Exiting." 1>&2
47 exit 1
48 fi
49 trap 'rm -rf $tempfile' 0 1 3 15
50
51 case "$#" in
52 0)
53 echo "pamstretch-gen: too few arguments" 1>&2
54 exit 1
55 ;;
56 1 )
57 if ! cat > $tempfile; then
58 echo "pamstretch-gen: error reading input" 1>&2
59 exit 1
60 fi
61 ;;
62 2 )
63 if ! cat $2 > $tempfile; then
64 echo "pamstretch-gen: error reading file "$2 1>&2
65 exit 1
66 fi
67 ;;
68 * )
69 echo "pamstretch-gen: misaligned arguments or too many arguments" 1>&2
70 exit 1
71 ;;
72 esac
73
74 # Calculate pamstretch scale factor (="iscale") and output width and
75 # height. Usually "int(scale) + 1" is sufficient for iscale but
76 # in some exceptional cases adjustment is necessary because of
77 # "-dropedge".
78
79 report=$(pamscale $quietopt -reportonly $1 $tempfile)
80 if [ $? -ne 0 ]; then
81 echo "pamstretch-gen: pamscale -reportonly $1 (file) failed" 1>&2
82 exit 1
83 fi
84
85 iscale_width_height=$(echo $report |\
86 awk 'NF!=6 || $1<=0 || $2<=0 || $3<=0 || $5<=0 || $6<=0 { exit 1 }
87 { if ($3 > 1.0) { iscale = int($3) + 1;
88 if (iscale * ($1-1) < $5 ||
89 iscale * ($2-1) < $6 )
90 ++iscale; }
91 else { iscale = 1 } # $3 <= 1.0
92 }
93 { print iscale, "-width="$5, "-height="$6}' )
94
95 # Note that $1, $2, ..., $6 here are fields of the input line fed to awk,
96 # not shell positional parameters.
97
98 iscale=${iscale_width_height% -width=* -height=*}
99 width_height=${iscale_width_height#* }
100 if [ -n "$verboseopt" ]; then
101 echo "pamstretch-gen: rounded scale factor=$iscale $width_height" 1>&2
102 fi
103
104 pamstretch -dropedge $quietopt $iscale $tempfile |\
105 pamscale $verboseopt $quietopt $plainopt $width_height
106
107
108 # Copyright (C) 1998,2000 Russell Marks.
109 # Modifications for "pamscale -reportonly" "pamstretch -dropedge" by
110 # Akira Urushibata (Jan. 2019)
111 #
112 # This program is free software; you can redistribute it and/or modify
113 # it under the terms of the GNU General Public License as published by
114 # the Free Software Foundation; either version 2 of the License, or (at
115 # your option) any later version.
116 #
117 # This program is distributed in the hope that it will be useful, but
118 # WITHOUT ANY WARRANTY; without even the implied warranty of
119 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
120 # General Public License for more details.
121 #
122 # You should have received a copy of the GNU General Public License
123 # along with this program; if not, write to the Free Software
124 # Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
125