Refactor to make reading images more efficient by supporting reading different formats.