-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkerv2.js
187 lines (176 loc) · 4.21 KB
/
workerv2.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// read from master
var lineStep = null;
var numLines = null;
var testLines = null;
self.addEventListener('message', function(e) {
lineStep = e.data.lineStep;
numLines = e.data.numLines;
testLines = e.data.testLines;
lineMaker(e.data.add, e.data.img, e.data.width, e.data.height);
}, false);
/* line creation */
function lineMaker(add, gray, width, height) {
// find darkest points then draw best line through point
for(var i=0; i < numLines; i++) {
var selection = [0];
var lum = gray[0];
for(var j=1; j < gray.length; j++) {
if(gray[j] == lum) {
selection.push(j);
}
if(add) {
if(gray[j] > lum) {
selection = [j];
lum = gray[j];
}
} else {
if(gray[j] < lum) {
selection = [j];
lum = gray[j];
}
}
}
var point = selection[Math.floor(Math.random() * selection.length)];
self.postMessage(makeLine(point % width, Math.floor(point/width), gray, width, height, add));
}
close();
}
/* take point and image data, return line as tuple */
function makeLine(x, y, img, width, height, add) {
var tests = [];
for(var i=0; i < testLines; i++) {
var slope = (Math.random()-0.5)/(Math.random()-0.5);
var lum = 0; // luminousity accumulator
var points = [];
points.push([x,y]);
// basically Bresenham
if(slope > 1 || slope < -1) { // swap if steep
var delX = Math.floor(Math.abs(slope));
var err = Math.abs(slope) - delX;
var xx = y;
var yy = x;
var wwidth = height;
var hheight = width;
var swap = true;
} else { // not steep
var delX = Math.floor(Math.abs(1/slope));
var err = Math.abs(1/slope) - delX;
var xx = x;
var yy = y;
var wwidth = width;
var hheight = height;
var swap = false;
}
if(slope >= 0) {
var sign = 1;
} else {
var sign = -1;
}
// go up
var currX = xx;
var currY = yy;
var cErr = 0;
up:
while(currY > 0) {
currY--;
for(var j=0; j < delX; j++) {
currX -= sign;
if(currX < 0 || currX >= wwidth) {
break up;
}
if(swap === false) {
lum += img[currY * width + currX];
points.push([currX,currY]);
} else {
lum += img[currX * width + currY];
points.push([currY,currX]);
}
}
cErr += err;
if(cErr >= 0.5) {
currX -= sign;
if(currX < 0 || currX >= wwidth) {
break up;
}
if(swap === false) {
lum += img[currY * width + currX];
points.push([currX,currY]);
} else {
lum += img[currX * width + currY];
points.push([currY,currX]);
}
cErr -= 1;
}
}
// go down
currX = xx;
currY = yy;
cErr = 0;
down:
while(currY < hheight) {
for(var j=0; j < delX; j++) {
currX += sign;
if(currX < 0 || currX >= wwidth) {
break down;
}
if(swap === false) {
lum += img[currY * width + currX];
points.unshift([currX,currY]);
} else {
lum += img[currX * width + currY];
points.unshift([currY,currX]);
}
}
cErr += err;
if(cErr >= 0.5) {
currX += sign;
if(currX < 0 || currX >= wwidth) {
break down;
}
if(swap === false) {
lum += img[currY * width + currX];
points.unshift([currX,currY]);
} else {
lum += img[currX * width + currY];
points.unshift([currY,currX]);
}
cErr -= 1;
}
currY++;
}
tests.push({lum: lum, points: points});
}
// find best candidate
if(add) { // additive
var max = 0; // array position
var lum = tests[0].lum/tests[0].points.length; // average lum
for(var i=1; i < tests.length; i++) {
var testLum = tests[i].lum/tests[i].points.length;
if(testLum > lum) {
max = i;
lum = testLum;
}
}
var line = tests[max].points;
// tick accumulators
for(var i=0; i < line.length; i++) {
img[line[i][1] * width + line[i][0]] -= lineStep;
}
} else { // subtractive
var min = 0; // array position
var lum = tests[0].lum/tests[0].points.length; // average lum
for(var i=1; i < tests.length; i++) {
var testLum = tests[i].lum/tests[i].points.length;
if(testLum < lum) {
min = i;
lum = testLum;
}
}
var line = tests[min].points;
// tick accumulators
for(var i=0; i < line.length; i++) {
img[line[i][1] * width + line[i][0]] += lineStep;
}
}
return [line[0][0], line[0][1], line[line.length-1][0], line[line.length-1][1]];
}