floydSteinberg = {
let ctx = DOM.canvas(data.width, data.height).getContext('2d');
ctx.canvas.style.width = `${data.width * 2}px`;
let clone = new ImageData(
new Uint8ClampedArray(data.data.data),
data.width,
data.height
);
function px(x, y) {
return x * 4 + y * data.width * 4;
}
for (let y = 0; y < data.height; y++) {
for (let x = 0; x < data.width; x++) {
let oldPixel = clone.data[px(x, y)];
let newPixel = oldPixel > 125 ? 255 : 0;
clone.data[px(x, y)] = clone.data[px(x, y) + 1] = clone.data[
px(x, y) + 2
] = newPixel;
let quantError = oldPixel - newPixel;
clone.data[px(x + 1, y)] = clone.data[px(x + 1, y) + 1] = clone.data[
px(x + 1, y) + 2
] = clone.data[px(x + 1, y)] + (quantError * 7) / 16;
clone.data[px(x - 1, y + 1)] = clone.data[
px(x - 1, y + 1) + 1
] = clone.data[px(x - 1, y + 1) + 2] =
clone.data[px(x - 1, y + 1)] + (quantError * 3) / 16;
clone.data[px(x, y + 1)] = clone.data[px(x, y + 1) + 1] = clone.data[
px(x, y + 1) + 2
] = clone.data[px(x, y + 1)] + (quantError * 5) / 16;
clone.data[px(x + 1, y + 1)] = clone.data[
px(x + 1, y + 1) + 1
] = clone.data[px(x + 1, y + 1) + 2] =
clone.data[px(x + 1, y + 1)] + (quantError * 1) / 16;
}
}
ctx.putImageData(clone, 0, 0);
yield ctx.canvas;
}