Skip to content

Commit d9ba918

Browse files
committed
bug fixes and cleanup
1 parent e653cf8 commit d9ba918

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

content/examples/Topics/Image Processing/PixelArray/PixelArray.pde

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,44 @@
55
* press and hold any key to see the current pixel being read.
66
* This program sequentially reads the color of every pixel of an image
77
* and displays this color to fill the window.
8+
*
9+
* Updated 28 February 2010.
810
*/
911

10-
PImage a;
11-
int[] aPixels;
12+
PImage img;
1213
int direction = 1;
13-
boolean onetime = true;
1414
float signal;
1515

16-
void setup()
17-
{
16+
void setup() {
1817
size(200, 200);
19-
aPixels = new int[width*height];
2018
noFill();
2119
stroke(255);
2220
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");
2722
}
2823

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) {
3226
direction = direction * -1;
3327
}
3428

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;
3933
} else {
40-
signal += (0.33*direction);
34+
signal += 0.33*direction;
4135
}
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);
5144
} 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);
5747
}
5848
}
59-
60-
61-
62-
63-

0 commit comments

Comments
 (0)