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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

the book that a primitive type is directly assigned a specific value (e.g., int i = 1;), but a<br />

reference type is internally assigned an address in the computer’s memory where the<br />

object is stored. Thus, the syntax array1 = array2 makes the two array variables point to<br />

the same place in the computer’s memory (where the array is stored)—generally not what<br />

you want to do. This is a fairly advanced topic, but something you do need to be aware of.<br />

I cover variables in detail in Chapter 3.<br />

There are a lot of other interesting things you can do using bitwise operations. Here’s a<br />

sketch, shown in Figure 10-32, that changes the contrast of an image (I used a 400 ✕ 400<br />

pixel image for the example).<br />

/* Contrast (using bitwise operations)<br />

created January 18, 2006<br />

updated December 4, 2006 */<br />

size(800, 400);<br />

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

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

arraycopy(img.pixels, img2.pixels);<br />

float threshold = 127.5;<br />

float contrast = .5; // pos or neg values<br />

loadPixels();<br />

for(int i=0; i> 16 & 0xFF;<br />

int g = img2.pixels[i] >> 8 & 0xFF;<br />

int b = img2.pixels[i] & 0xFF;<br />

int a = img2.pixels[i] >> 24 & 0xFF;<br />

// red<br />

r = int(r*(1.0 + (contrast)*((r-threshold)*.01)));<br />

r = constrain(r, 0, 255);<br />

// green<br />

g = int(g*(1.0 + (contrast)*((g-threshold)*.01)));<br />

g = constrain(g, 0, 255);<br />

// blue<br />

b = int(b*(1.0 + (contrast)*((b-threshold))*.01));<br />

b = constrain(b, 0, 255);<br />

// combine components back into a 32-bit integer<br />

img2.pixels[i] = (a

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

Saved successfully!

Ooh no, something went wrong!