summaryrefslogtreecommitdiff
path: root/webcam_buffer/webcam_buffer.pde
blob: 6744f19a4781839b3181c897fd00ae3d2f4fc0f8 (plain)
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
import processing.video.*;

Capture c;
VideoBuffer vb;

void setup() 
{
  size(200, 200);
  vb = new VideoBuffer(30, width, height);
  c = new Capture(this, width, height, 30);
}

void captureEvent(Capture c)
{
  c.read();
  vb.addFrame(c);
}

void draw()
{
  image(vb.getFrame(), 0, 0);
}

class VideoBuffer
{
  PImage[] b;
  int s;
  int w;
  int h;
  int cur;
  
  VideoBuffer(int s, int w, int h)
  {
    this.b = new PImage[s];
    for (int i=0; i<s; i++) {
      this.b[i] = new PImage(w, h);
    }
    this.s = s;
    this.w = w;
    this.h = h;
  }
  
  void addFrame(PImage f)
  {
    System.arraycopy(f.pixels, 0, b[cur].pixels, 0, w * h);
    b[cur].updatePixels();
    cur = (cur + 1) % s;
  }
  
  PImage getFrame()
  {
    int i = int(random(s));
    return b[i];
  }
}