
int IMGHEIGHT;
int IMGWIDTH;





PImage[] images = new PImage[5];
float[] weight=new float [5];
float[] inc=new float [5];
void setup() 
{
  int i;
  int x=86;
  String p;
  IMGHEIGHT=600;
  IMGWIDTH=800;

  frameRate(10);
    for(i=0;i<images.length;i++){
      p=str(int(random(x))+1);
      images[i] = loadImage("d"+p+".jpg");
      weight[i]=random(100);
      inc[i]=random(10)-5;
    }



  size(IMGWIDTH,IMGHEIGHT);
  background(0);
  //noLoop();
}
void draw()
{
  int x=86;
  String p;
   
 int k;
  int cols;
  int rows;
  boolean bStarted;
  bStarted=false;
  int sub;
  int i;
  float r;
  float g;
  float b;

  float sumR;
  float sumG;
  float sumB;
  
  float totalWeight;




  for(i=0;i<images.length;i++){
    weight[i]=weight[i]+inc[i];
   if (weight[i]<=0){
     weight[i]=1;
     inc[i] = inc[i] * -1;
     p=str(int(random(x))+1);
      images[i] = loadImage("d"+p+".jpg");
    
   } 
      if (weight[i]>=200){
     weight[i]=200;
     inc[i] = inc[i] * -1;
    
   } 
  }
    loadPixels();
   for (rows=0;rows<IMGHEIGHT;rows++){
      for (cols=0;cols<IMGWIDTH;cols++){
             
                   
                   sub=(rows*IMGWIDTH) + cols;
                  sumR=0;
                   sumG=0;
                   sumB=0;
                   totalWeight=0;
                 for(i=0;i<images.length;i++){
                   totalWeight=totalWeight+weight[i];
                 }
                   
           
                   
                   
                   r=red(pixels[sub])*totalWeight;
                    g=green(pixels[sub])*totalWeight;
                    
                     b=blue(pixels[sub])*totalWeight;
                    
                  
                   
                  sumR=sumR+r;
                    sumG=sumG+g;
                     sumB=sumB+b;
                     
                   for(i=0;i<images.length;i++){
                  
                     r=red(images[i].pixels[sub])*weight[i];
                     g=green(images[i].pixels[sub])*weight[i];;
                     b=blue(images[i].pixels[sub])*weight[i];
                     sumR=sumR+r;
                     sumG=sumG+g;
                     sumB=sumB+b;
                     totalWeight=totalWeight+weight[i];
                   }

                  pixels[sub]=color(int(sumR/totalWeight),int(sumG/totalWeight),int(sumB/totalWeight) );
      
    }
     updatePixels(); 

     
 }


 
  
 
  
}
