-
Notifications
You must be signed in to change notification settings - Fork 9
/
canvas-image-worker.js
123 lines (75 loc) · 3.09 KB
/
canvas-image-worker.js
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
function draw (data) {
if (data.processing.greyscaleMethod == "luminance") {
greyscale_luminance(data.image.data);
} else if (data.processing.greyscaleMethod == "average") {
greyscale_average(data.image.data);
}
if (data.processing.ditherMethod == "atkinson") {
dither_atkinson(data.image.data, data.image.width, (data.processing.greyscaleMethod == ""));
} else if (data.processing.ditherMethod == "threshold") {
dither_threshold(data.image.data, data.processing.ditherThreshold);
}
if (data.processing.replaceColours == true) {
replace_colours(data.image.data, data.processing.replaceColourMap.black, data.processing.replaceColourMap.white);
}
return data;
}
// Convert image data to greyscale based on luminance.
function greyscale_luminance (image) {
for (var i = 0; i <= image.data.length; i += 4) {
image.data[i] = image.data[i + 1] = image.data[i + 2] = parseInt(image.data[i] * 0.21 + image.data[i + 1] * 0.71 + image.data[i + 2] * 0.07, 10);
}
return image;
}
// Convert image data to greyscale based on average of R, G and B values.
function greyscale_average (image) {
for (var i = 0; i <= image.data.length; i += 4) {
image.data[i] = image.data[i + 1] = image.data[i + 2] = parseInt((image.data[i] + image.data[i + 1] + image.data[i + 2]) / 3, 10);
}
return image;
}
// Apply Atkinson Dither to Image Data
function dither_atkinson (image, imageWidth, drawColour) {
skipPixels = 4;
if (!drawColour)
drawColour = false;
if(drawColour == true)
skipPixels = 1;
imageLength = image.data.length;
for (currentPixel = 0; currentPixel <= imageLength; currentPixel += skipPixels) {
if (image.data[currentPixel] <= 128) {
newPixelColour = 0;
} else {
newPixelColour = 255;
}
err = parseInt((image.data[currentPixel] - newPixelColour) / 8, 10);
image.data[currentPixel] = newPixelColour;
image.data[currentPixel + 4] += err;
image.data[currentPixel + 8] += err;
image.data[currentPixel + (4 * imageWidth) - 4] += err;
image.data[currentPixel + (4 * imageWidth)] += err;
image.data[currentPixel + (4 * imageWidth) + 4] += err;
image.data[currentPixel + (8 * imageWidth)] += err;
if (drawColour == false)
image.data[currentPixel + 1] = image.data[currentPixel + 2] = image.data[currentPixel];
}
return image.data;
}
function dither_threshold (image, threshold_value) {
for (var i = 0; i <= image.data.length; i += 4) {
image.data[i] = (image.data[i] > threshold_value) ? 255 : 0;
image.data[i + 1] = (image.data[i + 1] > threshold_value) ? 255 : 0;
image.data[i + 2] = (image.data[i + 2] > threshold_value) ? 255 : 0;
}
}
function replace_colours (image, black, white) {
for (var i = 0; i <= image.data.length; i += 4) {
image.data[i] = (image.data[i] < 127) ? black.r : white.r;
image.data[i + 1] = (image.data[i + 1] < 127) ? black.g : white.g;
image.data[i + 2] = (image.data[i + 2] < 127) ? black.b : white.b;
image.data[i + 3] = (((image.data[i]+image.data[i+1]+image.data[i+2])/3) < 127) ? black.a : white.a;
}
}
self.addEventListener('message', function (e) {
self.postMessage(draw(e.data));
}, false);