Skip to content

Instantly share code, notes, and snippets.

@ipa
Created November 5, 2011 14:07
Show Gist options
  • Save ipa/1341542 to your computer and use it in GitHub Desktop.
Save ipa/1341542 to your computer and use it in GitHub Desktop.
pictureaddition
PImage picture1;
PImage picture2;
void setup() {
size(600, 400);
picture1 = loadImage("color.jpg");
picture2 = loadImage("watch.jpg");
// resize them
picture1.resize(width, height);
picture2.resize(width, height);
frameRate(0.5);
}
final int WAITTIME = 2000;
int actualMethod = -1;
void draw(){
actualMethod = (++actualMethod) % 3;
String method = "";
switch(actualMethod){
case 0:
image(imgAddition(picture1, picture2, 0.3), 0, 0);
method = "ADD";
break;
case 1:
image(imgSubtraction(picture1, picture2, 0.3), 0, 0);
method = "SUBTRACT";
break;
case 2:
image(imgMultiply(picture1, picture2, 1), 0, 0);
method = "MULTIPLY";
break;
}
fill(0, 0, 0);
text("Method: " + method, 5, 20);
}
PImage imgAddition(PImage img1, PImage img2, float factor) {
PImage result = new PImage(width, height);
for (int x = 0; x < width; x = x + 1) {
for (int y = 0; y < height; y = y + 1) {
color col1 = img1.get(x, y);
color col2 = img2.get(x, y);
int r = (int)min((red(col1)*factor + red(col2)), 255);
int g = (int)min((green(col1)*factor + green(col2)), 255);
int b = (int)min((blue(col1)*factor + blue(col2)), 255);
result.set(x, y, color(r, g, b));
}
}
return result;
}
PImage imgSubtraction(PImage img1, PImage img2, float factor) {
PImage result = new PImage(width, height);
for (int x = 0; x < width; x = x + 1) {
for (int y = 0; y < height; y = y + 1) {
color col1 = img1.get(x, y);
color col2 = img2.get(x, y);
int r = (int)max((red(col2) - red(col1)*factor), 0);
int g = (int)max((green(col2) - green(col1)*factor), 0);
int b = (int)max((blue(col2) - blue(col1)*factor), 0);
result.set(x, y, color(r, g, b));
}
}
return result;
}
PImage imgMultiply(PImage img1, PImage img2, float factor) {
PImage result = new PImage(width, height);
for (int x = 0; x < width; x = x + 1) {
for (int y = 0; y < height; y = y + 1) {
color col1 = img1.get(x, y);
color col2 = img2.get(x, y);
int r = (int)max((red(col2) + red(col1) * factor) / 2, 0);
int g = (int)max((green(col2) + green(col1) * factor) / 2, 0);
int b = (int)max((blue(col2) + blue(col1) * factor) / 2, 0);
result.set(x, y, color(r, g, b));
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment