-
Notifications
You must be signed in to change notification settings - Fork 63
/
helpers.js
266 lines (218 loc) · 6.76 KB
/
helpers.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
defaultControls = [
{label: 'Inverted', type:'checkbox'},
{label: 'Brightness', value: 0, min: -100, max: 100},
{label: 'Contrast', value: 0, min: -100, max: 100},
{label: 'Min brightness', value: 0, min: 0, max: 255},
{label: 'Max brightness', value: 255, min: 0, max: 255},
]
// Apply brightness / contrast and flatten to monochrome
// taken from squigglecam
function pixelProcessor(config, imagePixels){
const width = parseInt(config.width);
const contrast = parseInt(config.Contrast);
const brightness = parseInt(config.Brightness);
const minBrightness = parseInt(config['Min brightness']);
const maxBrightness = parseInt(config['Max brightness']);
const black = config.Inverted;
let contrastFactor = (259 * (contrast + 255)) / (255 * (259 - contrast));
return function(x, y) {
let b;
let pixIndex = Math.floor(x) + Math.floor(y) * width;
if (contrast !== 0) {
b = (0.2125 * ((contrastFactor * (imagePixels.data[4 * pixIndex] - 128) + 128) + brightness))
+ (0.7154 * ((contrastFactor * (imagePixels.data[4 * pixIndex + 1] - 128) + 128) + brightness))
+ (0.0721 * ((contrastFactor * (imagePixels.data[4 * pixIndex + 2] - 128) + 128) + brightness));
} else {
b = (0.2125 * (imagePixels.data[4*pixIndex] + brightness))
+ (0.7154 * (imagePixels.data[4*pixIndex + 1] + brightness))
+ (0.0721 * (imagePixels.data[4*pixIndex + 2] + brightness));
}
if (black) {
b = Math.min(255-minBrightness,255-b);
} else {
b = Math.max(minBrightness,b);
}
return Math.max(maxBrightness-b,0);
}
}
// autocontrast, my implementation
function autocontrast(pixData, cutoff){
function luma(x,y) {
let i = 4*(x+width*y)
return pixData.data[i]*0.299 + pixData.data[i+1]*0.587 + pixData.data[i+2]*0.114 // ITU-R 601-2
// return pixData.data[i]*0.2125 + pixData.data[i+1]*0.7154 + pixData.data[i+2]*0.0721 // ITU-R 709
}
let hist = []
for (let i=0;i<256;i++) hist[i]=0;
for (let x=0;x<width;x++) {
for (let y=0;y<height;y++) {
let b = Math.round(luma(x,y))
hist[b]++
}
}
let total=0, low=0, high=255
for (let i=0;i<256;i++){
total += hist[i];
}
cutoff*=total;
for (let i=0;i<255;i++) {
low+=hist[i]
if (low>cutoff) {low=i; break}
}
for (let i=255;i>1;i--) {
high+=hist[i]
if (high>=cutoff) {high=i; break}
}
let scale = (255/(high-low)) || 1
const pixelCache=[]
for (let x=0;x<width;x++) {
pixelCache[x]=[]
for (let y=0;y<height;y++) {
pixelCache[x][y] = Math.min(255,Math.max(0,(luma(x,y)-low)*scale ))
}
}
return (x,y)=>{
return (x>=0 && y>=0 && x<width &&y<height)
? pixelCache[x][y]
: 0
}
}
// perlin noise
// ported from lingdong's linedraw.py
(function(){
var PERLIN_YWRAPB = 4
var PERLIN_YWRAP = 1<<PERLIN_YWRAPB
var PERLIN_ZWRAPB = 8
var PERLIN_ZWRAP = 1<<PERLIN_ZWRAPB
var PERLIN_SIZE = 4095
var perlin_octaves = 4
var perlin_amp_falloff = 0.5
function scaled_cosine(i) {
return 0.5*(1.0-Math.cos(i*Math.PI))
}
var perlin = null
perlinNoise = function(x,y=0,z=0) {
if (perlin == null) {
perlin = []
for (let i =0;i<PERLIN_SIZE+1;i++) {
perlin.push(Math.random())
}
}
if (x<0) x=-x
if (y<0) y=-y
if (z<0) z=-z
let [ xi,yi,zi ] = [ ~~x, ~~y, ~~z ]
let xf = x-xi
let yf = y-yi
let zf = z-zi
let rxf, ryf
let r = 0
let ampl = 0.5
let n1, n2, n3
for (let o=0; o<perlin_octaves; o++) {
let of=xi+(yi<<PERLIN_YWRAPB)+(zi<<PERLIN_ZWRAPB)
rxf = scaled_cosine(xf)
ryf = scaled_cosine(yf)
n1 = perlin[of&PERLIN_SIZE]
n1 += rxf*(perlin[(of+1)&PERLIN_SIZE]-n1)
n2 = perlin[(of+PERLIN_YWRAP)&PERLIN_SIZE]
n2 += rxf*(perlin[(of+PERLIN_YWRAP+1)&PERLIN_SIZE]-n2)
n1 += ryf*(n2-n1)
of += PERLIN_ZWRAP
n2 = perlin[of&PERLIN_SIZE]
n2 += rxf*(perlin[(of+1)&PERLIN_SIZE]-n2)
n3 = perlin[(of+PERLIN_YWRAP)&PERLIN_SIZE]
n3 += rxf*(perlin[(of+PERLIN_YWRAP+1)&PERLIN_SIZE]-n3)
n2 += ryf*(n3-n2)
n1 += scaled_cosine(zf)*(n2-n1)
r += n1*ampl
ampl *= perlin_amp_falloff
xi<<=1
xf*=2
yi<<=1
yf*=2
zi<<=1
zf*=2
if (xf>=1.0) xi+=1, xf-=1
if (yf>=1.0) yi+=1, yf-=1
if (zf>=1.0) zi+=1, zf-=1
}
return r
}
})();
// Nearest-neighbour TSP solution, good enough for simple plotting
function sortlines(clines){
let slines = [clines.pop()]
let last = slines[0][slines[0].length-1]
function distance(a,b) {
return (a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])
}
while (clines.length) {
let closest, min = 1e9, backwards=false
for (let j in clines) {
let d1 = distance( clines[j][0], last )
let d2 = distance( clines[j][ clines[j].length-1 ], last )
if (d1<min) {
min=d1, closest=j, backwards=false
}
if (d2<min) {
min=d2, closest=j, backwards=true
}
}
let l = clines.splice(closest,1)[0]
if (backwards) {
l.reverse()
}
slines=slines.concat([l])
last = l[l.length-1]
}
return slines
}
// slowly draw the points list - useful for debugging
function animatePointList(output, speed){
let out=[],i=0,j=0;
speed = speed || 1;
(function f(){
for (let q=0;q<speed;q++) {
if (!out[i]) out[i]=[]
out[i][j] = output[i][j]
if (++j>=output[i].length) j=0,i++
}
postLines(out)
if (i<output.length) setTimeout(f,20)
})();
}
function postLines(data){
let pathstring="";
// either a list of points, or a list of lists of points
if (typeof data[0][0] !== "object") data = [data]
if (data[0][0].x) {
for (let p in data) {
pathstring += ' M'+data[p][0].x.toFixed(2)+','+data[p][0].y.toFixed(2);
for (let i=1;i<data[p].length;i++) pathstring+='L'+data[p][i].x.toFixed(2)+','+data[p][i].y.toFixed(2);
}
} else {
for (let p in data) {
pathstring += ' M'+data[p][0][0].toFixed(2)+','+data[p][0][1].toFixed(2);
for (let i=1;i<data[p].length;i++) pathstring+='L'+data[p][i][0].toFixed(2)+','+data[p][i][1].toFixed(2);
}
}
postMessage(['svg-path',pathstring])
}
function postCircles(data){
let pathstring = ""
if (data[0].x) {
for (let p in data) {
let {x,y,r}=data[p];
if (r<0.001) r=0.001;
pathstring += 'M'+x.toFixed(2)+','+(y-r).toFixed(2) +' a '+r.toFixed(3)+' '+r.toFixed(3)+' 0 1 0 0.001 0Z ';
}
} else {
for (let p in data) {
let [x,y,r]=data[p];
if (r<0.001) r=0.001;
pathstring += 'M'+x.toFixed(2)+','+(y-r).toFixed(2) +' a '+r.toFixed(3)+' '+r.toFixed(3)+' 0 1 0 0.001 0Z ';
}
}
postMessage(['svg-path',pathstring])
}