Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PixelSorter ps;
- void setup() {
- size(400, 400);
- ps = new PixelSorter(loadImage("caravaggio.jpg"), 2, 120);
- //surface.setSize(ps.img.width, ps.img.height);
- ps.sortPartialColumns();
- ps.img.save("img00.jpg");
- exit();
- }
- void draw() {
- }
- public class PixelSorter {
- PImage img;
- int mode; // mode 0 = sort by hue; mode 1 = sort by saturation; mode 2 = sort by brightness;
- float threshold;
- PixelSorter(PImage _img, int _mode, float _threshold) {
- img = _img;
- mode = _mode;
- threshold = _threshold;
- }
- void sortColumns() {
- img.loadPixels();
- for (int x = 0; x < img.width; x++) {
- for (int y = 0; y < img.height; y++) {
- int currentPixelIndex = x + y * img.width;
- int brightestPixel = 0;
- float record = -1;
- for (int y1 = y; y1 < img.height; y1++) {
- int i = x + y1 * img.width;
- float h = hue(img.pixels[i]);
- float s = saturation(img.pixels[i]);
- float b = brightness(img.pixels[i]);
- float pixelValue;
- switch(mode) {
- case 0:
- pixelValue = h;
- break;
- case 1:
- pixelValue = s;
- break;
- case 2:
- pixelValue = b;
- break;
- default:
- pixelValue = (h*0.45)+(s*0.15)+(b*0.40);
- break;
- }
- if (record < pixelValue) {
- record = pixelValue;
- brightestPixel = i;
- }
- }
- color temp = img.pixels[currentPixelIndex];
- img.pixels[currentPixelIndex] = img.pixels[brightestPixel];
- img.pixels[brightestPixel] = temp;
- }
- }
- img.updatePixels();
- }
- void sortPartialColumns() {
- img.loadPixels();
- for (int x = 0; x < img.width; x++) {
- for (int y = 0; y < img.height; y++) {
- int ystart = y;
- int yend = y;
- int currentPixelIndex = x + y * img.width;
- float h = hue(img.pixels[currentPixelIndex]);
- float s = saturation(img.pixels[currentPixelIndex]);
- float b = brightness(img.pixels[currentPixelIndex]);
- float pixelValue;
- switch(mode) {
- case 0:
- pixelValue = h;
- break;
- case 1:
- pixelValue = s;
- break;
- case 2:
- pixelValue = b;
- break;
- default:
- pixelValue = (h*0.45)+(s*0.15)+(b*0.40);
- break;
- }
- if (pixelValue > threshold) {
- while (yend < img.height) {
- yend++;
- if (pixelValue < threshold) break;
- }
- int sortingDist = yend - ystart;
- color[] sorted = new color[sortingDist];
- color[] unsorted = new color[sortingDist];
- for (int i = 0; i < sortingDist; i++) {
- unsorted[i] = img.pixels[x + (ystart+i) * img.width];
- }
- sorted = sortColors(unsorted);
- for (int i = 0; i < sortingDist; i++) {
- img.pixels[x + (ystart+i) * img.width] = sorted[i];
- }
- y = yend;
- }
- }
- }
- img.updatePixels();
- }
- color[] sortColors(color[] _p) {
- color[] p = _p.clone();
- for (int i = 0; i < p.length; i++) {
- int highestPixel = 0;
- float record = -1;
- for (int j = i; j < p.length; j++) {
- float h = hue(p[i]);
- float s = saturation(p[i]);
- float b = brightness(p[i]);
- float pixelValue;
- switch(mode) {
- case 0:
- pixelValue = h;
- break;
- case 1:
- pixelValue = s;
- break;
- case 2:
- pixelValue = b;
- break;
- default:
- pixelValue = (h*0.45)+(s*0.15)+(b*0.40);
- break;
- }
- if (record < pixelValue) {
- record = pixelValue;
- highestPixel = j;
- }
- }
- color temp = p[i];
- p[i] = p[highestPixel];
- p[highestPixel] = temp;
- }
- return p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement