A subdivision algorithm that adds another level of subdivision if it samples black pixels. Link to the source files and editor.


let quads = [];

let pg;
let img;

function preload() {
  img = loadImage('yes.jpg');
}


function setup(){
  createCanvas(800,800);
  
  pg = createGraphics(width, height);
  pg.image(img, 0, 0);
  
  let space = 400;
  let xnum = width/space;
  let ynum =  height/space;
  for(let i = 0; i < xnum; i++){
    for(let j = 0; j < ynum; j++){
      append(quads,new SqSample(i*space,j*space,space,8, color(random(255),random(255),random(255))));
    }
  }
}

function draw(){
  background(0);
  for(let i = 0; i < quads.length; i++){
    let sq = quads[i];
    sq.display();
  }
}

class SqSample{
  
  constructor( xpos,ypos,size,ct,c ) {
    this.x = xpos;
    this.y = ypos;
    this.w = size;
    this.sc = c;
    this.counter = ct-1;
    this.gs = 0;
    this.gslimit = random(30)+20;
    this.gstop = 0;
  }
  
  display(){
    noStroke();
    fill(this.sc);
    rect(this.x,this.y,this.w,this.w);
    this.gs++;
    if(this.gs > this.gslimit && this.gstop == 0 && this.counter > 0){
      
      let check = 0;
      for(let xc = this.x; xc < this.x + this.w; xc++){
        for(let yc = this.y; yc < this.y + this.w; yc++){
          let sample = pg.get(int(xc),int(yc));
          if(red(sample) <  100){
            check = 1;
          }
        }
      }
      
      if(check == 1){
        append(quads,new SqSample(this.x,this.y,this.w/2,this.counter, color(random(255),random(255),random(255))));
        append(quads,new SqSample(this.x+this.w/2,this.y,this.w/2,this.counter, color(random(255),random(255),random(255))));
        append(quads,new SqSample(this.x+this.w/2,this.y+this.w/2,this.w/2,this.counter, color(random(255),random(255),random(255))));
        append(quads,new SqSample(this.x,this.y+this.w/2,this.w/2,this.counter, color(random(255),random(255),random(255))));
      }
      this.gstop = 1;
    }
  }
}