Create jpgs in Pure Data

For Some of My Favourite Songs I utilised Pure Data Extended (I’m using a beta version) to read the audio files and then save them as images. Pure Data is usually used for the production of music and/or generative live visuals, so to using it to produce jpg images from almost nothing, or random data input is quite new to me!

In search of a jpg header

The most important part of this process is knowing how to construct and apply a jpg header to data. Wikipedia informed me that all jpg images begin with FF D8. I thought that all I would need to do is use a hex editor, such as Ghex or Bless Hex Editor, to add those byte values to a file.

Unfortunately this is not the case at all. There’s so much more in a jpg header, such as Huffman Tables, Quantization Tables, bytes to define the width and height of an image, and much more that I still don’t quite understand.

I attempted to grab data from the beginning of a random jpg file, but this included lots of extraneous data such as camera make, program(s) used to modify the photo, gps data and creation date. This data amounted to several kilobytes, which is far too much data for a header. What I needed was a “vanilla” or plain header that I could apply to any file.

mesmeon showed me the HEADer REMIX project by Ted Davis. The header values on the left of the screen are used for glitching every image, be it the default image or one taken by a user.

I saved the default image, manually extracted the header image, ran it through exiftool and then ended up with a header for a 640×480 image that is only 588 bytes!

Enter Pure Data

Now that I had a vanilla header I had to devise a way to use it in Pure Data. The [binfile] object allows the reading and writing of binary data. Adding data to [binfile] is a case of sending a message containing numbers to the object.

[binfile] reads and outputs data as decimal values i.e. numbers from 0 to 255. I needed to find a way to add the decimal values of the vanilla header to a message box. Martin Meredith helped me with this whilst we were tackling bugs at the Ubuntu Global Jam. Using hexdump I was able to output all of the hex values to decimal values.

hexdump -v -e '1/1 "%02u "' filename.here > decimalvalues.txt
255 216 255 219 00 132 00 03 02 02 03 02 02 03 03 03 03 04 03 03 04 05 08 05 05 04 04 05 10 07 07 06 08 12 10 12 12 11 10 11 11 13 14 18 16 13 14 17 14 11 11 16 22 16 17 19 20 21 21 21 12 15 23 24 22 20 24 18 20 21 20 01 03 04 04 05 04 05 09 05 05 09 20 13 11 13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 255 192 00 17 08 01 224 02 128 03 01 17 00 02 17 01 03 17 01 255 196 01 162 00 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 10 11 16 00 02 01 03 03 02 04 03 05 05 04 04 00 00 01 125 01 02 03 00 04 17 05 18 33 49 65 06 19 81 97 07 34 113 20 50 129 145 161 08 35 66 177 193 21 82 209 240 36 51 98 114 130 09 10 22 23 24 25 26 37 38 39 40 41 42 52 53 54 55 56 57 58 67 68 69 70 71 72 73 74 83 84 85 86 87 88 89 90 99 100 101 102 103 104 105 106 115 116 117 118 119 120 121 122 131 132 133 134 135 136 137 138 146 147 148 149 150 151 152 153 154 162 163 164 165 166 167 168 169 170 178 179 180 181 182 183 184 185 186 194 195 196 197 198 199 200 201 202 210 211 212 213 214 215 216 217 218 225 226 227 228 229 230 231 232 233 234 241 242 243 244 245 246 247 248 249 250 01 00 03 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 10 11 17 00 02 01 02 04 04 03 04 07 05 04 04 00 01 02 119 00 01 02 03 17 04 05 33 49 06 18 65 81 07 97 113 19 34 50 129 08 20 66 145 161 177 193 09 35 51 82 240 21 98 114 209 10 22 36 52 225 37 241 23 24 25 26 38 39 40 41 42 53 54 55 56 57 58 67 68 69 70 71 72 73 74 83 84 85 86 87 88 89 90 99 100 101 102 103 104 105 106 115 116 117 118 119 120 121 122 130 131 132 133 134 135 136 137 138 146 147 148 149 150 151 152 153 154 162 163 164 165 166 167 168 169 170 178 179 180 181 182 183 184 185 186 194 195 196 197 198 199 200 201 202 210 211 212 213 214 215 216 217 218 226 227 228 229 230 231 232 233 234 242 243 244 245 246 247 248 249 250 255 218 00 12 03 01 00 02 17 03 17 00 63

(The output is sent to a text file for ease of copy/pasting)

With this output I copy/pasted the values into a message box, and whenever I needed to add a jpg header to a file I clicked on the message box! To then write the file I sent the message [write filename.jpg( to the [binfile] object.

A jpg header in Pure Data

Using this data alone you may notice that the jpg image doesn’t open in certain image viewers or is blank/black. That is because all that was added is the header. Image data is also needed! For this I added a few [metro]s to generate random numbers between 0-255. The output image then looks a little bit more colourful now.

jpg created by Pure Data

For some websites and image viewers the End Of Image bytes (FF D9/255 217) need to be added in order for it to be viewed properly. To start this process again send [clear( to [binfile]. This clears all binary data. Below is all of this theory put into one patch.

The finished Pure Data patch

Pure Data [binfile]

Generate jpg images – click to download

To use it, first click on the button to start the jpg file, then click the toggle button to add lots of random data. This may take a minute or so. Once done turn off the toggle, click on the button to end the file and then write the jpg image.

Further options

If you know the structure of a certain file type, in theory, it is possible to construct one in a similar way to this. I’ve already used this method to construct a bmp, but they produce far less interesting results. png files seem to be more fragile and, as such, I haven’t managed to create one using this method.

If you use a second [binfile] object you can load the bytes from another file and use them, in conjunction with random data, to produce glitchy – but slightly recognisable – images!

Is it also possible to reduce the size of the jpg header even further?

Create jpgs in SuperCollider

Holger Ballweg (uiae) has recreated this progress in SuperCollider. Check it out!