Last active
August 29, 2015 14:27
-
-
Save shawnlau/cc3eae823fe652f486b8 to your computer and use it in GitHub Desktop.
The idea of this Processing program is to be able to paint on an image while displaying it in different ways.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The idea of this progam is to be able to paint on an image while it is displayed in different ways. | |
Eventually, it will have its own filters to manipulate the differnet layers. But right now, you weould have | |
use another program to filter your image in different ways and load them into other layers using the "b" key. | |
When you paint on one layer, you paint on all the layers with colors lifted from each specific layer. | |
There's an enhance mode ( "e" key) that allows you to change the contrast and brightnest of the displayed | |
image, but it doesn't affect the real pixel values. This will stutter the paint function slightly ( wish I could find | |
a quicker check for 0 and 255), but it will be needed to work in the high frequency ( small details) mode. I've edited out | |
the tablet pressure inputs so if you don't have a tablet, just commment out the import of the tabliet library. | |
This runs really bad on Processing 3 beta. Maybe someone can tell me why it runs better on Processing 2 than Processing 3. | |
*/ | |
import codeanticode.tablet.*; | |
import javax.swing.*; | |
Tablet tablet; | |
int numUndo = 5; | |
int curUndo = 0; | |
int numLayers = 4; | |
int undoInstances = 0; | |
int redoInstances = 0; | |
PImage img = null; | |
PGraphics []layers; | |
PGraphics []lfLayers; | |
PGraphics []hfLayers; | |
PGraphics []uLayers; | |
PGraphics temp,temp2; | |
PGraphics imgEnhanced; | |
int curLayer = 0; | |
boolean imageSelected = false; | |
float zoom = 1.0; | |
float cX = 0; //corner placement | |
float cY = 0; | |
float dX = 0.0; //dragged offset | |
float dY = 0.0; | |
float rdX = 0.0; | |
float rdY = 0.0; | |
float zX = 0; | |
float zY = 0; | |
int thickness = 10; | |
int strength = 50; | |
int oX = 0; // mouse offset | |
int oY = 0; | |
int mX; | |
int mY; | |
int medianSize = 5; | |
boolean locked = false; | |
boolean dragging = false; | |
boolean pickColor = false; | |
boolean saveUndo = false; | |
boolean clone = false; | |
boolean setClone = false; | |
boolean offsetSet = false; | |
boolean enhanced = false; | |
int cloneX = 0; | |
int cloneY = 0; | |
int cOffsetX = 0; | |
int cOffsetY = 0; | |
color []penColor; | |
String info; | |
float eContrast = 1.5; | |
float eBrightness = 0; | |
int numberOfPixels; | |
int frequencyMode = 0; | |
void setup() { | |
info = "Layer: " + curLayer + " Brushsize: " + thickness + " Strength: " + strength + " Zoom:" + zoom; | |
frame.setTitle(info); | |
tablet = new Tablet(this); | |
uLayers = new PGraphics[numUndo*numLayers]; | |
layers = new PGraphics[numLayers]; | |
hfLayers = new PGraphics[numLayers]; | |
lfLayers = new PGraphics[numLayers]; | |
penColor= new color[numLayers]; | |
frame.setResizable(true); | |
size(600, 600); | |
noFill(); | |
selectInput("Select a file to process:", "fileSelected"); | |
} | |
void draw() { | |
background(100); | |
if(enhanced){ | |
layers[curLayer].beginDraw(); | |
imgEnhanced.beginDraw(); | |
layers[curLayer].loadPixels(); | |
imgEnhanced.loadPixels(); | |
int r,g,b; | |
color inColor; | |
for(int i = 0; i< numberOfPixels; i++){ | |
inColor = layers[curLayer].pixels[i]; | |
r = (inColor >>16) & 0xFF; | |
g = (inColor >>8) & 0xFF; | |
b = inColor & 0xFF; | |
r = (int)(r * eContrast + eBrightness); | |
g = (int)(g * eContrast + eBrightness); | |
b = (int)(b * eContrast + eBrightness); | |
r = r < 0 ? 0 : r > 255 ? 255 : r; | |
g = g < 0 ? 0 : g > 255 ? 255 : g; | |
b = b < 0 ? 0 : b > 255 ? 255 : b; | |
imgEnhanced.pixels[i] = 0xff000000 | (r << 16) | (g << 8) | b; | |
} | |
// layers[curLayer].updatePixels(); | |
imgEnhanced.updatePixels(); | |
layers[curLayer].endDraw(); | |
imgEnhanced.endDraw(); | |
// layers[curLayer].beginDraw(); | |
// layers[curLayer].endDraw(); | |
// Enhance(); | |
cornerOffset(); //process the location of the corner x,y | |
image(imgEnhanced, cX, cY, img.width * zoom, img.height * zoom); | |
// image(canvas,0,0); | |
} | |
else if (imageSelected) { | |
layers[curLayer].beginDraw(); | |
layers[curLayer].endDraw(); | |
cornerOffset(); | |
image(layers[curLayer], cX, cY, img.width * zoom, img.height * zoom); | |
} | |
ellipse(mouseX,mouseY,thickness*zoom,thickness*zoom); | |
// info = "Layer: " + curLayer + " Brushsize: " + thickness + " Strength: " + strength + " Zoom:" + zoom +" X: " + mouseX + " Y: "+ mouseY | |
// + " dX: " + dX + " dY: " + dY + " rdX: " + rdX + " rdY: " + rdY; | |
// frame.setTitle(info); | |
} | |
void fileSelected(File selection) { | |
if (selection == null) | |
println("Press \" L \"to load an image "); // will be a prompt | |
else { | |
img = loadImage( selection.getAbsolutePath()); | |
imgEnhanced = createGraphics(img.width,img.height); | |
numberOfPixels = img.width * img.height; | |
for(int i = 0; i < numLayers;i++){ | |
layers[i] = createGraphics(img.width,img.height); | |
hfLayers[i]= createGraphics(img.width,img.height); | |
lfLayers[i]= createGraphics(img.width,img.height); | |
layers[i].beginDraw(); | |
hfLayers[i].beginDraw(); | |
lfLayers[i].beginDraw(); | |
layers[i].image(img,0,0); | |
layers[i].endDraw(); | |
hfLayers[i].endDraw(); | |
lfLayers[i].endDraw(); | |
} | |
temp = createGraphics(img.width,img.height); | |
temp.beginDraw(); | |
temp.endDraw(); | |
temp2 = createGraphics(img.width,img.height); | |
temp2.beginDraw(); | |
temp2.endDraw(); | |
for(int i= 0; i< numUndo * numLayers;i++){ | |
uLayers[i] = createGraphics(img.width,img.height); | |
uLayers[i].beginDraw(); | |
uLayers[i].endDraw(); | |
} | |
for(int i = 0; i < numLayers;i++){ | |
layers[i].beginDraw(); | |
layers[i].stroke(0,0,0); | |
layers[i].strokeWeight(thickness); | |
layers[i].endDraw(); | |
} | |
imageSelected = true; | |
int h = img.height; //process the window size | |
if(h > (int)((float) displayHeight * 0.9)) | |
h = (int)((float)displayHeight * 0.9); | |
int w = img.width; | |
if(w > (int)((float)displayWidth * 0.9)) | |
w = (int)((float)displayWidth * 0.9); | |
frame.setSize(w,h); | |
zoom = 1.0; | |
dX = dY = 0; | |
} | |
} | |
void keyPressed() { | |
switch(key){ | |
case '1': layers[curLayer].blendMode(SCREEN); | |
break; | |
case 'e': | |
case 'E': | |
enhanced = !enhanced; | |
break; | |
case 'a': assemble_pic(); | |
break; | |
case 'c': | |
clone = !clone; | |
if(clone){ | |
cursor(HAND); | |
setClone = true; | |
offsetSet = false; | |
} | |
else cursor(ARROW); | |
break; | |
case 'm': | |
medianSize = get_median_size(); | |
if(medianSize%2 == 0) medianSize++; | |
median2(layers[curLayer],medianSize); | |
break; | |
case 'b': | |
case 'B': | |
selectInput("Select B image:", "load_B_image"); | |
break; | |
case 's': | |
case 'S': | |
selectInput("SaveLayer", "save_layer"); | |
break; | |
case 'L': | |
case 'l': | |
selectInput("Select a file to process:", "fileSelected"); | |
break; | |
case 'Z': | |
case '+': | |
zoom_up(); | |
break; | |
case 'z': | |
case '-': | |
zoom_down(); | |
break; | |
case 'X': | |
case 'x': | |
zoom = 1.0; | |
dX =0; | |
dY = 0; | |
zX = 0; | |
zY = 0; | |
break; | |
case ' ': | |
dragging = true; | |
cursor(MOVE); | |
break; | |
case 'u': | |
case 'U': | |
undo(); | |
break; | |
case 'r': | |
case 'R': | |
redo(); | |
break; | |
case ']': if(thickness < 10) thickness+=1; | |
else thickness += (int)(thickness * 0.2); | |
set_thickness(); | |
break; | |
case '[': if(thickness < 10) thickness--; | |
else thickness -= (int)(thickness * 0.2); | |
if(thickness<=0) | |
thickness = 1; | |
set_thickness(); | |
break; | |
case ';': | |
case ':': | |
if(strength <10) strength -= 1; | |
else strength -= (int)(strength *0.1); | |
if(strength < 1) strength = 1; | |
break; | |
case '\'': | |
case '\"': | |
if(strength <10)strength+=1; | |
else strength += (int)(strength * 0.1); | |
if (strength > 255) strength = 255; | |
break; | |
case 'f': | |
case 'F': | |
flip_horizontal(); | |
break; | |
case DELETE: | |
eBrightness -=1; | |
break; | |
case CODED: | |
switch(keyCode){ | |
case UP: | |
if(curLayer < numLayers-1) curLayer++; | |
break; | |
case DOWN: | |
if(curLayer > 0) curLayer--; | |
break; | |
case ALT: | |
if(clone){ | |
setClone = true; | |
cursor(HAND); | |
offsetSet = false; | |
} | |
else{ | |
pickColor = true; | |
cursor(HAND); | |
} | |
break; | |
case 33: //page up | |
eContrast += 0.2; | |
break; | |
case 34: //page down | |
eContrast -=0.2; | |
break; | |
case 155: //insert | |
eBrightness +=1; | |
break; | |
case DELETE: //delete | |
eBrightness -=1; | |
break; | |
case 112: | |
layers[curLayer].beginDraw(); | |
layers[curLayer].endDraw(); | |
cursor(WAIT); | |
separate_frequencies(); | |
cursor(ARROW); | |
frequencyMode = 1; | |
break; | |
} | |
break; | |
} | |
info = "Layer: " + curLayer + " Brushsize: " + thickness + " Strength: " + strength + " Zoom:" + zoom; | |
frame.setTitle(info); | |
} | |
void keyReleased() { | |
if(dragging){ | |
dragging = false; | |
cursor(ARROW); | |
} | |
else if(pickColor){ | |
pickColor = false; | |
cursor(ARROW); | |
} | |
// else if(setClone){ | |
// if(!locked){ | |
// setClone = false; | |
// cursor(CROSS); | |
// } | |
// } | |
} | |
void cornerOffset(){ | |
cX = dX+zX; | |
cY= dY+zY; | |
} | |
void mousePressed() { | |
//println(mouseX, mouseY, mouseX /zoom, mouseY /zoom); | |
if(pickColor){ | |
float offsetX = dX+zX; | |
float offsetY= dY+zY; | |
for(int i = 0; i < numLayers;i++) | |
layers[i].beginDraw(); | |
for(int i = 0; i < numLayers;i++){ | |
penColor[i] = layers[i].get((int)((mouseX-offsetX)/zoom), (int)((mouseY-offsetY)/zoom)); | |
layers[i].stroke(penColor[i]); | |
layers[i].endDraw(); | |
} | |
} | |
else if(clone){ | |
if(setClone){ | |
cloneX = mouseX; | |
cloneY = mouseY; | |
locked = true; | |
} | |
else{ | |
if(!offsetSet){ | |
cOffsetX = mouseX-cloneX; | |
cOffsetY = mouseY-cloneY; | |
offsetSet = true; | |
} | |
save_undo(); | |
for(int i =0; i < numLayers;i++){ | |
layers[i].beginDraw(); | |
layers[i].copy(mouseX-cOffsetX,mouseY-cOffsetY,thickness,thickness,mouseX,mouseY,thickness,thickness); | |
layers[i].endDraw(); | |
oX = mouseX; | |
oY = mouseY; | |
} | |
} | |
} | |
else if(dragging){ | |
locked = true; | |
oX= mouseX; | |
oY=mouseY; | |
} | |
else{ | |
save_undo(); | |
float offsetX = dX+zX; | |
float offsetY= dY+zY; | |
oX= mouseX; | |
oY=mouseY; | |
//layers[curLayer].colorMode(HSB); | |
for(int i = 0; i < numLayers;i++){ | |
layers[i].beginDraw(); | |
// layers[i].stroke(penColor[i],strength*tablet.getPressure()); | |
layers[i].stroke(penColor[i],strength); | |
layers[i].line((oX-offsetX)/zoom, (oY-offsetY)/zoom, | |
(oX-offsetX)/zoom, (oY-offsetY)/zoom); | |
layers[i].endDraw(); | |
} | |
} | |
} | |
void mouseReleased() { | |
locked = false; | |
if(setClone){ | |
setClone=false; | |
cursor(CROSS); | |
} | |
saveUndo = false; | |
} | |
void mouseDragged() { | |
if(!dragging && !pickColor && !clone){ | |
mX = mouseX; mY = mouseY; | |
for(int i = 0; i< numLayers; i++){ | |
layers[i].beginDraw(); | |
// layers[i].stroke(penColor[i],strength*tablet.getPressure()); | |
layers[i].stroke(penColor[i],strength); | |
layers[i].line((oX-(dX+zX))/zoom, (oY-(dY+zY))/zoom, | |
(mouseX-(dX+zX))/zoom, (mouseY-(dY+zY))/zoom); | |
layers[i].endDraw(); | |
} | |
oX = mX; oY = mY; | |
} | |
else if(clone){ | |
if(setClone){ | |
cloneX = mouseX; | |
cloneY = mouseY; | |
} | |
else { | |
oX= mouseX; oY = mouseY; | |
for(int i =0; i < numLayers;i++){ | |
layers[i].beginDraw(); | |
layers[i].copy(oX-cOffsetX,oY-cOffsetY,thickness,thickness,oX,oY,thickness,thickness); | |
layers[i].endDraw(); | |
} | |
} | |
} | |
else if(locked && dragging) { | |
dX += mouseX-oX; | |
dY += mouseY-oY; | |
rdX= dX /zoom; | |
rdY = dY / zoom; | |
oX = mouseX; | |
oY = mouseY; | |
} | |
} | |
void undo(){ | |
if(undoInstances >0){ | |
for(int i = 0; i < numLayers; i++){ | |
uLayers[i*numUndo+curUndo-1].beginDraw(); | |
uLayers[i*numUndo+curUndo-1].endDraw(); | |
} | |
if(curUndo == 0)curUndo =numUndo; //wrap around | |
for(int i = 0; i < numLayers; i++){ | |
println(i * numUndo+curUndo); | |
layers[i].beginDraw(); | |
layers[i].endDraw(); //seems you need to set the pixels | |
temp.beginDraw(); | |
temp.image(layers[i],0,0); | |
temp.endDraw(); | |
layers[i].beginDraw(); | |
layers[i].image(uLayers[i*numUndo+curUndo-1],0,0); | |
layers[i].endDraw(); | |
uLayers[i*numUndo+curUndo-1].beginDraw(); | |
uLayers[i*numUndo+curUndo-1].image(temp,0,0); | |
uLayers[i*numUndo+curUndo-1].endDraw(); | |
} | |
curUndo--; | |
undoInstances--; | |
redoInstances++; | |
} | |
} | |
void redo(){ | |
if(redoInstances > 0){ | |
for(int i = 0; i < numLayers;i++){ | |
layers[i].beginDraw(); | |
layers[i].endDraw(); | |
temp.beginDraw(); | |
temp.image(layers[i],0,0); | |
temp.endDraw(); | |
layers[i].beginDraw(); | |
layers[i].image(uLayers[i*numUndo+curUndo],0,0); | |
layers[i].endDraw(); | |
uLayers[i*numUndo+curUndo].beginDraw(); | |
uLayers[i*numUndo+curUndo].image(temp,0,0); | |
uLayers[i*numUndo+curUndo].endDraw(); | |
} | |
curUndo++; | |
undoInstances++; | |
redoInstances--; | |
if(curUndo == numUndo) | |
curUndo = 0; | |
} | |
} | |
void save_undo(){ | |
for(int i = 0; i < numLayers;i++){ | |
layers[i].beginDraw(); | |
layers[i].endDraw(); | |
uLayers[i * numUndo +curUndo].beginDraw(); | |
uLayers[i * numUndo + curUndo].image(layers[i],0,0); | |
uLayers[i * numUndo + curUndo].endDraw(); | |
} | |
curUndo++; | |
if(curUndo == numUndo) curUndo = 0; | |
redoInstances = 0; | |
undoInstances++; | |
if(undoInstances>numUndo)undoInstances = numUndo; | |
// println("finished drawing ", curUndo,undoInstances, redoInstances); | |
} | |
void set_thickness(){ | |
for(int i = 0; i < numLayers; i++){ | |
layers[i].beginDraw(); | |
layers[i].strokeWeight(thickness); | |
layers[i].endDraw(); | |
} | |
} | |
void zoom_up(){ | |
if (zoom < 0.5){ | |
zoom = zoom+0.1; | |
dX = rdX * zoom; | |
dY = rdY * zoom; | |
zX= (width - width * zoom)/2; | |
zY = (height - height * zoom)/2; | |
} | |
else if (zoom < 20.0){ | |
zoom = zoom+0.5; | |
dX = rdX * zoom; | |
dY = rdY * zoom; | |
zX= (width - width * zoom)/2; | |
zY = (height - height * zoom)/2; | |
} | |
} | |
void zoom_down(){ | |
if(zoom > 0.5){ | |
zoom = zoom - 0.5; | |
if(zoom < 0.5) zoom = 0.5; | |
dX = rdX * zoom; | |
dY = rdY * zoom; | |
zX= (width - width * zoom)/2; | |
zY = (height - height * zoom)/2; | |
} | |
else { | |
zoom = zoom - 0.1; | |
if(zoom < 0.1) zoom = 0.1; | |
dX = rdX * zoom; | |
dY = rdY * zoom; | |
zX= (width - width * zoom)/2; | |
zY = (height - height * zoom)/2; | |
} | |
// dX=rdX * zoom; | |
// dY = rdY * zoom; | |
} | |
void load_B_image(File selection){ | |
if (selection == null) | |
; | |
else { | |
img = loadImage( selection.getAbsolutePath()); | |
layers[curLayer].beginDraw(); | |
layers[curLayer].image(img,0,0); | |
layers[curLayer].endDraw(); | |
curUndo = 0; | |
undoInstances = 0; | |
redoInstances = 0; | |
} | |
} | |
void save_layer(File selection){ | |
String name= selection.getAbsolutePath(); | |
boolean save = false; | |
if(selection.exists()){ | |
println("fileExist"); | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
String preset= name; | |
String op1s = JOptionPane.showInputDialog(frame, "File Exists!! Replace?", preset); | |
if(op1s != null) { | |
save = true; | |
name = op1s; | |
} | |
} | |
else save = true; | |
if(save){ | |
layers[curLayer].beginDraw(); | |
layers[curLayer].save(name); | |
layers[curLayer].endDraw(); | |
println(name); | |
} | |
} | |
void flip_horizontal(){ | |
int x, x2,y; | |
int w = temp.width; | |
int h = temp.height; | |
for(int i = 0; i < numLayers;i++){ | |
layers[i].beginDraw(); | |
layers[i].endDraw(); | |
temp.beginDraw(); | |
temp.endDraw(); | |
layers[i].beginDraw(); | |
temp.beginDraw(); | |
temp.image(layers[i],0,0); | |
layers[i].loadPixels(); | |
temp.loadPixels(); | |
for(y= 0; y < h;y++){ | |
for(x=0,x2 = w-1;x < w; x++,x2--){ | |
layers[i].pixels[y * w + x] = | |
temp.pixels[y*w+x2]; | |
} | |
} | |
layers[i].updatePixels(); | |
temp.endDraw(); | |
layers[i].endDraw(); | |
} | |
} | |
void median(int msize){ | |
float[]r= new float[msize]; | |
float[]g= new float[msize]; | |
float[]b= new float[msize]; | |
float tpix, mred, mgreen, mblue; | |
int w = layers[curLayer].width; | |
int h = layers[curLayer].height; | |
int loc = 0; //location of source pixel; | |
int pixi; // pixel indice ffor pixel arrays | |
temp2.beginDraw(); | |
temp2.endDraw(); | |
save_undo(); | |
layers[curLayer].beginDraw(); | |
temp.beginDraw(); | |
layers[curLayer].loadPixels(); | |
temp.loadPixels(); | |
for(int y= 0; y< h;y++){ | |
for(int x=0; x< w; x++){ | |
pixi = 0; | |
for(int kx = -msize/2; kx <= msize/2; kx++){ | |
if(x+ kx < 0||x+ kx >= w) | |
loc= y * w +x; | |
else | |
loc = y * w + x + kx; | |
r[pixi] = layers[curLayer].red(layers[curLayer].pixels[loc]); | |
g[pixi] = layers[curLayer].green(layers[curLayer].pixels[loc]); | |
b[pixi] = layers[curLayer].blue(layers[curLayer].pixels[loc]); | |
pixi++; | |
} | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = r[i]; | |
int j = i-1; | |
while(j >=0 && r[j]>tpix){ | |
r[j+1]=r[j]; | |
j = j-1; | |
} | |
r[j+1]= tpix; | |
} | |
mred = r[msize/2]; // once sorted, the middle index should hold the median value | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = g[i]; | |
int j = i-1; | |
while(j >=0 && g[j]>tpix){ | |
g[j+1]=g[j]; | |
j = j-1; | |
} | |
g[j+1]= tpix; | |
} | |
mgreen = g[msize/2]; // once sorted, the middle index should hold the median value | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = b[i]; | |
int j = i-1; | |
while(j >=0 && b[j]>tpix){ | |
b[j+1]=b[j]; | |
j = j-1; | |
} | |
b[j+1]= tpix; | |
} | |
mblue = b[msize/2]; // once sorted, the middle index should hold the median value | |
loc= y * w + x; | |
temp.pixels[loc] = color(mred,mgreen,mblue); | |
} | |
} | |
temp.updatePixels(); | |
temp.endDraw(); | |
layers[curLayer].updatePixels(); | |
layers[curLayer].endDraw(); | |
layers[curLayer].beginDraw(); | |
temp2.beginDraw(); | |
layers[curLayer].loadPixels(); | |
temp2.loadPixels(); | |
for(int x=0; x< w;x++){ | |
for(int y=0; y< h; y++){ | |
pixi = 0; | |
for(int ky = -msize/2; ky <= msize/2; ky++){ | |
if(y+ ky < 0||y+ ky >= h) | |
loc= y * w +x; | |
else loc = y * w + x + ky * w; | |
r[pixi] = layers[curLayer].red(layers[curLayer].pixels[loc]); | |
g[pixi] = layers[curLayer].green(layers[curLayer].pixels[loc]); | |
b[pixi] = layers[curLayer].blue(layers[curLayer].pixels[loc]); | |
pixi++; | |
} | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = r[i]; | |
int j = i-1; | |
while(j >=0 && r[j]>tpix){ | |
r[j+1]=r[j]; | |
j = j-1; | |
} | |
r[j+1]= tpix; | |
} | |
mred = r[msize/2]; // once sorted, the middle index should hold the median value | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = g[i]; | |
int j = i-1; | |
while(j >=0 && g[j]>tpix){ | |
g[j+1]=g[j]; | |
j = j-1; | |
} | |
g[j+1]= tpix; | |
} | |
mgreen = g[msize/2]; // once sorted, the middle index should hold the median value | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = b[i]; | |
int j = i-1; | |
while(j >=0 && b[j]>tpix){ | |
b[j+1]=b[j]; | |
j = j-1; | |
} | |
b[j+1]= tpix; | |
} | |
mblue = b[msize/2]; // once sorted, the middle index should hold the median value | |
loc= y * w + x; | |
temp2.pixels[loc] = color(mred,mgreen,mblue); | |
} | |
} | |
temp2.updatePixels(); | |
temp2.endDraw(); | |
layers[curLayer].updatePixels(); | |
layers[curLayer].endDraw(); | |
layers[curLayer].beginDraw(); | |
layers[curLayer].image(temp,0,0); | |
layers[curLayer].endDraw(); | |
layers[curLayer].beginDraw(); | |
layers[curLayer].tint(255,127); | |
layers[curLayer].image(temp2,0,0); | |
layers[curLayer].endDraw(); | |
} | |
void assemble_pic(){ | |
int w = layers[0].width; | |
int h = layers[0].height; | |
int np = w * h * 3; | |
int pixi; | |
float[]original = new float[np]; | |
float[]effect = new float[np]; | |
float[]coefficient = new float[np]; | |
float[]change = new float[np]; | |
float[]difference = new float[np]; | |
layers[0].beginDraw(); | |
layers[0].loadPixels(); | |
pixi = 0; | |
for(int y = 0; y < h; y++){ | |
int rowOffset = y *w; | |
for(int x = 0; x < w; x++){ | |
original[pixi++]=layers[0].red(layers[0].pixels[rowOffset+x]); | |
original[pixi++]=layers[0].green(layers[0].pixels[rowOffset+x]); | |
original[pixi++]=layers[0].blue(layers[0].pixels[rowOffset+x]); | |
} | |
} | |
layers[0].updatePixels(); | |
layers[0].endDraw(); | |
layers[1].beginDraw(); | |
layers[1].loadPixels(); | |
pixi = 0; | |
for(int y = 0; y < h; y++){ | |
int rowOffset = y *w; | |
for(int x = 0; x < w; x++){ | |
effect[pixi++]=layers[0].red(layers[1].pixels[rowOffset+x]); | |
effect[pixi++]=layers[0].green(layers[1].pixels[rowOffset+x]); | |
effect[pixi++]=layers[0].blue(layers[1].pixels[rowOffset+x]); | |
} | |
} | |
layers[1].updatePixels(); | |
layers[1].endDraw(); | |
layers[2].beginDraw(); | |
layers[2].loadPixels(); | |
pixi = 0; | |
for(int y = 0; y < h; y++){ | |
int rowOffset = y *w; | |
for(int x = 0; x < w; x++){ | |
change[pixi++]=layers[0].red(layers[2].pixels[rowOffset+x]); | |
change[pixi++]=layers[0].green(layers[2].pixels[rowOffset+x]); | |
change[pixi++]=layers[0].blue(layers[2].pixels[rowOffset+x]); | |
} | |
} | |
layers[2].updatePixels(); | |
layers[2].endDraw(); | |
pixi = 0; | |
for(int y = 0; y < h; y++){ | |
for(int x = 0; x < w; x++){ | |
if(original[pixi] ==0)coefficient[pixi] = 0; | |
else coefficient[pixi] = effect[pixi]/original[pixi]; //no zeros | |
pixi++; | |
if(original[pixi] ==0)coefficient[pixi] = 0; | |
else coefficient[pixi] = effect[pixi]/original[pixi]; //no zeros | |
pixi++; | |
if(original[pixi] ==0)coefficient[pixi] = 0; | |
else coefficient[pixi] = effect[pixi]/original[pixi]; //no zeros | |
pixi++; | |
} | |
} | |
pixi = 0; | |
for(int y = 0; y < h; y++){ | |
for(int x = 0; x < w; x++){ | |
difference[pixi] =effect[pixi]-change[pixi]; | |
pixi++; | |
difference[pixi] =effect[pixi]-change[pixi]; | |
pixi++; | |
difference[pixi] =effect[pixi]-change[pixi]; | |
pixi++; | |
} | |
} | |
layers[3].beginDraw(); | |
layers[3].loadPixels(); | |
pixi = 0; | |
float r,g,b; | |
for(int y = 0; y < h; y++){ | |
int rowOffset = y * w; | |
for(int x = 0; x < w; x++){ | |
r=original[pixi]+ difference[pixi]*coefficient[pixi]; | |
pixi++; | |
g=original[pixi]+ difference[pixi]*coefficient[pixi]; | |
pixi++; | |
b=original[pixi]+ difference[pixi]*coefficient[pixi]; | |
pixi++; | |
layers[3].pixels[rowOffset+ x]=color(r,g,b); | |
} | |
} | |
layers[3].updatePixels(); | |
layers[3].endDraw(); | |
} | |
int get_median_size(){ | |
int op1; | |
try{ | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
String preset=""+ medianSize; | |
String op1s = JOptionPane.showInputDialog(frame, "Set median size", preset); | |
if(op1s != null){ | |
try{ | |
op1=Integer.parseInt(op1s); | |
return op1; | |
} | |
catch(Exception e){ | |
return medianSize; | |
} | |
} | |
else return medianSize; | |
} | |
void median2( PGraphics input, int msize){ | |
float[]r= new float[msize]; | |
float[]g= new float[msize]; | |
float[]b= new float[msize]; | |
float tpix, mred, mgreen, mblue; | |
int w = input.width; | |
int h = input.height; | |
int loc = 0; //location of source pixel; | |
int pixi; // pixel indice ffor pixel arrays | |
int direction = 1; | |
int end; | |
temp2.beginDraw(); | |
temp2.endDraw(); | |
input.beginDraw(); | |
input.endDraw(); | |
save_undo(); | |
input.beginDraw(); | |
temp.beginDraw(); | |
input.loadPixels(); | |
temp.loadPixels(); | |
for(int y= 0; y< h;y++){ | |
int x; | |
if(y%2==1) { | |
x = w -1; | |
direction = -1; | |
end = -1; | |
} | |
else{ | |
x = 0; | |
direction = 1; | |
end = w; | |
} | |
for(; x!= end; x+= direction){ | |
pixi = 0; | |
for(int kx = -msize/2; kx <= msize/2; kx++){ | |
if(x+ kx < 0||x+ kx >= w) | |
loc= y * w +x; | |
else loc = y * w + x + kx; | |
r[pixi] = (input.pixels[loc]>>16)& 0xFF; | |
g[pixi] = (input.pixels[loc]>>8)& 0xFF; | |
b[pixi] = input.pixels[loc] & 0xFF; | |
pixi++; | |
} | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = r[i]; | |
int j = i-1; | |
while(j >=0 && r[j]>tpix){ | |
r[j+1]=r[j]; | |
j = j-1; | |
} | |
r[j+1]= tpix; | |
} | |
mred = r[msize/2]; // once sorted, the middle index should hold the median value | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = g[i]; | |
int j = i-1; | |
while(j >=0 && g[j]>tpix){ | |
g[j+1]=g[j]; | |
j = j-1; | |
} | |
g[j+1]= tpix; | |
} | |
mgreen = g[msize/2]; // once sorted, the middle index should hold the median value | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = b[i]; | |
int j = i-1; | |
while(j >=0 && b[j]>tpix){ | |
b[j+1]=b[j]; | |
j = j-1; | |
} | |
b[j+1]= tpix; | |
} | |
mblue = b[msize/2]; // once sorted, the middle index should hold the median value | |
loc= y * w + x; | |
temp.pixels[loc] =0xff000000 | ((int)mred << 16) | ((int)mgreen << 8) |(int) mblue; | |
} | |
} | |
temp.updatePixels(); | |
temp.endDraw(); | |
input.updatePixels(); | |
input.endDraw(); | |
input.beginDraw(); | |
temp2.beginDraw(); | |
input.loadPixels(); | |
temp2.loadPixels(); | |
for(int x=0; x< w;x++){ | |
int y; | |
if(x%2==1){ | |
y = h-1; | |
direction = -1; | |
end = -1; | |
} | |
else{ | |
y = 0; | |
direction =1; | |
end = h; | |
} | |
for(; y!=end; y+=direction){ | |
pixi = 0; | |
for(int ky = -msize/2; ky <= msize/2; ky++){ | |
if(y+ ky < 0||y+ ky >= h) | |
loc= y * w +x; | |
else loc = y * w + x + ky * w; | |
r[pixi] = (input.pixels[loc]>>16)& 0xFF; | |
g[pixi] = (input.pixels[loc]>>8)& 0xFF; | |
b[pixi] = input.pixels[loc] & 0xFF; | |
pixi++; | |
} | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = r[i]; | |
int j = i-1; | |
while(j >=0 && r[j]>tpix){ | |
r[j+1]=r[j]; | |
j = j-1; | |
} | |
r[j+1]= tpix; | |
} | |
mred = r[msize/2]; // once sorted, the middle index should hold the median value | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = g[i]; | |
int j = i-1; | |
while(j >=0 && g[j]>tpix){ | |
g[j+1]=g[j]; | |
j = j-1; | |
} | |
g[j+1]= tpix; | |
} | |
mgreen = g[msize/2]; // once sorted, the middle index should hold the median value | |
for(int i = 1; i < msize; i++){ // these for loops sort the red , blue and green | |
// array in order by value | |
tpix = b[i]; | |
int j = i-1; | |
while(j >=0 && b[j]>tpix){ | |
b[j+1]=b[j]; | |
j = j-1; | |
} | |
b[j+1]= tpix; | |
} | |
mblue = b[msize/2]; // once sorted, the middle index should hold the median value | |
loc= y * w + x; | |
temp2.pixels[loc] = 0xff000000 | ((int)mred << 16) | ((int)mgreen << 8) | (int)mblue; | |
} | |
} | |
temp2.updatePixels(); | |
temp2.endDraw(); | |
input.updatePixels(); | |
input.endDraw(); | |
input.beginDraw(); | |
input.image(temp,0,0); | |
input.endDraw(); | |
input.beginDraw(); | |
input.tint(255,127); | |
input.image(temp2,0,0); | |
input.endDraw(); | |
} | |
void separate_frequencies(){ | |
if(frequencyMode != 0) | |
; | |
else{ | |
for(int i=0;i<numLayers;i++){ | |
lfLayers[i].beginDraw(); | |
lfLayers[i].image(layers[i],0,0); | |
lfLayers[i].endDraw(); | |
} | |
for(int i = 0; i < numLayers;i++){ | |
median2(lfLayers[i],3); | |
median2(lfLayers[i],5); | |
median2(lfLayers[i],7); | |
// median2(layers[i],9); | |
} | |
for(int i =0; i < numLayers; i++) | |
subtract_layers(layers[i],lfLayers[i],hfLayers[i],127); | |
for(int i = 0; i < numLayers;i++){ | |
layers[i].beginDraw(); | |
layers[i].image(lfLayers[i],0,0); | |
layers[i].endDraw(); | |
} | |
} | |
} | |
void subtract_layers(PGraphics org, PGraphics filtered, PGraphics diff, int offset){ | |
//todo | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment