-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDither.pde
68 lines (60 loc) · 1.72 KB
/
Dither.pde
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
int index(int x, int y, PImage img)
{
return x + y * img.width;
}
void dither(PImage img) {
img.loadPixels();
for (int y = 0; y < img.height-1; y++) {
for (int x = 1; x < img.width-1; x++) {
color pix = img.pixels[index(x, y, img)];
float oldR = red(pix);
float oldG = green(pix);
float oldB = blue(pix);
int factor = 15;
int newR = round(factor * oldR / 255) * (255/factor);
int newG = round(factor * oldG / 255) * (255/factor);
int newB = round(factor * oldB / 255) * (255/factor);
img.pixels[index(x, y, img)] = color(newR, newG, newB);
float errR = oldR - newR;
float errG = oldG - newG;
float errB = oldB - newB;
int index = index(x, y, img);
color c = img.pixels[index];
float r = red(c);
float g = green(c);
float b = blue(c);
r = r + errR * 7/16.0;
g = g + errG * 7/16.0;
b = b + errB * 7/16.0;
img.pixels[index] = color(r, g, b);
index = index(x, y, img);
c = img.pixels[index];
r = red(c);
g = green(c);
b = blue(c);
r = r + errR * 3/16.0;
g = g + errG * 3/16.0;
b = b + errB * 3/16.0;
img.pixels[index] = color(r, g, b);
index = index(x, y, img);
c = img.pixels[index];
r = red(c);
g = green(c);
b = blue(c);
r = r + errR * 5/16.0;
g = g + errG * 5/16.0;
b = b + errB * 5/16.0;
img.pixels[index] = color(r, g, b);
index = index(x, y, img);
c = img.pixels[index];
r = red(c);
g = green(c);
b = blue(c);
r = r + errR * 1/16.0;
g = g + errG * 1/16.0;
b = b + errB * 1/16.0;
img.pixels[index] = color(r, g, b);
}
}
img.updatePixels();
}