Fix image scaling issues when image has only one dimension smaller than the limit (#1489274)
Other small code improvements.