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!

Feedback Loops in Pure Data

Recently I’ve been making a few video loops for Dreambait Recordings to use in their shows. The videos, made using video samples and Pure Data, focus on feedback loops. For BYOB Birmingham on Friday 16th March I decided to showcase these video feedbcak creations. Some photos of it in action:

BYOB Birmingham

BYOB Birmingham Flatpack Festival 2012

Photo by minuek

The Pure Data patch used to make these visuals, inspired by this patch is pretty simple: Put an object on screen, take a snapshot of the screen and then apply that snapshot as a texture to another object. You can download it below

Feedback Loops patch

Click to download

As a texture for the cube I used the Skin Cells video again. You could replace this with any video, image or webcam feed. The [pix_contrast] object is there purely to provide an over-saturated look (try bringing Saturation to a negative number). For BYOB I automated the controls by using random number generators (feeding [random] into [metro]). Here’s a render of what the audience saw:

All that is needed now is some cool audio to go with it! Thanks to all those that came to BYOB to see this and other awesome artworks!

Alpha Glitch

For my performance with Freecode as part of Network Music Festival I wanted to move away from producing visuals that consisted mostly of video playback and move towards generative art. Demos of this were posted on my Flickr site, and the first performance that utilised this new approach happened on 26th January

The feedback from people online and at the performance was really positive, with a lot of people were asking how to do something similar. The patch I made for it was very messy so I (albeit slowly) remade part of the patch that achieves that effect. It’s available for download below

Alpha Glitch

Click to Download

This isn’t strictly a generative patch as it still relies on an source image/video as a texture, but I think it’s more generative than it is video playback. The patch, made in Pure Data, works first by using [repeat] to generate many cubes which are zooming towards the screen. These, then, are textured with an image of your choice. The “magic” comes in the use of [pix_alpha]. The red, green and blue sliders remove a percentage of that colour from the image texturing the cubes, revealing the cube below. The green toggle button randomly removes a different percentage of each colour at different speeds. This, coupled with the constant movement of the cubes I think creates a sort of animated glitch using only a still image.

Sound confusing? Hopefully it’ll become clearer once you dissect the patch and view the help patches of each object. Here’s an example of the output from this patch using this image from my Skin Cells video:

If you know Pure Data well you can modify the patch so that it uses videos or a webcam feed instead of a still image. However, be aware that having that many objects on screen with a video stream can cause the output to be stuttery. This patch was made with Pure Data Extended 0.43 on Ubuntu 11.10.

Making Skin Cells

The making of Skin Cells was quite a long process. It started projecting my Bunnies video onto me and filming this. I then took this and ran it through the What Glitch? sgi script to create a glitched version of the video, leaving me with two versions of the video.

Skin Cells

Skin Cells

When it came to merging the two videos together I took some inspiration from Tidepool by Tabor Robak. Putting the videos on top of each other I wanted to use chromakeying to reveal parts of the video at the bottom at the same time as really oversaturating the video. For this I employed the help of Pure Data:

Skin Cells Pure Data patch

By using [pix_chroma_key] and setting the [range( to random values the patch was constantly hiding and revealing random parts of the videos. Some wizardry in Gridflow gave the videos that oversaturated look.

If you want to try this patch for yourself go ahead and download it. Although it may work on other setups, I used the following:

To use the patch, first load a directory of videos, create the GEM window and then press the big red start button. A video is automatically saved (using PDP), though do be careful as these files get very large very quickly! If, for any reason, saving the video doesn't work just delete the line going from [#from_pix, colorspace rgb] to [#to_pdp].

If any assistance is required please direct your attention to this thread on the Pure Data forum.

LÖVE Glitches

Whilst I was in Venice for the Laptop Meets Musicians festival with BiLE I had the pleasure of (finally) meeting {rukano} who later showed me this really awesome way of displaying uncleared video memory with LOVE and LICK. I’m using Ubuntu 11.04 with LÖVE version love_0.7.2-0natty2_i386.deb.

LÖVE glitches

Once you have downloaded and installed LÖVE and LICK (instructions for different platforms are provided on their websites) create the following files:

main.lua

require "LICK"
require "LICK/lib"
lick.reset = true
lick.clearFlag = true

function love.load()
  fb = love.graphics.newFramebuffer(800,600)
end

function love.draw()
  love.graphics.draw(fb, 0, 0)
end

function love.keypressed (a)
  print(a)
  if a == " " then
     fb = love.graphics.newFramebuffer(800,600)
  end
end

conf.lua

function love.conf(t)
   t.modules.joystick = true   -- Enable the joystick module (boolean)
   t.modules.audio = true      -- Enable the audio module (boolean)
   t.modules.keyboard = true   -- Enable the keyboard module (boolean)
   t.modules.event = true      -- Enable the event module (boolean)
   t.modules.image = true      -- Enable the image module (boolean)
   t.modules.graphics = true   -- Enable the graphics module (boolean)
   t.modules.timer = true      -- Enable the timer module (boolean)
   t.modules.mouse = true      -- Enable the mouse module (boolean)
   t.modules.sound = true      -- Enable the sound module (boolean)
   t.modules.physics = true    -- Enable the physics module (boolean)
   t.console = false           -- Attach a console (boolean, Windows only)
   t.title = "live_testproject"        -- The title of the window the game is in (string)
   t.author = "Your Name Here"        -- The author of the game (string)
   t.screen.fullscreen = false -- Enable fullscreen (boolean)
   t.screen.vsync = true       -- Enable vertical sync (boolean)
   t.screen.fsaa = 0           -- The number of FSAA-buffers (number)
   t.screen.height = 600       -- The window height (number)
   t.screen.width = 800        -- The window width (number)
   t.version = 0               -- The LÖVE version this game was made for (number)
end

Compile all of this code into something like Glitch.love. Instructions for this may be different for different operating systems. Before launching the program be sure to first open lots of videos and images. Once you’ve done that, launch the Glitch.love program and press spacebar to cycle through your uncleared video memory!

Shoutouts go to Tilmann Hars, who first showed this trick to rukano and who maintains the LICK library.

p.s. I’m still trying to find out how to do this kind of stuff using Pure Data. If anyone knows how please let me know!