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 />

446<br />

Figure 10-31. Tint sketch (using bitwise operations)<br />

Again, let me stress the fact that you do not need to use bitwise operations in <strong>Processing</strong>.<br />

The <strong>Processing</strong> functions red(c), green(c), blue(c), <strong>and</strong> alpha(c) also return the individual<br />

color component values. However, once you get past the strange bitwise syntax<br />

(i.e., >> 16 & 0xFF), you’ll realize they are relatively easy to use, <strong>and</strong> again, fast.<br />

The last sketch utilized two PImages; the first was created with the line<br />

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

which is the syntax I looked at earlier to create a PImage based on a loaded image. The second<br />

PImage was created with the following line:<br />

PImage img2 = createImage(img.width, img.height, RGB);<br />

This is the syntax to use when you want to create a PImage, but initially only specify its size<br />

<strong>and</strong> color format. Notice that I specified the size of the second PImage using the width <strong>and</strong><br />

height properties of the first PImage, ensuring that both images were the same size.<br />

As you may remember, every PImage includes a pixels[] array that contains all the color<br />

values in the image. Once I created the second PImage, I copied the pixels[] array from<br />

the first PImage into the second using <strong>Processing</strong>’s arrayCopy(src, dest) function.<br />

This is a pretty h<strong>and</strong>y function that you need to use to copy arrays. You can’t simply copy<br />

an array using syntax like array1 = array2, which only works for primitive types, such as<br />

int1 = int2. Arrays <strong>and</strong> objects are reference types. You may remember from earlier in

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

Saved successfully!

Ooh no, something went wrong!