png encoder: improve performance and fix heap crash for very large images