Created
October 2, 2014 08:04
-
-
Save carloe/6718ad715c58d0074560 to your computer and use it in GitHub Desktop.
Scaffolding for a processing pixel sorter
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
import java.util.Collections; | |
Sorter sorter; | |
PImage photo; | |
void setup() { | |
photo = loadImage("Foto3.JPG"); | |
sorter = new Sorter(); | |
sorter.init(photo); | |
photo = sorter.image(); | |
size(photo.width, photo.height); | |
} | |
void draw() { | |
image(photo, 0, 0); | |
} | |
class Sorter { | |
int _width = 0; | |
int _height = 0; | |
ArrayList<Node> _items = new ArrayList<Node>(); | |
void init(PImage p) { | |
_width = p.width; | |
_height = p.height; | |
p.loadPixels(); | |
for (int y = 0; y < _height ; y++) { | |
for (int x = 0; x < _width ; x++) { | |
Node n = new Node(); | |
int index = (_width * y)+x; | |
n.index = index; | |
n.originalColor = p.pixels[index]; | |
_items.add(n); | |
} | |
} | |
} | |
PImage image() { | |
Collections.sort(_items); | |
PImage returnImg = createImage(_width, _height, ARGB); | |
returnImg.loadPixels(); | |
for (int y = 0; y < _height ; y++) { | |
for (int x = 0; x < _width ; x++) { | |
int index = (_width * y)+x; | |
Node n = _items.get(index); | |
returnImg.pixels[index] = n.originalColor; | |
} | |
} | |
returnImg.updatePixels(); | |
return returnImg; | |
} | |
} | |
class Node implements Comparable { | |
int index = 0; | |
color originalColor; | |
float getValue() { | |
return (red(originalColor) + green(originalColor) + blue(originalColor))/3.0; | |
} | |
public int compareTo(Object o) { | |
Node n = (Node) o; | |
float i1 = getValue(); | |
float i2 = n.getValue(); | |
return i1 == i2 ? 0 : (i1 > i2 ? 1 : -1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment