User Tools

Site Tools


cs276:darklight
/*
 *  DarkLight
 *
 *  Lighten or darken a picture by a set amount.
 *  
 *  Modified 24 Sep 2007 by spc
 */

PImage myImage;
 
void setup() {
  myImage = loadImage("Image.jpg");
  size(myImage.width*2, myImage.height*2);  
  image(myImage, myImage.width, myImage.height);
  noLoop();
}

void draw() {
  lightenDarken(myImage, 75);  
  image(myImage, myImage.width,0);
  lightenDarken(myImage, -150); // undo previous and go darker
  image(myImage, 0, myImage.height);
  lightenDarken(myImage, -255);
  image(myImage, 0, 0);
}

/*
 * lightenDarken(pi, shift)
 * Used to lighten or darken an image. The shift argument indicates 
 * the change to the image - positive is lighter, negative is darker
 */

void lightenDarken(PImage myImage, int shift) {
  myImage.loadPixels();

  for (int col = 0; col < myImage.width; col++) {
     for (int row = 0; row < myImage.height; row ++) {
        color c = myImage.get(col, row);

        // simply add the shift value to each RGB value
        c = color(red(c) + shift, green(c) + shift, blue(c) + shift);
        myImage.set(col, row, c);
     }
  }
  myImage.updatePixels();

}
cs276/darklight.txt · Last modified: 2007/09/26 15:49 by scarl