import SGGUI.*;
import java.awt.geom.*;
 
SGGUI guiControl;
VerticalLayout parameterBox;
VerticalLayout lastIm,newIm;
 
SGSlider apRot;
SGSlider numBlades;
SGSlider angle;
SGSlider falloff;
SGButton render;
SGCheckBox donut;
 
boolean masking = false;
 
float flutter = 0;
int apertureDiameter = 200;
PImage img;
PImage foreGround;
 
PImage composite, lastComposite;
 
PGraphics previous;
 
PImage cover;
PImage coverA;
int kernelRes = 21;
 
SGComposite last, current;
 
float lastAp;
float fstop;
float currAp;
 
int lastBlades;
int currBlades;
PFont font;
 
float kernelSum;
 
float [][]kernel = new float[kernelRes][kernelRes];
 
void setup() {
size(550, 720,P3D);
frameRate(30);
 
img = loadImage("a.jpg");
 
 
foreGround = loadImage("fore.png");
cover = loadImage("cover.jpg");
coverA = loadImage("coverAlpha.jpg");
composite = new PImage(img.width,img.height);
lastComposite = new PImage(img.width,img.height);
composite = loadImage("a.jpg");
textMode(SCREEN);
textureMode(NORMALIZED);
font = loadFont("writing.vlw");
textFont(font);
guiControl = new SGGUI(this, font);
cover.mask(coverA);
 
parameterBox = new VerticalLayout(guiControl,30,30,150);
parameterBox.title = "Parameters";
lastIm = new VerticalLayout(guiControl,20,400,img.width);
newIm = new VerticalLayout(guiControl,330,400,img.width);
numBlades = new SGSlider( "Num Blades",6,1,1,10);
apRot = new SGSlider( "Rotation",0,.1,0,6.28);
angle = new SGSlider( "Size",1.4,.03,.2,1.5);
falloff = new SGSlider( "Falloff",0,1,-25,25);
donut = new SGCheckBox("Reflex",0);
render = new SGButton("Render");
 
render.addGUIListener(this);
parameterBox.add(numBlades);
parameterBox.add(apRot);
parameterBox.add(angle);
parameterBox.add(falloff);
parameterBox.add(donut);
parameterBox.add(render);
 
current = new SGComposite(composite, foreGround);
newIm.add(current);
newIm.title = "f/0.0";
last = new SGComposite(lastComposite, foreGround);
lastIm.add(last);
lastIm.title = "";
 
guiControl.add(lastIm);
guiControl.add(newIm);
guiControl.add(parameterBox);
 
 
mouseReleased();
 
}
 
void draw() {
 
 
background(135,125,107);
 
pushMatrix();
translate(281,250,-20);
 
 
strokeWeight(2);
stroke(0);
noStroke();
float sca = apertureDiameter/10;
if (falloff.getVal() >= 0){
fill(255);
ellipse(0,0,apertureDiameter,apertureDiameter);
fill(0,0,0,falloff.getVal()*2);
for (int i=0; i < 10; i++){
ellipse(0,0,sca*i,sca*i);
}
}
else{
fill(0);
ellipse(0,0,apertureDiameter,apertureDiameter);
fill(255,255,255,(25+falloff.getVal())*2);
for (int i=0; i < 10; i++){
ellipse(0,0,sca*i,sca*i);
}
}
 
stroke(20);
translate(0,-1,0);
float spacing = 2*PI/numBlades.getVal();
fill(0);
noStroke();
stroke(50,51,52);
rotate(apRot.getVal());
if (donut.getVal() == 0){
for (float i=0; i < 2*PI; i+=spacing){
pushMatrix();
rotate(i);
translate(apertureDiameter/2,0);
ellipse(0,0,10,10);
rotate(angle.getVal());
rotateX(.1);
rect(0,0,apertureDiameter/2,apertureDiameter);
popMatrix();
}
}
else{
ellipse(0,0,100*angle.getVal(),100*angle.getVal());
}
stroke(0);
strokeWeight(10);
noFill();
ellipse(0,0,apertureDiameter,apertureDiameter);
popMatrix();
if (!masking){
 
translate(0,0,20);
fill(255);
image(cover,0,0);
}
 
}
 
void mousePressed(){
if (guiControl.getMouseConsumed() == 0){
println("miss");
}
 
 
}
 
 
void mouseDragged(){
if (guiControl.getMouseConsumed() == 0){
 
}
 
}
 
 
void convolve(){
 
masking = true;
draw();
loadPixels();
int wl = 281 - apertureDiameter/2;
int hl = 250 - apertureDiameter/2;
int stepSize = apertureDiameter/(kernelRes);
color temp;
kernelSum = 0;
for (int i=0; i < kernelRes;i++){
for (int j=0; j < kernelRes;j++){
if (red(pixels[(i*stepSize+hl)*width + (j*stepSize+wl)]) == blue(pixels[(i*stepSize+hl)*width + (j*stepSize+wl)])){
kernel[j][i] = red(pixels[(i*stepSize+hl)*width + (j*stepSize+wl)]);
}
else{
kernel[j][i] = 0;
}
kernelSum += kernel[j][i];
 
}
}
masking = false;
 
 
fstop = 1.7*sqrt(PI)/(2*sqrt(kernelSum/(358*255)))/.88622695;
int bob = (int)(fstop*100);
fstop = (float)bob/100;
 
lastAp = currAp;
currAp = fstop;
 
lastBlades = currBlades;
currBlades = (int)numBlades.getVal();
color tmp;
float kval;
float rsum = 0;
float gsum = 0;
float bsum = 0;
for(int k=-kernelRes/2; k<=kernelRes/2; k++) {
for(int j=-kernelRes/2; j<=kernelRes/2; j++) {
int xp = x-j;
int yp = y-k;
if (xp < 0) {
xp = xp + img.width;
} else if (x-j >= img.width) {
xp = xp - img.width;
}
if (yp < 0) {
yp = yp + img.height;
} else if (yp >= img.height) {
yp = yp - img.height;
}
tmp = img.get(xp,yp);
kval = kernel[j+kernelRes/2][k+kernelRes/2];
rsum = rsum + kval * red(tmp);
gsum = gsum + kval * green(tmp);
bsum = bsum + kval * blue(tmp);
}
}
rsum /= kernelSum;
gsum /= kernelSum;
bsum /= kernelSum;
current.icon.pixels[y*img.width + x] = color(int(rsum),int(gsum),int(bsum));
}
}
 
}
 
void GUIEventPerformed(GUIEvent e){
last.icon.pixels = current.icon.pixels;
current.icon = new PImage(img.width,img.height);
convolve();
lastIm.title = "f/" + lastAp;
newIm.title = "f/" + currAp;
 
}