Copied code from TEMP-rust-png