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.

In the last example, I created an array of anonymous PImages using the get() comm<strong>and</strong>.<br />

This syntax may look odd, as I’m relying on the returned values of the get() calls (which<br />

are PImages) to populate the arrays. You’ll remember that arrays can be instantiated a few<br />

different ways; one such way is to declare <strong>and</strong> populate the array at the same time, with<br />

the values specified between curly braces, as illustrated in the example.<br />

A more ambitious but interesting thing to do with get() could be to write your own<br />

image-encrypting sketch. The simple encryption procedure will collect the image as<br />

sequential fragments <strong>and</strong> then re-create the image, reordering the image. I’ll keep it really<br />

simple <strong>and</strong> just reverse the order of the fragments. You need to use an image of 600<br />

(width) ✕ 400 pixels to run the example (see Figure 10-27):<br />

//Simple Image Encryption<br />

int ENCRYPTION_LOW = 0;<br />

int ENCRYPTION_MEDIUM = 1;<br />

int ENCRYPTION_HIGH = 2;<br />

int ENCRYPTION_VERY_HIGH = 3;<br />

void setup(){<br />

size(600, 400);<br />

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

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

// call the fragment function<br />

fragment(ENCRYPTION_HIGH);<br />

}<br />

void fragment(int encryptionLevel){<br />

int fragW = 0;<br />

int fragH = 0;<br />

int frags = 0;<br />

// set size of blocks<br />

switch(encryptionLevel){<br />

case 0:<br />

fragW = width/2;<br />

fragH = height/2;<br />

frags = 4;<br />

break;<br />

case 1:<br />

fragW = width/4;<br />

fragH = height/4;<br />

frags = 16;<br />

break;<br />

case 2:<br />

fragW = width/8;<br />

fragH = height/8;<br />

frags = 64;<br />

break;<br />

case 3:<br />

COLOR AND IMAGING<br />

437<br />

10

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

Saved successfully!

Ooh no, something went wrong!