Advertisement
Guest User

Asendorf sort edit

a guest
Jun 2nd, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 28.72 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. //map images don't work with the RGB mode yet
  4.  
  5. PImage img;
  6. PImage mapImg;
  7. PImage displayImg;
  8. PImage original;
  9. int mode = 1;
  10. ASDFSort ASDFob;
  11. ASDFRGB ASDFRGBob;
  12.  
  13.  
  14. void setup() {
  15.   selectInput("Image to process:", "fileSelector");
  16.   frame.setResizable(true);
  17.   /*you'll need a startfile.jpg in your sketch path.
  18.   It doesn't have to be called this it just needs to match
  19.   the FILENAME string. (Sketch -> Add File...)*/
  20.   String FILENAME = "startfile.jpg";
  21.   img = loadImage (FILENAME);
  22.   //Store a copy to reset the image from.
  23.   original = createImage(img.width,img.height,RGB);
  24.   original.copy(img,0,0,img.width,img.height,0,0,original.width,original.height);
  25.   size(img.width,img.height);
  26.   mapImg = createImage(img.width,img.height,RGB);
  27.   mapImg.copy(img, 0,0,img.width,img.height,0,0,mapImg.width,mapImg.height);
  28. }
  29.  
  30. void fileSelector(File selection) {
  31.   if (selection == null) {
  32.     println("Selection cancelled.");
  33.     return;
  34.   }else{
  35.     println(selection.getAbsolutePath());
  36.   }
  37.   img = loadImage(selection.getAbsolutePath());
  38.   //Store a copy to reset the image from.
  39.   original = createImage(img.width,img.height,RGB);
  40.   original.copy(img,0,0,img.width,img.height,0,0,original.width,original.height);
  41.  
  42.   /*Set the frame to fit the selected image.
  43.   The 16 and 38 account for the window borders in the windows 7 theme I'm using
  44.   your borders might be different.*/
  45.   frame.setSize(img.width+16,img.height+38);
  46.   mapImg = createImage(img.width,img.height,RGB);
  47.   mapImg.copy(img, 0,0,img.width,img.height,0,0,mapImg.width,mapImg.height);
  48. }
  49.  
  50. void fileSelector2(File selection) {
  51.   if (selection == null) {
  52.     println("Selection cancelled.");
  53.     return;
  54.   }else{
  55.     println(selection.getAbsolutePath());
  56.   }
  57.   mapImg = loadImage(selection.getAbsolutePath());
  58.   //mapImg.resize(img.width+1,img.height+1);
  59.   mapImg.resize(img.width,img.height);
  60. }
  61.  
  62. void draw() {
  63.   if (mode < 2) {
  64.     ASDFob = new ASDFSort(img,mapImg,mode);
  65.     displayImg = ASDFob.asdf();
  66.     ASDFob = null;
  67.   } else {
  68.     ASDFRGBob = new ASDFRGB(img,mapImg,mode%2);
  69.     displayImg = ASDFRGBob.asdf();
  70.     ASDFRGBob = null;
  71.   }
  72.  
  73.   //Draw the image to the screen
  74.   image(displayImg,0,0);
  75. }
  76.  
  77.  
  78. void keyPressed() {
  79.   if (key == 'x')
  80.     selectInput("Select Map Image:", "fileSelector2");
  81.   else if (key == 'z')
  82.     mode = (mode+1)%4;
  83. }
  84.  
  85.  
  86.  
  87. void mousePressed() {
  88.   //Saves the current image on a left click, selects a new image on a right click
  89.   if (mouseButton == RIGHT) {
  90.     File dir = new File (sketchPath(""));
  91.     String[] list = dir.list();
  92.     if (list == null) {
  93.       println("Folder does not exist or cannot be accessed.");
  94.     }
  95.     int m = list.length;
  96.     String i = Integer.toString(m);
  97.     displayImg.save(savePath(i+".png"));
  98.   } else if (mouseButton == LEFT) {
  99.     selectInput("Image to process:", "fileSelector");
  100.   }
  101. }
  102.  
  103.  
  104. class ASDFRGB {
  105. int mode = 1;
  106.  
  107. //MODE:
  108. //0 -> black
  109. //1 -> bright
  110. //2 -> white
  111. //b(16777216)
  112.  
  113. PImage img;
  114. PImage Red;
  115. PImage Blue;
  116. PImage Green;
  117. PImage img2;
  118. String imgFileName = "3";
  119. String fileType = "png";
  120.  
  121. int loops = 1;
  122. int f;
  123.  
  124. int blackValue = -16000000;
  125. int brigthnessValue = 60;
  126. int whiteValue = -13000000;
  127.  
  128. int row = 0;
  129. int column = 0;
  130.  
  131. boolean saved = false;
  132. ASDFRGB(PImage img2, PImage img,int mode) {
  133.   this.img = createImage(img.width,img.height,RGB);
  134.   this.img.copy(img,0,0,img.width,img.height,0,0,this.img.width,this.img.height);
  135.   this.Red = createImage(img.width,img.height,RGB);
  136.   this.Red.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
  137.   this.Green = createImage(img.width,img.height,RGB);
  138.   this.Green.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
  139.   this.Blue = createImage(img.width,img.height,RGB);
  140.   this.Blue.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
  141.   this.img2 = createImage(img2.width,img2.height,RGB);
  142.   this.img2.copy(img2,0,0,img2.width,img2.height,0,0,this.img2.width,this.img2.height);
  143.   this.mode = mode;
  144. }
  145.  
  146.  
  147.  
  148. PImage asdf() {
  149.   img.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
  150.   Red.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
  151.   Green.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
  152.   Blue.copy(img2, 0,0,img2.width,img2.height,0,0,img.width,img.height);
  153.   brigthnessValue = int(255*mouseX/float(width-10));
  154.   while(column < img.width-1) {
  155.     img.loadPixels();
  156.     sortRedColumn();
  157.     sortGreenColumn();
  158.     sortBlueColumn();
  159.     combineRGBColumn();
  160.     column++;
  161.     img.updatePixels();
  162.   }
  163.  
  164.   while(row < img.height-1) {
  165.     img.loadPixels();
  166.     sortRedRow();
  167.     sortGreenRow();
  168.     sortBlueRow();
  169.     combineRGBRow();
  170.     row++;
  171.     img.updatePixels();
  172.   }
  173.   row = 0;
  174.   column = 0;
  175.   //img.updatePixels();
  176.   return img;
  177.   /*if(!saved && frameCount >= loops) {
  178.     saveFrame(imgFileName+"_"+mode+".png");
  179.     saved = true;
  180.     println("DONE"+frameCount);
  181.     //System.exit(0);
  182.    
  183.   }*/
  184. }
  185.  
  186. void combineRGBRow() {
  187.   int y = row;
  188.   for (int x = 0; x < img.width; x++) {
  189.     img.pixels[(x + y*img.width)%(img.height*img.width)] = color(red(Red.pixels[(x + y*Red.width)%(Red.height*Red.width)]),green(Green.pixels[(x + y*Green.width)%(Green.height*Green.width)]),blue(Blue.pixels[(x + y*Blue.width)%(Blue.height*Blue.width)]));
  190.   }
  191. }
  192. void combineRGBColumn() {
  193.   int x = column;
  194.   for (int y = 0; y < img.height; y++) {
  195.     img.pixels[(y + x*img.height)%(img.height*img.width)] = color(red(Red.pixels[(y + x*Red.height)%(Red.height*Red.width)]),green(Green.pixels[(y + x*Green.height)%(Green.height*Green.width)]),blue(Blue.pixels[(y + x*Blue.height)%(Blue.height*Blue.width)]));
  196.   }
  197. }
  198. void sortRedRow() {
  199.   int x = 0;
  200.   int y = row;
  201.   int xend = 0;
  202.  
  203.   while(xend < img.width-1) {
  204.     switch(mode) {
  205.       case 0:
  206.         x = getFirstDarkRedX(x, y);
  207.         xend = getNextBrightRedX(x, y);
  208.         break;
  209.       case 1:
  210.         x = getFirstBrightRedX(x, y);
  211.         xend = getNextDarkRedX(x, y);
  212.         break;
  213.       /*case 2:
  214.         x = getFirstNotWhiteX(x, y);
  215.         xend = getNextWhiteX(x, y);
  216.         break;*/
  217.       default:
  218.         break;
  219.     }
  220.    
  221.     if(x < 0) break;
  222.    
  223.     int sortLength = xend-x;
  224.    
  225.     float[] unsortedRed = new float[sortLength];
  226.     float[] sortedRed = new float[sortLength];
  227.    
  228.     for(int i=0; i<sortLength; i++) {
  229.       unsortedRed[i] = red(Red.pixels[(x + i + y * Red.width)%(Red.width*Red.height)]);
  230.     }
  231.    
  232.     sortedRed = sort(unsortedRed);
  233.    
  234.     for(int i=0; i<sortLength; i++) {
  235.       Red.pixels[(x + i + y * Red.width)%(Red.width*Red.height)] = color(sortedRed[i],0,0);      
  236.     }
  237.    
  238.     x = xend+1;
  239.   }
  240. }
  241. void sortGreenRow() {
  242.   int x = 0;
  243.   int y = row;
  244.   int xend = 0;
  245.  
  246.   while(xend < img.width-1) {
  247.     switch(mode) {
  248.       case 0:
  249.         x = getFirstDarkGreenX(x, y);
  250.         xend = getNextBrightGreenX(x, y);
  251.         break;
  252.       case 1:
  253.         x = getFirstBrightGreenX(x, y);
  254.         xend = getNextDarkGreenX(x, y);
  255.         break;
  256.       /*case 2:
  257.         x = getFirstNotWhiteX(x, y);
  258.         xend = getNextWhiteX(x, y);
  259.         break;*/
  260.       default:
  261.         break;
  262.     }
  263.    
  264.     if(x < 0) break;
  265.    
  266.     int sortLength = xend-x;
  267.  
  268.     float[] unsortedGreen = new float[sortLength];
  269.     float[] sortedGreen = new float[sortLength];
  270.    
  271.     for(int i=0; i<sortLength; i++) {
  272.       unsortedGreen[i] = green(Green.pixels[(x + i + y * Green.width)%(Green.width*Green.height)]);
  273.     }
  274.    
  275.     sortedGreen = sort(unsortedGreen);
  276.    
  277.     for(int i=0; i<sortLength; i++) {
  278.       Green.pixels[(x + i + y * Green.width)%(Green.width*Green.height)] = color(0,sortedGreen[i],0);      
  279.     }
  280.    
  281.     x = xend+1;
  282.   }
  283. }
  284. void sortBlueRow() {
  285.   int x = 0;
  286.   int y = row;
  287.   int xend = 0;
  288.  
  289.   while(xend < img.width-1) {
  290.     switch(mode) {
  291.       case 0:
  292.         x = getFirstDarkBlueX(x, y);
  293.         xend = getNextBrightBlueX(x, y);
  294.         break;
  295.       case 1:
  296.         x = getFirstBrightBlueX(x, y);
  297.         xend = getNextDarkBlueX(x, y);
  298.         break;
  299.       /*case 2:
  300.         x = getFirstNotWhiteX(x, y);
  301.         xend = getNextWhiteX(x, y);
  302.         break;*/
  303.       default:
  304.         break;
  305.     }
  306.    
  307.     if(x < 0) break;
  308.    
  309.     int sortLength = xend-x;
  310.    
  311.     float[] unsortedBlue = new float[sortLength];
  312.     float[] sortedBlue = new float[sortLength];
  313.    
  314.     for(int i=0; i<sortLength; i++) {
  315.       unsortedBlue[i] = blue(Blue.pixels[(x + i + y * Blue.width)%(Blue.width*Blue.height)]);
  316.     }
  317.    
  318.     sortedBlue = sort(unsortedBlue);
  319.    
  320.     for(int i=0; i<sortLength; i++) {
  321.       Blue.pixels[(x + i + y * Blue.width)%(Blue.width*Blue.height)] = color(0,0,sortedBlue[i]);      
  322.     }
  323.    
  324.     x = xend+1;
  325.   }
  326. }
  327.  
  328. void sortRedColumn() {
  329.   int x = column;
  330.   int y = 0;
  331.   int yend = 0;
  332.  
  333.   while(yend < img.height-1) {
  334.     switch(mode) {
  335.       case 0:
  336.         y = getFirstDarkRedY(x, y);
  337.         yend = getNextBrightRedY(x, y);
  338.         break;
  339.       case 1:
  340.         y = getFirstBrightRedY(x, y);
  341.         yend = getNextDarkRedY(x, y);
  342.         break;
  343.       /*case 2:
  344.         y = getFirstNotWhiteY(x, y);
  345.         yend = getNextWhiteY(x, y);
  346.         break;*/
  347.       default:
  348.         break;
  349.     }
  350.    
  351.     if(y < 0) break;
  352.    
  353.     int sortLength = yend-y;
  354.    
  355.     float[] unsorted = new float[sortLength];
  356.     float[] sorted = new float[sortLength];
  357.    
  358.     for(int i=0; i<sortLength; i++) {
  359.       unsorted[i] = red(Red.pixels[(x + (y+i) * Red.width)%(Red.width*Red.height)]);
  360.     }
  361.    
  362.     sorted = sort(unsorted);
  363.    
  364.     for(int i=0; i<sortLength; i++) {
  365.       Red.pixels[(x + (y+i) * Red.width)%(Red.width*Red.height)] = color(sorted[i],0,0);
  366.     }
  367.    
  368.     y = yend+1;
  369.   }
  370. }
  371. void sortGreenColumn() {
  372.   int x = column;
  373.   int y = 0;
  374.   int yend = 0;
  375.  
  376.   while(yend < img.height-1) {
  377.     switch(mode) {
  378.       case 0:
  379.         y = getFirstDarkGreenY(x, y);
  380.         yend = getNextBrightGreenY(x, y);
  381.         break;
  382.       case 1:
  383.         y = getFirstBrightGreenY(x, y);
  384.         yend = getNextDarkGreenY(x, y);
  385.         break;
  386.       /*case 2:
  387.         y = getFirstNotWhiteY(x, y);
  388.         yend = getNextWhiteY(x, y);
  389.         break;*/
  390.       default:
  391.         break;
  392.     }
  393.    
  394.     if(y < 0) break;
  395.    
  396.     int sortLength = yend-y;
  397.    
  398.     float[] unsorted = new float[sortLength];
  399.     float[] sorted = new float[sortLength];
  400.    
  401.     for(int i=0; i<sortLength; i++) {
  402.       unsorted[i] = green(Green.pixels[(x + (y+i) * Green.width)%(Green.width*Green.height)]);
  403.     }
  404.    
  405.     sorted = sort(unsorted);
  406.    
  407.     for(int i=0; i<sortLength; i++) {
  408.       Green.pixels[(x + (y+i) * Green.width)%(Green.width*Green.height)] = color(0,sorted[i%sorted.length],0);
  409.     }
  410.    
  411.     y = yend+1;
  412.   }
  413. }
  414.  
  415. void sortBlueColumn() {
  416.   int x = column;
  417.   int y = 0;
  418.   int yend = 0;
  419.  
  420.   while(yend < img.height-1) {
  421.     switch(mode) {
  422.       case 0:
  423.         y = getFirstDarkBlueY(x, y);
  424.         yend = getNextBrightBlueY(x, y);
  425.         break;
  426.       case 1:
  427.         y = getFirstBrightBlueY(x, y);
  428.         yend = getNextDarkBlueY(x, y);
  429.         break;
  430.       /*case 2:
  431.         y = getFirstNotWhiteY(x, y);
  432.         yend = getNextWhiteY(x, y);
  433.         break;*/
  434.       default:
  435.         break;
  436.     }
  437.    
  438.     if(y < 0) break;
  439.    
  440.     int sortLength = yend-y;
  441.    
  442.     float[] unsorted = new float[sortLength];
  443.     float[] sorted = new float[sortLength];
  444.    
  445.     for(int i=0; i<sortLength; i++) {
  446.       unsorted[i] = blue(Blue.pixels[(x + (y+i) * Blue.width)%(Blue.width*Blue.height)]);
  447.     }
  448.    
  449.     sorted = sort(unsorted);
  450.    
  451.     for(int i=0; i<sortLength; i++) {
  452.       Blue.pixels[(x + (y+i) * Blue.width)%(Blue.width*Blue.height)] = color(0,0,sorted[i%sorted.length]);
  453.     }
  454.    
  455.     y = yend+1;
  456.   }
  457. }
  458.  
  459. //BLACK
  460. int getFirstNotBlackX(int _x, int _y) {
  461.   int x = _x;
  462.   int y = _y;
  463.   color c;
  464.   while((c = img.pixels[(x + y * img.width)%(img.width*img.height)]) < blackValue) {
  465.     x++;
  466.     if(x >= img.width) return -1;
  467.   }
  468.   return x;
  469. }
  470.  
  471. int getNextBlackX(int _x, int _y) {
  472.   int x = _x+1;
  473.   int y = _y;
  474.   color c;
  475.   while((c = img.pixels[(x + y * img.width)%(img.width*img.height)]) > blackValue) {
  476.     x++;
  477.     if(x >= img.width) return img.width-1;
  478.   }
  479.   return x-1;
  480. }
  481.  
  482. //BRIGHTNESS
  483. int getFirstBrightRedX(int _x, int _y) {
  484.   int x = _x;
  485.   int y = _y;
  486.   float c;
  487.   while(brightness(color(c = red(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  488.     x++;
  489.     if(x >= img.width) return -1;
  490.   }
  491.   return x;
  492. }
  493. int getFirstBrightGreenX(int _x, int _y) {
  494.   int x = _x;
  495.   int y = _y;
  496.   float c;
  497.   while(brightness(color(c = green(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  498.     x++;
  499.     if(x >= img.width) return -1;
  500.   }
  501.   return x;
  502. }
  503. int getFirstBrightBlueX(int _x, int _y) {
  504.   int x = _x;
  505.   int y = _y;
  506.   float c;
  507.   while(brightness(color(c = blue(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  508.     x++;
  509.     if(x >= img.width) return -1;
  510.   }
  511.   return x;
  512. }
  513. int getFirstDarkRedX(int _x, int _y) {
  514.   int x = _x;
  515.   int y = _y;
  516.   float c;
  517.   while(brightness(color(c = red(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  518.     x++;
  519.     if(x >= img.width) return -1;
  520.   }
  521.   return x;
  522. }
  523. int getFirstDarkGreenX(int _x, int _y) {
  524.   int x = _x;
  525.   int y = _y;
  526.   float c;
  527.   while(brightness(color(c = green(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  528.     x++;
  529.     if(x >= img.width) return -1;
  530.   }
  531.   return x;
  532. }
  533. int getFirstDarkBlueX(int _x, int _y) {
  534.   int x = _x;
  535.   int y = _y;
  536.   float c;
  537.   while(brightness(color(c = blue(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  538.     x++;
  539.     if(x >= img.width) return -1;
  540.   }
  541.   return x;
  542. }
  543.  
  544. int getNextDarkRedX(int _x, int _y) {
  545.   int x = _x+1;
  546.   int y = _y;
  547.   float c;
  548.   while(brightness(color(c = red(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  549.     x++;
  550.     if(x >= img.width) return img.width-1;
  551.   }
  552.   return x-1;
  553. }
  554. int getNextDarkGreenX(int _x, int _y) {
  555.   int x = _x+1;
  556.   int y = _y;
  557.   float c;
  558.   while(brightness(color(c = green(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  559.     x++;
  560.     if(x >= img.width) return img.width-1;
  561.   }
  562.   return x-1;
  563. }int getNextDarkBlueX(int _x, int _y) {
  564.   int x = _x+1;
  565.   int y = _y;
  566.   float c;
  567.   while(brightness(color(c = blue(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  568.     x++;
  569.     if(x >= img.width) return img.width-1;
  570.   }
  571.   return x-1;
  572. }
  573. int getNextBrightRedX(int _x, int _y) {
  574.   int x = _x+1;
  575.   int y = _y;
  576.   float c;
  577.   while(brightness(color(c = red(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  578.     x++;
  579.     if(x >= img.width) return img.width-1;
  580.   }
  581.   return x-1;
  582. }
  583. int getNextBrightGreenX(int _x, int _y) {
  584.   int x = _x+1;
  585.   int y = _y;
  586.   float c;
  587.   while(brightness(color(c = green(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  588.     x++;
  589.     if(x >= img.width) return img.width-1;
  590.   }
  591.   return x-1;
  592. }
  593. int getNextBrightBlueX(int _x, int _y) {
  594.   int x = _x+1;
  595.   int y = _y;
  596.   float c;
  597.   while(brightness(color(c = blue(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  598.     x++;
  599.     if(x >= img.width) return img.width-1;
  600.   }
  601.   return x-1;
  602. }
  603.  
  604. //WHITE
  605. int getFirstNotWhiteX(int _x, int _y) {
  606.   int x = _x;
  607.   int y = _y;
  608.   color c;
  609.   while((c = img.pixels[(x + y * img.width)%(img.width*img.height)]) > whiteValue) {
  610.     x++;
  611.     if(x >= img.width) return -1;
  612.   }
  613.   return x;
  614. }
  615.  
  616. int getNextWhiteX(int _x, int _y) {
  617.   int x = _x+1;
  618.   int y = _y;
  619.   color c;
  620.   while((c = img.pixels[(x + y * img.width)%(img.width*img.height)]) < whiteValue) {
  621.     x++;
  622.     if(x >= img.width) return img.width-1;
  623.   }
  624.   return x-1;
  625. }
  626.  
  627.  
  628. //BLACK
  629. int getFirstNotBlackY(int _x, int _y) {
  630.   int x = _x;
  631.   int y = _y;
  632.   color c;
  633.   if(y < img.height) {
  634.     while((c = img.pixels[(x + y * img.width)%(img.width*img.height)]) < blackValue) {
  635.       y++;
  636.       if(y >= img.height) return -1;
  637.     }
  638.   }
  639.   return y;
  640. }
  641.  
  642. int getNextBlackY(int _x, int _y) {
  643.   int x = _x;
  644.   int y = _y+1;
  645.   color c;
  646.   if(y < img.height) {
  647.     while((c = img.pixels[(x + y * img.width)%(img.width*img.height)]) > blackValue) {
  648.       y++;
  649.       if(y >= img.height) return img.height-1;
  650.     }
  651.   }
  652.   return y-1;
  653. }
  654.  
  655. //BRIGHTNESS
  656. int getFirstBrightRedY(int _x, int _y) {
  657.   int x = _x;
  658.   int y = _y;
  659.   float c;
  660.   if(y < img.height) {
  661.     while(brightness(color(c = red(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  662.       y++;
  663.       if(y >= img.height) return -1;
  664.     }
  665.   }
  666.   return y;
  667. }
  668. int getFirstBrightGreenY(int _x, int _y) {
  669.   int x = _x;
  670.   int y = _y;
  671.   float c;
  672.   if(y < img.height) {
  673.     while(brightness(color(c = green(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  674.       y++;
  675.       if(y >= img.height) return -1;
  676.     }
  677.   }
  678.   return y;
  679. }
  680. int getFirstBrightBlueY(int _x, int _y) {
  681.   int x = _x;
  682.   int y = _y;
  683.   float c;
  684.   if(y < img.height) {
  685.     while(brightness(color(c = blue(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  686.       y++;
  687.       if(y >= img.height) return -1;
  688.     }
  689.   }
  690.   return y;
  691. }
  692. int getFirstDarkRedY(int _x, int _y) {
  693.   int x = _x;
  694.   int y = _y;
  695.   float c;
  696.   if(y < img.height) {
  697.     while(brightness(color(c = red(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  698.       y++;
  699.       if(y >= img.height) return -1;
  700.     }
  701.   }
  702.   return y;
  703. }
  704. int getFirstDarkGreenY(int _x, int _y) {
  705.   int x = _x;
  706.   int y = _y;
  707.   float c;
  708.   if(y < img.height) {
  709.     while(brightness(color(c = green(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  710.       y++;
  711.       if(y >= img.height) return -1;
  712.     }
  713.   }
  714.   return y;
  715. }int getFirstDarkBlueY(int _x, int _y) {
  716.   int x = _x;
  717.   int y = _y;
  718.   float c;
  719.   if(y < img.height) {
  720.     while(brightness(color(c = blue(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  721.       y++;
  722.       if(y >= img.height) return -1;
  723.     }
  724.   }
  725.   return y;
  726. }
  727.  
  728. int getNextDarkRedY(int _x, int _y) {
  729.   int x = _x;
  730.   int y = _y+1;
  731.   float c;
  732.   if(y < img.height) {
  733.     while(brightness(color(c = red(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  734.       y++;
  735.       if(y >= img.height) return img.height-1;
  736.     }
  737.   }
  738.   return y-1;
  739. }
  740. int getNextDarkGreenY(int _x, int _y) {
  741.   int x = _x;
  742.   int y = _y+1;
  743.   float c;
  744.   if(y < img.height) {
  745.     while(brightness(color(c = green(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  746.       y++;
  747.       if(y >= img.height) return img.height-1;
  748.     }
  749.   }
  750.   return y-1;
  751. }
  752. int getNextDarkBlueY(int _x, int _y) {
  753.   int x = _x;
  754.   int y = _y+1;
  755.   float c;
  756.   if(y < img.height) {
  757.     while(brightness(color(c = blue(img.pixels[(x + y * img.width)%(img.width*img.height)]))) > brigthnessValue) {
  758.       y++;
  759.       if(y >= img.height) return img.height-1;
  760.     }
  761.   }
  762.   return y-1;
  763. }
  764. int getNextBrightRedY(int _x, int _y) {
  765.   int x = _x;
  766.   int y = _y+1;
  767.   float c;
  768.   if(y < img.height) {
  769.     while(brightness(color(c = red(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  770.       y++;
  771.       if(y >= img.height) return img.height-1;
  772.     }
  773.   }
  774.   return y-1;
  775. }
  776. int getNextBrightGreenY(int _x, int _y) {
  777.   int x = _x;
  778.   int y = _y+1;
  779.   float c;
  780.   if(y < img.height) {
  781.     while(brightness(color(c = green(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  782.       y++;
  783.       if(y >= img.height) return img.height-1;
  784.     }
  785.   }
  786.   return y-1;
  787. }
  788. int getNextBrightBlueY(int _x, int _y) {
  789.   int x = _x;
  790.   int y = _y+1;
  791.   float c;
  792.   if(y < img.height) {
  793.     while(brightness(color(c = blue(img.pixels[(x + y * img.width)%(img.width*img.height)]))) < brigthnessValue) {
  794.       y++;
  795.       if(y >= img.height) return img.height-1;
  796.     }
  797.   }
  798.   return y-1;
  799. }
  800.  
  801. //WHITE
  802. int getFirstNotWhiteY(int _x, int _y) {
  803.   int x = _x;
  804.   int y = _y;
  805.   color c;
  806.   if(y < img.height) {
  807.     while((c = img.pixels[(x + y * img.width)%(img.width*img.height)]) > whiteValue) {
  808.       y++;
  809.       if(y >= img.height) return -1;
  810.     }
  811.   }
  812.   return y;
  813. }
  814.  
  815. int getNextWhiteY(int _x, int _y) {
  816.   int x = _x;
  817.   int y = _y+1;
  818.   color c;
  819.   if(y < img.height) {
  820.     while((c = img.pixels[(x + y * img.width)%(img.width*img.height)]) < whiteValue) {
  821.       y++;
  822.       if(y >= img.height) return img.height-1;
  823.     }
  824.   }
  825.   return y-1;
  826. }
  827. }
  828.  
  829. class ASDFSort {
  830.   int mode = 1;
  831.  
  832. //MODE:
  833. //0 -> black
  834. //1 -> bright
  835. //2 -> white
  836. //b(16777216)
  837.  
  838. PImage asdfimg;
  839. PImage asdfimg2;
  840. String imgFileName = "3";
  841. String fileType = "png";
  842.  
  843. int loops = 1;
  844. int f;
  845.  
  846. int blackValue = -16000000;
  847. int brigthnessValue = 60;
  848. int whiteValue = -13000000;
  849.  
  850. int row = 0;
  851. int column = 0;
  852.  
  853. boolean saved = false;
  854. ASDFSort (PImage asdfimg, PImage asdfimg2, int mode) {
  855.   this.asdfimg = createImage(asdfimg.width,asdfimg.height,RGB);
  856.   this.asdfimg.copy(asdfimg,0,0,asdfimg.width,asdfimg.height,0,0,this.asdfimg.width,this.asdfimg.height);
  857.   this.asdfimg2 = createImage(asdfimg2.width,asdfimg2.height,RGB);
  858.   this.asdfimg2.copy(asdfimg2,0,0,asdfimg2.width,asdfimg2.height,0,0,this.asdfimg2.width,this.asdfimg2.height);
  859.   this.mode = mode;
  860. }
  861.  
  862.  
  863. PImage asdf() {
  864.   brigthnessValue = int(255*mouseX/float(width-10));
  865.   while(column < asdfimg.width-1) {
  866.     asdfimg.loadPixels();
  867.     sortColumn();
  868.     column++;
  869.     asdfimg.updatePixels();
  870.   }
  871.  
  872.   while(row < asdfimg.height-1) {
  873.     asdfimg.loadPixels();
  874.     sortRow();
  875.     row++;
  876.     asdfimg.updatePixels();
  877.   }
  878.   row = 0;
  879.   column = 0;
  880.  
  881.   return asdfimg;
  882.   /*if(!saved && frameCount >= loops) {
  883.     saveFrame(asdfimgFileName+"_"+mode+".png");
  884.     saved = true;
  885.     println("DONE"+frameCount);
  886.     //System.exit(0);
  887.    
  888.   }*/
  889. }
  890.  
  891.  
  892. void sortRow() {
  893.   int x = 0;
  894.   int y = row;
  895.   int xend = 0;
  896.  
  897.   while(xend < asdfimg.width-1) {
  898.     switch(mode) {
  899.       case 0:
  900.         x = getFirstDarkX(x, y);
  901.         xend = getNextBrightX(x, y);
  902.         break;
  903.       case 1:
  904.         x = getFirstBrightX(x, y);
  905.         xend = getNextDarkX(x, y);
  906.         break;
  907.       case 2:
  908.         x = getFirstNotWhiteX(x, y);
  909.         xend = getNextWhiteX(x, y);
  910.         break;
  911.       default:
  912.         break;
  913.     }
  914.    
  915.     if(x < 0) break;
  916.    
  917.     int sortLength = abs(xend-x);
  918.    
  919.     color[] unsorted = new color[sortLength];
  920.     color[] sorted = new color[sortLength];
  921.    
  922.     for(int i=0; i<sortLength; i++) {
  923.       unsorted[i] = asdfimg.pixels[(x + i + y * asdfimg.width)%(asdfimg.width*asdfimg.height)];
  924.     }
  925.    
  926.     sorted = sort(unsorted);
  927.    
  928.     for(int i=0; i<sortLength; i++) {
  929.       asdfimg.pixels[(x + i + y * asdfimg.width)%(asdfimg.width*asdfimg.height)] = sorted[i];      
  930.     }
  931.    
  932.     x = xend+1;
  933.   }
  934. }
  935.  
  936.  
  937. void sortColumn() {
  938.   int x = column;
  939.   int y = 0;
  940.   int yend = 0;
  941.  
  942.   while(yend < asdfimg.height-1) {
  943.     switch(mode) {
  944.       case 0:
  945.         y = getFirstDarkY(x, y);
  946.         yend = getNextBrightY(x, y);
  947.         break;
  948.       case 1:
  949.         y = getFirstBrightY(x, y);
  950.         yend = getNextDarkY(x, y);
  951.         break;
  952.       /*case 2:
  953.         y = getFirstNotWhiteY(x, y);
  954.         yend = getNextWhiteY(x, y);
  955.         break;*/
  956.       default:
  957.         break;
  958.     }
  959.    
  960.     if(y < 0) break;
  961.    
  962.     int sortLength = yend-y;
  963.    
  964.     color[] unsorted = new color[sortLength];
  965.     color[] sorted = new color[sortLength];
  966.    
  967.     for(int i=0; i<sortLength; i++) {
  968.       unsorted[i] = asdfimg.pixels[(x + (y+i) * asdfimg.width)%(asdfimg.width*asdfimg.height)];
  969.     }
  970.    
  971.     sorted = sort(unsorted);
  972.    
  973.     for(int i=0; i<sortLength; i++) {
  974.       asdfimg.pixels[(x + (y+i) * asdfimg.width)%(asdfimg.width*asdfimg.height)] = sorted[i];
  975.     }
  976.    
  977.     y = yend+1;
  978.   }
  979. }
  980.  
  981.  
  982. //BLACK
  983. int getFirstNotBlackX(int _x, int _y) {
  984.   int x = _x;
  985.   int y = _y;
  986.   color c;
  987.   while((c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) < blackValue) {
  988.     x++;
  989.     if(x >= asdfimg2.width) return -1;
  990.   }
  991.   return x;
  992. }
  993.  
  994. int getNextBlackX(int _x, int _y) {
  995.   int x = _x+1;
  996.   int y = _y;
  997.   color c;
  998.   while((c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) > blackValue) {
  999.     x++;
  1000.     if(x >= asdfimg2.width) return asdfimg2.width-1;
  1001.   }
  1002.   return x-1;
  1003. }
  1004.  
  1005. //BRIGHTNESS
  1006. int getFirstBrightX(int _x, int _y) {
  1007.   int x = _x;
  1008.   int y = _y;
  1009.   color c;
  1010.   while(brightness(c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) < brigthnessValue) {
  1011.     x++;
  1012.     if(x >= asdfimg2.width) return -1;
  1013.   }
  1014.   return x;
  1015. }
  1016. int getFirstDarkX(int _x, int _y) {
  1017.   int x = _x;
  1018.   int y = _y;
  1019.   color c;
  1020.   while(brightness(c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) > brigthnessValue) {
  1021.     x++;
  1022.     if(x >= asdfimg2.width) return -1;
  1023.   }
  1024.   return x;
  1025. }
  1026.  
  1027. int getNextDarkX(int _x, int _y) {
  1028.   int x = _x+1;
  1029.   int y = _y;
  1030.   color c;
  1031.   while(brightness(c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) > brigthnessValue) {
  1032.     x++;
  1033.     if(x >= asdfimg2.width) return asdfimg2.width-1;
  1034.   }
  1035.   return x-1;
  1036. }
  1037. int getNextBrightX(int _x, int _y) {
  1038.   int x = _x+1;
  1039.   int y = _y;
  1040.   color c;
  1041.   while(brightness(c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) < brigthnessValue) {
  1042.     x++;
  1043.     if(x >= asdfimg2.width) return asdfimg2.width-1;
  1044.   }
  1045.   return x-1;
  1046. }
  1047.  
  1048. //WHITE
  1049. int getFirstNotWhiteX(int _x, int _y) {
  1050.   int x = _x;
  1051.   int y = _y;
  1052.   color c;
  1053.   while((c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) > whiteValue) {
  1054.     x++;
  1055.     if(x >= asdfimg2.width) return -1;
  1056.   }
  1057.   return x;
  1058. }
  1059.  
  1060. int getNextWhiteX(int _x, int _y) {
  1061.   int x = _x+1;
  1062.   int y = _y;
  1063.   color c;
  1064.   while((c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) < whiteValue) {
  1065.     x++;
  1066.     if(x >= asdfimg2.width) return asdfimg2.width-1;
  1067.   }
  1068.   return x-1;
  1069. }
  1070.  
  1071.  
  1072. //BLACK
  1073. int getFirstNotBlackY(int _x, int _y) {
  1074.   int x = _x;
  1075.   int y = _y;
  1076.   color c;
  1077.   if(y < asdfimg2.height) {
  1078.     while((c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) < blackValue) {
  1079.       y++;
  1080.       if(y >= asdfimg2.height) return -1;
  1081.     }
  1082.   }
  1083.   return y;
  1084. }
  1085.  
  1086. int getNextBlackY(int _x, int _y) {
  1087.   int x = _x;
  1088.   int y = _y+1;
  1089.   color c;
  1090.   if(y < asdfimg2.height) {
  1091.     while((c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) > blackValue) {
  1092.       y++;
  1093.       if(y >= asdfimg2.height) return asdfimg2.height-1;
  1094.     }
  1095.   }
  1096.   return y-1;
  1097. }
  1098.  
  1099. //BRIGHTNESS
  1100. int getFirstBrightY(int _x, int _y) {
  1101.   int x = _x;
  1102.   int y = _y;
  1103.   color c;
  1104.   if(y < asdfimg2.height) {
  1105.     while(brightness(c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) < brigthnessValue) {
  1106.       y++;
  1107.       if(y >= asdfimg2.height) return -1;
  1108.     }
  1109.   }
  1110.   return y;
  1111. }
  1112. int getFirstDarkY(int _x, int _y) {
  1113.   int x = _x;
  1114.   int y = _y;
  1115.   color c;
  1116.   if(y < asdfimg2.height) {
  1117.     while(brightness(c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) > brigthnessValue) {
  1118.       y++;
  1119.       if(y >= asdfimg2.height) return -1;
  1120.     }
  1121.   }
  1122.   return y;
  1123. }
  1124.  
  1125. int getNextDarkY(int _x, int _y) {
  1126.   int x = _x;
  1127.   int y = _y+1;
  1128.   color c;
  1129.   if(y < asdfimg2.height) {
  1130.     while(brightness(c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) > brigthnessValue) {
  1131.       y++;
  1132.       if(y >= asdfimg2.height) return asdfimg2.height-1;
  1133.     }
  1134.   }
  1135.   return y-1;
  1136. }
  1137. int getNextBrightY(int _x, int _y) {
  1138.   int x = _x;
  1139.   int y = _y+1;
  1140.   color c;
  1141.   if(y < asdfimg2.height) {
  1142.     while(brightness(c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) < brigthnessValue) {
  1143.       y++;
  1144.       if(y >= asdfimg2.height) return asdfimg2.height-1;
  1145.     }
  1146.   }
  1147.   return y-1;
  1148. }
  1149.  
  1150. //WHITE
  1151. int getFirstNotWhiteY(int _x, int _y) {
  1152.   int x = _x;
  1153.   int y = _y;
  1154.   color c;
  1155.   if(y < asdfimg2.height) {
  1156.     while((c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) > whiteValue) {
  1157.       y++;
  1158.       if(y >= asdfimg2.height) return -1;
  1159.     }
  1160.   }
  1161.   return y;
  1162. }
  1163.  
  1164. int getNextWhiteY(int _x, int _y) {
  1165.   int x = _x;
  1166.   int y = _y+1;
  1167.   color c;
  1168.   if(y < asdfimg.height) {
  1169.     while((c = asdfimg2.pixels[(x + y * asdfimg2.width)%(asdfimg2.width*asdfimg2.height)]) < whiteValue) {
  1170.       y++;
  1171.       if(y >= asdfimg2.height) return asdfimg2.height-1;
  1172.     }
  1173.   }
  1174.   return y-1;
  1175. }
  1176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement