Fix potential write outside of the image