-
Notifications
You must be signed in to change notification settings - Fork 63
/
linescan.js
121 lines (113 loc) · 3.24 KB
/
linescan.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
/**
* Algorithm by j-waal
*/
importScripts('helpers.js')
postMessage(['sliders', [
{ label: 'Spacing', value: 5, min: 1, max: 20, step: 1 },
{ label: 'Threshold', value: 128, min: 0, max: 255, step: 1 },
{ label: 'Minlength', value: 1, min: 0, max: 32, step: 1 },
{ label: 'Alternate', type:'checkbox', checked:false},
{ label: 'Direction', type:'select', value:'Horizontal', options:['Horizontal','Vertical','Both']},
]]);
onmessage = function (e) {
const [config, pixData] = e.data;
// User variables
const spacing = config.Spacing // spacing between lines
const threshold = config.Threshold
const minlength = config.Minlength // dont output short line segments
const direction = config.Direction
const alternate = config.Alternate
// Image processor (gets a value for all pixels)
const getPixel = pixelProcessor(config, pixData)
// Create an empty array of points
let points = [];
if (direction == 'Horizontal' || direction == 'Both'){
let toggle = true; // toggle to alternate line direction
for (let y = 0; y < config.height; y += spacing) { // scan horizontal
let thisline = []
let mode = false
let storex
for (let x = 0; x <= config.width - 1; x += 1) {
pixelval = getPixel(x, y);
if (pixelval > threshold && mode == false){
// start line here
storex = x
mode = true
}
if (pixelval < threshold && mode == true){
// store only the start and end of the lines
if (x-storex > minlength){
if (toggle){
thisline.push([[storex ,y], [x ,y]]);
}else{
thisline.push([[x ,y], [storex ,y]]);
}
}
mode = false
}
}
if (mode == true){
if (config.width-storex > minlength){
if (toggle){
thisline.push([[storex ,y], [config.width ,y]]);
}else{
thisline.push([[config.width ,y], [storex ,y]]);
}
}
}
if (toggle){
points = points.concat(thisline)
}else{
points = points.concat(thisline.reverse())
}
if (alternate && thisline.length){ // dont change direction on empty lines
toggle=!toggle;
}
}
}
if (direction == 'Vertical' || direction == 'Both'){
let toggle = true; // toggle to alternate line direction
for (let x = 0; x < config.width; x += spacing) { // scan vertical
let thisline = []
let mode = false
let storey
for (let y = 0; y <= config.height - 1; y += 1) {
pixelval = getPixel(x, y);
if (pixelval > threshold && mode == false){
// start line here
storey = y
mode = true
}
if (pixelval < threshold && mode == true){
// store only the start and end of the lines
if (y-storey > minlength){
if (toggle){
thisline.push([[x ,storey], [x ,y]]);
}else{
thisline.push([[x ,y], [x ,storey]]);
}
}
mode = false
}
}
if (mode == true){
if (config.height-storey > minlength){
if (toggle){
thisline.push([[x ,storey], [x, config.height]]);
}else{
thisline.push([[x, config.height], [x ,storey]]);
}
}
}
if (toggle){
points = points.concat(thisline)
}else{
points = points.concat(thisline.reverse())
}
if (alternate && thisline.length){ // dont change direction on empty lines
toggle=!toggle;
}
}
}
postLines(points);
}