-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoScrambler.java
More file actions
167 lines (147 loc) · 4.51 KB
/
VideoScrambler.java
File metadata and controls
167 lines (147 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package videoscrambler;
import processing.core.*;
import processing.video.*;
public class VideoScrambler extends PApplet {
private final int FRAME_RATE = 30;
ParamWindow controlPanel;
VideoSource video;
public VideoScrambler() {
controlPanel = new ParamWindow();
}
public void setup() {
size(640,480);
frameRate(FRAME_RATE);
frame.setResizable(true);
}
private void openMovie(String filename) {
if (filename != null) {
if (video != null)
video.dispose();
try {
video = new VideoSource(this, filename);
video.start();
} catch (Exception e) {
System.out.println("Can't open video file " + filename);
}
}
}
public void draw () {
if (controlPanel.newFileSelected()) {
String filename = controlPanel.getFilename();
openMovie(filename);
}
if (controlPanel.switchToWebcam())
switchToWebcam();
if (video != null && video.available()) {
//video.loadPixels();
video.read();
frame.setSize(video.width, video.height); // annoying to have to set this every frame, but it may be unavoidable
image(video,0,0);
if (glitchFrame()) {
loadPixels();
selectAndPlaceSamples();
if (controlPanel.saveFrames())
saveFrame(video.getName() + "-########" + ".jpg");
}
}
}
private void switchToWebcam() {
try {
if (video != null)
video.dispose();
video = new VideoSource(this); // this constructor uses the default webcam
video.start();
} catch (Exception e) {
System.out.println("Coudln't find the webcam.");
}
}
private boolean glitchFrame() {
return random(1) < controlPanel.getGlitchProbability();
}
private void selectAndPlaceSamples() {
// we don't want the values of these parameters to change within the loop, since this is all within one frame
int samples = controlPanel.getNumSamples();
boolean snap = controlPanel.snapToGrid();
int uniformHeight = 0;
if (snap) uniformHeight = controlPanel.getMaxSampleHeight();
for (int i = 1; i <= samples; i++) {
int selectionHeight, selectionWidth;
if (snap) {
selectionHeight = uniformHeight;
selectionWidth = selectionHeight;
}
else {
selectionHeight = randomHeight();
selectionWidth = randomWidth(selectionHeight);
}
PImage temp_image = selectSample(selectionWidth, selectionHeight);
int new_x = randomXCoord(selectionWidth, snap);
int new_y = randomYCoord(selectionHeight, snap);
image(temp_image,new_x,new_y);
}
}
private int randomWidth(int selectionHeight) {
int selectionWidth = (int)(random(controlPanel.getHorizWarp())*selectionHeight);
if (selectionWidth > width)
return width;
return selectionWidth;
}
private int randomHeight() {
int selection_height = (int)(random(controlPanel.getMaxSampleHeight()));
if (selection_height > height)
return height;
return selection_height;
}
private PImage selectSample(int selection_width, int selection_height) {
PImage sample = new PImage(selection_width,selection_height);
int start_x = (int)(random(width-selection_width));
int start_y = (int)(random(height-selection_height));
int temp_image_loc = 0;
sample.loadPixels();
for (int y = start_y; y < (start_y + selection_height); y++) {
for (int x = start_x; x < (start_x + selection_width); x++) {
int loc = x + y * width;
sample.pixels[temp_image_loc] = pixels[loc];
temp_image_loc++;
}
}
return sample;
}
/**
* Precondition: sampleWidth > 0
* @param sampleWidth
* @return
*/
private int randomXCoord(int sampleWidth, boolean snap) {
int x;
if (snap) {
x = (int)(random(width));
x -= x % sampleWidth;
} else x = (int)(random(width)) - sampleWidth/2;
if (x < 0)
return 0;
return x;
}
/**
* Precondition: sampleHeight >= 0
* @param sampleHeight
* @return
*/
private int randomYCoord(int sampleHeight, boolean snap) {
int y;
if (snap) {
y = (int)(random(height));
y -= y % sampleHeight;
} else
y = (int)(random(height)) - sampleHeight/2;
if (y < 0)
return 0;
return y;
}
public void mousePressed() {
exit();
}
public static void main(String _args[]) {
PApplet.main(new String[] { videoscrambler.VideoScrambler.class.getName() });
}
}