Examples mods for 2.0

This commit is contained in:
Casey Reas
2011-09-16 05:00:28 +00:00
parent c3b8b312a0
commit 472fd13b2f
20 changed files with 210 additions and 241 deletions
@@ -2,41 +2,45 @@
* Convolution
* by Daniel Shiffman.
*
* Applies a convolution matrix to a portion of the index.
* Applies a convolution matrix to a portion of an image.
* Move mouse to apply filter to different parts of the image.
*/
// @pjs preload must be used to preload media if the program is
// running with Processing.js
/* @pjs preload="moon-wide.jpg"; */
PImage img;
int w = 80;
int w = 120;
// It's possible to convolve the image with
// many different matrices
float[][] matrix = { { -1, -1, -1 },
{ -1, 9, -1 },
{ -1, -1, -1 } };
// many different matrices to produce different effects.
// This is a high-pass filter; it accentuates the edges.
float[][] matrix = { { -1, -1, -1 },
{ -1, 9, -1 },
{ -1, -1, -1 } };
void setup() {
size(200, 200);
frameRate(30);
img = loadImage("end.jpg");
size(640, 360);
img = loadImage("moon-wide.jpg");
}
void draw() {
// We're only going to process a portion of the image
// so let's set the whole image as the background first
image(img,0,0);
image(img, 0, 0);
// Where is the small rectangle we will process
int xstart = constrain(mouseX-w/2,0,img.width);
int ystart = constrain(mouseY-w/2,0,img.height);
int xend = constrain(mouseX+w/2,0,img.width);
int yend = constrain(mouseY+w/2,0,img.height);
int xstart = constrain(mouseX - w/2, 0, img.width);
int ystart = constrain(mouseY - w/2, 0, img.height);
int xend = constrain(mouseX + w/2, 0, img.width);
int yend = constrain(mouseY + w/2, 0, img.height);
int matrixsize = 3;
loadPixels();
// Begin our loop for every pixel
for (int x = xstart; x < xend; x++) {
for (int y = ystart; y < yend; y++ ) {
color c = convolution(x,y,matrix,matrixsize,img);
color c = convolution(x, y, matrix, matrixsize, img);
int loc = x + y*img.width;
pixels[loc] = c;
}
@@ -44,7 +48,7 @@ void draw() {
updatePixels();
}
color convolution(int x, int y, float[][] matrix,int matrixsize, PImage img)
color convolution(int x, int y, float[][] matrix, int matrixsize, PImage img)
{
float rtotal = 0.0;
float gtotal = 0.0;
@@ -65,10 +69,10 @@ color convolution(int x, int y, float[][] matrix,int matrixsize, PImage img)
}
}
// Make sure RGB is within range
rtotal = constrain(rtotal,0,255);
gtotal = constrain(gtotal,0,255);
btotal = constrain(btotal,0,255);
rtotal = constrain(rtotal, 0, 255);
gtotal = constrain(gtotal, 0, 255);
btotal = constrain(btotal, 0, 255);
// Return the resulting color
return color(rtotal,gtotal,btotal);
return color(rtotal, gtotal, btotal);
}