GMU:Processing im Park/Emilio Aguas/sample code final

From Medien Wiki
< GMU:Processing im Park‎ | Emilio Aguas
Revision as of 17:36, 1 April 2016 by EmilioAguas (talk | contribs) (Created page with "====--- Processing3.0 Code--- ==== Discover the Images of nature that are hidden in the dark with the help of a lighter <source lang="java"> import processing.video.*; int k,...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

--- Processing3.0 Code---

Discover the Images of nature that are hidden in the dark with the help of a lighter

import processing.video.*;

int k, l; //coordinates for the brightest pixel
int photos = 8; //maximum of photos
int index = 0; // first image
PImage[] pics = new PImage[photos]; //images array
Capture cam; // Declare Capture object

void setup() {
  //fullScreen();
  size (1440, 900);
  background (0);
  smooth ();
  noStroke();
  
  //initialize the web cam
  cam = new Capture (this, 800, 600, "FaceTime HD Camera (Built-in)", 30);
  cam.start();

  //load the images into the array
  for (int i = 0; i < pics.length; i++ ) {
    pics[i] = loadImage ("IMG_024"+ (i+2) + ".jpg");
  }
}

void draw () {
  loadPixels();

  //load the pixels of every image
  pics[index].loadPixels();

  //load the pixels of the web cam
  if (cam.available()) {
    cam.read();
    cam.loadPixels();

    // Start values of the brightness of the pixels
    float theLight = 0;
    int brightnessPix =0;

    // campare if the actual brightness values net to be replaced for new ones higher than 0 
    for (int i=0; i<cam.pixels.length; i++) {
      if (brightness(cam.pixels[i]) > theLight) {
        theLight= brightness (cam.pixels[i]);
        brightnessPix = i;
      }
    }

    k = brightnessPix % cam.width; // moving in the colums of the pixel array
    l = brightnessPix / cam.width; // moving in the rows of the pixel array

    //moving through the pixels in two dimensions
    for (int x =0; x <  pics[index].width; x++) {
      for (int y =0; y <  pics[index].height; y++) {

        //position of the pixels in one dimension
        int pixel_Loc = x + (y * pics[index].width);

        //take the r,g.b values from the image pixel by pixel
        float red = red (pics[index].pixels [pixel_Loc]);
        float green = green (pics[index].pixels [pixel_Loc]);
        float blue = blue (pics[index].pixels [pixel_Loc]);

        //change the brightness according to the proximity of the mouse
        float flash = dist (x, y, k, l);//using dist function with the 4 values points in X and Y coordinates
        float flashLightX = (60-flash) / 50;// change the values for filters or the "100" for the size of the flashlight
        float flashLightY = (80-flash) / 50;// change the values for filters or the "100" for the size of the flashlight
        red *= flashLightX;
        green *= flashLightY;
        blue *= flashLightX;

        // Constrain rgb values
        red = constrain (red, 0, 255);
        green = constrain (green, 0, 255);
        blue = constrain (blue, 0, 255);

        //change the pixels for a new value
        color images = color (red, green, blue);
        pixels[pixel_Loc] = images;
      }
    }
    //dispaly the pixels
    updatePixels();
  }
}

void mousePressed() {
  //change the image every time the mouse is clicked
  index = (index + 1) % pics.length;
}