FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Progressive fractal "rendering"
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Progressive fractal "rendering"  (Read 912 times)
rgovostes

rgovostes
Progressive fractal "rendering"
« on: Dec 29th, 2004, 10:45pm »

This code should take an image and progressively "render" it with a fractal-like method: it draws a solid rectangle representing the 'average' color of corresponding pixels in the image. Then it splits itself into 4 blocks.
 
However, only the top-left quarter actually renders correctly, the others appear stretched. I'm betting its a small math problem I'm overlooking. (Update: fixed, doh)
 
Then, there is the problem that it slows down horribly. After the 4th or 5th iteration, it isn't at all responsive. I realize I'm asking for a bit from the CPU, but it shouldn't crash...
 
Code:
// Render2D by Ryan Govostes
// Progressively "renders" the source image
 
/* Cat image from http://pantransit.reptiles.org/images/sorted/photo/animal/,
   credit given to Lisa Nielsen. */
    
BImage cat = loadImage("cat.jpg");
RenderBox[] renderers = new RenderBox[1];
RenderBox[] nextRenderers;
 
void setup() {
  size(350, 350);
  framerate(2);
  
  renderers[0] = new RenderBox(0, 0, width, height);
}
 
void loop() {
  if(renderers.length > 16) return; /* remove for fun */
  nextRenderers = new RenderBox[0];
  
  for(int i = 0; i < renderers.length; i ++) {
    if(!renderers[i].used) renderers[i].drawAndIterate();
  }
  
  renderers = nextRenderers;
}
 
color averageColor(float x, float y, float w, float h, BImage src) {
  BImage pxl = new BImage(1, 1);
  pxl.replicate(src, (int)x, (int)y, int(x + w), int(y + h), 0, 0, 1, 1);
  return pxl.pixels[0];
}
 
void appendRenderer(RenderBox newRenderer) {
  RenderBox[] renderersPlus = new RenderBox[nextRenderers.length + 1];  
  System.arraycopy(nextRenderers, 0, renderersPlus, 0, nextRenderers.length);  
  renderersPlus[nextRenderers.length] = newRenderer;
  nextRenderers = renderersPlus;
}
 
class RenderBox {
  public boolean used;
  private float x, y, w, h;
  
  RenderBox(float tx, float ty, float tw, float th) {
    x = tx; y = ty; w = tw; h = th; used = false;
  }
  
  void drawAndIterate() {
    noStroke();
    fill(averageColor( x, y, w, h, cat ));
    rect( x, y, w, h );
    
    if(w >= 2 && h >= 2) {
      appendRenderer( new RenderBox( x, y, (w / 2), (h / 2) ) );
      appendRenderer( new RenderBox( x + (w / 2), y, (w / 2), (h / 2) ) );
      appendRenderer( new RenderBox( x, y + (h / 2), (w / 2), (h / 2) ) );
      appendRenderer( new RenderBox( x + (w / 2), y + (h / 2), (w / 2), (h / 2) ) );
    }
 
    used = true;
  }
  
}
« Last Edit: Dec 29th, 2004, 10:53pm by rgovostes »  
Pages: 1 

« Previous topic | Next topic »