Skip to content

Instantly share code, notes, and snippets.

@rboyce
Last active December 29, 2015 22:59
Show Gist options
  • Save rboyce/7739797 to your computer and use it in GitHub Desktop.
Save rboyce/7739797 to your computer and use it in GitHub Desktop.
import gifAnimation.*;
PImage img;
GifMaker gifExport;
String imgFileName = "garden-500";
String fileType = "jpg";
int brightnessDelta = 10;
int frameDelay = 0;
int previousBrightness = 0;
int brightnessValue = 0;
int row = 0;
int column = 0;
int getBrightnessValue() {
return (int) (255 / ( 1 + 90 * exp(frameCount * -.04) ));
}
int roundToNearest(int i, float val) {
return i * round(val / i);
}
void setup() {
frameRate(30);
img = loadImage(imgFileName+"."+fileType);
size(img.width, img.height);
image(img, 0, 0);
brightnessValue = 0;
gifExport = new GifMaker(this, imgFileName+"_anim_1.gif");
gifExport.setRepeat(0); // make it an "endless" animation
}
void draw() {
int row = 0;
int column = 0;
previousBrightness = brightnessValue;
brightnessValue = getBrightnessValue() > 255 ? 0 : 255 - getBrightnessValue();
if (brightnessValue <= 0) {
// push old image to GIF
gifExport.setDelay(frameDelay);
gifExport.addFrame();
frameRate(0);
gifExport.finish();
println("DONE");
System.exit(0);
} else if (roundToNearest(brightnessDelta, previousBrightness) == roundToNearest(brightnessDelta, brightnessValue)) {
frameDelay++;
} else {
println("frame brightness: "+brightnessValue);
// push old image to GIF
gifExport.setDelay(frameDelay);
gifExport.addFrame();
frameDelay = 1;
// draw updated image
while(row < height-1) {
img.loadPixels();
// sortRowUpTo(row, frameCount);
sortRow(row);
row++;
img.updatePixels();
}
image(img,0,0);
}
}
void sortRow(int y) {
sortRowBetween(y, 0, width);
}
void sortRowBetween(int y, int _xstart, int _xend) {
int x = _xstart;
int xend = 0;
while(xend < width-1) {
x = getFirstBrightX(x, y);
xend = getNextDarkX(x, y);
if (x < 0) break;
xend = _xend > xend ? xend : _xend;
int sortLength = xend-x;
if(sortLength <= 0) {
x = xend + 1;
continue;
}
color[] unsorted = new color[sortLength];
color[] sorted = new color[sortLength];
for (int i=0; i<sortLength; i++) {
unsorted[i] = img.pixels[x + i + y * img.width];
}
sorted = sort(unsorted);
for (int i=0; i<sortLength; i++) {
img.pixels[x + i + y * img.width] = sorted[i];
}
x = xend+1;
}
}
//BRIGHTNESS
int getFirstBrightX(int _x, int _y) {
int x = _x;
int y = _y;
color c;
while (brightness(c = img.pixels[x + y * img.width]) < brightnessValue) {
x++;
if (x >= width) return -1;
}
return x;
}
int getNextDarkX(int _x, int _y) {
int x = _x+1;
int y = _y;
color c;
while (brightness(c = img.pixels[x + y * img.width]) > brightnessValue) {
x++;
if (x >= width) return width-1;
}
return x-1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment