Skip to content

Instantly share code, notes, and snippets.

@0x5d
Last active July 27, 2018 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x5d/47d80a2c1ca64f8f6a9f5208b59eccde to your computer and use it in GitHub Desktop.
Save 0x5d/47d80a2c1ca64f8f6a9f5208b59eccde to your computer and use it in GitHub Desktop.
final int threshold = 38;
final color black = color(0);
final color white = color(255);
PImage img;
boolean isSorted = false;
void setup() {
img = loadImage("IMG_0010.jpg");
size(1, 1);
img.loadPixels();
}
void draw() {
if (isSorted) return;
PImage filtered = filter(img, threshold);
filtered.loadPixels();
for (int x = 0; x < img.width; x++) {
sortColumn(x, 0, getEnd(x, filtered));
}
isSorted = true;
img.updatePixels();
img.save("landscape-reverse-sorted.jpg");
exit();
}
PImage filter(PImage source, color threshold) {
PImage filtered = createImage(source.width, source.height, RGB);
for (int x = 0; x < source.width; x++) {
for (int y = 0; y < source.height; y++ ) {
int i = x + y * source.width;
filtered.pixels[i] = brightness(source.pixels[i]) > threshold
? white
: black;
}
}
return filtered;
}
void sortColumn(int col, int start, int end) {
int sortLength = end - start;
color[] unsorted = new color[sortLength];
for(int i = 0; i < sortLength; i++) {
unsorted[i] = img.pixels[col + i * img.width];
}
color[] sorted = reverse(sort(unsorted));
for(int i = 0; i < sortLength; i++) {
img.pixels[col + i * img.width] = sorted[i];
}
}
int getEnd(int col, PImage brightMap) {
for (int i = 0; i < img.height; i++) {
if (brightMap.pixels[col + i * img.width] == black) {
return i;
}
}
return img.height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment