I’ve spent a lot of time thinking about this because of the Playable Quotes [0] project that I worked on with @rndmcnlly.<p>First off, I love embedding data into images. It’s surprising how useful it is to do this! The main reason that I like embedding data into an image is because you get a “thumbnail” essentially for free.<p>In the future, I hope to see a more widespread use of this technique, especially for images that are the output of a set of data. Imagine a chart with the source data embedded into it! Or an SVG embedded in a PNG.<p>This is what we did for Playable Quotes, each quote is a PNG of the framebuffer at the time the quote was started. All of the data needed to play that moment in the game is embedded into the PNG.<p>That said, there are at least three main approaches that can be used to embed data into a PNG. Each has various trade offs:<p>The approaches are as follows:
1: Concatenate the PNG with a ZIP file
2: Use PNG chunks (this is what is done in the article)
3: Embed data using a stenography<p>Here are the pros and cons of each approach:<p>Option 1: Concatenate the PNG with a ZIP file<p>This is a clever technique because (in short) PNGs have length information in them and zip software will attempt to read files from the end of the file if they can’t read from the start.<p>Pros:<p>- Super easy to do: “cat original.png file.zip > new.png”<p>- Easy to work with. You can extract data by using the “zip” command or renaming the new.png to new.zip<p>Cons:<p>- The data is lost when sharing the image. Most image hosts and even some chat programs will strip out the zip data from the image.<p>Option 2: Use PNG chunks (this is what is done in the article)<p>This approach is nice and clean, as the PNG specification explicitly allows for this. In essence, a PNG is a little key/value database where the keys are four ASCII printable bytes.<p>Pros:<p>- Complies with the PNG standard<p>- Data is usually maintained, but not always!<p>Cons:<p>- Requires special tooling to use. Standard PNG command line tools don’t offer easy ways to get chunk information out of PNGs<p>- The extra chunks are SOMETIMES removed. Especially by services like Twitter which will try and optimize the images before hosting them<p>Option 3: Embed data using a stenography<p>This is the approach that PICO-8 uses for its game “cartridges” and what we ultimately used for Playable Quotes<p>Pros:<p>- The most durable way to keep the data attached to the image<p>Cons:<p>- The amount of data you can store is limited by the size of the image<p>- Special tooling is needed to extract the data<p>In general, my personal favorite is option 1, as it is the most useful. However, it’s sadly not a practical solution to use in a world where the tools we use to share images (Twitter, Imgur, etc) modify the images in transit.<p>Footnotes:<p>0: <a href="https://tenmile.quote.games" rel="nofollow">https://tenmile.quote.games</a>