Image loading

edited February 2014 in Share Your Work

Here is an example of loading lots of images, then making them spin. This is what it looks like ,youtu.be/U6Yu7sVMAnA

the good bits are voids reload, ldloop mloop, floop,

hope this doesn't look to messy, regards, Alan.

    //
    // STEREO image rotator2
    // -= AGW 20140102 =-
    //

    File[] list; // Files, not Strings
    //
    //
    //PImage[] images = new PImage[numFrames];
    PImage[] imgs ; // fn matched images
    // STEREO image buffers
    PImage[] imgsun  ;
    PImage[] imgc2   ;
    PImage[] imgcora ;
    PImage[] imgcorb ;
    PImage[] imghi1a ;
    PImage[] imghi2a ;
    PImage[] imghi1b ;
    PImage[] imghi2b ;
    // STEREO viewport images
    PImage vp;  //viewport image
    PImage vpsun  ;
    PImage vpc2   ;
    PImage vpcora ;
    PImage vpcorb ;
    PImage vphi1a ;
    PImage vphi2a ;
    PImage vphi1b ;
    PImage vphi2b ;
    //viewport image counters
    int vpcnt;  //viewport image counters
    int vpsuncnt  ;
    int vpc2cnt   ;
    int vpcoracnt ;
    int vpcorbcnt ;
    int vphi1acnt ;
    int vphi2acnt ;
    int vphi1bcnt ;
    int vphi2bcnt ;

    //

    int Nimg = 0;
    int sframes = 0;
    int eframes = 0;
    int nframes = 0;
    int lprate = 20 ;
    int lpmode = 1;

    int frate = 20 ;
    int lastCheck=0;

    int rot = 0;
    int rotb = 0;
    //
    // scale images 1,2,4,8
    float scl = 4; // 4 is good
    //
    // change too 1 or 10 or 73 for rotation
    // or make rot val equal to fits keyword value??
    float rotstep = 1 ;//  degree
    //
    long timeToWait = 0 ;// in miliseconds
    long lastTime;
    //

    //PFont font;
    //
    //String fnmatch = "201312" ; // files matched to this string
    //String fpath = "C:\\SOHO\\C3" ;
    //
    String    fnmatch = "2013112" ; // files matched to this string
    String    fpath = "C:\\QLKimgs\\STEREO\\a\\hi_1\\P201311" ;
    //
    //
    void setup()
    {
    //  
      size(1300, 800, OPENGL);
      frameRate(frate);
      //
      //  load HI2A 
      fnmatch = "201311" ; // files matched to this string
      fpath = "C:\\QLKimgs\\STEREO\\a\\hi_2\\P201311" ;
      //
      reload();
      //
      imghi2a = new PImage[nframes]; // setup overlay buffer
      arrayCopy(imgs, imghi2a );     // copy current images to ovly buffer
      print( " num HI2A images "+imghi2a.length+"\n" );
      //
      // load HI1A   
      fnmatch = "2013112" ; // files matched to this string
      fpath = "C:\\QLKimgs\\STEREO\\a\\hi_1\\P201311" ;
      //
      reload();
      //
      imghi1a = new PImage[nframes]; // setup overlay buffer
      arrayCopy(imgs, imghi1a );     // copy current images to ovly buffer
      print( " num HI1A images "+imghi1a.length+"\n" );
      //

       //  load Cor2A 
      fnmatch = "2013112" ; // files matched to this string
      fpath = "C:\\QLKimgs\\STEREO\\a\\cor2\\P201311" ;
      //
      reload();
      //
      imgcora = new PImage[nframes]; // setup overlay buffer
      arrayCopy(imgs, imgcora );     // copy current images to ovly buffer
      print( " num Cor images "+imgcora.length+"\n" );
      //
      // copy and alter above load statements for STEREO B data sets
      //
      textAlign(LEFT);
      //
      imageMode(CENTER);
      //
    } 

    void draw() 
    { 
      background(10);
      noStroke();
      // lights();
      //
      text("Frame: "+frameCount+"\nAngle: "+rot+"\nScl: "+scl, 600, 150);
      //
      floop(); 
      //
      if (rot > 360) {  
        rot = 0;  // exit(); // uncoment if using saveframes below
      }
      //
      pushMatrix();
      //
      translate(width/2, height/2);
      //
      rotateY(radians(rot/rotstep));
      //
      image(vphi2a, (-1664/scl), (0/scl), (2048/scl), (2048/scl));  //HI2A
      image(vphi1a, (-384/scl), (0/scl), (512/scl), (512/scl));  //HI1A
      image(vpcora, (0/scl), (0/scl), (256/scl), (256/scl));  //COR2
      // Add EUV or SDO AIA here??
      text("STEREO A", (-(128*3)/scl), (-(128*3)/scl));
      translate(-width/2, -height/2);
      //
      translate(width/2, height/2);
      //
      rotateY(radians((-rot*2)/rotstep));
      //
      // change these to STEREO B images ie vphi1b ...
      image(vpcora, (0/scl), (0/scl), (256/scl), (256/scl));  //COR2
      image(vphi1a, (384/scl), (0/scl), (512/scl), (512/scl));  //HI1B
      image(vphi2a, (1664/scl), (0/scl), (2048/scl), (2048/scl));  //HI2B
      // Add EUV or SDO AIA here??
      text("STEREO B", ((128*1)/scl), (-(128*3)/scl));
      //
      sphereDetail(10);
      sphere((8/scl));
      //
      noFill();
      stroke(128);
      ellipse((0/scl), (0/scl), (256/scl), (256/scl));
      noStroke();
      fill(128);
      //
      translate(width/2, height/2);
      rotateY(radians(0));
      popMatrix();
      //
      // uncoment to save images see // exit(); above
      // saveFrame("output/frames-"+rot+".png");
      //
       rot+=1;
      //
    }


    void floop() {
      //
      // Get next frame from all buffers into viewport images
      // Not time synced,  needs more work??
      // stop at last frame 
      if (abs(frameCount) >= imgcora.length ) {
        vpcoracnt = imgcora.length-1;
      } 
      else { 
        vpcoracnt = abs(frameCount);
      }
      //
      if (abs(frameCount) >= imghi1a.length ) {
        vphi1acnt = imghi1a.length-1;
      } 
      else { 
        vphi1acnt = abs(frameCount);
      }
      //
      if (abs(frameCount) >= imghi2a.length ) {
        vphi2acnt= imghi2a.length-1;
      } 
      else { 
        vphi2acnt = abs(frameCount);
      }
      //
      // // uncoment for StereoB images   
      // if (abs(frameCount) >= imghi1b.length ) {
      //   vphi1bcnt = imghi1b.length-1;
      // } else { vphi1bcnt = abs(frameCount);}
      //
      // if (abs(frameCount) >= imghi2b.length ) {
      //   vphi2bcnt= imghi2b.length-1;
      // } else { vphi2bcnt = abs(frameCount);}
      //
      //
      vp = imgs[abs(frameCount)];

      vpcora = imgcora[ceil(vpcoracnt)] ;
      ;
      vphi1a = imghi1a[ceil(vphi1acnt)] ;
      vphi2a = imghi2a[ceil(vphi2acnt)] ;
      // uncoment for StereoB images
      // vphi1b = imghi2b[ceil(vphi2bcnt)] ; 
      // vphi2b = imghi2b[ceil(vphi2bcnt)] ;   

    // simple sweep using negative framecounts...
      if (frameCount >= nframes-1) { 
        frameCount = -(nframes) ;
      } 
      if (frameCount == -1) { 
        frameCount = 1 ;
      }
      //
    }

    void mloop() {
      for ( Nimg = 0; Nimg < list.length ; Nimg++) {
        //
        String fn = list[Nimg].getAbsolutePath();
        String[] m2 = match(fn, fnmatch );
        if (m2 != null) {
          eframes = Nimg  ;
          nframes++ ;
          sframes = (eframes - nframes)  ;
          // println(list.length+" "+sframes+" "+eframes+" "+nframes+ "Match found in '" + list[Nimg] + "'\n"+fn);
        }   
        else {
          // This will print to the console, since no match was found.
          sframes = (eframes - nframes)  ;
          // println(sframes+" "+eframes+" "+nframes+ "No match found in '" + list[Nimg] + "'\n"+fn);
        }
      }
    }

    void ldloop() {
      // load matched images
      imgs = new PImage[nframes];  

      for (int x = 1; x < nframes; x++)
      {
        imgs[x] = loadImage(list[(sframes+x)].getAbsolutePath());
        print( x + " images loaded "+list[sframes+x]+"\n ") ;
      }
    }

    void reload() {
      //
      Nimg = 0;
      sframes = 0;
      eframes = 0;
      nframes = 0;
      //
      fnmatch = fnmatch ; // files matched to this string
      fpath = fpath  ;    // files in this path
      File dir = new File(dataPath(fpath)); 
      list = dir.listFiles();
      //
      mloop() ;  // match file names
      //
      ldloop();  // load matched files
      //
      frameCount = 0;
    }
Tagged:
Sign In or Register to comment.