User Tools

Site Tools


cs276:modifycolorsfast
/*
 *  ModifyColorsFast 
 *
 *  Modify the colors in an image (a seascape photo) to produce
 *  the "sunset effect" using a user-defined function modifyRGB.
 *  The sunset effect is given by reducing Green and Blue components
 *  by 30% (multiply by 0.7)
 *
 *  Note that modifyRGB can increase or decrease the color component specified.
 *
 *  Created 18 Sept. 2007 by spc
 */

import java.lang.*;

PImage pi, pi_copy;

void setup() {
  pi = loadImage("Blowhole002.jpg");
  try {
    pi_copy = (PImage) pi.clone();
  } catch (Exception e) {}
  // make room for two images side by side
  size(pi.width*2, pi.height);
  
  image(pi, 0, 0);
  noLoop();
}

void draw() {
  // create the "sunset effect"
  modifyRGB(pi, "green", 0.7);
  modifyRGB(pi, "blue", 0.7);
  image(pi, pi.width, 0);
}

void mousePressed() {
  // for comparison, decrease red by 50% in original image
  modifyRGB(pi_copy, "red", 0.5);
  image(pi_copy, 0, 0);
  redraw();
}

/* modifyRGB
 * Given an image, a string specifying color, and an amount,
 * modify that color component in all pixels of the image.  
 * Note that an amount < 1.0 reduces the amount of that color (but
 * never less than 0), > 1.0 increases (but never greater than 255),
 * and 1.0 leaves it unchanged
 */

void modifyRGB(PImage pi, String colorString, float amount) {
  float value = 0; 
  color c;
  pi.loadPixels();

  // for each pixel in the image
  for (int i = 0; i < pi.pixels.length; i++) {
    c = pi.pixels[i];
    if (colorString.equals("red")) {
      value = red(c) * amount;
      c = color(value, green(c), blue(c));
    } else if (colorString.equals("green")) {
      value = green(c) * amount;
      c = color(red(c), value, blue(c));
    } else if (colorString.equals("blue")) {
      value = blue(c) * amount; 
      c = color(red(c), green(c), value);
    }
    pi.pixels[i] = c;
  }
  // display the modified pixels   
  pi.updatePixels();
}
cs276/modifycolorsfast.txt · Last modified: 2007/09/26 15:45 by scarl