-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsobel.js
More file actions
59 lines (56 loc) · 1.97 KB
/
sobel.js
File metadata and controls
59 lines (56 loc) · 1.97 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
async function sobel(dest){
// adapted from https://github.com/Elements-/sobel-operator
await new Promise((res,rej)=>{
const png = new pnge({filterType:-1})
var xderivatives = [
[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]
];
var yderivatives = [
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]
];
function getIndex(x,y){return (png.width * y + x) << 2}
function getBump(x, y) {
var xbump = 0;
var ybump = 0;
for(var xOffset = -1; xOffset <= 1; xOffset++) {
for(var yOffset = -1; yOffset <= 1; yOffset++) {
var idx = getIndex(x + xOffset, y + yOffset);
var colorWeights = 0;
for(var color = 0; color <=2; color++) {
if(png.data[idx+color]) {
colorWeights += png.data[idx+color]
} else {
return 0;
}
}
var greyscale = Math.floor(colorWeights / 3);
xbump += greyscale * xderivatives[yOffset+1][xOffset+1];
ybump += greyscale * yderivatives[yOffset+1][xOffset+1];
}
}
var bump = Math.floor(Math.sqrt(Math.pow(xbump, 2) + Math.pow(ybump, 2)) / 3);
return bump;
}
fs.createReadStream(dest).pipe(png).on('parsed',()=>{
var newData = new Buffer(png.width*png.height*4);
const test = []
for(var y = 0; y < png.height; y ++) {
for(var x = 0; x < png.width; x ++) {
var index = getIndex(x,y);
var bump = getBump(x, y);
for(var color = 0; color <= 2; color++) {
newData[index+color] = bump < 127 ? 0 : 255;
test.push(bump)
}
newData[index+3] = 255;
}
}
png.data = newData
png.pack().pipe(fs.createWriteStream(dest)).on('finish',res);
})
})
}