Fix bugs with rectangular image rendering; add unit test