Backporting png generation fixes from trunk