Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Left click to open an new image, right click to save
- //press z to change modes, and x to open a new map file
- PImage img;
- PImage mapImg;
- PImage displayImg;
- PImage original;
- int mode = 0;
- int hInsets;
- int wInsets;
- ASDFSort ASDFob;
- java.awt.Insets insets;
- void setup() {
- frame.setResizable(true);
- img = createImage(100,100,RGB);
- size(img.width,img.height);
- mapImg = createImage(img.width,img.height,RGB);
- mapImg.copy(img, 0,0,img.width,img.height,0,0,mapImg.width,mapImg.height);
- selectInput("Image to process:", "fileSelector");
- noStroke();
- }
- void fileSelector(File selection) {
- if (selection == null) {
- println("Selection cancelled.");
- return;
- }else{
- println(selection.getAbsolutePath());
- }
- img = loadImage(selection.getAbsolutePath());
- //Set the frame to fit the selected image.
- insets = frame.getInsets();
- wInsets = insets.left+insets.right;
- hInsets = insets.top+insets.bottom;
- frame.setSize(img.width+wInsets,img.height+hInsets);
- mapImg = createImage(img.width,img.height,RGB);
- mapImg.copy(img, 0,0,img.width,img.height,0,0,mapImg.width,mapImg.height);
- }
- void fileSelector2(File selection) {
- if (selection == null) {
- println("Selection cancelled.");
- return;
- }else{
- println(selection.getAbsolutePath());
- }
- mapImg = loadImage(selection.getAbsolutePath());
- mapImg.resize(img.width,img.height);
- }
- void draw() {
- ASDFob = new ASDFSort(img,mapImg,mode);
- displayImg = ASDFob.asdf();
- image(displayImg,0,0);
- }
- void keyPressed() {
- if (key == 'x')
- selectInput("Select Map Image:", "fileSelector2");
- else if (key == 'z')
- mode = (mode+1)%10;
- else if (key == 'r')
- mapImg.copy(img,0,0,img.width,img.height,0,0,mapImg.width,mapImg.height);
- }
- void mousePressed() {
- //Saves the current image on a left click, selects a new image on a right click
- if (mouseButton == RIGHT) {
- saveImage();
- } else if (mouseButton == LEFT) {
- selectInput("Image to process:", "fileSelector");
- }
- }
- void saveImage() {
- File dir = new File (sketchPath(""));
- String[] list = dir.list();
- if (list == null) {
- println("Folder does not exist or cannot be accessed.");
- return;
- }
- int m = checkFileDir(list.length,list);
- String i = Integer.toString(m);
- displayImg.save(savePath(i+".png"));
- }
- int checkFileDir(int i,String[] list) {
- File dir = new File (sketchPath(""));
- for (int j = 0; j < list.length; j++) {
- if ((i+".png").equals(list[j])) {
- i++;
- checkFileDir(i,list);
- }
- }
- return i;
- }
- class ASDFSort {
- int mode = 1;
- //MODE:
- //0 -> black
- //1 -> bright
- //2 -> white
- //b(16777216)
- PImage img;
- PImage img2;
- String imgFileName = "3";
- String fileType = "png";
- int loops = 1;
- int f;
- int blackValue = -16000000;
- int brigthnessValue = 60;
- int whiteValue = -13000000;
- int row = 0;
- int column = 0;
- boolean saved = false;
- ASDFSort (PImage img, PImage img2, int mode) {
- this.img2 = createImage(img2.width,img2.height,RGB);
- this.img2.copy(img2,0,0,img2.width,img2.height,0,0,this.img2.width,this.img2.height);
- this.img = createImage(img2.width,img2.height,RGB);
- this.img.copy(img,0,0,img.width,img.height,0,0,this.img.width,this.img.height);
- this.mode = mode;
- }
- PImage asdf() {
- //img.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
- brigthnessValue = int(255*mouseX/float(width-10));
- while(column < img.width-1) {
- img.loadPixels();
- sortColumn();
- column++;
- img.updatePixels();
- }
- while(row < img.height-1) {
- img.loadPixels();
- sortRow();
- row++;
- img.updatePixels();
- }
- row = 0;
- column = 0;
- return img;
- /*if(!saved && frameCount >= loops) {
- saveFrame(imgFileName+"_"+mode+".png");
- saved = true;
- println("DONE"+frameCount);
- //System.exit(0);
- }*/
- }
- void sortRow() {
- int x = 0;
- int y = row;
- int xend = 0;
- while(xend < img.width-1) {
- switch(mode) {
- case 0:
- x = getFirstDarkX(x, y);
- xend = getNextBrightX(x, y);
- break;
- case 1:
- x = getFirstBrightX(x, y);
- xend = getNextDarkX(x, y);
- break;
- default:
- break;
- }
- if(x < 0) break;
- int sortLength = xend-x;
- HashMap<Float,Integer> unsorted = new HashMap<Float,Integer>();
- float[] unsorted2 = new float[sortLength];
- float[] sorted = new float[sortLength];
- for(int i=0; i<sortLength; i++) {
- unsorted.put(brightness(img.pixels[(x + i + y * img.width)%(img.width*img.height)]),img.pixels[(x + i + y * img.width)%(img.width*img.height)]);
- }
- for(int i=0; i<sortLength; i++) {
- unsorted2[i] = brightness(img.pixels[(x + i + y * img.width)%(img.width*img.height)]);
- }
- sorted = sort(unsorted2);
- try {
- for(int i=0; i<sortLength; i++) {
- img.pixels[(x + i + y * img.width)%(img.width*img.height)] = unsorted.get(sorted[i]);
- }
- } catch (NullPointerException e) {
- e.printStackTrace();
- }
- x = xend+1;
- }
- }
- void sortColumn() {
- int x = column;
- int y = 0;
- int yend = 0;
- while(yend < img.height-1) {
- switch(mode) {
- case 0:
- y = getFirstDarkY(x, y);
- yend = getNextBrightY(x, y);
- break;
- case 1:
- y = getFirstBrightY(x, y);
- yend = getNextDarkY(x, y);
- break;
- default:
- break;
- }
- if(y < 0) break;
- int sortLength = yend-y;
- HashMap<Float,Integer> unsorted = new HashMap<Float,Integer>();
- float[] unsorted2 = new float[sortLength];
- float[] sorted = new float[sortLength];
- for(int i=0; i<sortLength; i++) {
- unsorted.put(brightness(img.pixels[(x + (y+i) * img.width)%(img.width*img.height)]),img.pixels[(x + (y+i) * img.width)%(img.width*img.height)]);
- }
- for(int i=0; i<sortLength; i++) {
- unsorted2[i] = brightness(img.pixels[(x + (y+i) * img.width)%(img.width*img.height)]);
- }
- sorted = sort(unsorted2);
- for(int i=0; i<sortLength; i++) {
- img.pixels[(x + (y+i) * img.width)%(img.width*img.height)] = unsorted.get(sorted[i]);
- }
- y = yend+1;
- }
- }
- //brightness
- int getFirstBrightX(int _x, int _y) {
- int x = _x;
- int y = _y;
- color c;
- while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) < brigthnessValue) {
- x++;
- if(x >= img2.width) return -1;
- }
- return x;
- }
- int getFirstDarkX(int _x, int _y) {
- int x = _x;
- int y = _y;
- color c;
- while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) > brigthnessValue) {
- x++;
- if(x >= img2.width) return -1;
- }
- return x;
- }
- int getNextDarkX(int _x, int _y) {
- int x = _x+1;
- int y = _y;
- color c;
- while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) > brigthnessValue) {
- x++;
- if(x >= img2.width) return img2.width-1;
- }
- return x-1;
- }
- int getNextBrightX(int _x, int _y) {
- int x = _x+1;
- int y = _y;
- color c;
- while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) < brigthnessValue) {
- x++;
- if(x >= img2.width) return img2.width-1;
- }
- return x-1;
- }
- //brightness
- int getFirstBrightY(int _x, int _y) {
- int x = _x;
- int y = _y;
- color c;
- if(y < img2.height) {
- while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) < brigthnessValue) {
- y++;
- if(y >= img2.height) return -1;
- }
- }
- return y;
- }
- int getFirstDarkY(int _x, int _y) {
- int x = _x;
- int y = _y;
- color c;
- if(y < img2.height) {
- while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) > brigthnessValue) {
- y++;
- if(y >= img2.height) return -1;
- }
- }
- return y;
- }
- int getNextDarkY(int _x, int _y) {
- int x = _x;
- int y = _y+1;
- color c;
- if(y < img2.height) {
- while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) > brigthnessValue) {
- y++;
- if(y >= img2.height) return img2.height-1;
- }
- }
- return y-1;
- }
- int getNextBrightY(int _x, int _y) {
- int x = _x;
- int y = _y+1;
- color c;
- if(y < img2.height) {
- while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) < brigthnessValue) {
- y++;
- if(y >= img2.height) return img2.height-1;
- }
- }
- return y-1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement