JPEG Crush

I've been interested in glitch art for a little while now mainly after I saw something on the Creators Project by Paul Davis. There's also a little mention about it on Wikipedia.

So, where do we begin?

Well, JPEGs (like other files) have markers in them which break up segments denoted by one 0xFF byte and a proceeding byte to name the marker. Below I've listed the complete 16bit pair:

0xFFD8 - This is the 'Start of Image' or 'SOI' marker - the start of the JPEG file.

0xFFDA - This bad boy tells us that from here onwards (until the next marker) there's image data. It's official name is 'Start of Scan' or 'SOS'.

0xFFD9 - This is the JPEG end marker.

Between 0xFFD8 and 0xFFDA you'll find Exif data and copy right info etc.

There's a stack of info on other JPEG markers here.

Go land…

I've been playing in Go recently so that's what this thing is written in. I also hacked together a Ruby version before doing the same in Go.

When we use Go to chop things up these come out at 8bit regular bytes (0xFF etc). I ended up doing a little shimmy to look ahead to check the HEX value of the next byte, to check for the marker.

Go has a damn useful standard lib called bytes. Bytes has a very useful method called Index which I used to scan over the byte array to find where I needed to fiddle… Once I had that I used ioutil to query for the start / end offsets in the byte array. Here's a snippet of the code from that bit (no pun intended ;)…


  // up where we're declaring vars...

  var (
    JPEG_START = []byte{ 0xff, 0xda }
    JPEG_END   = []byte{ 0xff, 0xd9 }
  )

  // lower down...

  start := bytes.Index(f, JPEG_START)
  end   := bytes.Index(f, JPEG_END)

From then on it was pretty much trial and error. Picking an offset and overwriting bytes in that offset with random HEX values.

In the Go I slung together for this I used my name in HEX.

Example time!

What does this thing look like then James? Glad you asked. Here's the original then a little GIF of corrupted JPEGs…

Before corruption

Corrupted…

Corrupted JPEGs

Feel free to go and peruse & clone the repo for the Go version over at GitHub.