hh2=`convert xc: -format "%[fx:$distort*$h2]" info:`
hh3=`convert xc: -format "%[fx:$height-$distort*$h2]" info:`
im_version=`convert -list configure | \
sed '/^LIB_VERSION_NUMBER /!d; s//,/; s/,/,0/g; s/,0*\([0-9][0-9]\)/\1/g'`
if [ "$im_version" -lt "06030507" ]
then
coords="0,0 $width,0 $width,$height 0,$height 0,0 $width,$hh2 $width,$hh3 0,$height"
else
coords="0,0 0,0 $width,0 $width,$hh2 $width,$height $width,$hh3 0,$height 0,$height"
fi
$ signifies the use of a variable holding the value.
h2 was calculated before to be simply the height of the predistorted image image divided by 2.
I split the image in two halves so that I can distort only the top or only the bottom if desired.
hh2 is a scaled version of h2 - scaled by the distortion desired ( I am using fx here as a calculator).
im_version is an extraction of the IM version being used as the perspective coordinates usage changed at IM version 6.3.5.7 (actually you have an old version of the script as I later found out that it really changed at 6.3.6.0 - so download the current version of the script). In older versions the coordinates needed to specified as "srcxy1 ... srcxy4 dstxy1 ... dstxy4". In the newer versions the scr and dst coordinates are alternating "srcxy1 dstxy1 ... srcxy4 dstxy4".
The if statement switches the order of the coordinates to be consistent with the IM version usage of the coordinates in -distort perspective.
I compute the coordinate ahead of time and use them later in the actual -distort perspective section.
AND----------------------------------------------------------
elif [ "$effect" = "bulge" -o "$effect" = "pinch" -o "$effect" = "bulge-top" -o "$effect" = "bulge-bottom" -o "$effect" = "pinch-top" -o "$effect" = "pinch-bottom" ]
then
convert $tmp0 $vp -background $bc \
-monitor -fx \
"xd=(i-$xc); yd=(j-$yc); rd=hypot(xd,yd)/$sf; ys=(yd/($a+$b*rd))+$yc; $result" \
-background $bc -trim -bordercolor $bc -border $pad \
$tmp0
This is part of the "effects" calculation and is the section for doing the bulge-like effects. It is selected by the elif statement as part of a greater if, elif, elif... fi conditional. This is kind of hard to explain as it is a complex mathematical statement, using -fx to do the mathematical computation and application of the warping.
$vp defines the -virtual-pixel option, namely, vp="-virtual-pixel background" if the background color bc is not none. If it bc=none then vp="-matte -channel RGBA -virtual-pixel transparent" in order to make the background transparent. There is an earlier place that sets this definition for vp. The -monitor just enables the terminal display of a progress monitor as -fx is slow.
xd=i-$xc and yd=j-$yc is just making the origin of the image coordinates at the center of the image (xc,yc);
rd=hypot(xd,yd) is a shortcut to sqrt(xd^2,yd^2) that converts to circular coordinates (symmetry); and $sf is a scale factor that is the average of the half width and half height.
ys=(yd/($a+$b*rd)) is the bulge-pinch distortion formula/equation, a and b are related to the user provided distort value. You will have to see the earlier section where those are defined as the definition depends upon whether it is a bulge or a pinch. It is something like a=1-distort and b=distort.
All of this leads up to the part of fx that does the calculation $result="u.p{i,ys}" which simply takes the pixel at the input coordinates (i,ys) and loads then into the outut image at coordinates (i,j). Note this is the formula for the full bulge or pinch. To do the top or bottom it is modified to distort the top/bottom and leave the opposite half unchanged.
After the -fx, I trim whatever border there is and then put a border back in of the desired size.
I hope this helps.
Fred