Advertisement
itblanco

pixelSorter

Dec 20th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. PixelSorter ps;
  2.  
  3.  
  4. void setup() {
  5.   size(400, 400);
  6.  
  7.   ps = new PixelSorter(loadImage("caravaggio.jpg"), 2, 120);
  8.   //surface.setSize(ps.img.width, ps.img.height);
  9.   ps.sortPartialColumns();
  10.   ps.img.save("img00.jpg");
  11.   exit();
  12. }
  13.  
  14. void draw() {
  15. }
  16.  
  17. public class PixelSorter {
  18.   PImage img;
  19.   int mode; // mode 0 = sort by hue; mode 1 = sort by saturation; mode 2 = sort by brightness;
  20.   float threshold;  
  21.  
  22.   PixelSorter(PImage _img, int _mode, float _threshold) {
  23.     img = _img;
  24.     mode = _mode;
  25.     threshold = _threshold;
  26.   }
  27.  
  28.   void sortColumns() {
  29.     img.loadPixels();
  30.     for (int x = 0; x < img.width; x++) {
  31.  
  32.       for (int y = 0; y < img.height; y++) {
  33.         int currentPixelIndex = x + y * img.width;
  34.         int brightestPixel = 0;
  35.         float record = -1;
  36.  
  37.         for (int y1 = y; y1 < img.height; y1++) {
  38.           int i = x + y1 * img.width;
  39.           float h = hue(img.pixels[i]);
  40.           float s = saturation(img.pixels[i]);
  41.           float b = brightness(img.pixels[i]);
  42.           float pixelValue;
  43.  
  44.           switch(mode) {
  45.           case 0:
  46.             pixelValue = h;
  47.             break;
  48.           case 1:
  49.             pixelValue = s;
  50.             break;
  51.           case 2:
  52.             pixelValue = b;
  53.             break;
  54.           default:
  55.             pixelValue = (h*0.45)+(s*0.15)+(b*0.40);
  56.             break;
  57.           }
  58.  
  59.           if (record < pixelValue) {
  60.             record = pixelValue;
  61.             brightestPixel = i;
  62.           }
  63.         }
  64.         color temp = img.pixels[currentPixelIndex];
  65.         img.pixels[currentPixelIndex] = img.pixels[brightestPixel];
  66.         img.pixels[brightestPixel] = temp;
  67.       }
  68.     }
  69.     img.updatePixels();
  70.   }
  71.  
  72.   void sortPartialColumns() {
  73.     img.loadPixels();
  74.     for (int x = 0; x < img.width; x++) {
  75.       for (int y = 0; y < img.height; y++) {
  76.         int ystart = y;
  77.         int yend = y;
  78.         int currentPixelIndex = x + y * img.width;
  79.         float h = hue(img.pixels[currentPixelIndex]);
  80.         float s = saturation(img.pixels[currentPixelIndex]);
  81.         float b = brightness(img.pixels[currentPixelIndex]);
  82.         float pixelValue;
  83.  
  84.         switch(mode) {
  85.         case 0:
  86.           pixelValue = h;
  87.           break;
  88.         case 1:
  89.           pixelValue = s;
  90.           break;
  91.         case 2:
  92.           pixelValue = b;
  93.           break;
  94.         default:
  95.           pixelValue = (h*0.45)+(s*0.15)+(b*0.40);
  96.           break;
  97.         }
  98.  
  99.         if (pixelValue > threshold) {
  100.           while (yend < img.height) {
  101.             yend++;            
  102.             if (pixelValue < threshold) break;
  103.           }
  104.          
  105.           int sortingDist = yend - ystart;
  106.           color[] sorted = new color[sortingDist];
  107.           color[] unsorted = new color[sortingDist];
  108.  
  109.           for (int i = 0; i < sortingDist; i++) {
  110.             unsorted[i] = img.pixels[x + (ystart+i) * img.width];
  111.           }
  112.  
  113.           sorted = sortColors(unsorted);
  114.           for (int i = 0; i < sortingDist; i++) {
  115.             img.pixels[x + (ystart+i) * img.width] = sorted[i];
  116.           }
  117.           y = yend;
  118.         }
  119.       }
  120.     }
  121.     img.updatePixels();
  122.   }
  123.  
  124.   color[] sortColors(color[] _p) {
  125.     color[] p = _p.clone();
  126.  
  127.     for (int i = 0; i < p.length; i++) {      
  128.       int highestPixel = 0;
  129.       float record = -1;
  130.       for (int j = i; j < p.length; j++) {
  131.         float h = hue(p[i]);
  132.         float s = saturation(p[i]);
  133.         float b = brightness(p[i]);
  134.         float pixelValue;
  135.  
  136.         switch(mode) {
  137.         case 0:
  138.           pixelValue = h;
  139.           break;
  140.         case 1:
  141.           pixelValue = s;
  142.           break;
  143.         case 2:
  144.           pixelValue = b;
  145.           break;
  146.         default:
  147.           pixelValue = (h*0.45)+(s*0.15)+(b*0.40);
  148.           break;
  149.         }
  150.  
  151.         if (record < pixelValue) {
  152.           record = pixelValue;
  153.           highestPixel = j;
  154.         }
  155.       }
  156.  
  157.       color temp = p[i];
  158.       p[i] = p[highestPixel];
  159.       p[highestPixel] = temp;
  160.     }
  161.    
  162.     return p;
  163.   }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement