ASDF Pixel Sort with different sections (color threshold)

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!



/********

 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;
}
1 Like

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




// 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;
}

1 Like

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?

I explained already, that’s your answer

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

// 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;
}

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

Does it work?

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

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

this example could work

please try

it makes use of your function

where are your 3 values like -12345678 from?



// 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;
}

1 Like

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

void whiteSections(int whiteValue, int y){

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

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

// 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;
}

you set mode to 0, it was 1

where are the 3 values from?

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

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



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

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

can you post the initial image

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

// 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;
}

please follow the program logic.

What happens when?

mode must be 0

you must correct this code:

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

it’s good that you call

whiteSections(y);

now.

Thanks!

Where do the 3 values come from?

Hello @humano,

I did a quick exploration of this from original source:

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

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

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

:)

1 Like