-
Notifications
You must be signed in to change notification settings - Fork 219
/
SmoothWandTool.txt
80 lines (76 loc) · 2.04 KB
/
SmoothWandTool.txt
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
//Smooth Wand version 1d
//N.Vischer
//01.07.17 20:20
var count, polyX, polyY
getSelectionBounds(x, y, w, h);
if (w<2 || h<2)
exit("Width and height must be > 1 px");
if (selectionType != 4)
exit("No traced selection found");
getThreshold(lowThr, hiThr);
getSelectionCoordinates(xx, yy);
len = xx.length;
polyX = newArray(len);
polyY = newArray(len);
count = 0;
for(jj = 0; jj < len; jj++) {
x1 = xx[jj];
y1 = yy[jj];
x2 = xx[(jj+1)%len];
y2 = yy[(jj+1)%len];
dd = 1;
if (y1 == y2) {//horizontal separator
if (x1 > x2) dd = -1;
for(x = x1; x != x2; x+= dd){
processPixelPair(x, y1, x+dd, y1);
}
}
else {//vertical separator
if (y1 > y2) dd = -1;
for(y = y1; y != y2; y+= dd){
processPixelPair(x1, y, x1, y+dd);
}
}
}
polyX = Array.trim(polyX, count);
polyY = Array.trim(polyY, count);
makeSelection("polygon", polyX, polyY);
run("Interpolate", "interval=1 adjust"); //after button released
//processes neighbors of this separator line and adds vertex
function processPixelPair(x1, y1, x2, y2) {
if (x1 == x2) {
val1 = getPixel(x1, minOf(y1, y2));
val2 = getPixel(x1 - 1, minOf(y1, y2));
}
if (y1 == y2){
val1 = getPixel(minOf(x1, x2), y1);
val2 = getPixel(minOf(x1, x2), y1 -1);
}
bright = maxOf(val1, val2);
dark = minOf(val1, val2);
if (bright>=lowThr && dark<=lowThr)
thr = lowThr;
if (bright>=hiThr && dark<=hiThr)
thr = hiThr;
if (dark==bright)
fraction = 0.5;
else
fraction = (thr - dark)/(bright - dark);
if (val1 < val2)
fraction = 1 - fraction;
if (y1 == y2) {
newY = minOf(y1, y2) + fraction - 0.5;
newX = (x1 + x2)/2;
}
if (x1 == x2) {
newX = minOf(x1, x2) + fraction - 0.5;
newY = (y1 + y2)/2;
}
polyX[count] = newX;
polyY[count] = newY;
count++;
if (count == polyX.length){
polyX = Array.concat(polyX, polyX);
polyY = Array.concat(polyY, polyY);
}
}