Extract write_png function

A small refactoring that moves most of `store_png` into a new `write_png` function. This allows any `Writer`, like stdout, to receive the png bytes. Now `store_png` is responsible only for creating the file and delegates the png serialization to `write_png`.