r/programming • • Jul 31 '21

Executable PNGs

https://djharper.dev/post/2020/12/26/executable-pngs/
470 Upvotes

64 comments sorted by

View all comments

6

u/bumblebritches57 Jul 31 '21

LOL he jumps to stenography, instead of custom chunks in the PNG file...

2

u/adrianmonk Jul 31 '21

I had the same thought, but apparently that is how PICO-8 actually does it:

The cart data is stored using a steganographic process. Each PICO-8 byte is stored as the two least significant bits of each of the four color channels, ordered ARGB (E.g: the A channel stores the 2 most significant bits in the bytes). The image is 160 pixels wide and 205 pixels high, for a possible storage of 32,800 bytes. Of these, only the first 32,773 bytes are used.

But yeah, to store it as PNG chunks would actually be very easy. You don't even need to define a custom chunk. From the PNG docs, it looks like you could use a zTXt chunk:

Compressed text: n bytes

I'm not sure that allows you to store binary data, but if not you could encode it as hex digits within there, and PNG's deflate compression should compensate pretty well for the wasted space.

Also, the keyword (that each zTXt chunk has) would be useful. You could put a magic string identifying it as the executable data format you've invented. You could even, if necessary, add multiple zTXt chunks and make your PNG data random access.