refactor PNG chunk handling code