How to optimize this image class?
in
Programming Questions
•
2 years ago
Hello to all.
I am working on a sketch that loads around 100 images (640x480) and processes them. I am studying the following example I found on openprocessing.com (sorry can't remember the name of the owner). However, I've noticed performance issues when loading more than 50 images. Can someone have a look and tell me if you can see where the mistake is?
Thanks
- int IMGHEIGHT;
- int IMGWIDTH;
- int CANVASHEIGHT;
- int CANVASWIDTH;
- Image[] j = new Image[50];
- void setup()
- {
- IMGHEIGHT=600;
- IMGWIDTH=800;
- CANVASHEIGHT=600;
- CANVASWIDTH=800;
- size(CANVASWIDTH,CANVASHEIGHT);
- frameRate(12);
- background(0,0,0);
- }
- void draw()
- {
- float freq;
- float amp;
- float pan;
- for(int i=0;i<j.length;i++){
- if(j[i] != null){
- if( j[i].draw()){
- j[i]=null;
- }
- }
- else{
- j[i]=new Image(CANVASHEIGHT,CANVASWIDTH,IMGHEIGHT,IMGWIDTH);
- }
- }
- }
- class Image{
- int N=50;
- PImage i1;
- int x;
- int y;
- int o;
- int r;
- int g;
- int b;
- int h;
- int w;
- int dx;
- int dy;
- float ds;
- float dt;
- int ch;
- int cw;
- public Image(int acHeight, int acWidth,int aiHeight, int aiWidth){
- String f=str(int(random(N))+1);
- float rd;
- rd=random(.3,.7);
- i1=loadImage("Image_"+f+".jpg");
- h=int(rd*aiHeight);
- w=int(rd*aiWidth);
- x=int(random(acWidth));
- y=int(random(acHeight));
- cw=acWidth;
- ch=acHeight;
- o=int(random(10,20));
- r=int(random(0,255));
- g=int(random(0,255));
- b=int(random(0,255));
- dx=int(random(-1,2));
- dy=int(random(-1,2));
- ds=random(.99,1.01);
- dt=random(.99, 1.01);
- }
- public boolean draw(){
- boolean done;
- done=false;
- if (random(100)>60){
- x=x+dx;
- y=y+dy;
- }
- h=int(h*ds);
- w=int(w*ds);
- o=int(o*dt);
- tint(r,g,b,o);
- image(i1,x,y,w,h);
- if(random(20)>17){
- dx=int(random(-1,2));
- dy=int(random(-1,2));
- ds=random(.95,1.05);
- }
- if (random(100)>96){
- done=true;
- }
- return done;
- }
- }
1