|
5 | 5 | * press and hold any key to see the current pixel being read. |
6 | 6 | * This program sequentially reads the color of every pixel of an image |
7 | 7 | * and displays this color to fill the window. |
| 8 | + * |
| 9 | + * Updated 28 February 2010. |
8 | 10 | */ |
9 | 11 |
|
10 | | -PImage a; |
11 | | -int[] aPixels; |
| 12 | +PImage img; |
12 | 13 | int direction = 1; |
13 | | -boolean onetime = true; |
14 | 14 | float signal; |
15 | 15 |
|
16 | | -void setup() |
17 | | -{ |
| 16 | +void setup() { |
18 | 17 | size(200, 200); |
19 | | - aPixels = new int[width*height]; |
20 | 18 | noFill(); |
21 | 19 | stroke(255); |
22 | 20 | frameRate(30); |
23 | | - a = loadImage("ystone08.jpg"); |
24 | | - for(int i=0; i<width*height; i++) { |
25 | | - aPixels[i] = a.pixels[i]; |
26 | | - } |
| 21 | + img = loadImage("ystone08.jpg"); |
27 | 22 | } |
28 | 23 |
|
29 | | -void draw() |
30 | | -{ |
31 | | - if (signal > width*height-1 || signal < 0) { |
| 24 | +void draw() { |
| 25 | + if (signal > img.width*img.height-1 || signal < 0) { |
32 | 26 | direction = direction * -1; |
33 | 27 | } |
34 | 28 |
|
35 | | - if(mousePressed) { |
36 | | - if(mouseY > height-1) { mouseY = height-1; } |
37 | | - if(mouseY < 0) { mouseY = 0; } |
38 | | - signal = mouseY*width+mouseX; |
| 29 | + if (mousePressed) { |
| 30 | + int mx = constrain(mouseX, 0, img.width-1); |
| 31 | + int my = constrain(mouseY, 0, img.height-1); |
| 32 | + signal = my*img.width + mx; |
39 | 33 | } else { |
40 | | - signal += (0.33*direction); |
| 34 | + signal += 0.33*direction; |
41 | 35 | } |
42 | | - |
43 | | - if(keyPressed) { |
44 | | - loadPixels(); |
45 | | - for (int i=0; i<width*height; i++) { |
46 | | - pixels[i] = aPixels[i]; |
47 | | - } |
48 | | - updatePixels(); |
49 | | - rect(signal%width-5, int(signal/width)-5, 10, 10); |
50 | | - point(signal%width, int(signal/width)); |
| 36 | + |
| 37 | + int sx = int(signal) % img.width; |
| 38 | + int sy = int(signal) / img.width; |
| 39 | + |
| 40 | + if (keyPressed) { |
| 41 | + set(0, 0, img); // fast way to draw an image |
| 42 | + point(sx, sy); |
| 43 | + rect(sx - 5, sy - 5, 10, 10); |
51 | 44 | } else { |
52 | | - loadPixels(); |
53 | | - for (int i=0; i<width*height; i++) { |
54 | | - pixels[i] = aPixels[int(signal)]; |
55 | | - } |
56 | | - updatePixels(); |
| 45 | + color c = img.get(sx, sy); |
| 46 | + background(c); |
57 | 47 | } |
58 | 48 | } |
59 | | - |
60 | | - |
61 | | - |
62 | | - |
63 | | - |
0 commit comments