Java I
Jonah Warrenjonah@parsons.edu
http://a.parsons.edu/~java2004
Lab 8
Code from class... ALL USE "utah.jpg", a 200x200 JPG that can be found here.
USING PIXELS[] TO REDRAW AN IMAGE
BImage a;
void setup() {
size(200,200);
a = loadImage("utah.jpg");
}
void loop() {
for(int x=0;x<width;x++) {
for(int y=0;y<height;y++) {
float rVal = red(a.pixels[x+y*a.width]);
float gVal = green(a.pixels[x+y*a.width]);
float bVal = blue(a.pixels[x+y*a.width]);
int loc = x+y*width;
pixels[loc]=color(rVal,gVal,bVal);
}
}
}
FADE TO BLACK USING MOUSEX
BImage a;
void setup() {
size(200,200);
a = loadImage("utah.jpg");
}
void loop() {
for(int x=0;x<width;x++) {
for(int y=0;y<height;y++) {
float rVal = red(a.pixels[x+y*a.width])*((float)mouseX/width);
float gVal = green(a.pixels[x+y*a.width])*((float)mouseX/width);
float bVal = blue(a.pixels[x+y*a.width])*((float)mouseX/width);
int loc = x+y*width;
pixels[loc]=color(rVal,gVal,bVal);
}
}
}
USING RANDOM POINTS IN THE IMAGE TO DRAW SHAPES
int MAX=5000;
BImage a;
int[] xVals = new int[MAX];
int[] yVals = new int[MAX];
void setup() {
size(200,200);
a = loadImage("utah.jpg");
background(255);
for(int i=0;i<MAX;i++) {
xVals[i] = (int)random(width);
yVals[i] = (int)random(height);
int loc = xVals[i]+yVals[i]*a.width;
loc = constrain(loc,0,width*height);
color col = a.pixels[loc];
noStroke();
fill(col);
ellipse(xVals[i],yVals[i],5,5);
}
}
void loop() {
}
USING RANDOM POINTS IN THE IMAGE TO DRAW SHAPES... CONTINUALLY TO ANIMATE
int MAX=5000;
BImage a;
int[] xVals = new int[MAX];
int[] yVals = new int[MAX];
void setup() {
size(200,200);
a = loadImage("utah.jpg");
}
void loop() {
background(255);
for(int i=0; i<MAX;i++) {
xVals[i] = (int)random(width);
yVals[i] = (int)random(height);
}
for(int i=0;i<MAX;i++) {
int loc = xVals[i]+yVals[i]*a.width;
loc = constrain(loc,0,width*height);
color col = a.pixels[loc];
noStroke();
fill(col);
ellipse(xVals[i],yVals[i],5,5);
}
}
A MATRIX BLUR
BImage a;
void setup() {
size(200,200);
a = loadImage("utah.jpg");
background(0);
for(int x=0;x<width;x++) {
for(int y=0;y<height;y++) {
pixels[x+y*width]=blurPixels(x,y);
}
}
}
void loop() {
}
color blurPixels(int x, int y) {
float sumR = 0.0;
float sumG = 0.0;
float sumB = 0.0;
float[] Matrix= { 0.11,0.11,0.11,
0.11, 0.11,0.11,
0.11,0.11,0.11 };
int countMatrix = 0;
for (int i =-1; i<=1; i++){
for (int j=-1; j<=1; j++){
//what pixel are we testing
int xloc = x+i;
int yloc = y+j;
int loc = xloc + a.width*(yloc+j);
//make sure we haven't walked off our image
loc = constrain(loc,0,a.pixels.length-1);
//calculate the convolution
sumB += blue(a.pixels[loc])*Matrix[countMatrix];
sumG += green(a.pixels[loc])*Matrix[countMatrix];
sumR += red(a.pixels[loc])*Matrix[countMatrix];
countMatrix++;
}
}
//return the resulting color
return color(sumR,sumG,sumB);
}
SCRIBBLE AN IMAGE
float xPos;
float yPos;
float prevX;
float prevY;
BImage a;
void setup() {
size(200,200);
a = loadImage("utah.jpg");
xPos = 100;
yPos = 100;
prevX = 100;
prevY = 100;
background(255);
}
void loop() {
xPos += random(-8, 8);
yPos += random(-8, 8);
xPos = constrain(xPos, 0, width);
yPos = constrain(yPos, 0, height);
color col = a.pixels[constrain((int)xPos+(int)yPos*width,0,(width*height)-1)];
stroke(col);
line(xPos,yPos, prevX, prevY);
prevX = xPos;
prevY = yPos;
}
Homework 8
(Please email me if you have any questions or problems)- Using one picture, create 3 different custom image processing techniques to alter your image. Substitute pixels for shapes, lines, or drawings.
- Use another picture and make an image processing composition that is interactive or animating. You can make a composition that draws itself, you can make it a reactive surface (rollover a pixel and something happens), you can have it simply respond to the mouse position or you can use any other animation / interactive technique you can think of.
- Here's a cool one...