Refactor some code in PngWriter._build_image_data()