source: gs2-extensions/imagemagick/trunk/src/bin/script/aspectpad.sh@ 26865

Last change on this file since 26865 was 26865, checked in by davidb, 11 years ago

Added in the aspectpad.sh script (from 'Fred') to help with the panorama viewer part of Greenstone3

  • Property svn:executable set to *
File size: 8.8 KB
Line 
1#!/bin/bash
2#
3# Developed by Fred Weinhaus 12/1/2009 .......... revised 12/4/2009
4#
5# USAGE: aspectpad [-a aspect] [-m mode] [-p pcolor] [-t toler] infile outfile
6# USAGE: aspectpad [-help]
7#
8# OPTIONS:
9#
10# -a aspect aspect ratio value desired; float>=1; default=2
11# -m mode mode for aspect; al, ap, l or p; default=al
12# l=force landscape pad; p=force portrait pad;
13# al=automatic pad (landscape for square images);
14# ap=automatic pad (portrait for square images)
15# -p pcolor pad color; any valid IM color; default=black
16# -t toler aspect tolerance; float>=0; if absolute difference
17# between desired aspect and image aspect is less
18# than or equal to tolerance, then no padding;
19# default=0
20#
21###
22#
23# NAME: ASPECTPAD
24#
25# PURPOSE: To pad an image with a color to a specified aspect ratio
26# and orientation.
27#
28# DESCRIPTION: ASPECTPAD pads an image with a color to a specified aspect
29# ratio and orientation. The user can choose to force the pad to either
30# landscape or portrait orientation or preserve the orientation in automatic
31# mode. All padding will result in the image being centered.
32#
33# OPTIONS:
34#
35# -a aspect ... ASPECT is the desired aspect ratio. Values are floats>=1.
36# The default=2
37#
38# -m mode ... MODE is the padding mode. Choices are: l, p, al or ap. When
39# mode=l, the padding will force the result to be landscape at the desired
40# aspect value. When mode=p, the padding will force the result to be portrait.
41# When mode=al, the padding will preserve the aspect of the original image, but
42# will pad a square image into landscape format. When mode=ap, the padding will
43# preserve the aspect of the original image, but will pad a square image into
44# portrait format. The default=al.
45#
46# -p pcolor ... PCOLOR is the desired padding color. Any valid IM color
47# specification may be used. The default=black
48#
49# -t toler ... TOLER is the aspect tolerance. If the absolute difference
50# between desired aspect and image aspect is less than or equal to toler,
51# then no padding will be applied. Values are floats>=0. The default=0
52#
53# CAVEAT: No guarantee that this script will work on all platforms,
54# nor that trapping of inconsistent parameters is complete and
55# foolproof. Use At Your Own Risk.
56#
57######
58#
59
60# set default values
61aspect="2" # aspect>=1
62mode="al" # al, ap, l, p; a=auto
63pcolor="black" # pad color
64toler=0 # toler>=0
65
66# set directory for temporary files
67dir="." # suggestions are dir="." or dir="/tmp"
68
69# set up functions to report Usage and Usage with Description
70PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path
71PROGDIR=`dirname $PROGNAME` # extract directory of program
72PROGNAME=`basename $PROGNAME` # base name of program
73usage1()
74 {
75 echo >&2 ""
76 echo >&2 "$PROGNAME:" "$@"
77 sed >&2 -n '/^###/q; /^#/!q; s/^#//; s/^ //; 4,$p' "$PROGDIR/$PROGNAME"
78 }
79usage2()
80 {
81 echo >&2 ""
82 echo >&2 "$PROGNAME:" "$@"
83 sed >&2 -n '/^######/q; /^#/!q; s/^#*//; s/^ //; 4,$p' "$PROGDIR/$PROGNAME"
84 }
85
86
87# function to report error messages
88errMsg()
89 {
90 echo ""
91 echo $1
92 echo ""
93 usage1
94 exit 1
95 }
96
97
98# function to test for minus at start of value of second part of option 1 or 2
99checkMinus()
100 {
101 test=`echo "$1" | grep -c '^-.*$'` # returns 1 if match; 0 otherwise
102 [ $test -eq 1 ] && errMsg "$errorMsg"
103 }
104
105# test for correct number of arguments and get values
106if [ $# -eq 0 ]
107 then
108 # help information
109 echo ""
110 usage2
111 exit 0
112elif [ $# -gt 10 ]
113 then
114 errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
115else
116 while [ $# -gt 0 ]
117 do
118 # get parameter values
119 case "$1" in
120 -help) # help information
121 echo ""
122 usage2
123 exit 0
124 ;;
125 -m) # get mode
126 shift # to get the next parameter
127 # test if parameter starts with minus sign
128 errorMsg="--- INVALID MODE SPECIFICATION ---"
129 checkMinus "$1"
130 mode=`echo "$1" | tr '[A-Z]' '[a-z]'`
131 case "$mode" in
132 l) ;;
133 p) ;;
134 al) ;;
135 ap) ;;
136 *) errMsg "--- MODE=$mode IS AN INVALID VALUE ---"
137 esac
138 ;;
139 -a) # get aspect
140 shift # to get the next parameter
141 # test if parameter starts with minus sign
142 errorMsg="--- INVALID ASPECT SPECIFICATION ---"
143 checkMinus "$1"
144 aspect=`expr "$1" : '\([.0-9]*\)'`
145 aspecttest=`echo "$aspect < 1" | bc`
146 [ $aspecttest -eq 1 ] && errMsg "--- ASPECT=$aspect MUST BE A FLOAT GREATER THAN OR EQUAL TO 1 ---"
147 ;;
148 -p) # get pcolor
149 shift # to get the next parameter
150 # test if parameter starts with minus sign
151 errorMsg="--- INVALID PCOLOR SPECIFICATION ---"
152 checkMinus "$1"
153 pcolor="$1"
154 ;;
155 -t) # get toler
156 shift # to get the next parameter
157 # test if parameter starts with minus sign
158 errorMsg="--- INVALID TOLER SPECIFICATION ---"
159 checkMinus "$1"
160 toler=`expr "$1" : '\([.0-9]*\)'`
161 tolertest=`echo "$toler < 0" | bc`
162 [ $tolertest -eq 1 ] && errMsg "--- TOLER=$toler MUST BE A NON-NEGATIVE FLOAT ---"
163 ;;
164 -) # STDIN and end of arguments
165 break
166 ;;
167 -*) # any other - argument
168 errMsg "--- UNKNOWN OPTION ---"
169 ;;
170 *) # end of arguments
171 break
172 ;;
173 esac
174 shift # next option
175 done
176 #
177 # get infile and outfile
178 infile=$1
179 outfile=$2
180fi
181
182# test that infile provided
183[ "$infile" = "" ] && errMsg "NO INPUT FILE SPECIFIED"
184
185# test that outfile provided
186[ "$outfile" = "" ] && errMsg "NO OUTPUT FILE SPECIFIED"
187
188# setup temporary images
189tmpA1="$dir/aspectpad_1_$$.mpc"
190tmpA2="$dir/aspectpad_1_$$.cache"
191trap "rm -f $tmpA1 $tmpA2; exit 0" 0
192trap "rm -f $tmpA1 $tmpA2; exit 1" 1 2 3 15
193
194
195# read the input image and test validity.
196convert -quiet -regard-warnings "$infile" +repage "$tmpA1" ||
197 errMsg "--- FILE $infile DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAS ZERO SIZE ---"
198
199# get size and aspect ratio of input
200ww=`convert $tmpA1 -ping -format "%w" info:`
201hh=`convert $tmpA1 -ping -format "%h" info:`
202wratio=`convert xc: -format "%[fx:$ww/$hh]" info:`
203hratio=`convert xc: -format "%[fx:$hh/$ww]" info:`
204#echo "ww=$ww; hh=$hh wratio=$wratio; hratio=$hratio"
205
206# test if aspect >= ratio
207wtest=`convert xc: -format "%[fx:$aspect>=$wratio?1:0]" info:`
208htest=`convert xc: -format "%[fx:$aspect>=$hratio?1:0]" info:`
209#echo "wtest=$wtest; htest=$htest"
210
211# test if within toler of desired aspect
212tratio=`convert xc: -format "%[fx:$wratio>=1?$wratio:$hratio]" info:`
213ttest=`convert xc: -format "%[fx:abs($tratio-$aspect)<=$toler?1:0]" info:`
214#echo "tratio=$tratio; ttest=$ttest"
215
216# copy input to output if image aspect is within toler
217if [ $ttest -eq 1 ]; then
218 convert $tmpA1 $outfile
219 exit
220fi
221
222# force landscape mode
223if [ "$mode" = "l" -a $wtest -eq 1 ]; then
224 ww=`convert xc: -format "%[fx:$hh*$aspect]" info:`
225 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
226 exit
227elif [ "$mode" = "l" -a $wtest -eq 0 ]; then
228 hh=`convert xc: -format "%[fx:$hh*$wratio/$aspect]" info:`
229 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
230 exit
231fi
232
233# force portrait mode
234if [ "$mode" = "p" -a $htest -eq 1 ]; then
235 hh=`convert xc: -format "%[fx:$ww*$aspect]" info:`
236 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
237 exit
238elif [ "$mode" = "p" -a $htest -eq 0 ]; then
239 ww=`convert xc: -format "%[fx:$ww*$hratio/$aspect]" info:`
240 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
241 exit
242fi
243
244
245# auto square image
246if [ "$mode" = "al" -a $ww -eq $hh ]; then
247 ww=`convert xc: -format "%[fx:$hh*$aspect]" info:`
248 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
249 exit
250elif [ "$mode" = "ap" -a $ww -eq $hh ]; then
251 hh=`convert xc: -format "%[fx:$ww*$aspect]" info:`
252 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
253 exit
254fi
255
256
257# auto landscape image
258if [ $ww -gt $hh -a $wtest -eq 1 ]; then
259 ww=`convert xc: -format "%[fx:$hh*$aspect]" info:`
260 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
261 exit
262elif [ $ww -gt $hh -a $wtest -eq 0 ]; then
263 hh=`convert xc: -format "%[fx:$hh*$wratio/$aspect]" info:`
264 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
265 exit
266fi
267
268
269# auto portrait image
270if [ $hh -gt $ww -a $htest -eq 1 ]; then
271 hh=`convert xc: -format "%[fx:$ww*$aspect]" info:`
272 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
273 exit
274elif [ $hh -gt $ww -a $htest -eq 0 ]; then
275 ww=`convert xc: -format "%[fx:$ww*$hratio/$aspect]" info:`
276 convert $tmpA1 -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
277 exit
278fi
279
Note: See TracBrowser for help on using the repository browser.