/*
* LinearPixels by Tom Gerhardt:
* turns image to greyscale
* upps the brightness of pixels whose vlaues are above the average of all pixels
* creates 'linear' pixes by averaging 24 pixels together
*/

import processing.core.*;
import processing.video.*;

public class LinearPixels extends PApplet {
Capture video;
int whiteOutPixels = 0;
int darkInc = 20;
int width = 640;
int height = 480;
float avg = 0;
float lastAvg = 128;

public void setup() {
size(width, height);
video = new Capture(this, width, height);
}

public void draw() {
if (video.available()) {
video.read();
video.loadPixels();
//renders pixels to b/w and upps the white on half
for (int i = 0; i < width*height; i++){
int p = video.pixels[i];
float r = red(p);
float g = green(p);
float b = blue(p);
float bw = (r+g+b)/3;
avg += bw;
if(bw > lastAvg){
bw += (bw-lastAvg)/(255-lastAvg)*(700-lastAvg);
}
if(bw > 255){
bw = 255;
}
video.pixels[i] = color(bw,bw,bw);
}
lastAvg = avg/(width*height);
avg = 0;
//merges half the pixels
int avgNum = 24;
for (int i = 0; i < width*height; i+=avgNum){
float avgBar = 0;
for(int r = 0; r < avgNum; r++){
int colorR = video.pixels[i+r];
float bwR = red(colorR);
avgBar += bwR;
}
float mergeColor = (avgBar)/avgNum;
for(int r = 0; r < avgNum; r++){
video.pixels[i+r] = color(mergeColor,mergeColor,mergeColor);
}
}
video.updatePixels();
}
image(video, 0, 0);
}

 

public void mousePressed() {
if (key == 's') {
video.settings();
}
}

public static void main(String args[]) {
PApplet.main(new String[] { "--present", "LinearPixels" });
}
}