summaryrefslogtreecommitdiff
path: root/Year_2/IandM/bitplane/bitplane.pde
blob: 33cda9ce4f395fbe523623ae5a004df66622b5c4 (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
int nb = 0;

void setup() {
  size(512, 256);
}

void draw() {
  PImage img = loadImage("lena.png");
  img.resize(256, 256);
  img.filter(GRAY);

  image(img, 0, 0);
  image(bitplane(img, nb), 256, 0);
}

void keyPressed() {
  if(key == '+' && nb < 7) {
    nb++;
  } else if(key == '-' && nb > 1) {
    nb--;
  }
}

PImage bitplane(PImage I, int nb) {
  PImage out = I.copy();
  out.loadPixels();

  int x, r;

  for (int i = 0; i < out.pixels.length; ++i) {
    x = int(red(out.pixels[i]));
    r = (x >> nb) & 1;
    out.pixels[i] = color(255*r);
  }

  out.updatePixels();

  return out;
}