-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdither.js
30 lines (25 loc) · 1.1 KB
/
dither.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
(function(imageproc) {
"use strict";
/*
* Apply ordered dithering to the input data
*/
imageproc.dither = function(inputData, outputData, matrix, levels) {
for (var y = 0; y < inputData.height; y++) {
for (var x = 0; x < inputData.width; x++) {
var pixel = imageproc.getPixel(inputData, x, y);
/* Change the colour to grayscale and normalize it */
var value = pixel.r * 0.2126 +
pixel.g * 0.7152 +
pixel.b * 0.0722;
value = value / 255 * levels;
/* Get the corresponding threshold of the pixel */
var threshold = matrix[y % matrix.length][x % matrix[0].length];
/* Set the colour to black or white based on threshold */
var i = (x + y * outputData.width) * 4;
outputData.data[i] =
outputData.data[i + 1] =
outputData.data[i + 2] = (value < threshold)? 0 : 255;
}
}
}
}(window.imageproc = window.imageproc || {}));