Advertisement
Guest User

Brightness Asendorf Sort

a guest
Jul 4th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.88 KB | None | 0 0
  1. //Left click to open an new image, right click to save
  2. //press z to change modes, and x to open a new map file
  3.  
  4. PImage img;
  5. PImage mapImg;
  6. PImage displayImg;
  7. PImage original;
  8. int mode = 0;
  9. int hInsets;
  10. int wInsets;
  11. ASDFSort ASDFob;
  12.  
  13. java.awt.Insets insets;
  14.  
  15. void setup() {
  16.  
  17.   frame.setResizable(true);
  18.   img = createImage(100,100,RGB);
  19.   size(img.width,img.height);
  20.   mapImg = createImage(img.width,img.height,RGB);
  21.   mapImg.copy(img, 0,0,img.width,img.height,0,0,mapImg.width,mapImg.height);
  22.   selectInput("Image to process:", "fileSelector");
  23.   noStroke();
  24. }
  25.  
  26. void fileSelector(File selection) {
  27.   if (selection == null) {
  28.     println("Selection cancelled.");
  29.     return;
  30.   }else{
  31.     println(selection.getAbsolutePath());
  32.   }
  33.   img = loadImage(selection.getAbsolutePath());
  34.  
  35.   //Set the frame to fit the selected image.
  36.   insets = frame.getInsets();
  37.   wInsets = insets.left+insets.right;
  38.   hInsets = insets.top+insets.bottom;
  39.   frame.setSize(img.width+wInsets,img.height+hInsets);
  40.   mapImg = createImage(img.width,img.height,RGB);
  41.   mapImg.copy(img, 0,0,img.width,img.height,0,0,mapImg.width,mapImg.height);
  42. }
  43.  
  44. void fileSelector2(File selection) {
  45.   if (selection == null) {
  46.     println("Selection cancelled.");
  47.     return;
  48.   }else{
  49.     println(selection.getAbsolutePath());
  50.   }
  51.   mapImg = loadImage(selection.getAbsolutePath());
  52.   mapImg.resize(img.width,img.height);
  53. }
  54.  
  55. void draw() {
  56.   ASDFob = new ASDFSort(img,mapImg,mode);
  57.   displayImg = ASDFob.asdf();
  58.   image(displayImg,0,0);
  59. }
  60.  
  61. void keyPressed() {
  62.   if (key == 'x')
  63.     selectInput("Select Map Image:", "fileSelector2");
  64.   else if (key == 'z')
  65.     mode = (mode+1)%10;
  66.   else if (key == 'r')
  67.     mapImg.copy(img,0,0,img.width,img.height,0,0,mapImg.width,mapImg.height);
  68. }
  69.  
  70. void mousePressed() {
  71.   //Saves the current image on a left click, selects a new image on a right click
  72.   if (mouseButton == RIGHT) {
  73.     saveImage();
  74.   } else if (mouseButton == LEFT) {
  75.     selectInput("Image to process:", "fileSelector");
  76.   }
  77. }
  78.  
  79. void saveImage() {
  80.   File dir = new File (sketchPath(""));
  81.     String[] list = dir.list();
  82.     if (list == null) {
  83.       println("Folder does not exist or cannot be accessed.");
  84.       return;
  85.     }
  86.     int m = checkFileDir(list.length,list);
  87.     String i = Integer.toString(m);
  88.    
  89.     displayImg.save(savePath(i+".png"));
  90. }
  91.  
  92. int checkFileDir(int i,String[] list) {
  93.   File dir = new File (sketchPath(""));
  94.   for (int j = 0; j < list.length; j++) {
  95.     if ((i+".png").equals(list[j])) {
  96.       i++;
  97.       checkFileDir(i,list);
  98.     }
  99.   }
  100.  
  101.   return i;
  102. }
  103.  
  104. class ASDFSort {
  105.  
  106.   int mode = 1;
  107.  
  108.   //MODE:
  109.   //0 -> black
  110.   //1 -> bright
  111.   //2 -> white
  112.   //b(16777216)
  113.  
  114.   PImage img;
  115.   PImage img2;
  116.   String imgFileName = "3";
  117.   String fileType = "png";
  118.  
  119.   int loops = 1;
  120.   int f;
  121.  
  122.   int blackValue = -16000000;
  123.   int brigthnessValue = 60;
  124.   int whiteValue = -13000000;
  125.  
  126.   int row = 0;
  127.   int column = 0;
  128.  
  129.   boolean saved = false;
  130.  
  131.   ASDFSort (PImage img, PImage img2, int mode) {
  132.     this.img2 = createImage(img2.width,img2.height,RGB);
  133.     this.img2.copy(img2,0,0,img2.width,img2.height,0,0,this.img2.width,this.img2.height);
  134.     this.img = createImage(img2.width,img2.height,RGB);
  135.     this.img.copy(img,0,0,img.width,img.height,0,0,this.img.width,this.img.height);
  136.     this.mode = mode;
  137.   }
  138.  
  139.   PImage asdf() {
  140.     //img.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
  141.     brigthnessValue = int(255*mouseX/float(width-10));
  142.     while(column < img.width-1) {
  143.       img.loadPixels();
  144.       sortColumn();
  145.       column++;
  146.       img.updatePixels();
  147.     }
  148.  
  149.     while(row < img.height-1) {
  150.       img.loadPixels();
  151.       sortRow();
  152.       row++;
  153.       img.updatePixels();
  154.     }
  155.     row = 0;
  156.     column = 0;
  157.     return img;
  158.     /*if(!saved && frameCount >= loops) {
  159.       saveFrame(imgFileName+"_"+mode+".png");
  160.       saved = true;
  161.       println("DONE"+frameCount);
  162.       //System.exit(0);
  163.    
  164.     }*/
  165.   }
  166.  
  167.  
  168.   void sortRow() {
  169.     int x = 0;
  170.     int y = row;
  171.     int xend = 0;
  172.  
  173.     while(xend < img.width-1) {
  174.       switch(mode) {
  175.         case 0:
  176.           x = getFirstDarkX(x, y);
  177.           xend = getNextBrightX(x, y);
  178.           break;
  179.         case 1:
  180.           x = getFirstBrightX(x, y);
  181.           xend = getNextDarkX(x, y);
  182.           break;
  183.         default:
  184.           break;
  185.       }
  186.    
  187.       if(x < 0) break;
  188.      
  189.       int sortLength = xend-x;
  190.    
  191.       HashMap<Float,Integer> unsorted = new HashMap<Float,Integer>();
  192.       float[] unsorted2 = new float[sortLength];
  193.    
  194.       float[] sorted = new float[sortLength];
  195.       for(int i=0; i<sortLength; i++) {
  196.         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)]);
  197.       }
  198.       for(int i=0; i<sortLength; i++) {
  199.         unsorted2[i] = brightness(img.pixels[(x + i + y * img.width)%(img.width*img.height)]);
  200.       }
  201.    
  202.       sorted = sort(unsorted2);
  203.       try {
  204.         for(int i=0; i<sortLength; i++) {
  205.           img.pixels[(x + i + y * img.width)%(img.width*img.height)] = unsorted.get(sorted[i]);      
  206.         }
  207.       } catch (NullPointerException e) {
  208.       e.printStackTrace();
  209.       }
  210.    
  211.       x = xend+1;
  212.     }
  213.   }
  214.  
  215.  
  216.   void sortColumn() {
  217.     int x = column;
  218.     int y = 0;
  219.     int yend = 0;
  220.  
  221.     while(yend < img.height-1) {
  222.       switch(mode) {
  223.         case 0:
  224.           y = getFirstDarkY(x, y);
  225.           yend = getNextBrightY(x, y);
  226.           break;
  227.         case 1:
  228.           y = getFirstBrightY(x, y);
  229.           yend = getNextDarkY(x, y);
  230.           break;
  231.         default:
  232.           break;
  233.       }
  234.    
  235.       if(y < 0) break;
  236.      
  237.       int sortLength = yend-y;
  238.    
  239.       HashMap<Float,Integer> unsorted = new HashMap<Float,Integer>();
  240.       float[] unsorted2 = new float[sortLength];
  241.    
  242.       float[] sorted = new float[sortLength];
  243.    
  244.       for(int i=0; i<sortLength; i++) {
  245.         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)]);
  246.       }
  247.       for(int i=0; i<sortLength; i++) {
  248.         unsorted2[i] = brightness(img.pixels[(x + (y+i) * img.width)%(img.width*img.height)]);
  249.       }
  250.    
  251.       sorted = sort(unsorted2);
  252.      
  253.       for(int i=0; i<sortLength; i++) {
  254.         img.pixels[(x + (y+i) * img.width)%(img.width*img.height)] = unsorted.get(sorted[i]);
  255.       }
  256.    
  257.       y = yend+1;
  258.     }
  259.   }
  260.  
  261.  
  262.  
  263.  
  264.   //brightness
  265.   int getFirstBrightX(int _x, int _y) {
  266.     int x = _x;
  267.     int y = _y;
  268.     color c;
  269.     while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) < brigthnessValue) {
  270.       x++;
  271.       if(x >= img2.width) return -1;
  272.     }
  273.     return x;
  274.   }
  275.   int getFirstDarkX(int _x, int _y) {
  276.     int x = _x;
  277.     int y = _y;
  278.     color c;
  279.     while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) > brigthnessValue) {
  280.       x++;
  281.       if(x >= img2.width) return -1;
  282.     }
  283.     return x;
  284.   }
  285.  
  286.   int getNextDarkX(int _x, int _y) {
  287.     int x = _x+1;
  288.     int y = _y;
  289.     color c;
  290.     while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) > brigthnessValue) {
  291.       x++;
  292.       if(x >= img2.width) return img2.width-1;
  293.     }
  294.     return x-1;
  295.   }
  296.   int getNextBrightX(int _x, int _y) {
  297.     int x = _x+1;
  298.     int y = _y;
  299.     color c;
  300.     while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) < brigthnessValue) {
  301.       x++;
  302.       if(x >= img2.width) return img2.width-1;
  303.     }
  304.     return x-1;
  305.   }
  306.  
  307.  
  308.  
  309.  
  310.  
  311.   //brightness
  312.   int getFirstBrightY(int _x, int _y) {
  313.     int x = _x;
  314.     int y = _y;
  315.     color c;
  316.     if(y < img2.height) {
  317.       while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) < brigthnessValue) {
  318.         y++;
  319.         if(y >= img2.height) return -1;
  320.       }
  321.     }
  322.     return y;
  323.   }
  324.   int getFirstDarkY(int _x, int _y) {
  325.     int x = _x;
  326.     int y = _y;
  327.     color c;
  328.     if(y < img2.height) {
  329.       while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) > brigthnessValue) {
  330.         y++;
  331.         if(y >= img2.height) return -1;
  332.       }
  333.     }
  334.     return y;
  335.   }
  336.  
  337.   int getNextDarkY(int _x, int _y) {
  338.     int x = _x;
  339.     int y = _y+1;
  340.     color c;
  341.     if(y < img2.height) {
  342.       while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) > brigthnessValue) {
  343.         y++;
  344.         if(y >= img2.height) return img2.height-1;
  345.       }
  346.     }
  347.     return y-1;
  348.   }
  349.   int getNextBrightY(int _x, int _y) {
  350.     int x = _x;
  351.     int y = _y+1;
  352.     color c;
  353.     if(y < img2.height) {
  354.       while(brightness(c = img2.pixels[(x + y * img2.width)%(img2.width*img2.height)]) < brigthnessValue) {
  355.         y++;
  356.         if(y >= img2.height) return img2.height-1;
  357.       }
  358.     }
  359.     return y-1;
  360.   }
  361.  
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement