04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

732<br />

Pixels<br />

The Pixels section includes functions that operate at the pixel level. In addition to functions<br />

that provide access to the entire pixel array of the display, there are two simple yet<br />

powerful functions for blending <strong>and</strong> filtering image data that provide programmatically<br />

controlled Photoshop-like capabilities. If you don’t want to use these cool included functions<br />

for doing things like blurs, posterization, image masking, <strong>and</strong> compositing, you can<br />

easily spin your own, as well; just grab the image pixels using the loadPixels() function,<br />

do some bit shifting, <strong>and</strong> then just call updatePixels() to rewrite the pixel data to the<br />

screen. In addition to using the get() function or the PImage get() method to access the<br />

color of a pixel, it is also possible (<strong>and</strong> faster) to access the pixels[] array directly. Similar<br />

to the two get() comm<strong>and</strong>s just discussed, there are two pixels[] arrays in <strong>Processing</strong>—<br />

one represents the contents of the display window <strong>and</strong> the other is a property within the<br />

PImage class. Similar to the get() method, this pixels[] array property requires a PImage<br />

object to be accessed (e.g., myImage.pixels), <strong>and</strong> the array data only relates to the PImage<br />

object, independent of the display window. Here’s an example that uses both get() comm<strong>and</strong>s<br />

<strong>and</strong> pixels[] arrays. This example also uses the portrait.jpg image, which should<br />

be in the sketch’s data directory.<br />

size(400, 400);<br />

//create a Pimage <strong>and</strong> load an external image<br />

PImage img = loadImage("portrait.jpg");<br />

//PImage method get() works even without drawing image to the screen<br />

println("PImage get() method = " + img.get(50, 50));<br />

//PImage pixels[] array may also be accessed without drawing ➥<br />

the image to the screen<br />

println("PImage pixel array = " + img.pixels[50*width+50]);<br />

//draw the image to the screen<br />

image(img, 0, 0);<br />

// display window get() function works without calling loadPixels();<br />

println("get() function = " + get(50, 50));<br />

loadPixels();<br />

// loadPixels() must be called prior to accessing display window ➥<br />

pixels[] array directly<br />

println("pixel array = " + pixels[50*width+50]);<br />

It is confusing that these two arrays have the same name but function differently. As I<br />

mentioned, you can access the pixel values of a PImage object directly, even prior to drawing<br />

the image to the screen, using object-oriented dot syntax, as in myImage.pixels.<br />

However, to access the pixel values of the display window, you must first call<br />

loadPixels(), as I did in the preceding example. With regard to the two get() comm<strong>and</strong>s,<br />

neither comm<strong>and</strong> requires loadPixels() to be called prior to being used. Once<br />

loadImage() has been called, the PImage get() method can access pixel values within the<br />

image. However, to access the image pixel value using the display window get() function,<br />

you need to draw the image to the screen using the image() comm<strong>and</strong>.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!