encode: factor copy_and_pad out of encode_img_in