diff --git a/java/libraries/video/examples/Capture/AsciiVideo/AsciiVideo.pde b/java/libraries/video/examples/Capture/AsciiVideo/AsciiVideo.pde index 2faf42c20..ccfb2e103 100644 --- a/java/libraries/video/examples/Capture/AsciiVideo/AsciiVideo.pde +++ b/java/libraries/video/examples/Capture/AsciiVideo/AsciiVideo.pde @@ -1,139 +1,139 @@ -/** - * ASCII Video - * by Ben Fry. - * - * GSVideo version by Andres Colubri. - * - * Text characters have been used to represent images since the earliest computers. - * This sketch is a simple homage that re-interprets live video as ASCII text. - * See the keyPressed function for more options, like changing the font size. - */ - -import processing.opengl.*; -import codeanticode.gsvideo.*; - -GSCapture video; -boolean cheatScreen; - -// All ASCII characters, sorted according to their visual density -String letterOrder = - " .`-_':,;^=+/\"|)\\<>)iv%xclrs{*}I?!][1taeo7zjLu" + - "nT#JCwfy325Fp6mqSghVd4EgXPGZbYkOA&8U$@KHDBWNMR0Q"; -char[] letters; - -float[] bright; -char[] chars; - -PFont font; -float fontSize = 1.5; - - -public void setup() { - size(640, 480, P3D); - // Or run full screen, more fun! Use with Sketch -> Present - //size(screen.width, screen.height, OPENGL); - - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, 160, 120); - video.play(); - int count = video.width * video.height; - println(count); - - font = loadFont("UniversLTStd-Light-48.vlw"); - - // for the 256 levels of brightness, distribute the letters across - // the an array of 256 elements to use for the lookup - letters = new char[256]; - for (int i = 0; i < 256; i++) { - int index = int(map(i, 0, 256, 0, letterOrder.length())); - letters[i] = letterOrder.charAt(index); - } - - // current characters for each position in the video - chars = new char[count]; - - // current brightness for each point - bright = new float[count]; - for (int i = 0; i < count; i++) { - // set each brightness at the midpoint to start - bright[i] = 128; - } -} - - -public void captureEvent(GSCapture c) { - c.read(); -} - - -void draw() { - background(0); - - pushMatrix(); - - float hgap = width / float(video.width); - float vgap = height / float(video.height); - - scale(max(hgap, vgap) * fontSize); - textFont(font, fontSize); - - int index = 0; - for (int y = 1; y < video.height; y++) { - - // Move down for next line - translate(0, 1.0 / fontSize); - - pushMatrix(); - for (int x = 0; x < video.width; x++) { - int pixelColor = video.pixels[index]; - // Faster method of calculating r, g, b than red(), green(), blue() - int r = (pixelColor >> 16) & 0xff; - int g = (pixelColor >> 8) & 0xff; - int b = pixelColor & 0xff; - - // Another option would be to properly calculate brightness as luminance: - // luminance = 0.3*red + 0.59*green + 0.11*blue - // Or you could instead red + green + blue, and make the the values[] array - // 256*3 elements long instead of just 256. - int pixelBright = max(r, g, b); - - // The 0.1 value is used to damp the changes so that letters flicker less - float diff = pixelBright - bright[index]; - bright[index] += diff * 0.1; - - fill(pixelColor); - int num = int(bright[index]); - text(letters[num], 0, 0); - - // Move to the next pixel - index++; - - // Move over for next character - translate(1.0 / fontSize, 0); - } - popMatrix(); - } - popMatrix(); - - if (cheatScreen) { - //image(video, 0, height - video.height); - // set() is faster than image() when drawing untransformed images - set(0, height - video.height, video); - } -} - - -/** - * Handle key presses: - * 'c' toggles the cheat screen that shows the original image in the corner - * 'g' grabs an image and saves the frame to a tiff image - * 'f' and 'F' increase and decrease the font size - */ -public void keyPressed() { - switch (key) { - case 'g': saveFrame(); break; - case 'c': cheatScreen = !cheatScreen; break; - case 'f': fontSize *= 1.1; break; - case 'F': fontSize *= 0.9; break; - } -} +/** + * ASCII Video + * by Ben Fry. + * + * GSVideo version by Andres Colubri. + * + * Text characters have been used to represent images since the earliest computers. + * This sketch is a simple homage that re-interprets live video as ASCII text. + * See the keyPressed function for more options, like changing the font size. + */ + +import processing.opengl.*; +import codeanticode.gsvideo.*; + +GSCapture video; +boolean cheatScreen; + +// All ASCII characters, sorted according to their visual density +String letterOrder = + " .`-_':,;^=+/\"|)\\<>)iv%xclrs{*}I?!][1taeo7zjLu" + + "nT#JCwfy325Fp6mqSghVd4EgXPGZbYkOA&8U$@KHDBWNMR0Q"; +char[] letters; + +float[] bright; +char[] chars; + +PFont font; +float fontSize = 1.5; + + +public void setup() { + size(640, 480, P3D); + // Or run full screen, more fun! Use with Sketch -> Present + //size(screen.width, screen.height, OPENGL); + + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, 160, 120); + video.start(); + int count = video.width * video.height; + println(count); + + font = loadFont("UniversLTStd-Light-48.vlw"); + + // for the 256 levels of brightness, distribute the letters across + // the an array of 256 elements to use for the lookup + letters = new char[256]; + for (int i = 0; i < 256; i++) { + int index = int(map(i, 0, 256, 0, letterOrder.length())); + letters[i] = letterOrder.charAt(index); + } + + // current characters for each position in the video + chars = new char[count]; + + // current brightness for each point + bright = new float[count]; + for (int i = 0; i < count; i++) { + // set each brightness at the midpoint to start + bright[i] = 128; + } +} + + +public void captureEvent(GSCapture c) { + c.read(); +} + + +void draw() { + background(0); + + pushMatrix(); + + float hgap = width / float(video.width); + float vgap = height / float(video.height); + + scale(max(hgap, vgap) * fontSize); + textFont(font, fontSize); + + int index = 0; + for (int y = 1; y < video.height; y++) { + + // Move down for next line + translate(0, 1.0 / fontSize); + + pushMatrix(); + for (int x = 0; x < video.width; x++) { + int pixelColor = video.pixels[index]; + // Faster method of calculating r, g, b than red(), green(), blue() + int r = (pixelColor >> 16) & 0xff; + int g = (pixelColor >> 8) & 0xff; + int b = pixelColor & 0xff; + + // Another option would be to properly calculate brightness as luminance: + // luminance = 0.3*red + 0.59*green + 0.11*blue + // Or you could instead red + green + blue, and make the the values[] array + // 256*3 elements long instead of just 256. + int pixelBright = max(r, g, b); + + // The 0.1 value is used to damp the changes so that letters flicker less + float diff = pixelBright - bright[index]; + bright[index] += diff * 0.1; + + fill(pixelColor); + int num = int(bright[index]); + text(letters[num], 0, 0); + + // Move to the next pixel + index++; + + // Move over for next character + translate(1.0 / fontSize, 0); + } + popMatrix(); + } + popMatrix(); + + if (cheatScreen) { + //image(video, 0, height - video.height); + // set() is faster than image() when drawing untransformed images + set(0, height - video.height, video); + } +} + + +/** + * Handle key presses: + * 'c' toggles the cheat screen that shows the original image in the corner + * 'g' grabs an image and saves the frame to a tiff image + * 'f' and 'F' increase and decrease the font size + */ +public void keyPressed() { + switch (key) { + case 'g': saveFrame(); break; + case 'c': cheatScreen = !cheatScreen; break; + case 'f': fontSize *= 1.1; break; + case 'F': fontSize *= 0.9; break; + } +} diff --git a/java/libraries/video/examples/Capture/BackgroundSubtraction/BackgroundSubtraction.pde b/java/libraries/video/examples/Capture/BackgroundSubtraction/BackgroundSubtraction.pde index efc09e8c1..3f6619ca6 100644 --- a/java/libraries/video/examples/Capture/BackgroundSubtraction/BackgroundSubtraction.pde +++ b/java/libraries/video/examples/Capture/BackgroundSubtraction/BackgroundSubtraction.pde @@ -1,71 +1,71 @@ -/** - * Background Subtraction - * by Golan Levin. - * - * GSVideo version by Andres Colubri. - * - * Detect the presence of people and objects in the frame using a simple - * background-subtraction technique. To initialize the background, press a key. - */ - - -import codeanticode.gsvideo.*; - -int numPixels; -int[] backgroundPixels; -GSCapture video; - -void setup() { - // Change size to 320 x 240 if too slow at 640 x 480 - size(640, 480); - - video = new GSCapture(this, width, height); - video.play(); - numPixels = video.width * video.height; - // Create array to store the background image - backgroundPixels = new int[numPixels]; - // Make the pixels[] array available for direct manipulation - loadPixels(); -} - -void draw() { - if (video.available()) { - video.read(); // Read a new video frame - video.loadPixels(); // Make the pixels of video available - // Difference between the current frame and the stored background - int presenceSum = 0; - for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... - // Fetch the current color in that location, and also the color - // of the background in that spot - color currColor = video.pixels[i]; - color bkgdColor = backgroundPixels[i]; - // Extract the red, green, and blue components of the current pixelÕs color - int currR = (currColor >> 16) & 0xFF; - int currG = (currColor >> 8) & 0xFF; - int currB = currColor & 0xFF; - // Extract the red, green, and blue components of the background pixelÕs color - int bkgdR = (bkgdColor >> 16) & 0xFF; - int bkgdG = (bkgdColor >> 8) & 0xFF; - int bkgdB = bkgdColor & 0xFF; - // Compute the difference of the red, green, and blue values - int diffR = abs(currR - bkgdR); - int diffG = abs(currG - bkgdG); - int diffB = abs(currB - bkgdB); - // Add these differences to the running tally - presenceSum += diffR + diffG + diffB; - // Render the difference image to the screen - pixels[i] = color(diffR, diffG, diffB); - // The following line does the same thing much faster, but is more technical - //pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB; - } - updatePixels(); // Notify that the pixels[] array has changed - println(presenceSum); // Print out the total amount of movement - } -} - -// When a key is pressed, capture the background image into the backgroundPixels -// buffer, by copying each of the current frameÕs pixels into it. -void keyPressed() { - video.loadPixels(); - arraycopy(video.pixels, backgroundPixels); -} +/** + * Background Subtraction + * by Golan Levin. + * + * GSVideo version by Andres Colubri. + * + * Detect the presence of people and objects in the frame using a simple + * background-subtraction technique. To initialize the background, press a key. + */ + + +import codeanticode.gsvideo.*; + +int numPixels; +int[] backgroundPixels; +GSCapture video; + +void setup() { + // Change size to 320 x 240 if too slow at 640 x 480 + size(640, 480); + + video = new GSCapture(this, width, height); + video.start(); + numPixels = video.width * video.height; + // Create array to store the background image + backgroundPixels = new int[numPixels]; + // Make the pixels[] array available for direct manipulation + loadPixels(); +} + +void draw() { + if (video.available()) { + video.read(); // Read a new video frame + video.loadPixels(); // Make the pixels of video available + // Difference between the current frame and the stored background + int presenceSum = 0; + for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... + // Fetch the current color in that location, and also the color + // of the background in that spot + color currColor = video.pixels[i]; + color bkgdColor = backgroundPixels[i]; + // Extract the red, green, and blue components of the current pixel�s color + int currR = (currColor >> 16) & 0xFF; + int currG = (currColor >> 8) & 0xFF; + int currB = currColor & 0xFF; + // Extract the red, green, and blue components of the background pixel�s color + int bkgdR = (bkgdColor >> 16) & 0xFF; + int bkgdG = (bkgdColor >> 8) & 0xFF; + int bkgdB = bkgdColor & 0xFF; + // Compute the difference of the red, green, and blue values + int diffR = abs(currR - bkgdR); + int diffG = abs(currG - bkgdG); + int diffB = abs(currB - bkgdB); + // Add these differences to the running tally + presenceSum += diffR + diffG + diffB; + // Render the difference image to the screen + pixels[i] = color(diffR, diffG, diffB); + // The following line does the same thing much faster, but is more technical + //pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB; + } + updatePixels(); // Notify that the pixels[] array has changed + println(presenceSum); // Print out the total amount of movement + } +} + +// When a key is pressed, capture the background image into the backgroundPixels +// buffer, by copying each of the current frame�s pixels into it. +void keyPressed() { + video.loadPixels(); + arraycopy(video.pixels, backgroundPixels); +} diff --git a/java/libraries/video/examples/Capture/BrightnessThresholding/BrightnessThresholding.pde b/java/libraries/video/examples/Capture/BrightnessThresholding/BrightnessThresholding.pde index c83c9826f..825a4a92d 100644 --- a/java/libraries/video/examples/Capture/BrightnessThresholding/BrightnessThresholding.pde +++ b/java/libraries/video/examples/Capture/BrightnessThresholding/BrightnessThresholding.pde @@ -1,60 +1,60 @@ -/** - * Brightness Thresholding - * by Golan Levin. - * - * GSVideo version by Andres Colubri. - * - * Determines whether a test location (such as the cursor) is contained within - * the silhouette of a dark object. - */ - - -import codeanticode.gsvideo.*; - -color black = color(0); -color white = color(255); -int numPixels; -GSCapture video; - -void setup() { - size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 - strokeWeight(5); - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, width, height); - video.play(); - numPixels = video.width * video.height; - noCursor(); - smooth(); -} - -void draw() { - if (video.available()) { - video.read(); - video.loadPixels(); - int threshold = 127; // Set the threshold value - float pixelBrightness; // Declare variable to store a pixel's color - // Turn each pixel in the video frame black or white depending on its brightness - loadPixels(); - for (int i = 0; i < numPixels; i++) { - pixelBrightness = brightness(video.pixels[i]); - if (pixelBrightness > threshold) { // If the pixel is brighter than the - pixels[i] = white; // threshold value, make it white - } - else { // Otherwise, - pixels[i] = black; // make it black - } - } - updatePixels(); - // Test a location to see where it is contained. Fetch the pixel at the test - // location (the cursor), and compute its brightness - int testValue = get(mouseX, mouseY); - float testBrightness = brightness(testValue); - if (testBrightness > threshold) { // If the test location is brighter than - fill(black); // the threshold set the fill to black - } - else { // Otherwise, - fill(white); // set the fill to white - } - ellipse(mouseX, mouseY, 20, 20); - } -} +/** + * Brightness Thresholding + * by Golan Levin. + * + * GSVideo version by Andres Colubri. + * + * Determines whether a test location (such as the cursor) is contained within + * the silhouette of a dark object. + */ + + +import codeanticode.gsvideo.*; + +color black = color(0); +color white = color(255); +int numPixels; +GSCapture video; + +void setup() { + size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 + strokeWeight(5); + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, width, height); + video.start(); + numPixels = video.width * video.height; + noCursor(); + smooth(); +} + +void draw() { + if (video.available()) { + video.read(); + video.loadPixels(); + int threshold = 127; // Set the threshold value + float pixelBrightness; // Declare variable to store a pixel's color + // Turn each pixel in the video frame black or white depending on its brightness + loadPixels(); + for (int i = 0; i < numPixels; i++) { + pixelBrightness = brightness(video.pixels[i]); + if (pixelBrightness > threshold) { // If the pixel is brighter than the + pixels[i] = white; // threshold value, make it white + } + else { // Otherwise, + pixels[i] = black; // make it black + } + } + updatePixels(); + // Test a location to see where it is contained. Fetch the pixel at the test + // location (the cursor), and compute its brightness + int testValue = get(mouseX, mouseY); + float testBrightness = brightness(testValue); + if (testBrightness > threshold) { // If the test location is brighter than + fill(black); // the threshold set the fill to black + } + else { // Otherwise, + fill(white); // set the fill to white + } + ellipse(mouseX, mouseY, 20, 20); + } +} diff --git a/java/libraries/video/examples/Capture/BrightnessTracking/BrightnessTracking.pde b/java/libraries/video/examples/Capture/BrightnessTracking/BrightnessTracking.pde index d06044f1a..01678794e 100644 --- a/java/libraries/video/examples/Capture/BrightnessTracking/BrightnessTracking.pde +++ b/java/libraries/video/examples/Capture/BrightnessTracking/BrightnessTracking.pde @@ -1,55 +1,55 @@ -/** - * Brightness Tracking - * by Golan Levin. - * - * GSVideo version by Andres Colubri. - * - * Tracks the brightest pixel in a live video signal. - */ - - -import codeanticode.gsvideo.*; - -GSCapture video; - -void setup() { - size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, width, height); - video.play(); - noStroke(); - smooth(); -} - -void draw() { - if (video.available()) { - video.read(); - image(video, 0, 0, width, height); // Draw the webcam video onto the screen - int brightestX = 0; // X-coordinate of the brightest video pixel - int brightestY = 0; // Y-coordinate of the brightest video pixel - float brightestValue = 0; // Brightness of the brightest video pixel - // Search for the brightest pixel: For each row of pixels in the video image and - // for each pixel in the yth row, compute each pixel's index in the video - video.loadPixels(); - int index = 0; - for (int y = 0; y < video.height; y++) { - for (int x = 0; x < video.width; x++) { - // Get the color stored in the pixel - int pixelValue = video.pixels[index]; - // Determine the brightness of the pixel - float pixelBrightness = brightness(pixelValue); - // If that value is brighter than any previous, then store the - // brightness of that pixel, as well as its (x,y) location - if (pixelBrightness > brightestValue) { - brightestValue = pixelBrightness; - brightestY = y; - brightestX = x; - } - index++; - } - } - // Draw a large, yellow circle at the brightest pixel - fill(255, 204, 0, 128); - ellipse(brightestX, brightestY, 200, 200); - } -} +/** + * Brightness Tracking + * by Golan Levin. + * + * GSVideo version by Andres Colubri. + * + * Tracks the brightest pixel in a live video signal. + */ + + +import codeanticode.gsvideo.*; + +GSCapture video; + +void setup() { + size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, width, height); + video.start(); + noStroke(); + smooth(); +} + +void draw() { + if (video.available()) { + video.read(); + image(video, 0, 0, width, height); // Draw the webcam video onto the screen + int brightestX = 0; // X-coordinate of the brightest video pixel + int brightestY = 0; // Y-coordinate of the brightest video pixel + float brightestValue = 0; // Brightness of the brightest video pixel + // Search for the brightest pixel: For each row of pixels in the video image and + // for each pixel in the yth row, compute each pixel's index in the video + video.loadPixels(); + int index = 0; + for (int y = 0; y < video.height; y++) { + for (int x = 0; x < video.width; x++) { + // Get the color stored in the pixel + int pixelValue = video.pixels[index]; + // Determine the brightness of the pixel + float pixelBrightness = brightness(pixelValue); + // If that value is brighter than any previous, then store the + // brightness of that pixel, as well as its (x,y) location + if (pixelBrightness > brightestValue) { + brightestValue = pixelBrightness; + brightestY = y; + brightestX = x; + } + index++; + } + } + // Draw a large, yellow circle at the brightest pixel + fill(255, 204, 0, 128); + ellipse(brightestX, brightestY, 200, 200); + } +} diff --git a/java/libraries/video/examples/Capture/ColorSorting/ColorSorting.pde b/java/libraries/video/examples/Capture/ColorSorting/ColorSorting.pde index 5efbff70e..b61ac8b6b 100644 --- a/java/libraries/video/examples/Capture/ColorSorting/ColorSorting.pde +++ b/java/libraries/video/examples/Capture/ColorSorting/ColorSorting.pde @@ -1,146 +1,146 @@ -/** - * Color Sorting - * by Ben Fry. - * - * GSVideo version by Andres Colubri. - * - * Example that sorts all colors from the incoming video - * and arranges them into vertical bars. - */ - - -import codeanticode.gsvideo.*; - -GSCapture video; -boolean cheatScreen; - -Tuple[] captureColors; -Tuple[] drawColors; -int[] bright; - -// How many pixels to skip in either direction -int increment = 5; - - -public void setup() { - size(800, 600, P3D); - - noCursor(); - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, 160, 120); - video.play(); - - int count = (video.width * video.height) / (increment * increment); - bright = new int[count]; - captureColors = new Tuple[count]; - drawColors = new Tuple[count]; - for (int i = 0; i < count; i++) { - captureColors[i] = new Tuple(); - drawColors[i] = new Tuple(0.5, 0.5, 0.5); - } -} - - -public void draw() { - if (video.available()) { - video.read(); - - background(0); - noStroke(); - - int index = 0; - for (int j = 0; j < video.height; j += increment) { - for (int i = 0; i < video.width; i += increment) { - int pixelColor = video.pixels[j*video.width + i]; - - int r = (pixelColor >> 16) & 0xff; - int g = (pixelColor >> 8) & 0xff; - int b = pixelColor & 0xff; - - // Technically would be sqrt of the following, but no need to do - // sqrt before comparing the elements since we're only ordering - bright[index] = r*r + g*g + b*b; - captureColors[index].set(r, g, b); - - index++; - } - } - sort(index, bright, captureColors); - - beginShape(QUAD_STRIP); - for (int i = 0; i < index; i++) { - drawColors[i].target(captureColors[i], 0.1); - drawColors[i].phil(); - - float x = map(i, 0, index, 0, width); - vertex(x, 0); - vertex(x, height); - } - endShape(); - - if (cheatScreen) { - //image(video, 0, height - video.height); - // Faster method of displaying pixels array on screen - set(0, height - video.height, video); - } - } -} - - -public void keyPressed() { - if (key == 'g') { - saveFrame(); - } else if (key == 'c') { - cheatScreen = !cheatScreen; - } -} - - -// Functions to handle sorting the color data - - -void sort(int length, int[] a, Tuple[] stuff) { - sortSub(a, stuff, 0, length - 1); -} - - -void sortSwap(int[] a, Tuple[] stuff, int i, int j) { - int T = a[i]; - a[i] = a[j]; - a[j] = T; - - Tuple v = stuff[i]; - stuff[i] = stuff[j]; - stuff[j] = v; -} - - -void sortSub(int[] a, Tuple[] stuff, int lo0, int hi0) { - int lo = lo0; - int hi = hi0; - int mid; - - if (hi0 > lo0) { - mid = a[(lo0 + hi0) / 2]; - - while (lo <= hi) { - while ((lo < hi0) && (a[lo] < mid)) { - ++lo; - } - while ((hi > lo0) && (a[hi] > mid)) { - --hi; - } - if (lo <= hi) { - sortSwap(a, stuff, lo, hi); - ++lo; - --hi; - } - } - - if (lo0 < hi) - sortSub(a, stuff, lo0, hi); - - if (lo < hi0) - sortSub(a, stuff, lo, hi0); - } -} +/** + * Color Sorting + * by Ben Fry. + * + * GSVideo version by Andres Colubri. + * + * Example that sorts all colors from the incoming video + * and arranges them into vertical bars. + */ + + +import codeanticode.gsvideo.*; + +GSCapture video; +boolean cheatScreen; + +Tuple[] captureColors; +Tuple[] drawColors; +int[] bright; + +// How many pixels to skip in either direction +int increment = 5; + + +public void setup() { + size(800, 600, P3D); + + noCursor(); + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, 160, 120); + video.start(); + + int count = (video.width * video.height) / (increment * increment); + bright = new int[count]; + captureColors = new Tuple[count]; + drawColors = new Tuple[count]; + for (int i = 0; i < count; i++) { + captureColors[i] = new Tuple(); + drawColors[i] = new Tuple(0.5, 0.5, 0.5); + } +} + + +public void draw() { + if (video.available()) { + video.read(); + + background(0); + noStroke(); + + int index = 0; + for (int j = 0; j < video.height; j += increment) { + for (int i = 0; i < video.width; i += increment) { + int pixelColor = video.pixels[j*video.width + i]; + + int r = (pixelColor >> 16) & 0xff; + int g = (pixelColor >> 8) & 0xff; + int b = pixelColor & 0xff; + + // Technically would be sqrt of the following, but no need to do + // sqrt before comparing the elements since we're only ordering + bright[index] = r*r + g*g + b*b; + captureColors[index].set(r, g, b); + + index++; + } + } + sort(index, bright, captureColors); + + beginShape(QUAD_STRIP); + for (int i = 0; i < index; i++) { + drawColors[i].target(captureColors[i], 0.1); + drawColors[i].phil(); + + float x = map(i, 0, index, 0, width); + vertex(x, 0); + vertex(x, height); + } + endShape(); + + if (cheatScreen) { + //image(video, 0, height - video.height); + // Faster method of displaying pixels array on screen + set(0, height - video.height, video); + } + } +} + + +public void keyPressed() { + if (key == 'g') { + saveFrame(); + } else if (key == 'c') { + cheatScreen = !cheatScreen; + } +} + + +// Functions to handle sorting the color data + + +void sort(int length, int[] a, Tuple[] stuff) { + sortSub(a, stuff, 0, length - 1); +} + + +void sortSwap(int[] a, Tuple[] stuff, int i, int j) { + int T = a[i]; + a[i] = a[j]; + a[j] = T; + + Tuple v = stuff[i]; + stuff[i] = stuff[j]; + stuff[j] = v; +} + + +void sortSub(int[] a, Tuple[] stuff, int lo0, int hi0) { + int lo = lo0; + int hi = hi0; + int mid; + + if (hi0 > lo0) { + mid = a[(lo0 + hi0) / 2]; + + while (lo <= hi) { + while ((lo < hi0) && (a[lo] < mid)) { + ++lo; + } + while ((hi > lo0) && (a[hi] > mid)) { + --hi; + } + if (lo <= hi) { + sortSwap(a, stuff, lo, hi); + ++lo; + --hi; + } + } + + if (lo0 < hi) + sortSub(a, stuff, lo0, hi); + + if (lo < hi0) + sortSub(a, stuff, lo, hi0); + } +} diff --git a/java/libraries/video/examples/Capture/Disgrand/Disgrand.pde b/java/libraries/video/examples/Capture/Disgrand/Disgrand.pde index 9b1eda976..ad06f8a8c 100644 --- a/java/libraries/video/examples/Capture/Disgrand/Disgrand.pde +++ b/java/libraries/video/examples/Capture/Disgrand/Disgrand.pde @@ -1,146 +1,146 @@ -/** - * Disgrand - * by Ben Fry. - * - * GSVideo version by Andres Colubri. - * - * Example that sorts all colors from the incoming video - * and arranges them into vertical bars. - */ - - -import codeanticode.gsvideo.*; - -GSCapture video; -boolean cheatScreen; - -Tuple[] captureColors; -Tuple[] drawColors; -int[] bright; - -// How many pixels to skip in either direction -int increment = 5; - - -public void setup() { - size(800, 600, P3D); - - noCursor(); - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, 160, 120); - video.play(); - - int count = (video.width * video.height) / (increment * increment); - bright = new int[count]; - captureColors = new Tuple[count]; - drawColors = new Tuple[count]; - for (int i = 0; i < count; i++) { - captureColors[i] = new Tuple(); - drawColors[i] = new Tuple(0.5, 0.5, 0.5); - } -} - - -public void draw() { - if (video.available()) { - video.read(); - - background(0); - noStroke(); - - int index = 0; - for (int j = 0; j < video.height; j += increment) { - for (int i = 0; i < video.width; i += increment) { - int pixelColor = video.pixels[j*video.width + i]; - - int r = (pixelColor >> 16) & 0xff; - int g = (pixelColor >> 8) & 0xff; - int b = pixelColor & 0xff; - - // Technically would be sqrt of the following, but no need to do - // sqrt before comparing the elements since we're only ordering - bright[index] = r*r + g*g + b*b; - captureColors[index].set(r, g, b); - - index++; - } - } - sort(index, bright, captureColors); - - beginShape(QUAD_STRIP); - for (int i = 0; i < index; i++) { - drawColors[i].target(captureColors[i], 0.1); - drawColors[i].phil(); - - float x = map(i, 0, index, 0, width); - vertex(x, 0); - vertex(x, height); - } - endShape(); - - if (cheatScreen) { - //image(video, 0, height - video.height); - // Faster method of displaying pixels array on screen - set(0, height - video.height, video); - } - } -} - - -public void keyPressed() { - if (key == 'g') { - saveFrame(); - } else if (key == 'c') { - cheatScreen = !cheatScreen; - } -} - - -// Functions to handle sorting the color data - - -void sort(int length, int[] a, Tuple[] stuff) { - sortSub(a, stuff, 0, length - 1); -} - - -void sortSwap(int[] a, Tuple[] stuff, int i, int j) { - int T = a[i]; - a[i] = a[j]; - a[j] = T; - - Tuple v = stuff[i]; - stuff[i] = stuff[j]; - stuff[j] = v; -} - - -void sortSub(int[] a, Tuple[] stuff, int lo0, int hi0) { - int lo = lo0; - int hi = hi0; - int mid; - - if (hi0 > lo0) { - mid = a[(lo0 + hi0) / 2]; - - while (lo <= hi) { - while ((lo < hi0) && (a[lo] < mid)) { - ++lo; - } - while ((hi > lo0) && (a[hi] > mid)) { - --hi; - } - if (lo <= hi) { - sortSwap(a, stuff, lo, hi); - ++lo; - --hi; - } - } - - if (lo0 < hi) - sortSub(a, stuff, lo0, hi); - - if (lo < hi0) - sortSub(a, stuff, lo, hi0); - } -} +/** + * Disgrand + * by Ben Fry. + * + * GSVideo version by Andres Colubri. + * + * Example that sorts all colors from the incoming video + * and arranges them into vertical bars. + */ + + +import codeanticode.gsvideo.*; + +GSCapture video; +boolean cheatScreen; + +Tuple[] captureColors; +Tuple[] drawColors; +int[] bright; + +// How many pixels to skip in either direction +int increment = 5; + + +public void setup() { + size(800, 600, P3D); + + noCursor(); + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, 160, 120); + video.start(); + + int count = (video.width * video.height) / (increment * increment); + bright = new int[count]; + captureColors = new Tuple[count]; + drawColors = new Tuple[count]; + for (int i = 0; i < count; i++) { + captureColors[i] = new Tuple(); + drawColors[i] = new Tuple(0.5, 0.5, 0.5); + } +} + + +public void draw() { + if (video.available()) { + video.read(); + + background(0); + noStroke(); + + int index = 0; + for (int j = 0; j < video.height; j += increment) { + for (int i = 0; i < video.width; i += increment) { + int pixelColor = video.pixels[j*video.width + i]; + + int r = (pixelColor >> 16) & 0xff; + int g = (pixelColor >> 8) & 0xff; + int b = pixelColor & 0xff; + + // Technically would be sqrt of the following, but no need to do + // sqrt before comparing the elements since we're only ordering + bright[index] = r*r + g*g + b*b; + captureColors[index].set(r, g, b); + + index++; + } + } + sort(index, bright, captureColors); + + beginShape(QUAD_STRIP); + for (int i = 0; i < index; i++) { + drawColors[i].target(captureColors[i], 0.1); + drawColors[i].phil(); + + float x = map(i, 0, index, 0, width); + vertex(x, 0); + vertex(x, height); + } + endShape(); + + if (cheatScreen) { + //image(video, 0, height - video.height); + // Faster method of displaying pixels array on screen + set(0, height - video.height, video); + } + } +} + + +public void keyPressed() { + if (key == 'g') { + saveFrame(); + } else if (key == 'c') { + cheatScreen = !cheatScreen; + } +} + + +// Functions to handle sorting the color data + + +void sort(int length, int[] a, Tuple[] stuff) { + sortSub(a, stuff, 0, length - 1); +} + + +void sortSwap(int[] a, Tuple[] stuff, int i, int j) { + int T = a[i]; + a[i] = a[j]; + a[j] = T; + + Tuple v = stuff[i]; + stuff[i] = stuff[j]; + stuff[j] = v; +} + + +void sortSub(int[] a, Tuple[] stuff, int lo0, int hi0) { + int lo = lo0; + int hi = hi0; + int mid; + + if (hi0 > lo0) { + mid = a[(lo0 + hi0) / 2]; + + while (lo <= hi) { + while ((lo < hi0) && (a[lo] < mid)) { + ++lo; + } + while ((hi > lo0) && (a[hi] > mid)) { + --hi; + } + if (lo <= hi) { + sortSwap(a, stuff, lo, hi); + ++lo; + --hi; + } + } + + if (lo0 < hi) + sortSub(a, stuff, lo0, hi); + + if (lo < hi0) + sortSub(a, stuff, lo, hi0); + } +} diff --git a/java/libraries/video/examples/Capture/FrameDifferencing/FrameDifferencing.pde b/java/libraries/video/examples/Capture/FrameDifferencing/FrameDifferencing.pde index 7452203c8..897bc8cfd 100644 --- a/java/libraries/video/examples/Capture/FrameDifferencing/FrameDifferencing.pde +++ b/java/libraries/video/examples/Capture/FrameDifferencing/FrameDifferencing.pde @@ -1,67 +1,67 @@ -/** - * Frame Differencing - * by Golan Levin. - * - * GSVideo version by Andres Colubri. - * - * Quantify the amount of movement in the video frame using frame-differencing. - */ - - -import codeanticode.gsvideo.*; - -int numPixels; -int[] previousFrame; -GSCapture video; - -void setup() { - size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, width, height); - video.play(); - numPixels = video.width * video.height; - // Create an array to store the previously captured frame - previousFrame = new int[numPixels]; - loadPixels(); -} - -void draw() { - if (video.available()) { - // When using video to manipulate the screen, use video.available() and - // video.read() inside the draw() method so that it's safe to draw to the screen - video.read(); // Read the new frame from the camera - video.loadPixels(); // Make its pixels[] array available - - int movementSum = 0; // Amount of movement in the frame - for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... - color currColor = video.pixels[i]; - color prevColor = previousFrame[i]; - // Extract the red, green, and blue components from current pixel - int currR = (currColor >> 16) & 0xFF; // Like red(), but faster - int currG = (currColor >> 8) & 0xFF; - int currB = currColor & 0xFF; - // Extract red, green, and blue components from previous pixel - int prevR = (prevColor >> 16) & 0xFF; - int prevG = (prevColor >> 8) & 0xFF; - int prevB = prevColor & 0xFF; - // Compute the difference of the red, green, and blue values - int diffR = abs(currR - prevR); - int diffG = abs(currG - prevG); - int diffB = abs(currB - prevB); - // Add these differences to the running tally - movementSum += diffR + diffG + diffB; - // Render the difference image to the screen - pixels[i] = color(diffR, diffG, diffB); - // The following line is much faster, but more confusing to read - //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB; - // Save the current color into the 'previous' buffer - previousFrame[i] = currColor; - } - // To prevent flicker from frames that are all black (no movement), - // only update the screen if the image has changed. - if (movementSum > 0) { - updatePixels(); - println(movementSum); // Print the total amount of movement to the console - } - } -} +/** + * Frame Differencing + * by Golan Levin. + * + * GSVideo version by Andres Colubri. + * + * Quantify the amount of movement in the video frame using frame-differencing. + */ + + +import codeanticode.gsvideo.*; + +int numPixels; +int[] previousFrame; +GSCapture video; + +void setup() { + size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, width, height); + video.start(); + numPixels = video.width * video.height; + // Create an array to store the previously captured frame + previousFrame = new int[numPixels]; + loadPixels(); +} + +void draw() { + if (video.available()) { + // When using video to manipulate the screen, use video.available() and + // video.read() inside the draw() method so that it's safe to draw to the screen + video.read(); // Read the new frame from the camera + video.loadPixels(); // Make its pixels[] array available + + int movementSum = 0; // Amount of movement in the frame + for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... + color currColor = video.pixels[i]; + color prevColor = previousFrame[i]; + // Extract the red, green, and blue components from current pixel + int currR = (currColor >> 16) & 0xFF; // Like red(), but faster + int currG = (currColor >> 8) & 0xFF; + int currB = currColor & 0xFF; + // Extract red, green, and blue components from previous pixel + int prevR = (prevColor >> 16) & 0xFF; + int prevG = (prevColor >> 8) & 0xFF; + int prevB = prevColor & 0xFF; + // Compute the difference of the red, green, and blue values + int diffR = abs(currR - prevR); + int diffG = abs(currG - prevG); + int diffB = abs(currB - prevB); + // Add these differences to the running tally + movementSum += diffR + diffG + diffB; + // Render the difference image to the screen + pixels[i] = color(diffR, diffG, diffB); + // The following line is much faster, but more confusing to read + //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB; + // Save the current color into the 'previous' buffer + previousFrame[i] = currColor; + } + // To prevent flicker from frames that are all black (no movement), + // only update the screen if the image has changed. + if (movementSum > 0) { + updatePixels(); + println(movementSum); // Print the total amount of movement to the console + } + } +} diff --git a/java/libraries/video/examples/Capture/Framingham/Framingham.pde b/java/libraries/video/examples/Capture/Framingham/Framingham.pde index b7ae30c99..e522c9aab 100644 --- a/java/libraries/video/examples/Capture/Framingham/Framingham.pde +++ b/java/libraries/video/examples/Capture/Framingham/Framingham.pde @@ -1,61 +1,61 @@ -/** - * Framingham - * by Ben Fry. - * - * GSVideo version by Andres Colubri. - * - * Show subsequent frames from video input as a grid. Also fun with movie files. - */ - - -import codeanticode.gsvideo.*; - -GSCapture video; -int column; -int columnCount; -int lastRow; - -// Buffer used to move all the pixels up -int[] scoot; - - -void setup() { - size(640, 480, P3D); - - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, 160, 120); - video.play(); - // Also try with other video sizes - - column = 0; - columnCount = width / video.width; - int rowCount = height / video.height; - lastRow = rowCount - 1; - - scoot = new int[lastRow*video.height * width]; - background(0); -} - - -void draw() { - // By using video.available, only the frame rate need be set inside setup() - if (video.available()) { - video.read(); - set(video.width*column, video.height*lastRow, video); - column++; - if (column == columnCount) { - loadPixels(); - - // Scoot everybody up one row - arraycopy(pixels, video.height*width, scoot, 0, scoot.length); - arraycopy(scoot, 0, pixels, 0, scoot.length); - - // Set the moved row to black - for (int i = scoot.length; i < width*height; i++) { - pixels[i] = #000000; - } - column = 0; - updatePixels(); - } - } -} +/** + * Framingham + * by Ben Fry. + * + * GSVideo version by Andres Colubri. + * + * Show subsequent frames from video input as a grid. Also fun with movie files. + */ + + +import codeanticode.gsvideo.*; + +GSCapture video; +int column; +int columnCount; +int lastRow; + +// Buffer used to move all the pixels up +int[] scoot; + + +void setup() { + size(640, 480, P3D); + + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, 160, 120); + video.start(); + // Also try with other video sizes + + column = 0; + columnCount = width / video.width; + int rowCount = height / video.height; + lastRow = rowCount - 1; + + scoot = new int[lastRow*video.height * width]; + background(0); +} + + +void draw() { + // By using video.available, only the frame rate need be set inside setup() + if (video.available()) { + video.read(); + set(video.width*column, video.height*lastRow, video); + column++; + if (column == columnCount) { + loadPixels(); + + // Scoot everybody up one row + arraycopy(pixels, video.height*width, scoot, 0, scoot.length); + arraycopy(scoot, 0, pixels, 0, scoot.length); + + // Set the moved row to black + for (int i = scoot.length; i < width*height; i++) { + pixels[i] = #000000; + } + column = 0; + updatePixels(); + } + } +} diff --git a/java/libraries/video/examples/Capture/GettingStartedCaptureLinux/GettingStartedCaptureLinux.pde b/java/libraries/video/examples/Capture/GettingStartedCaptureLinux/GettingStartedCaptureLinux.pde index 370db7722..f2016788a 100644 --- a/java/libraries/video/examples/Capture/GettingStartedCaptureLinux/GettingStartedCaptureLinux.pde +++ b/java/libraries/video/examples/Capture/GettingStartedCaptureLinux/GettingStartedCaptureLinux.pde @@ -34,7 +34,7 @@ void setup() { */ cam = new GSCapture(this, 640, 480); - cam.play(); + cam.start(); /* // You can get the resolutions supported by the diff --git a/java/libraries/video/examples/Capture/GettingStartedCaptureMac/GettingStartedCaptureMac.pde b/java/libraries/video/examples/Capture/GettingStartedCaptureMac/GettingStartedCaptureMac.pde index 2fa79694e..e9b43a8da 100644 --- a/java/libraries/video/examples/Capture/GettingStartedCaptureMac/GettingStartedCaptureMac.pde +++ b/java/libraries/video/examples/Capture/GettingStartedCaptureMac/GettingStartedCaptureMac.pde @@ -29,7 +29,7 @@ void setup() { } */ cam = new GSCapture(this, 640, 480); - cam.play(); + cam.start(); /* // You can get the resolutions supported by the diff --git a/java/libraries/video/examples/Capture/GettingStartedCaptureWin/GettingStartedCaptureWin.pde b/java/libraries/video/examples/Capture/GettingStartedCaptureWin/GettingStartedCaptureWin.pde index 1d7dcf937..42b798b1d 100644 --- a/java/libraries/video/examples/Capture/GettingStartedCaptureWin/GettingStartedCaptureWin.pde +++ b/java/libraries/video/examples/Capture/GettingStartedCaptureWin/GettingStartedCaptureWin.pde @@ -1,59 +1,59 @@ -/** - * Getting Started with Capture. - * - * GSVideo version by Andres Colubri. - * - * Reading and displaying an image from an attached Capture device. - */ -import codeanticode.gsvideo.*; - -GSCapture cam; - -void setup() { - size(640, 480); - - String[] cameras = GSCapture.list(); - - if (cameras.length == 0) - { - println("There are no cameras available for capture."); - exit(); - } else { - println("Available cameras:"); - for (int i = 0; i < cameras.length; i++) { - println(cameras[i]); - } - cam = new GSCapture(this, 640, 480, cameras[0]); - cam.play(); - - /* - // You can get the resolutions supported by the - // capture device using the resolutions() method. - // It must be called after creating the capture - // object. - int[][] res = cam.resolutions(); - for (int i = 0; i < res.length; i++) { - println(res[i][0] + "x" + res[i][1]); - } - */ - - /* - // You can also get the framerates supported by the - // capture device: - String[] fps = cam.framerates(); - for (int i = 0; i < fps.length; i++) { - println(fps[i]); - } - */ - } -} - -void draw() { - if (cam.available() == true) { - cam.read(); - image(cam, 0, 0); - // The following does the same, and is faster when just drawing the image - // without any additional resizing, transformations, or tint. - //set(0, 0, cam); - } -} +/** + * Getting Started with Capture. + * + * GSVideo version by Andres Colubri. + * + * Reading and displaying an image from an attached Capture device. + */ +import codeanticode.gsvideo.*; + +GSCapture cam; + +void setup() { + size(640, 480); + + String[] cameras = GSCapture.list(); + + if (cameras.length == 0) + { + println("There are no cameras available for capture."); + exit(); + } else { + println("Available cameras:"); + for (int i = 0; i < cameras.length; i++) { + println(cameras[i]); + } + cam = new GSCapture(this, 640, 480, cameras[0]); + cam.start(); + + /* + // You can get the resolutions supported by the + // capture device using the resolutions() method. + // It must be called after creating the capture + // object. + int[][] res = cam.resolutions(); + for (int i = 0; i < res.length; i++) { + println(res[i][0] + "x" + res[i][1]); + } + */ + + /* + // You can also get the framerates supported by the + // capture device: + String[] fps = cam.framerates(); + for (int i = 0; i < fps.length; i++) { + println(fps[i]); + } + */ + } +} + +void draw() { + if (cam.available() == true) { + cam.read(); + image(cam, 0, 0); + // The following does the same, and is faster when just drawing the image + // without any additional resizing, transformations, or tint. + //set(0, 0, cam); + } +} diff --git a/java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde b/java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde index 712486b11..b56e68f8b 100644 --- a/java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde +++ b/java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde @@ -1,208 +1,208 @@ -/** - * HSV Space - * by Ben Fry. - * - * GSVideo version by Andres Colubri. - * - * Arrange the pixels from live video into the HSV Color Cone. - */ -import processing.opengl.*; -import codeanticode.gsvideo.*; -import java.awt.Color; - -GSCapture video; -int count; -boolean cheatScreen = true; - -static final float BOX_SIZE = 0.75; -static final float CONE_HEIGHT = 1.2; -static final float MAX_RADIUS = 10; -static final float ROT_INCREMENT = 3.0; -static final float TRANS_INCREMENT = 1; -static final float STEP_AMOUNT = 0.1; - -Tuple[] farbe; -Tuple[] trans; - -float[] hsb = new float[3]; - -float leftRightAngle; -float upDownAngle; -float fwdBackTrans; -float upDownTrans; -float leftRightTrans; -boolean motion; - -boolean blobby = false; - - -public void setup() { - size(640, 480, P3D); - //size(screen.width, screen.height, OPENGL); - - video = new GSCapture(this, 160, 120); - video.play(); - count = video.width * video.height; - - sphereDetail(60); - - upDownTrans = 0; - leftRightTrans = 0; - motion = false; - - leftRightAngle = 101.501297; - upDownAngle = -180.098694; - fwdBackTrans = 14.800003; - - farbe = new Tuple[count]; - trans = new Tuple[count]; - for (int i = 0; i < count; i++) { - farbe[i] = new Tuple(); - trans[i] = new Tuple(); - } -} - - -void draw() { - background(0); - - if (!blobby) lights(); - - pushMatrix(); - translate(width/2, height/2); - scale(min(width, height) / 10.0); - - translate(0, 0, -20 + fwdBackTrans); - rotateY(radians(36 + leftRightAngle)); //, 0, 1, 0); - rotateX(radians(-228 + upDownAngle)); //, 1, 0, 0); - - if (blobby) { - stroke(0.35, 0.35, 0.25, 0.15); - wireCone(MAX_RADIUS, MAX_RADIUS * CONE_HEIGHT, 18, 18); - } - else { - stroke(0.35, 0.35, 0.25, 0.25); - wireCone(MAX_RADIUS, MAX_RADIUS * CONE_HEIGHT, 180, 18); - } - - noStroke(); - for (int i = 0; i < count; i++) { - int pixelColor = video.pixels[i]; - int r = (pixelColor >> 16) & 0xff; - int g = (pixelColor >> 8) & 0xff; - int b = pixelColor & 0xff; - Color.RGBtoHSB(r, g, b, hsb); - - float radius = hsb[1] * hsb[2]; - float angle = hsb[0] * 360.0 * DEG_TO_RAD; - float nx = MAX_RADIUS * radius * cos(angle); - float ny = MAX_RADIUS * radius * sin(angle); - float nz = hsb[2] * MAX_RADIUS * CONE_HEIGHT; - - trans[i].set(trans[i].x - (trans[i].x - nx)*STEP_AMOUNT, - trans[i].y - (trans[i].y - ny)*STEP_AMOUNT, - trans[i].z - (trans[i].z - nz)*STEP_AMOUNT); - - farbe[i].set(farbe[i].x - (farbe[i].x - r)*STEP_AMOUNT, - farbe[i].y - (farbe[i].y - g)*STEP_AMOUNT, - farbe[i].z - (farbe[i].z - b)*STEP_AMOUNT); - - pushMatrix(); - farbe[i].phil(); - trans[i].tran(); - - rotate(radians(45), 1, 1, 0); - if (blobby) { - sphere(BOX_SIZE * 2); //, 20, 20); - } else { - box(BOX_SIZE); - } - - popMatrix(); - } - popMatrix(); - - if (motion) { - upDownAngle--; - leftRightAngle--; - } - - if (cheatScreen) { - image(video, 0, height - video.height); - } -} - - -void captureEvent(GSCapture c) { - c.read(); - c.loadPixels(); -} - - -void keyPressed() { - switch (key) { - case 'g': - saveFrame(); - break; - case 'c': - cheatScreen = !cheatScreen; - break; - - case 'm': - motion = !motion; - break; - case '=': - fwdBackTrans += TRANS_INCREMENT; - break; - case '-': - fwdBackTrans -= TRANS_INCREMENT; - break; - case 'b': - blobby = !blobby; - break; - } -} - - -void mouseDragged() { - float dX, dY; - - switch (mouseButton) { - case LEFT: // left right up down - dX = pmouseX - mouseX; - dY = pmouseY - mouseY; - leftRightAngle -= dX * 0.2; - upDownAngle += dY * 0.4; - break; - - case CENTER: - dX = pmouseX - mouseX; - dY = pmouseY - mouseY; - leftRightTrans -= TRANS_INCREMENT * dX; - upDownTrans -= TRANS_INCREMENT * dY; - break; - - case RIGHT: // in and out - dY = (float) (pmouseY - mouseY); - fwdBackTrans -= TRANS_INCREMENT * dY; - break; - } -} - - -void wireCone(float radius, float height, int stepX, int stepY) { - int steps = 10; - stroke(40); - for (int i = 0; i < steps; i++) { - float angle = map(i, 0, steps, 0, TWO_PI); - float x = radius * cos(angle); - float y = radius * sin(angle); - line(x, y, height, 0, 0, 0); - } - noFill(); - pushMatrix(); - translate(0, 0, height); - ellipseMode(CENTER_RADIUS); - ellipse(0, 0, radius, radius); - popMatrix(); -} +/** + * HSV Space + * by Ben Fry. + * + * GSVideo version by Andres Colubri. + * + * Arrange the pixels from live video into the HSV Color Cone. + */ +import processing.opengl.*; +import codeanticode.gsvideo.*; +import java.awt.Color; + +GSCapture video; +int count; +boolean cheatScreen = true; + +static final float BOX_SIZE = 0.75; +static final float CONE_HEIGHT = 1.2; +static final float MAX_RADIUS = 10; +static final float ROT_INCREMENT = 3.0; +static final float TRANS_INCREMENT = 1; +static final float STEP_AMOUNT = 0.1; + +Tuple[] farbe; +Tuple[] trans; + +float[] hsb = new float[3]; + +float leftRightAngle; +float upDownAngle; +float fwdBackTrans; +float upDownTrans; +float leftRightTrans; +boolean motion; + +boolean blobby = false; + + +public void setup() { + size(640, 480, P3D); + //size(screen.width, screen.height, OPENGL); + + video = new GSCapture(this, 160, 120); + video.start(); + count = video.width * video.height; + + sphereDetail(60); + + upDownTrans = 0; + leftRightTrans = 0; + motion = false; + + leftRightAngle = 101.501297; + upDownAngle = -180.098694; + fwdBackTrans = 14.800003; + + farbe = new Tuple[count]; + trans = new Tuple[count]; + for (int i = 0; i < count; i++) { + farbe[i] = new Tuple(); + trans[i] = new Tuple(); + } +} + + +void draw() { + background(0); + + if (!blobby) lights(); + + pushMatrix(); + translate(width/2, height/2); + scale(min(width, height) / 10.0); + + translate(0, 0, -20 + fwdBackTrans); + rotateY(radians(36 + leftRightAngle)); //, 0, 1, 0); + rotateX(radians(-228 + upDownAngle)); //, 1, 0, 0); + + if (blobby) { + stroke(0.35, 0.35, 0.25, 0.15); + wireCone(MAX_RADIUS, MAX_RADIUS * CONE_HEIGHT, 18, 18); + } + else { + stroke(0.35, 0.35, 0.25, 0.25); + wireCone(MAX_RADIUS, MAX_RADIUS * CONE_HEIGHT, 180, 18); + } + + noStroke(); + for (int i = 0; i < count; i++) { + int pixelColor = video.pixels[i]; + int r = (pixelColor >> 16) & 0xff; + int g = (pixelColor >> 8) & 0xff; + int b = pixelColor & 0xff; + Color.RGBtoHSB(r, g, b, hsb); + + float radius = hsb[1] * hsb[2]; + float angle = hsb[0] * 360.0 * DEG_TO_RAD; + float nx = MAX_RADIUS * radius * cos(angle); + float ny = MAX_RADIUS * radius * sin(angle); + float nz = hsb[2] * MAX_RADIUS * CONE_HEIGHT; + + trans[i].set(trans[i].x - (trans[i].x - nx)*STEP_AMOUNT, + trans[i].y - (trans[i].y - ny)*STEP_AMOUNT, + trans[i].z - (trans[i].z - nz)*STEP_AMOUNT); + + farbe[i].set(farbe[i].x - (farbe[i].x - r)*STEP_AMOUNT, + farbe[i].y - (farbe[i].y - g)*STEP_AMOUNT, + farbe[i].z - (farbe[i].z - b)*STEP_AMOUNT); + + pushMatrix(); + farbe[i].phil(); + trans[i].tran(); + + rotate(radians(45), 1, 1, 0); + if (blobby) { + sphere(BOX_SIZE * 2); //, 20, 20); + } else { + box(BOX_SIZE); + } + + popMatrix(); + } + popMatrix(); + + if (motion) { + upDownAngle--; + leftRightAngle--; + } + + if (cheatScreen) { + image(video, 0, height - video.height); + } +} + + +void captureEvent(GSCapture c) { + c.read(); + c.loadPixels(); +} + + +void keyPressed() { + switch (key) { + case 'g': + saveFrame(); + break; + case 'c': + cheatScreen = !cheatScreen; + break; + + case 'm': + motion = !motion; + break; + case '=': + fwdBackTrans += TRANS_INCREMENT; + break; + case '-': + fwdBackTrans -= TRANS_INCREMENT; + break; + case 'b': + blobby = !blobby; + break; + } +} + + +void mouseDragged() { + float dX, dY; + + switch (mouseButton) { + case LEFT: // left right up down + dX = pmouseX - mouseX; + dY = pmouseY - mouseY; + leftRightAngle -= dX * 0.2; + upDownAngle += dY * 0.4; + break; + + case CENTER: + dX = pmouseX - mouseX; + dY = pmouseY - mouseY; + leftRightTrans -= TRANS_INCREMENT * dX; + upDownTrans -= TRANS_INCREMENT * dY; + break; + + case RIGHT: // in and out + dY = (float) (pmouseY - mouseY); + fwdBackTrans -= TRANS_INCREMENT * dY; + break; + } +} + + +void wireCone(float radius, float height, int stepX, int stepY) { + int steps = 10; + stroke(40); + for (int i = 0; i < steps; i++) { + float angle = map(i, 0, steps, 0, TWO_PI); + float x = radius * cos(angle); + float y = radius * sin(angle); + line(x, y, height, 0, 0, 0); + } + noFill(); + pushMatrix(); + translate(0, 0, height); + ellipseMode(CENTER_RADIUS); + ellipse(0, 0, radius, radius); + popMatrix(); +} diff --git a/java/libraries/video/examples/Capture/LivePocky/LivePocky.pde b/java/libraries/video/examples/Capture/LivePocky/LivePocky.pde index d632cd059..b1ec0ba67 100644 --- a/java/libraries/video/examples/Capture/LivePocky/LivePocky.pde +++ b/java/libraries/video/examples/Capture/LivePocky/LivePocky.pde @@ -1,56 +1,56 @@ -/** - * Live Pocky - * by Ben Fry. - * - * GSVideo version by Andres Colubri. - * - * Unwrap each frame of live video into a single line of pixels. - */ - -import codeanticode.gsvideo.*; - -GSCapture video; -int count; -int writeRow; -int maxRows; -int topRow; -int buffer[]; - - -void setup() { - size(600, 400); - - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, 160, 120); - video.play(); - - maxRows = height * 2; - buffer = new int[width * maxRows]; - writeRow = height - 1; - topRow = 0; - - //frameRate(10); - background(0); - loadPixels(); -} - - -void draw() { - for (int y = 0; y < height; y++) { - int row = (topRow + y) % maxRows; - arraycopy(buffer, row * width, g.pixels, y*width, width); - } - updatePixels(); -} - - -void captureEvent(GSCapture c) { - c.read(); - c.loadPixels(); - arraycopy(c.pixels, 0, buffer, writeRow * width, width); - writeRow++; - if (writeRow == maxRows) { - writeRow = 0; - } - topRow++; -} +/** + * Live Pocky + * by Ben Fry. + * + * GSVideo version by Andres Colubri. + * + * Unwrap each frame of live video into a single line of pixels. + */ + +import codeanticode.gsvideo.*; + +GSCapture video; +int count; +int writeRow; +int maxRows; +int topRow; +int buffer[]; + + +void setup() { + size(600, 400); + + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, 160, 120); + video.start(); + + maxRows = height * 2; + buffer = new int[width * maxRows]; + writeRow = height - 1; + topRow = 0; + + //frameRate(10); + background(0); + loadPixels(); +} + + +void draw() { + for (int y = 0; y < height; y++) { + int row = (topRow + y) % maxRows; + arraycopy(buffer, row * width, g.pixels, y*width, width); + } + updatePixels(); +} + + +void captureEvent(GSCapture c) { + c.read(); + c.loadPixels(); + arraycopy(c.pixels, 0, buffer, writeRow * width, width); + writeRow++; + if (writeRow == maxRows) { + writeRow = 0; + } + topRow++; +} diff --git a/java/libraries/video/examples/Capture/Mirror/Mirror.pde b/java/libraries/video/examples/Capture/Mirror/Mirror.pde index a385e1d7f..91bc36b57 100644 --- a/java/libraries/video/examples/Capture/Mirror/Mirror.pde +++ b/java/libraries/video/examples/Capture/Mirror/Mirror.pde @@ -1,75 +1,75 @@ -/** - * Mirror - * by Daniel Shiffman. - * - * GSVideo version by Andres Colubri. - * - * Each pixel from the video source is drawn as a rectangle with rotation based on brightness. - */ - -import codeanticode.gsvideo.*; - - -// Size of each cell in the grid -int cellSize = 20; -// Number of columns and rows in our system -int cols, rows; -// Variable for capture device -GSCapture video; - - -void setup() { - size(640, 480, P3D); - frameRate(30); - cols = width / cellSize; - rows = height / cellSize; - colorMode(RGB, 255, 255, 255, 100); - - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, width, height); - video.play(); - - background(0); -} - - -void draw() { - if (video.available()) { - video.read(); - video.loadPixels(); - - // Not bothering to clear background - // background(0); - - // Begin loop for columns - for (int i = 0; i < cols; i++) { - // Begin loop for rows - for (int j = 0; j < rows; j++) { - - // Where are we, pixel-wise? - int x = i*cellSize; - int y = j*cellSize; - int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image - - float r = red(video.pixels[loc]); - float g = green(video.pixels[loc]); - float b = blue(video.pixels[loc]); - // Make a new color with an alpha component - color c = color(r, g, b, 75); - - // Code for drawing a single rect - // Using translate in order for rotation to work properly - pushMatrix(); - translate(x+cellSize/2, y+cellSize/2); - // Rotation formula based on brightness - rotate((2 * PI * brightness(c) / 255.0)); - rectMode(CENTER); - fill(c); - noStroke(); - // Rects are larger than the cell for some overlap - rect(0, 0, cellSize+6, cellSize+6); - popMatrix(); - } - } - } -} +/** + * Mirror + * by Daniel Shiffman. + * + * GSVideo version by Andres Colubri. + * + * Each pixel from the video source is drawn as a rectangle with rotation based on brightness. + */ + +import codeanticode.gsvideo.*; + + +// Size of each cell in the grid +int cellSize = 20; +// Number of columns and rows in our system +int cols, rows; +// Variable for capture device +GSCapture video; + + +void setup() { + size(640, 480, P3D); + frameRate(30); + cols = width / cellSize; + rows = height / cellSize; + colorMode(RGB, 255, 255, 255, 100); + + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, width, height); + video.start(); + + background(0); +} + + +void draw() { + if (video.available()) { + video.read(); + video.loadPixels(); + + // Not bothering to clear background + // background(0); + + // Begin loop for columns + for (int i = 0; i < cols; i++) { + // Begin loop for rows + for (int j = 0; j < rows; j++) { + + // Where are we, pixel-wise? + int x = i*cellSize; + int y = j*cellSize; + int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image + + float r = red(video.pixels[loc]); + float g = green(video.pixels[loc]); + float b = blue(video.pixels[loc]); + // Make a new color with an alpha component + color c = color(r, g, b, 75); + + // Code for drawing a single rect + // Using translate in order for rotation to work properly + pushMatrix(); + translate(x+cellSize/2, y+cellSize/2); + // Rotation formula based on brightness + rotate((2 * PI * brightness(c) / 255.0)); + rectMode(CENTER); + fill(c); + noStroke(); + // Rects are larger than the cell for some overlap + rect(0, 0, cellSize+6, cellSize+6); + popMatrix(); + } + } + } +} diff --git a/java/libraries/video/examples/Capture/Mirror2/Mirror2.pde b/java/libraries/video/examples/Capture/Mirror2/Mirror2.pde index d8128b209..4ba79c591 100644 --- a/java/libraries/video/examples/Capture/Mirror2/Mirror2.pde +++ b/java/libraries/video/examples/Capture/Mirror2/Mirror2.pde @@ -1,62 +1,62 @@ -/** - * Mirror 2 - * by Daniel Shiffman. - * - * GSVideo version by Andres Colubri. - * - * Each pixel from the video source is drawn as a rectangle with size based on brightness. - */ - -import codeanticode.gsvideo.*; - -// Size of each cell in the grid -int cellSize = 15; -// Number of columns and rows in our system -int cols, rows; -// Variable for capture device -GSCapture video; - - -void setup() { - size(640, 480, P3D); - //set up columns and rows - cols = width / cellSize; - rows = height / cellSize; - colorMode(RGB, 255, 255, 255, 100); - rectMode(CENTER); - - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, width, height); - video.play(); - - background(0); -} - - -void draw() { - if (video.available()) { - video.read(); - video.loadPixels(); - - background(0, 0, 255); - - // Begin loop for columns - for (int i = 0; i < cols;i++) { - // Begin loop for rows - for (int j = 0; j < rows;j++) { - - // Where are we, pixel-wise? - int x = i * cellSize; - int y = j * cellSize; - int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image - - // Each rect is colored white with a size determined by brightness - color c = video.pixels[loc]; - float sz = (brightness(c) / 255.0) * cellSize; - fill(255); - noStroke(); - rect(x + cellSize/2, y + cellSize/2, sz, sz); - } - } - } -} +/** + * Mirror 2 + * by Daniel Shiffman. + * + * GSVideo version by Andres Colubri. + * + * Each pixel from the video source is drawn as a rectangle with size based on brightness. + */ + +import codeanticode.gsvideo.*; + +// Size of each cell in the grid +int cellSize = 15; +// Number of columns and rows in our system +int cols, rows; +// Variable for capture device +GSCapture video; + + +void setup() { + size(640, 480, P3D); + //set up columns and rows + cols = width / cellSize; + rows = height / cellSize; + colorMode(RGB, 255, 255, 255, 100); + rectMode(CENTER); + + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, width, height); + video.start(); + + background(0); +} + + +void draw() { + if (video.available()) { + video.read(); + video.loadPixels(); + + background(0, 0, 255); + + // Begin loop for columns + for (int i = 0; i < cols;i++) { + // Begin loop for rows + for (int j = 0; j < rows;j++) { + + // Where are we, pixel-wise? + int x = i * cellSize; + int y = j * cellSize; + int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image + + // Each rect is colored white with a size determined by brightness + color c = video.pixels[loc]; + float sz = (brightness(c) / 255.0) * cellSize; + fill(255); + noStroke(); + rect(x + cellSize/2, y + cellSize/2, sz, sz); + } + } + } +} diff --git a/java/libraries/video/examples/Capture/RadialPocky/RadialPocky.pde b/java/libraries/video/examples/Capture/RadialPocky/RadialPocky.pde index eddac3636..61fa34fc9 100644 --- a/java/libraries/video/examples/Capture/RadialPocky/RadialPocky.pde +++ b/java/libraries/video/examples/Capture/RadialPocky/RadialPocky.pde @@ -1,78 +1,78 @@ -/** - * Radial Pocky - * by Ben Fry. - * - * GSVideo version by Andres Colubri. - * - * Unwrap each frame of live video into a single line of pixels along a circle - */ - -import codeanticode.gsvideo.*; - - -GSCapture video; -int videoCount; -int currentAngle; -int pixelCount; -int angleCount = 200; // how many divisions - -int radii[]; -int angles[]; - - -void setup() { - // size must be set to video.width*video.height*2 in both directions - size(600, 600); - - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, 160, 120); - video.play(); - videoCount = video.width * video.height; - - pixelCount = width*height; - int centerX = width / 2; - int centerY = height / 2; - radii = new int[pixelCount]; - angles = new int[pixelCount]; - - int offset = 0; - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int dx = centerX - x; - int dy = centerY - y; - - float angle = atan2(dy, dx); - if (angle < 0) angle += TWO_PI; - angles[offset] = (int) (angleCount * (angle / TWO_PI)); - - int radius = (int) mag(dx, dy); - if (radius >= videoCount) { - radius = -1; - angles[offset] = -1; - } - radii[offset] = radius; - - offset++; - } - } - background(0); -} - - -void draw() { - if (video.available()) { - video.read(); - video.loadPixels(); - - loadPixels(); - for (int i = 0; i < pixelCount; i++) { - if (angles[i] == currentAngle) { - pixels[i] = video.pixels[radii[i]]; - } - } - updatePixels(); - - currentAngle++; - if (currentAngle == angleCount) currentAngle = 0; - } -} +/** + * Radial Pocky + * by Ben Fry. + * + * GSVideo version by Andres Colubri. + * + * Unwrap each frame of live video into a single line of pixels along a circle + */ + +import codeanticode.gsvideo.*; + + +GSCapture video; +int videoCount; +int currentAngle; +int pixelCount; +int angleCount = 200; // how many divisions + +int radii[]; +int angles[]; + + +void setup() { + // size must be set to video.width*video.height*2 in both directions + size(600, 600); + + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, 160, 120); + video.start(); + videoCount = video.width * video.height; + + pixelCount = width*height; + int centerX = width / 2; + int centerY = height / 2; + radii = new int[pixelCount]; + angles = new int[pixelCount]; + + int offset = 0; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int dx = centerX - x; + int dy = centerY - y; + + float angle = atan2(dy, dx); + if (angle < 0) angle += TWO_PI; + angles[offset] = (int) (angleCount * (angle / TWO_PI)); + + int radius = (int) mag(dx, dy); + if (radius >= videoCount) { + radius = -1; + angles[offset] = -1; + } + radii[offset] = radius; + + offset++; + } + } + background(0); +} + + +void draw() { + if (video.available()) { + video.read(); + video.loadPixels(); + + loadPixels(); + for (int i = 0; i < pixelCount; i++) { + if (angles[i] == currentAngle) { + pixels[i] = video.pixels[radii[i]]; + } + } + updatePixels(); + + currentAngle++; + if (currentAngle == angleCount) currentAngle = 0; + } +} diff --git a/java/libraries/video/examples/Capture/SlitScan/SlitScan.pde b/java/libraries/video/examples/Capture/SlitScan/SlitScan.pde index 20353a0d0..9cded760f 100644 --- a/java/libraries/video/examples/Capture/SlitScan/SlitScan.pde +++ b/java/libraries/video/examples/Capture/SlitScan/SlitScan.pde @@ -1,57 +1,57 @@ -/** - * Simple Real-Time Slit-Scan Program. - * By Golan Levin. - * - * GSVideo version by Andres Colubri. - * - * This demonstration depends on the canvas height being equal - * to the video capture height. If you would prefer otherwise, - * consider using the image copy() function rather than the - * direct pixel-accessing approach I have used here. - * - * Created December 2006. - * Updated June 2007 by fry. - */ -import codeanticode.gsvideo.*; - -GSCapture video; - -int videoSliceX; -int drawPositionX; - - -void setup() { - size(600, 240); - - // Uses the default video input, see the reference if this causes an error - video = new GSCapture(this, 320, 240); - video.play(); - - videoSliceX = video.width / 2; - drawPositionX = width - 1; - background(0); -} - - -void draw() { - if (video.available()) { - video.read(); - video.loadPixels(); - - // Copy a column of pixels from the middle of the video - // To a location moving slowly across the canvas. - loadPixels(); - for (int y = 0; y < video.height; y++){ - int setPixelIndex = y*width + drawPositionX; - int getPixelIndex = y*video.width + videoSliceX; - pixels[setPixelIndex] = video.pixels[getPixelIndex]; - } - updatePixels(); - - drawPositionX--; - // Wrap the position back to the beginning if necessary. - if (drawPositionX < 0) { - drawPositionX = width - 1; - } - } -} +/** + * Simple Real-Time Slit-Scan Program. + * By Golan Levin. + * + * GSVideo version by Andres Colubri. + * + * This demonstration depends on the canvas height being equal + * to the video capture height. If you would prefer otherwise, + * consider using the image copy() function rather than the + * direct pixel-accessing approach I have used here. + * + * Created December 2006. + * Updated June 2007 by fry. + */ +import codeanticode.gsvideo.*; + +GSCapture video; + +int videoSliceX; +int drawPositionX; + + +void setup() { + size(600, 240); + + // Uses the default video input, see the reference if this causes an error + video = new GSCapture(this, 320, 240); + video.start(); + + videoSliceX = video.width / 2; + drawPositionX = width - 1; + background(0); +} + + +void draw() { + if (video.available()) { + video.read(); + video.loadPixels(); + + // Copy a column of pixels from the middle of the video + // To a location moving slowly across the canvas. + loadPixels(); + for (int y = 0; y < video.height; y++){ + int setPixelIndex = y*width + drawPositionX; + int getPixelIndex = y*video.width + videoSliceX; + pixels[setPixelIndex] = video.pixels[getPixelIndex]; + } + updatePixels(); + + drawPositionX--; + // Wrap the position back to the beginning if necessary. + if (drawPositionX < 0) { + drawPositionX = width - 1; + } + } +}