Glitchy Cookies: Working with Images and Quicksort in Processing
I recently was working on a Processing project where I had to incorporate media into the my sketch. The incorporation of the media itself isn't very difficult; Processing is well-adapted to use external images in its sketches, but I wanted to do something interesting with the image once I put it into Processing. I really like the idea of glitching images, (a really cool example of that is here), but I hadn't ever worked with intentionally glitching images, and I definitely hadn't done anything like it before in Processing. The projects I had made were random glitches, but they didn't have any sort of retraceable algorithm that caused how the pixels were altered. I wanted to find a way where I could take an image, break it down into its individual pixels, and then do controlled changes of these pixels to glitch the image.
Well, it turns Processing has a great way to extract pixels from an image and store them in an array via the command loadPixels(),which puts each pixel into an array, and updatePixels(), which updates updates the new array of pixels once (or if) it has been altered. I've manipulated arrays before in my experience in programming Python and C++, and I figured I would be able to manipulate these arrays in Processing using similar techniques, and then use these updatePixels() command to make the new images.
Manipulating big arrays without loops and recursion is a pain, so I decided to implement a recursive technique to sort the pixels into different configurations. The technique I used is a classic Data Structures sorting algorithm called Quicksort. I had already written some Processing code that took the image, threw the pixels into an array, and then would modify the array and update the image based on where the user clicks the mouse, but I needed some way to modify the data all at once. I did some digging around other Processing code to look for an implementation, and then I threw it into my code as the driving force behind the image glitching function.
Now, what my code does is:
- Upload the image and store each pixel in an array
- Resort the pixels in the array based on the X and Y locations of the mousePressed
- Update the array, and then draw the image based on the new pixel configurations.
Results are below. The source code will be up ASAP once OpenProcessing starts behaving. I got the source code for the Quicksort from here.
Noooo Cookie Monster what's happening!