# ASDF Pixel Sort with different sections (color threshold)

**URL:** <https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416>\
**Category:** Coding Questions\
**Created:** [December 7, 2023, 8:37pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416 "2023-12-07T20:37:00Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 7, 2023, 8:37pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/1 "2023-12-07T20:37:01Z")

</div>

Hi!  
I am playing with Kim Asendorf`s code for pixel sorting, trying to use different values of sorting in different sections of the picture. That is way I am adding this to his code:

> if (y\<300){  
> int whiteValue = -12345678;  
> } else if (y\<500){  
> int whiteValue = -15345678;  
> } else {  
> int whiteValue = -10345678;  
> }

But is not working. Can you please tell me why? Thanks!

```auto

/ ********

 ASDF Pixel Sort
 Kim Asendorf | 2010 | kimasendorf.com
 
 Sorting modes
 0 = white
 1 = black
 2 = bright
 3 = dark
 
 */

int mode = 0;

// image path is relative to sketch directory
PImage img;
String imgFileName = "ciudad";
String fileType = "png";

int loops = 1;

// threshold values to determine sorting start and end pixels
// using the absolute rgb value
// r*g*b = 255*255*255 = 16581375
// 0 = white
// -16581375 = black
// sort all pixels whiter than the threshold
if (y<300){
int whiteValue = -12345678;
} else if (y<500){
int whiteValue = -15345678;
} else {
int whiteValue = -10345678;
}
// sort all pixels blacker than the threshold
int blackValue = -3456789;
// using the brightness value
// sort all pixels brighter than the threshold
int brightValue = 127;
// sort all pixels darker than the threshold
int darkValue = 223;

int row = 0;
int column = 0;

boolean saved = false;

void setup() {
  img = loadImage(imgFileName + "." + fileType);
  
  // use only numbers (not variables) for the size() command, Processing 3
  size(1, 1);
  
  // allow resize and update surface to image dimensions
  surface.setResizable(true);
  surface.setSize(img.width, img.height);
  
  // load image onto surface - scale to the available width,height for display
  image(img, 0, 0, width, height);
}

void draw() {
  
  if (frameCount <= loops) {
    
    // loop through columns
    println("Sorting Columns");
    while (column < img.width-1) {
      img.loadPixels();
      sortColumn();
      column++;
      img.updatePixels();
    }
    
    // loop through rows
    println("Sorting Rows");
    while (row < img.height-1) {
      img.loadPixels();
      sortRow();
      row++;
      img.updatePixels();
    }
  }
  
  // load updated image onto surface and scale to fit the display width and height
  image(img, 0, 0, width, height);
    
  if (!saved && frameCount >= loops) {
    // save img
    img.save(imgFileName + "_" + mode + ".png");
  
    saved = true;
    println("Saved frame " + frameCount);
    
    // exiting here can interrupt file save, wait for user to trigger exit
    println("Click or press any key to exit...");
  }
}

void keyPressed() {
  if (saved) {
    System.exit(0);
  }
}

void mouseClicked() {
  if (saved) {
    System.exit(0);
  }
}

void sortRow() {
  // current row
  int y = row;
  
  // where to start sorting
  int x = 0;
  
  // where to stop sorting
  int xEnd = 0;
  
  while (xEnd < img.width-1) {
    switch (mode) {
      case 0:
        x = getFirstNoneWhiteX(x, y);
        xEnd = getNextWhiteX(x, y);
        break;
      case 1:
        x = getFirstNoneBlackX(x, y);
        xEnd = getNextBlackX(x, y);
        break;
      case 2:
        x = getFirstNoneBrightX(x, y);
        xEnd = getNextBrightX(x, y);
        break;
      case 3:
        x = getFirstNoneDarkX(x, y);
        xEnd = getNextDarkX(x, y);
        break;
      default:
        break;
    }
    
    if (x < 0) break;
    
    int sortingLength = xEnd-x;
    
    color[] unsorted = new color[sortingLength];
    color[] sorted = new color[sortingLength];
    
    for (int i = 0; i < sortingLength; i++) {
      unsorted[i] = img.pixels[x + i + y * img.width];
    }
    
    sorted = sort(unsorted);
    
    for (int i = 0; i < sortingLength; i++) {
      img.pixels[x + i + y * img.width] = sorted[i];      
    }
    
    x = xEnd+1;
  }
}

void sortColumn() {
  // current column
  int x = column;
  
  // where to start sorting
  int y = 0;
  
  // where to stop sorting
  int yEnd = 0;
  
  while (yEnd < img.height-1) {
    switch (mode) {
      case 0:
        y = getFirstNoneWhiteY(x, y);
        yEnd = getNextWhiteY(x, y);
        break;
      case 1:
        y = getFirstNoneBlackY(x, y);
        yEnd = getNextBlackY(x, y);
        break;
      case 2:
        y = getFirstNoneBrightY(x, y);
        yEnd = getNextBrightY(x, y);
        break;
      case 3:
        y = getFirstNoneDarkY(x, y);
        yEnd = getNextDarkY(x, y);
        break;
      default:
        break;
    }
    
    if (y < 0) break;
    
    int sortingLength = yEnd-y;
    
    color[] unsorted = new color[sortingLength];
    color[] sorted = new color[sortingLength];
    
    for (int i = 0; i < sortingLength; i++) {
      unsorted[i] = img.pixels[x + (y+i) * img.width];
    }
    
    sorted = sort(unsorted);
    
    for (int i = 0; i < sortingLength; i++) {
      img.pixels[x + (y+i) * img.width] = sorted[i];
    }
    
    y = yEnd+1;
  }
}

// white x
int getFirstNoneWhiteX(int x, int y) {
  while (img.pixels[x + y * img.width] < whiteValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextWhiteX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] > whiteValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// black x
int getFirstNoneBlackX(int x, int y) {
  while (img.pixels[x + y * img.width] > blackValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBlackX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] < blackValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// bright x
int getFirstNoneBrightX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) < brightValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBrightX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) > brightValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// dark x
int getFirstNoneDarkX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) > darkValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextDarkX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) < darkValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// white y
int getFirstNoneWhiteY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < whiteValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextWhiteY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > whiteValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// black y
int getFirstNoneBlackY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > blackValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBlackY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < blackValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// bright y
int getFirstNoneBrightY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < brightValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBrightY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > brightValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// dark y
int getFirstNoneDarkY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > darkValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextDarkY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < darkValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 7, 2023, 10:31pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/2 "2023-12-07T22:31:44Z")

</div>

you code doesn’t run.

The if-clause you posted must be inside a function (in a place where `whiteValue` is used; function getFirstNoneWhiteX())

here is a _stripped down_ version, that sorts the columns in a rough way

(it’s without `sortRow()`, and also sortColumn is simplified (no while-loop, for mode==0 other handling of y and yEnd…))

(`whiteValue` is not in use yet)

This version is to get you started.

adjust your file name please

Chrisir

```auto

// please set your image file name !!!

/ ********
 
 ASDF Pixel Sort
 Kim Asendorf | 2010 | kimasendorf.com
 
 Sorting modes
 0 = white
 1 = black
 2 = bright
 3 = dark
 
 */

final int whiteValue = 66; // ??? test

final int mode = 0;

// image path is relative to sketch directory
final String imgFileName = "IMG_1502";
final String fileType = "PNG";

// ---------------------------------------------------------------------------------------------------

PImage img;

int loops = 1;

// threshold values to determine sorting start and end pixels
// using the absolute rgb value
// r*g*b = 255*255*255 = 16581375
// 0 = white
// -16581375 = black
// sort all pixels whiter than the threshold

// sort all pixels blacker than the threshold
int blackValue = -3456789;
// using the brightness value
// sort all pixels brighter than the threshold
int brightValue = 127;
// sort all pixels darker than the threshold
int darkValue = 223;

int row = 0;
int column = 0;

boolean saved = false;

// ------------------------------------------------------------------------------------------------------

void setup() {
  img = loadImage(imgFileName + "." + fileType);

  // use only numbers (not variables) for the size() command, Processing 3
  size(1, 1);

  // allow resize and update surface to image dimensions
  surface.setResizable(true);
  // surface.setSize(img.width, img.height);
  surface.setSize(800, 800);

  // load image onto surface - scale to the available width,height for display
  image(img, 0, 0, width, height);

  println("wait");
}

void draw() {

  if (frameCount <= loops) {

    println("step 1");

    // loop through columns
    println("Sorting Columns");
    while (column < img.width-1) {
      img.loadPixels();
      sortColumn();
      column++;
      img.updatePixels();
    }

    // loop through rows
    /*
    println("Sorting Rows");
     while (row < img.height-1) {
     img.loadPixels();
     sortRow();
     row++;
     img.updatePixels();
     }
     */
    println("done\n");
  }

  // -------------------------------------------------------------------

  // load updated image onto surface and scale to fit the display width and height
  image(img, 0, 0, width, height);

  if (!saved && frameCount >= loops) {
    // save img
    img.save(imgFileName + "_" + mode + ".png");

    saved = true;
    println("Saved frame " + frameCount);

    // exiting here can interrupt file save, wait for user to trigger exit
    println("Click or press any key to exit...");
  }
}// draw

// --------------------------------------------------------------------------------------------------------------------

void keyPressed() {
  if (saved) {
    System.exit(0);
  }
}

void mouseClicked() {
  if (saved) {
    System.exit(0);
  }
}

void sortRow() {
  // current row
  int y = row;

  // where to start sorting
  int x = 0;

  // where to stop sorting
  int xEnd = 0;

  while (xEnd < img.width-1) {
    switch (mode) {
    case 0:
      x = getFirstNoneWhiteX(x, y);
      xEnd = getNextWhiteX(x, y);
      break;
    case 1:
      x = getFirstNoneBlackX(x, y);
      xEnd = getNextBlackX(x, y);
      break;
    case 2:
      x = getFirstNoneBrightX(x, y);
      xEnd = getNextBrightX(x, y);
      break;
    case 3:
      x = getFirstNoneDarkX(x, y);
      xEnd = getNextDarkX(x, y);
      break;
    default:
      break;
    }

    if (x < 0) break;

    int sortingLength = xEnd-x;

    color[] unsorted = new color[sortingLength];
    color[] sorted = new color[sortingLength];

    for (int i = 0; i < sortingLength; i++) {
      unsorted[i] = img.pixels[x + i + y * img.width];
    }

    sorted = sort(unsorted);

    for (int i = 0; i < sortingLength; i++) {
      img.pixels[x + i + y * img.width] = sorted[i];
    }

    x = xEnd+1;
  }
}

void sortColumn() {
  // current column
  int x = column;

  // where to start sorting
  int y = 0;

  // where to stop sorting
  int yEnd = 0;

  // while (yEnd < img.height-1) {
  switch (mode) {
  case 0:
    y = 0; // getFirstNoneWhiteY(x, y); // ???????
    yEnd = img.height-33; // getNextWhiteY(x, y);
    break;
  case 1:
    y = getFirstNoneBlackY(x, y);
    yEnd = getNextBlackY(x, y);
    break;
  case 2:
    y = getFirstNoneBrightY(x, y);
    yEnd = getNextBrightY(x, y);
    break;
  case 3:
    y = getFirstNoneDarkY(x, y);
    yEnd = getNextDarkY(x, y);
    break;
  default:
    break;
  }

  /*
    if (y < 0)
   break;
   */

  int sortingLength = yEnd-y;

  color[] unsorted = new color[sortingLength];
  color[] sorted = new color[sortingLength];

  // READ
  for (int i = 0; i < sortingLength; i++) {
    unsorted[i] = img.pixels[x + (y+i) * img.width];
  }

  // SORT
  sorted = sort(unsorted);

  // WRITE BACK
  for (int i = 0; i < sortingLength; i++) {
    img.pixels[x + (y+i) * img.width] = sorted[i];
  }

  // y = yEnd+1;
  //}
}

// white x
int getFirstNoneWhiteX(int x, int y) {
  while (img.pixels[x + y * img.width] < whiteValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextWhiteX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] > whiteValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// black x
int getFirstNoneBlackX(int x, int y) {
  while (img.pixels[x + y * img.width] > blackValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBlackX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] < blackValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// bright x
int getFirstNoneBrightX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) < brightValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBrightX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) > brightValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// dark x
int getFirstNoneDarkX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) > darkValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextDarkX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) < darkValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// white y
int getFirstNoneWhiteY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < whiteValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextWhiteY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > whiteValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// black y
int getFirstNoneBlackY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > blackValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBlackY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < blackValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// bright y
int getFirstNoneBrightY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < brightValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBrightY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > brightValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// dark y
int getFirstNoneDarkY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > darkValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextDarkY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < darkValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

```

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 8, 2023, 12:26am UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/3 "2023-12-08T00:26:46Z")

</div>

thank you so much. I can open my picture here and I see if I change to final int mode = 1 I can get different results changing int blackvalue. But I have no idea how to delimit different sections, changing final int whiteValue = 66; I don’t see any effect How I can start?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 8, 2023, 8:15am UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/4 "2023-12-08T08:15:45Z")

</div>

> [@Chrisir](#):
>
> The if-clause you posted must be inside a function (in a place where `whiteValue` is used; function getFirstNoneWhiteX())

I explained already, that’s your answer

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 8, 2023, 1:03pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/5 "2023-12-08T13:03:47Z")

</div>

like this? But I don’t see those sections so clearly…

```auto
// please set your image file name !!!

/ ********
 
 ASDF Pixel Sort
 Kim Asendorf | 2010 | kimasendorf.com
 
 Sorting modes
 0 = white
 1 = black
 2 = bright
 3 = dark
 
 */

final int whiteValue = 66; // ??? test

final int mode = 1;

// image path is relative to sketch directory
final String imgFileName = "ciudad";
final String fileType = "PNG";

// ---------------------------------------------------------------------------------------------------

PImage img;

int loops = 1;

// threshold values to determine sorting start and end pixels
// using the absolute rgb value
// r*g*b = 255*255*255 = 16581375
// 0 = white
// -16581375 = black
// sort all pixels whiter than the threshold

void whiteSections(int whiteValue, int y){
if (y<100){
 whiteValue = -12345678;
} else if (y<500){
 whiteValue = -15345678;
} else {
 whiteValue = -10345678;
}
}

// sort all pixels blacker than the threshold
int blackValue = -156789;
// using the brightness value
// sort all pixels brighter than the threshold
int brightValue = 127;
// sort all pixels darker than the threshold
int darkValue = 223;

int row = 0;
int column = 0;

boolean saved = false;

// ------------------------------------------------------------------------------------------------------

void setup() {
  img = loadImage("ciudad.png") ;

  // use only numbers (not variables) for the size() command, Processing 3
  size(1, 1);

  // allow resize and update surface to image dimensions
  surface.setResizable(true);
  // surface.setSize(img.width, img.height);
  surface.setSize(800, 800);

  // load image onto surface - scale to the available width,height for display
  image(img, 0, 0, width, height);

  println("wait");
}

void draw() {

  if (frameCount <= loops) {

    println("step 1");

    // loop through columns
    println("Sorting Columns");
    while (column < img.width-1) {
      img.loadPixels();
      sortColumn();
      column++;
      img.updatePixels();
    }

    // loop through rows
    /*
    println("Sorting Rows");
     while (row < img.height-1) {
     img.loadPixels();
     sortRow();
     row++;
     img.updatePixels();
     }
     */
    println("done\n");
  }

  // -------------------------------------------------------------------

  // load updated image onto surface and scale to fit the display width and height
  image(img, 0, 0, width, height);

  if (!saved && frameCount >= loops) {
    // save img
    img.save(imgFileName + "_" + mode + ".png");

    saved = true;
    println("Saved frame " + frameCount);

    // exiting here can interrupt file save, wait for user to trigger exit
    println("Click or press any key to exit...");
  }
}// draw

// --------------------------------------------------------------------------------------------------------------------

void keyPressed() {
  if (saved) {
    System.exit(0);
  }
}

void mouseClicked() {
  if (saved) {
    System.exit(0);
  }
}

void sortRow() {
  // current row
  int y = row;

  // where to start sorting
  int x = 0;

  // where to stop sorting
  int xEnd = 0;

  while (xEnd < img.width-1) {
    switch (mode) {
    case 0:
      x = getFirstNoneWhiteX(x, y);
      xEnd = getNextWhiteX(x, y);
      break;
    case 1:
      x = getFirstNoneBlackX(x, y);
      xEnd = getNextBlackX(x, y);
      break;
    case 2:
      x = getFirstNoneBrightX(x, y);
      xEnd = getNextBrightX(x, y);
      break;
    case 3:
      x = getFirstNoneDarkX(x, y);
      xEnd = getNextDarkX(x, y);
      break;
    default:
      break;
    }

    if (x < 0) break;

    int sortingLength = xEnd-x;

    color[] unsorted = new color[sortingLength];
    color[] sorted = new color[sortingLength];

    for (int i = 0; i < sortingLength; i++) {
      unsorted[i] = img.pixels[x + i + y * img.width];
    }

    sorted = sort(unsorted);

    for (int i = 0; i < sortingLength; i++) {
      img.pixels[x + i + y * img.width] = sorted[i];
    }

    x = xEnd+1;
  }
}

void sortColumn() {
  // current column
  int x = column;

  // where to start sorting
  int y = 0;

  // where to stop sorting
  int yEnd = 0;

  // while (yEnd < img.height-1) {
  switch (mode) {
  case 0:
    y = 0; // getFirstNoneWhiteY(x, y); // ???????
    yEnd = img.height-33; // getNextWhiteY(x, y);
    break;
  case 1:
    y = getFirstNoneBlackY(x, y);
    yEnd = getNextBlackY(x, y);
    break;
  case 2:
    y = getFirstNoneBrightY(x, y);
    yEnd = getNextBrightY(x, y);
    break;
  case 3:
    y = getFirstNoneDarkY(x, y);
    yEnd = getNextDarkY(x, y);
    break;
  default:
    break;
  }

  /*
    if (y < 0)
   break;
   */

  int sortingLength = yEnd-y;

  color[] unsorted = new color[sortingLength];
  color[] sorted = new color[sortingLength];

  // READ
  for (int i = 0; i < sortingLength; i++) {
    unsorted[i] = img.pixels[x + (y+i) * img.width];
  }

  // SORT
  sorted = sort(unsorted);

  // WRITE BACK
  for (int i = 0; i < sortingLength; i++) {
    img.pixels[x + (y+i) * img.width] = sorted[i];
  }

  // y = yEnd+1;
  //}
}

// white x
int getFirstNoneWhiteX(int x, int y) {
  while (img.pixels[x + y * img.width] < whiteValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextWhiteX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] > whiteValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// black x
int getFirstNoneBlackX(int x, int y) {
  while (img.pixels[x + y * img.width] > blackValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBlackX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] < blackValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// bright x
int getFirstNoneBrightX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) < brightValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBrightX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) > brightValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// dark x
int getFirstNoneDarkX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) > darkValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextDarkX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) < darkValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// white y
int getFirstNoneWhiteY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < whiteValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextWhiteY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > whiteValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// black y
int getFirstNoneBlackY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > blackValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBlackY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < blackValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// bright y
int getFirstNoneBrightY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < brightValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBrightY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > brightValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// dark y
int getFirstNoneDarkY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > darkValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextDarkY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < darkValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 8, 2023, 1:35pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/6 "2023-12-08T13:35:22Z")

</div>

> [@humano](#):
>
> `whiteSections(int whiteValue`

You must not use whiteValue  
as a Parameter, it’s a global  
variable.

Does it work?

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 8, 2023, 3:34pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/7 "2023-12-08T15:34:39Z")

</div>

but I can not remove it because it has to be in the function somehow, hasn’t it?

```auto
void whiteSections(int y){
if (y<100){
 whiteValue = -12345678;
} else if (y<500){
 whiteValue = -15345678;
} else{
 whiteValue = -10345678;
}
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 8, 2023, 3:57pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/8 "2023-12-08T15:57:39Z")

</div>

this example could work

please try

it makes use of your function

where are your 3 values like -12345678 from?

```auto

// please set your image file name !!!

/ ********
 
 ASDF Pixel Sort
 Kim Asendorf | 2010 | kimasendorf.com
 
 Sorting modes
 0 = white
 1 = black
 2 = bright
 3 = dark
 
 */

final int mode = 1;

// image path is relative to sketch directory
final String imgFileName = "ciudad";
final String fileType = "PNG";

// ---------------------------------------------------------------------------------------------------

int whiteValue = 66; // ??? test

PImage img;

int loops = 1;

// threshold values to determine sorting start and end pixels
// using the absolute rgb value
// r*g*b = 255*255*255 = 16581375
// 0 = white
// -16581375 = black
// sort all pixels whiter than the threshold

void whiteSections(int y) {
  if (y<100) {
    whiteValue = -12345678;
  } else if (y<500) {
    whiteValue = -15345678;
  } else {
    whiteValue = -10345678;
  }
}

// sort all pixels blacker than the threshold
int blackValue = -156789;
// using the brightness value
// sort all pixels brighter than the threshold
int brightValue = 127;
// sort all pixels darker than the threshold
int darkValue = 223;

int row = 0;
int column = 0;

boolean saved = false;

// ------------------------------------------------------------------------------------------------------

void setup() {
  img = loadImage("ciudad.png") ;

  // use only numbers (not variables) for the size() command, Processing 3
  size(1, 1);

  // allow resize and update surface to image dimensions
  surface.setResizable(true);
  // surface.setSize(img.width, img.height);
  surface.setSize(800, 800);

  // load image onto surface - scale to the available width,height for display
  image(img, 0, 0, width, height);

  println("wait");
}

void draw() {

  if (frameCount <= loops) {

    println("step 1");

    // loop through columns
    println("Sorting Columns");
    while (column < img.width-1) {
      img.loadPixels();
      sortColumn();
      column++;
      img.updatePixels();
    }

    // loop through rows
    /*
    println("Sorting Rows");
     while (row < img.height-1) {
     img.loadPixels();
     sortRow();
     row++;
     img.updatePixels();
     }
     */
    println("done\n");
  }

  // -------------------------------------------------------------------

  // load updated image onto surface and scale to fit the display width and height
  image(img, 0, 0, width, height);

  if (!saved && frameCount >= loops) {
    // save img
    img.save(imgFileName + "_" + mode + ".png");

    saved = true;
    println("Saved frame " + frameCount);

    // exiting here can interrupt file save, wait for user to trigger exit
    println("Click or press any key to exit...");
  }
}// draw

// --------------------------------------------------------------------------------------------------------------------

void keyPressed() {
  if (saved) {
    System.exit(0);
  }
}

void mouseClicked() {
  if (saved) {
    System.exit(0);
  }
}

void sortRow() {
  // current row
  int y = row;

  // where to start sorting
  int x = 0;

  // where to stop sorting
  int xEnd = 0;

  while (xEnd < img.width-1) {
    switch (mode) {
    case 0:
      x = getFirstNoneWhiteX(x, y);
      xEnd = getNextWhiteX(x, y);
      break;
    case 1:
      x = getFirstNoneBlackX(x, y);
      xEnd = getNextBlackX(x, y);
      break;
    case 2:
      x = getFirstNoneBrightX(x, y);
      xEnd = getNextBrightX(x, y);
      break;
    case 3:
      x = getFirstNoneDarkX(x, y);
      xEnd = getNextDarkX(x, y);
      break;
    default:
      break;
    }

    if (x < 0) break;

    int sortingLength = xEnd-x;

    color[] unsorted = new color[sortingLength];
    color[] sorted = new color[sortingLength];

    for (int i = 0; i < sortingLength; i++) {
      unsorted[i] = img.pixels[x + i + y * img.width];
    }

    sorted = sort(unsorted);

    for (int i = 0; i < sortingLength; i++) {
      img.pixels[x + i + y * img.width] = sorted[i];
    }

    x = xEnd+1;
  }
}

void sortColumn() {
  // current column
  int x = column;

  // where to start sorting
  int y = 0;

  // where to stop sorting
  int yEnd = 0;

  // while (yEnd < img.height-1) {
  switch (mode) {
  case 0:
    y = 0; // getFirstNoneWhiteY(x, y); // ???????
    yEnd = img.height-33; // getNextWhiteY(x, y);
    break;
  case 1:
    y = getFirstNoneBlackY(x, y);
    yEnd = getNextBlackY(x, y);
    break;
  case 2:
    y = getFirstNoneBrightY(x, y);
    yEnd = getNextBrightY(x, y);
    break;
  case 3:
    y = getFirstNoneDarkY(x, y);
    yEnd = getNextDarkY(x, y);
    break;
  default:
    break;
  }

  /*
    if (y < 0)
   break;
   */

  int sortingLength = yEnd-y;

  color[] unsorted = new color[sortingLength];
  color[] sorted = new color[sortingLength];

  // READ
  for (int i = 0; i < sortingLength; i++) {
    unsorted[i] = img.pixels[x + (y+i) * img.width];
  }

  // SORT
  sorted = sort(unsorted);

  // WRITE BACK
  for (int i = 0; i < sortingLength; i++) {
    img.pixels[x + (y+i) * img.width] = sorted[i];
  }

  // y = yEnd+1;
  //}
}

// white x
int getFirstNoneWhiteX(int x, int y) {
  whiteSections(y);
  while (img.pixels[x + y * img.width] < whiteValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextWhiteX(int x, int y) {
  x++;
  whiteSections(y);
  while (img.pixels[x + y * img.width] > whiteValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// black x
int getFirstNoneBlackX(int x, int y) {
  while (img.pixels[x + y * img.width] > blackValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBlackX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] < blackValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// bright x
int getFirstNoneBrightX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) < brightValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBrightX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) > brightValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// dark x
int getFirstNoneDarkX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) > darkValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextDarkX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) < darkValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// white y
int getFirstNoneWhiteY(int x, int y) {
  if (y < img.height) {
    whiteSections(y);
    while (img.pixels[x + y * img.width] < whiteValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextWhiteY(int x, int y) {
  y++;
  whiteSections(y);
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > whiteValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// black y
int getFirstNoneBlackY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > blackValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBlackY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < blackValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// bright y
int getFirstNoneBrightY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < brightValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBrightY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > brightValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// dark y
int getFirstNoneDarkY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > darkValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextDarkY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < darkValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

```

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 8, 2023, 4:17pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/9 "2023-12-08T16:17:36Z")

</div>

actually my function does not run if I don’t include whiteValue

```auto
void whiteSections(int whiteValue, int y){

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 8, 2023, 4:59pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/10 "2023-12-08T16:59:57Z")

</div>

you have to remove the word final where you declare your variable

my code runs, did you try it?

I just don’t have a suitable image to test

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 8, 2023, 5:30pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/11 "2023-12-08T17:30:01Z")

</div>

thanks! Yes, it runs now… but it does not do nothing ; (

```auto
// please set your image file name !!!

/ ********
 
 ASDF Pixel Sort
 Kim Asendorf | 2010 | kimasendorf.com
 
 Sorting modes
 0 = white
 1 = black
 2 = bright
 3 = dark
 
 */

 int whiteValue = 66; // ??? test

final int mode = 0;

// image path is relative to sketch directory
final String imgFileName = "ciudad";
final String fileType = "PNG";

// ---------------------------------------------------------------------------------------------------

PImage img;

int loops = 0;

// threshold values to determine sorting start and end pixels
// using the absolute rgb value
// r*g*b = 255*255*255 = 16581375
// 0 = white
// -16581375 = black
// sort all pixels whiter than the threshold

void whiteSections(int whiteValue, int y){
if (y<100){
 whiteValue = -1112345678;
} else if (y<500){
 whiteValue = -45678;
} else{
 whiteValue = -1110345678;
}
}

// sort all pixels blacker than the threshold
int blackValue = -156789;
// using the brightness value
// sort all pixels brighter than the threshold
int brightValue = 127;
// sort all pixels darker than the threshold
int darkValue = 223;

int row = 0;
int column = 0;

boolean saved = false;

// ------------------------------------------------------------------------------------------------------

void setup() {
  img = loadImage("ciudad.png") ;

  // use only numbers (not variables) for the size() command, Processing 3
  size(1, 1);

  // allow resize and update surface to image dimensions
  surface.setResizable(true);
  // surface.setSize(img.width, img.height);
  surface.setSize(800, 800);

  // load image onto surface - scale to the available width,height for display
  image(img, 0, 0, width, height);

  println("wait");
}

void draw() {

  if (frameCount <= loops) {

    println("step 1");

    // loop through columns
    println("Sorting Columns");
    while (column < img.width-1) {
      img.loadPixels();
      sortColumn();
      column++;
      img.updatePixels();
    }

    // loop through rows
    /*
    println("Sorting Rows");
     while (row < img.height-1) {
     img.loadPixels();
     sortRow();
     row++;
     img.updatePixels();
     }
     */
    println("done\n");
  }

  // -------------------------------------------------------------------

  // load updated image onto surface and scale to fit the display width and height
  image(img, 0, 0, width, height);

  if (!saved && frameCount >= loops) {
    // save img
    img.save(imgFileName + "_" + mode + ".png");

    saved = true;
    println("Saved frame " + frameCount);

    // exiting here can interrupt file save, wait for user to trigger exit
    println("Click or press any key to exit...");
  }
}// draw

// --------------------------------------------------------------------------------------------------------------------

void keyPressed() {
  if (saved) {
    System.exit(0);
  }
}

void mouseClicked() {
  if (saved) {
    System.exit(0);
  }
}

void sortRow() {
  // current row
  int y = row;

  // where to start sorting
  int x = 0;

  // where to stop sorting
  int xEnd = 0;

  while (xEnd < img.width-1) {
    switch (mode) {
    case 0:
      x = getFirstNoneWhiteX(x, y);
      xEnd = getNextWhiteX(x, y);
      break;
    case 1:
      x = getFirstNoneBlackX(x, y);
      xEnd = getNextBlackX(x, y);
      break;
    case 2:
      x = getFirstNoneBrightX(x, y);
      xEnd = getNextBrightX(x, y);
      break;
    case 3:
      x = getFirstNoneDarkX(x, y);
      xEnd = getNextDarkX(x, y);
      break;
    default:
      break;
    }

    if (x < 0) break;

    int sortingLength = xEnd-x;

    color[] unsorted = new color[sortingLength];
    color[] sorted = new color[sortingLength];

    for (int i = 0; i < sortingLength; i++) {
      unsorted[i] = img.pixels[x + i + y * img.width];
    }

    sorted = sort(unsorted);

    for (int i = 0; i < sortingLength; i++) {
      img.pixels[x + i + y * img.width] = sorted[i];
    }

    x = xEnd+1;
  }
}

void sortColumn() {
  // current column
  int x = column;

  // where to start sorting
  int y = 0;

  // where to stop sorting
  int yEnd = 0;

  // while (yEnd < img.height-1) {
  switch (mode) {
  case 0:
    y = 0; // getFirstNoneWhiteY(x, y); // ???????
    yEnd = img.height-33; // getNextWhiteY(x, y);
    break;
  case 1:
    y = getFirstNoneBlackY(x, y);
    yEnd = getNextBlackY(x, y);
    break;
  case 2:
    y = getFirstNoneBrightY(x, y);
    yEnd = getNextBrightY(x, y);
    break;
  case 3:
    y = getFirstNoneDarkY(x, y);
    yEnd = getNextDarkY(x, y);
    break;
  default:
    break;
  }

  /*
    if (y < 0)
   break;
   */

  int sortingLength = yEnd-y;

  color[] unsorted = new color[sortingLength];
  color[] sorted = new color[sortingLength];

  // READ
  for (int i = 0; i < sortingLength; i++) {
    unsorted[i] = img.pixels[x + (y+i) * img.width];
  }

  // SORT
  sorted = sort(unsorted);

  // WRITE BACK
  for (int i = 0; i < sortingLength; i++) {
    img.pixels[x + (y+i) * img.width] = sorted[i];
  }

  // y = yEnd+1;
  //}
}

// white x
int getFirstNoneWhiteX(int x, int y) {
  while (img.pixels[x + y * img.width] < whiteValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextWhiteX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] > whiteValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// black x
int getFirstNoneBlackX(int x, int y) {
  while (img.pixels[x + y * img.width] > blackValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBlackX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] < blackValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// bright x
int getFirstNoneBrightX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) < brightValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBrightX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) > brightValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// dark x
int getFirstNoneDarkX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) > darkValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextDarkX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) < darkValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// white y
int getFirstNoneWhiteY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < whiteValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextWhiteY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > whiteValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// black y
int getFirstNoneBlackY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > blackValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBlackY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < blackValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// bright y
int getFirstNoneBrightY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < brightValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBrightY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > brightValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// dark y
int getFirstNoneDarkY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > darkValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextDarkY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < darkValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 8, 2023, 5:57pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/12 "2023-12-08T17:57:48Z")

</div>

you set mode to 0, it was 1

where are the 3 values from?

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 8, 2023, 6:07pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/13 "2023-12-08T18:07:58Z")

</div>

but if I am typing whiteValues, sorting mode has to be 0, the white one, hasn’t it?

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 8, 2023, 6:27pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/14 "2023-12-08T18:27:16Z")

</div>

in these three pictures I have used different values in int whiteValue, getting different versions of one picture.

 ![ciudad_0](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/2/9/29854ec6e6a97025341beeaf8dc18b389cf96c7e.jpeg)  
 ![ciudad_8](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/1/812f10d6acdb90eb3e74f1a99b21a6e487638832.jpeg)  
 ![ciudad_99](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/b/4b18c422ebcbbc16a776d77ccea8c8fba0ad1567.jpeg)

I could cut one section of everyone and paste them in photoshop, like this

 ![Sin título-1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/2/8/28c64ec4e7e792b855edcd323978cd053a479c93.jpeg)

But I would like to do it with processing, just with one code. So that why I set mode 0 and changed whiteValues, but it does not work at all

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 8, 2023, 6:33pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/15 "2023-12-08T18:33:23Z")

</div>

can you post the initial image

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [December 8, 2023, 6:46pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/17 "2023-12-08T18:46:20Z")

</div>

I would say I am using your code. I created whiteSections function as I was been told to to, I can’t call it whiteValues because it does not run

```auto
// please set your image file name !!!

/ ********
 
 ASDF Pixel Sort
 Kim Asendorf | 2010 | kimasendorf.com
 
 Sorting modes
 0 = white
 1 = black
 2 = bright
 3 = dark
 
 */

final int mode = 1;

// image path is relative to sketch directory
final String imgFileName = "ciudad";
final String fileType = "PNG";

// ---------------------------------------------------------------------------------------------------

int whiteValue = 66; // ??? test

PImage img;

int loops = 0;

// threshold values to determine sorting start and end pixels
// using the absolute rgb value
// r*g*b = 255*255*255 = 16581375
// 0 = white
// -16581375 = black
// sort all pixels whiter than the threshold

void whiteSections(int x) {
  if (x<100) {
    whiteValue = -12345678;
  } else if (x<500) {
    whiteValue = -15345678;
  } else {
    whiteValue = -10345678;
  }
}

// sort all pixels blacker than the threshold
int blackValue = -156789;
// using the brightness value
// sort all pixels brighter than the threshold
int brightValue = 127;
// sort all pixels darker than the threshold
int darkValue = 223;

int row = 0;
int column = 0;

boolean saved = false;

// ------------------------------------------------------------------------------------------------------

void setup() {
  img = loadImage("ciudad.png") ;

  // use only numbers (not variables) for the size() command, Processing 3
  size(1, 1);

  // allow resize and update surface to image dimensions
  surface.setResizable(true);
  // surface.setSize(img.width, img.height);
  surface.setSize(800, 800);

  // load image onto surface - scale to the available width,height for display
  image(img, 0, 0, width, height);

  println("wait");
}

void draw() {

  if (frameCount <= loops) {

    println("step 1");

    // loop through columns
    println("Sorting Columns");
    while (column < img.width-1) {
      img.loadPixels();
      sortColumn();
      column++;
      img.updatePixels();
    }

    // loop through rows
    /*
    println("Sorting Rows");
     while (row < img.height-1) {
     img.loadPixels();
     sortRow();
     row++;
     img.updatePixels();
     }
     */
    println("done\n");
  }

  // -------------------------------------------------------------------

  // load updated image onto surface and scale to fit the display width and height
  image(img, 0, 0, width, height);

  if (!saved && frameCount >= loops) {
    // save img
    img.save(imgFileName + "_" + mode + ".png");

    saved = true;
    println("Saved frame " + frameCount);

    // exiting here can interrupt file save, wait for user to trigger exit
    println("Click or press any key to exit...");
  }
}// draw

// --------------------------------------------------------------------------------------------------------------------

void keyPressed() {
  if (saved) {
    System.exit(0);
  }
}

void mouseClicked() {
  if (saved) {
    System.exit(0);
  }
}

void sortRow() {
  // current row
  int y = row;

  // where to start sorting
  int x = 0;

  // where to stop sorting
  int xEnd = 0;

  while (xEnd < img.width-1) {
    switch (mode) {
    case 0:
      x = getFirstNoneWhiteX(x, y);
      xEnd = getNextWhiteX(x, y);
      break;
    case 1:
      x = getFirstNoneBlackX(x, y);
      xEnd = getNextBlackX(x, y);
      break;
    case 2:
      x = getFirstNoneBrightX(x, y);
      xEnd = getNextBrightX(x, y);
      break;
    case 3:
      x = getFirstNoneDarkX(x, y);
      xEnd = getNextDarkX(x, y);
      break;
    default:
      break;
    }

    if (x < 0) break;

    int sortingLength = xEnd-x;

    color[] unsorted = new color[sortingLength];
    color[] sorted = new color[sortingLength];

    for (int i = 0; i < sortingLength; i++) {
      unsorted[i] = img.pixels[x + i + y * img.width];
    }

    sorted = sort(unsorted);

    for (int i = 0; i < sortingLength; i++) {
      img.pixels[x + i + y * img.width] = sorted[i];
    }

    x = xEnd+1;
  }
}

void sortColumn() {
  // current column
  int x = column;

  // where to start sorting
  int y = 0;

  // where to stop sorting
  int yEnd = 0;

  // while (yEnd < img.height-1) {
  switch (mode) {
  case 0:
    y = 0; // getFirstNoneWhiteY(x, y); // ???????
    yEnd = img.height-33; // getNextWhiteY(x, y);
    break;
  case 1:
    y = getFirstNoneBlackY(x, y);
    yEnd = getNextBlackY(x, y);
    break;
  case 2:
    y = getFirstNoneBrightY(x, y);
    yEnd = getNextBrightY(x, y);
    break;
  case 3:
    y = getFirstNoneDarkY(x, y);
    yEnd = getNextDarkY(x, y);
    break;
  default:
    break;
  }

  /*
    if (y < 0)
   break;
   */

  int sortingLength = yEnd-y;

  color[] unsorted = new color[sortingLength];
  color[] sorted = new color[sortingLength];

  // READ
  for (int i = 0; i < sortingLength; i++) {
    unsorted[i] = img.pixels[x + (y+i) * img.width];
  }

  // SORT
  sorted = sort(unsorted);

  // WRITE BACK
  for (int i = 0; i < sortingLength; i++) {
    img.pixels[x + (y+i) * img.width] = sorted[i];
  }

  // y = yEnd+1;
  //}
}

// white x
int getFirstNoneWhiteX(int x, int y) {
  whiteSections(y);
  while (img.pixels[x + y * img.width] < whiteValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextWhiteX(int x, int y) {
  x++;
  whiteSections(y);
  while (img.pixels[x + y * img.width] > whiteValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// black x
int getFirstNoneBlackX(int x, int y) {
  while (img.pixels[x + y * img.width] > blackValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBlackX(int x, int y) {
  x++;
  while (img.pixels[x + y * img.width] < blackValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// bright x
int getFirstNoneBrightX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) < brightValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextBrightX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) > brightValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// dark x
int getFirstNoneDarkX(int x, int y) {
  while (brightness(img.pixels[x + y * img.width]) > darkValue) {
    x++;
    if (x >= img.width) return -1;
  }
  return x;
}

int getNextDarkX(int x, int y) {
  x++;
  while (brightness(img.pixels[x + y * img.width]) < darkValue) {
    x++;
    if (x >= img.width) return img.width-1;
  }
  return x-1;
}

// white y
int getFirstNoneWhiteY(int x, int y) {
  if (y < img.height) {
    whiteSections(y);
    while (img.pixels[x + y * img.width] < whiteValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextWhiteY(int x, int y) {
  y++;
  whiteSections(y);
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > whiteValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// black y
int getFirstNoneBlackY(int x, int y) {
  if (y < img.height) {
    while (img.pixels[x + y * img.width] > blackValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBlackY(int x, int y) {
  y++;
  if (y < img.height) {
    while (img.pixels[x + y * img.width] < blackValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// bright y
int getFirstNoneBrightY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < brightValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextBrightY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > brightValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

// dark y
int getFirstNoneDarkY(int x, int y) {
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) > darkValue) {
      y++;
      if (y >= img.height) return -1;
    }
  }
  return y;
}

int getNextDarkY(int x, int y) {
  y++;
  if (y < img.height) {
    while (brightness(img.pixels[x + y * img.width]) < darkValue) {
      y++;
      if (y >= img.height) return img.height-1;
    }
  }
  return y-1;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 8, 2023, 6:51pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/18 "2023-12-08T18:51:01Z")

</div>

please follow the program logic.

What happens when?

mode must be 0

you must correct this code:

```auto
switch (mode) {
  case 0:
    y = 0; // getFirstNoneWhiteY(x, y); // ???????
    yEnd = img.height-33; // getNextWhiteY(x, y);
    break;

```

it’s good that you call

```auto
whiteSections(y);

```

now.

Thanks!

Where do the 3 values come from?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 10, 2023, 1:06pm UTC](https://discourse.processing.org/t/asdf-pixel-sort-with-different-sections-color-threshold/43416/19 "2023-12-10T13:06:18Z")

</div>

Hello @humano,

> [@humano](#):
>
> trying to use different values of sorting in different sections of the picture

I did a quick exploration of this from original source:

> **[GitHub - kimasendorf/ASDFPixelSort: Processing script to sort portions of...](https://github.com/kimasendorf/ASDFPixelSort)**
>
> Processing script to sort portions of pixels in an image. - GitHub - kimasendorf/ASDFPixelSort: Processing script to sort portions of pixels in an image.

It was able to spit sections and set mode for each section like this:

```auto
if (frameCount <= loops) 
    {
    mode = 0;
    println(row);
    println("Sorting Rows");
    while (row < img.height/2-1) // Top half
      {
      img.loadPixels();
      sortRow();
      row++;
      img.updatePixels();
      }
    
    println(row);
    
    mode = 1;
    println("Sorting Rows");
    while (row < img.height) // bottom half
      {
      img.loadPixels();
      sortRow();
      row++;
      img.updatePixels();
      }
    println(row);
    }

```

You can see the top and bottom half:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/e/1eef968de81a0df754e934054afca4ad8a35934f.jpeg)

You can then try changing the thresholds as desired before each block of code.

`:)`
