-
Notifications
You must be signed in to change notification settings - Fork 11
/
worker.coffee
33 lines (29 loc) · 992 Bytes
/
worker.coffee
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
threshold = (i) -> if i <= 128 then 0 else 255
luminance = (imagedata) ->
pixels = imagedata.data
for i in [0..pixels.length] by 4
# luminance: red x 0.3 + green x 0.59 + blue x 0.11
pixels[i] = pixels[i+1] = pixels[i+2] =
parseInt(
pixels[i] * 0.3 +
pixels[i+1] * 0.59 +
pixels[i+2] * 0.11
, 10)
#@postMessage progress: "luminance done"
return imagedata
atkinson = (imagedata) ->
pixels = imagedata.data
w = imagedata.width
for i in [0..pixels.length] by 4
neighbours = [i+4, i+8, i+(4*w)-4, i+(4*w), i+(4*w)+4, i+(8*w)]
mono = threshold pixels[i]
err = parseInt (pixels[i] - mono) / 8, 10
pixels[i] = mono
for one of neighbours
pixels[neighbours[one]] += err
pixels[i+1] = pixels[i+2] = pixels[i]
#@postMessage progress: "atkinson done"
return imagedata
@process = (image) -> atkinson luminance image
@addEventListener "message", (event) ->
@postMessage image: atkinson luminance event.data