-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
137 lines (102 loc) · 4.72 KB
/
Copy pathfunctions.js
File metadata and controls
137 lines (102 loc) · 4.72 KB
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
// ************************************************
// * *
// * RESIZE of IMAGE *
// * *
// ************************************************
function img2res(inp, s) {
let img = createGraphics(inp.width*s,inp.height*s);
img.image(inp,0,0,img.width,img.height);
return img;
}
// ************************************************
// * *
// * CHANEL MIXER of IMAGE *
// * *
// ************************************************
function img2rgb(inp, kr, kg, kb) {
let img = createGraphics(inp.width,inp.height);
img.image(inp,0,0);
img.loadPixels();
let a = [];
for (let x = 0; x < img.width; x++) { a[x] = []; for (let y = 0; y < img.height; y++) { let c = img.get(x,y); a[x][y] = red(c)*kr + green(c)*kg + blue(c)*kb; } }
return a;
}
// ************************************************
// * *
// * HSB of IMAGE *
// * *
// ************************************************
function img2hsb(inp, mode) {
let img = createGraphics(inp.width,inp.height);
img.image(inp,0,0);
img.loadPixels();
let a = [];
for (let x = 0; x < img.width; x++) { a[x] = []; for (let y = 0; y < img.height; y++) {
let c = img.get(x,y);
if (mode == "h") a[x][y] = hue(c);
else if (mode == "s") a[x][y] = saturation(c);
else if (mode == "b") a[x][y] = brightness(c);
else if (mode == "l") a[x][y] = lightness(c);
else if (mode == "sb") a[x][y] = saturation(c) * brightness(c)/255 * 2.0;
} }
return a;
}
// ************************************************
// * *
// * EDGES DETECTION of ARRAY *
// * *
// ************************************************
function arr2edg(a) { let w = a.length; let h = a[0].length;
let o = []; for (let x = 0; x < w; x++) { o[x] = []; for (let y = 0; y < h; y++) { o[x][y] = 0; } }
let k1 = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]];
let k2 = [[-1, -2, -1], [ 0, 0, 0], [ 1, 2, 1]];
for (let x = 1; x < w-1; x++) {
for (let y = 1; y < h-1; y++) {
let s1 = 0; for (let sx = -1; sx <=1 ; sx++) { for (let sy = -1; sy <=1; sy++) { s1 += a[x+sx][y+sy]*k1[sx+1][sy+1]; } }
let s2 = 0; for (let sx = -1; sx <=1 ; sx++) { for (let sy = -1; sy <=1; sy++) { s2 += a[x+sx][y+sy]*k2[sx+1][sy+1]; } }
o[x][y] = map(sqrt(s1*s1+s2*s2),0,1414,0,255);
}
}
return(o);
}
// ************************************************
// * *
// * MEDIAN of ARRAY *
// * *
// ************************************************
function arr2med(a) { let w = a.length; let h = a[0].length;
let d = 1000;
let xm = []; for (let x = 0; x < w; x++) { xm[x] = 0; }
let ym = []; for (let y = 0; y < h; y++) { ym[y] = 0; }
let s = 0;
for (let x = 0; x < w; x++) { for (let y = 0; y < h; y++) { xm[x] += a[x][y]; s+=a[x][y]; } }
for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { ym[y] += a[x][y]; } }
let xl = 0; let xr = 0; let il = 0; let ir = xm.length-1;
let yu = 0; let yd = 0; let iu = 0; let id = ym.length-1;
for (let i = 0; i < xm.length; i++) { if (il<ir) { if (xl<=xr+d) { xl += xm[il]; il++; } if (xl>=xr-d) { xr += xm[ir]; ir--; } } }
for (let i = 0; i < ym.length; i++) { if (iu<id) { if (yu<=yd+d) { yu += ym[iu]; iu++; } if (yu>=yd-d) { yd += ym[id]; id--; } } }
return([il,iu,xm,ym,s/xm.length/ym.length/255]);
}
// ************************************************
// * *
// * WEIGHT of ARRAY *
// * *
// ************************************************
function arr2bal(a) { let w = a.length; let h = a[0].length;
let sum = 0;
for (let x = 0; x < w; x++) { for (let y = 0; y < h; y++) { sum += a[x][y]; } }
sum = sum/(w*h*255);
return(sum);
}
// ************************************************
// * *
// * ARRAY to IMAGE *
// * *
// ************************************************
function arr2img(a) { let w = a.length; let h = a[0].length;
let img = createGraphics(w,h);
img.loadPixels();
for (let x = 0; x < w; x++) { for (let y = 0; y < h; y++) { img.set(x,y,a[x][y]); } }
img.updatePixels();
return(img);
}