Created
July 20, 2024 05:38
-
-
Save zazaulola/96eebe2f984e24538d509e01dd12429a to your computer and use it in GitHub Desktop.
Grayscale image halftone dithering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onmessage = ({ data: { data, width, height } }) => { | |
const add = (d, i, v) => { | |
if (i >= d.length) return; | |
d[i] += v; | |
}; | |
const w = width * 4; | |
let i = 0; | |
for (let y = 0; y < height; y++) { | |
for (let x = 0; x < width; x++, i += 4) { | |
const curPixel = data[i]; | |
const newPixel = curPixel < 129 ? 0 : 255; | |
const err = (curPixel - newPixel) / 16; | |
data[i + 0] = newPixel; | |
data[i + 1] = newPixel; | |
data[i + 2] = newPixel; | |
add(data, i + 4, err * 7); | |
add(data, i + w - 4, err * 3); | |
add(data, i + w, err * 5); | |
add(data, i + w + 4, err); | |
} | |
} | |
postMessage(data); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment