import processing.core.*; 
import processing.xml.*; 

import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class moving_photo extends PApplet {

PImage b, c;
int x,y,n,w;
int win_width;
int win_height;
int y_corner;

public void setup() {
  size(screen.width, screen.height);//, 800);
  //size(1200, 900);
  //b=loadImage("01_06_western landscape.jpg");
  //b=loadImage("/home/robert/Desktop/Photos/03_washington dc bedroom 2.jpg");
  b=loadImage("C:\\Swap\\Photos\\03_washington dc bedroom 2.jpg");
  win_height=PApplet.parseInt(b.height*0.9f);
  win_width=PApplet.parseInt(b.width*0.9f);//win_height/height*width);
  y_corner=PApplet.parseInt((b.height-win_height)/2);
  //noLoop();
  x=0;
  y=0;
  w=0;
  n=0;
  frameRate(24);
}

public void auto_contrast(PImage in) {
  // adjust contrast in place on input image
  in.loadPixels();
  // loop over pixels to find min and max Intensity
    
  // loop over pixels to scale RGB to min and max Intensity
  int dimension = (in.width*in.height);
  for(int i=0;i<dimension;i++) {
      int rgb=in.pixels[i];
      
      int r = (rgb >> 16) & 0xFF;  // Faster way of getting red(argb)
      int g = (rgb >> 8) & 0xFF;   // Faster way of getting green(argb)
      int b = rgb & 0xFF;          // Faster way of getting blue(argb)

      r=PApplet.parseInt(r*0.5f);
      g=PApplet.parseInt(g*0.5f);
      b=PApplet.parseInt(b*0.5f);
      in.pixels[i]=r+g+b;
  };
  
  in.updatePixels();
};

public void draw() {
  
  c = b.get(x, y+y_corner, win_width, win_height);

  auto_contrast(c);

  blend(c, 0, 0, win_width, win_height, 0, 0, width, height, BLEND);
  //blend(b, x, y+y_corner, (win_width), (win_height), 0, 0, width, height, BLEND);
  
  //filter(ERODE);
  //saveFrame("/Volumes/Swimming Pool/Processing/west-#####.tif");
  //println(x+":"+b.width+" "+y+":"+b.height);
  
  // update motion
  n++;
  x+=PApplet.parseInt(random(0, 5));
  y=PApplet.parseInt(random(0,10));
  w=PApplet.parseInt(random(-5,0));
}



  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#F0F0F0", "moving_photo" });
  }
}
