Skip to content

Commit bb6d2b3

Browse files
author
monkstone
committed
update
1 parent 6c44d7d commit bb6d2b3

9 files changed

+160
-178
lines changed

chapter_16/01_display_video.rb

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
require 'ruby-processing'
21

3-
class CaptureVideoSketch < Processing::App
42

5-
# Step 1. Import the video library
6-
load_library "video"
7-
# We need the video classes to be included here.
8-
import "processing.video"
3+
# Step 1. Import the video library
4+
load_library :video
5+
# We need the video classes to be included here.
6+
include_package "processing.video"
97

10-
def setup
11-
# Step 2. Declare a Capture object
12-
@video = Capture.new(self, width, height, 30)
13-
end
14-
15-
def draw
16-
# Step 3 and 4: Read from the camera if it's available.
17-
@video.read if @video.available
8+
def setup
9+
size 320, 240
10+
# Step 2. Declare a Capture object
11+
@video = Capture.new(self, width, height, 30)
12+
end
1813

19-
# Step 5. Display the video image.
20-
image @video, 0, 0
21-
end
14+
def draw
15+
# Step 3 and 4: Read from the camera if it's available.
16+
@video.read if @video.available
2217

18+
# Step 5. Display the video image.
19+
image @video, 0, 0
2320
end
2421

25-
CaptureVideoSketch.new :title => "Capture Video", :width => 320, :height => 240
22+
+18-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
require 'ruby-processing'
2-
3-
class ManipulateVideoImageSketch < Processing::App
4-
5-
load_library "video"
6-
import "processing.video"
7-
8-
def setup
9-
@video = Capture.new(self, width, height, 30)
10-
end
11-
12-
def draw
13-
@video.read if @video.available
14-
15-
# Tinting using mouse location
16-
tint mouse_x, mouse_y, 255
17-
18-
# A video image can also be tinted and resized just as with a PImage.
19-
image @video, 0, 0, mouse_x, mouse_y
20-
end
1+
# Step 1. Import the video library
2+
load_library :video
3+
# We need the video classes to be included here.
4+
include_package "processing.video"
5+
6+
def setup
7+
size 320, 240
8+
@video = Capture.new(self, width, height, 30)
9+
end
2110

11+
def draw
12+
@video.read if @video.available
13+
14+
# Tinting using mouse location
15+
tint mouse_x, mouse_y, 255
16+
17+
# A video image can also be tinted and resized just as with a PImage.
18+
image @video, 0, 0, mouse_x, mouse_y
2219
end
2320

24-
ManipulateVideoImageSketch.new :title => "Manipulate Video Image", :width => 320, :height => 240
21+
+39-43
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,45 @@
11
# Example 16-3: Adjust video brightness
2-
require 'ruby-processing'
3-
4-
class AdjustVideoBrightness < Processing::App
5-
load_library "video"
6-
import "processing.video"
7-
8-
def setup
9-
size 240, 180, P3D
10-
11-
# Initialize Capture object via Constructor
12-
# video is 320 x 240, @15 fps
13-
@video = Capture.new self, 240, 180, 15
14-
background 0
15-
end
16-
17-
def draw
18-
# Check to see if a new frame is available
19-
# If so, read it.
20-
@video.read if @video.available?
21-
22-
load_pixels
23-
@video.load_pixels
24-
pixs = @video.pixels
25-
mx, my = mouse_x, mouse_y
26-
max_dist = 100
27-
vid_width, vid_height = @video.width, @video.height
28-
29-
vid_width.times do |x|
30-
vid_height.times do |y|
31-
# Calculate the 1D location from a 2D grid
32-
loc = x + y * vid_width
33-
pix = pixs[loc]
34-
35-
# Calculate an amount to change brightness based on proximity to the mouse
36-
d = dist(x, y, mx, my)
37-
fudge = (max_dist - d) / max_dist
38-
r, g, b = red(pix) * fudge, green(pix) * fudge, blue(pix) * fudge
2+
# Step 1. Import the video library
3+
load_library :video
4+
# We need the video classes to be included here.
5+
package_include "processing.video"
6+
7+
def setup
8+
size 240, 180, P3D
9+
10+
# Initialize Capture object via Constructor
11+
# video is 320 x 240, @15 fps
12+
@video = Capture.new self, 240, 180, 15
13+
background 0
14+
end
3915

40-
# Make a new color and set pixel in the window
41-
pixels[loc] = color(r, g, b)
42-
end
16+
def draw
17+
# Check to see if a new frame is available
18+
# If so, read it.
19+
@video.read if @video.available?
20+
21+
load_pixels
22+
@video.load_pixels
23+
pixs = @video.pixels
24+
mx, my = mouse_x, mouse_y
25+
max_dist = 100
26+
vid_width, vid_height = @video.width, @video.height
27+
28+
vid_width.times do |x|
29+
vid_height.times do |y|
30+
# Calculate the 1D location from a 2D grid
31+
loc = x + y * vid_width
32+
pix = pixs[loc]
33+
34+
# Calculate an amount to change brightness based on proximity to the mouse
35+
d = dist(x, y, mx, my)
36+
fudge = (max_dist - d) / max_dist
37+
r, g, b = red(pix) * fudge, green(pix) * fudge, blue(pix) * fudge
38+
39+
# Make a new color and set pixel in the window
40+
pixels[loc] = color(r, g, b)
4341
end
44-
update_pixels
4542
end
46-
43+
update_pixels
4744
end
4845

49-
AdjustVideoBrightness.new :title => "Adjust Video Brightness"

chapter_17/02_text_align.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def draw
1515
text_align CENTER
1616
text "This text is centered.", width / 2, 60
1717
text_align LEFT
18-
text "This text is left aligned.", width/2, 100
18+
text "This text is left aligned.", width / 2, 100
1919
text_align RIGHT
2020
text "This text is right aligned.", width / 2, 140
2121
end

chapter_17/04_text_mirror.rb

+55-58
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,65 @@
11
# This example demonstrates how to use the video library. You'll need a
22
# video camera hooked in to your computer.
33

4-
class TextMirror < Processing::App
5-
load_library "video"
6-
import "processing.video"
74

8-
# Size of each cell in the grid, ratio of window size to video size
9-
VIDEOSCALE = 14
5+
load_library :video
6+
include_package "processing.video"
107

11-
# The source text used in the mosaic pattern.
12-
# A longer String might produce more interesting results.
13-
CHARS = "thetextmirror"
8+
# Size of each cell in the grid, ratio of window size to video size
9+
VIDEOSCALE = 14
1410

15-
def setup
16-
size 640, 480
17-
smooth
18-
# Set up columns and rows
19-
@cols = width / VIDEOSCALE # Number of columns and...
20-
@rows = height / VIDEOSCALE # rows in our system
21-
@video = Capture.new(self, @cols, @rows, 15)
11+
# The source text used in the mosaic pattern.
12+
# A longer String might produce more interesting results.
13+
CHARS = "thetextmirror"
2214

23-
# Load the font
24-
# Using a fixed-width font. In most fonts, individual characters have different widths.
25-
# In a fixed-width font, all characters have the same width.
26-
# This is useful here since we intend to display the letters one at a time spaced out evenly.
27-
# See Section 17.7 for how to display text character by character with a nonfixed width font.
28-
@f = load_font "Courier-Bold-20.vlw"
29-
end
30-
31-
def draw
32-
background 0
33-
34-
# Read image from the camera
35-
@video.read if @video.available?
36-
@video.load_pixels
37-
38-
# Use a variable to count through chars in String
39-
charcount = 0
40-
# Begin loop for rows
41-
@rows.times do |j|
42-
# Begin loop for columns
43-
@cols.times do |i|
44-
45-
# Where are we, pixel-wise?
46-
x = i * VIDEOSCALE
47-
y = j * VIDEOSCALE
48-
49-
# Looking up the appropriate color in the pixel array
50-
c = @video.pixels[i + j * @video.width]
51-
52-
# Displaying an individual character from the String instead of a rectangle
53-
text_font @f
54-
fill c
55-
56-
# One character from the source text is displayed colored accordingly to the pixel location.
57-
# A counter variableâ charcountâ is used to walk through the source String one character at a time.
58-
text CHARS[charcount].chr, x, y
59-
60-
# Go on to the next character
61-
charcount = (charcount + 1) % CHARS.length
62-
end
63-
end
64-
end
15+
def setup
16+
size 640, 480
17+
smooth
18+
# Set up columns and rows
19+
@cols = width / VIDEOSCALE # Number of columns and...
20+
@rows = height / VIDEOSCALE # rows in our system
21+
@video = Capture.new(self, @cols, @rows, 15)
22+
23+
# Load the font
24+
# Using a fixed-width font. In most fonts, individual characters have different widths.
25+
# In a fixed-width font, all characters have the same width.
26+
# This is useful here since we intend to display the letters one at a time spaced out evenly.
27+
# See Section 17.7 for how to display text character by character with a nonfixed width font.
28+
@f = load_font "Courier-Bold-20.vlw"
29+
end
6530

31+
def draw
32+
background 0
33+
34+
# Read image from the camera
35+
@video.read if @video.available?
36+
@video.load_pixels
37+
38+
# Use a variable to count through chars in String
39+
charcount = 0
40+
# Begin loop for rows
41+
@rows.times do |j|
42+
# Begin loop for columns
43+
@cols.times do |i|
44+
45+
# Where are we, pixel-wise?
46+
x = i * VIDEOSCALE
47+
y = j * VIDEOSCALE
48+
49+
# Looking up the appropriate color in the pixel array
50+
c = @video.pixels[i + j * @video.width]
51+
52+
# Displaying an individual character from the String instead of a rectangle
53+
text_font @f
54+
fill c
55+
56+
# One character from the source text is displayed colored accordingly to the pixel location.
57+
# A counter variableâ charcountâ is used to walk through the source String one character at a time.
58+
text CHARS[charcount].chr, x, y
59+
60+
# Go on to the next character
61+
charcount = (charcount + 1) % CHARS.length
62+
end
63+
end
6664
end
6765

68-
TextMirror.new :title => "Text Mirror"

chapter_17/05_rotating_text.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ def draw
1818
text @message, 0, 0
1919

2020
# Increase rotation
21-
@theta += 0.05;
21+
@theta += 0.05
2222
end

0 commit comments

Comments
 (0)