local ratio="$orig_height/$orig_width" step=-1 unset completed while [ ! "$completed" ]; do step=$(( $step + 1 )) local up_step=$(( $width + ($step * 16) )) local down_step=$(( $width - ($step * 16) )) for x_step in $down_step $up_step; do local x_width=$(( $x_step - ($x_step % 16) )) local x_height=$( echo "scale=0; $x_width*$ratio/1" | bc ) if (( ($x_width % 16) + ($x_height % 16) == 0 )); then completed="y" width=$x_width height=$x_height fi done done