-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient.js
More file actions
116 lines (101 loc) · 3.08 KB
/
Copy pathgradient.js
File metadata and controls
116 lines (101 loc) · 3.08 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
/*
Compute color vector distance.
*/
function colorDistance(pixels, p1, p2) {
var rDiff = pixels[p1] - pixels[p2];
var gDiff = pixels[p1 + 1] - pixels[p2 + 1];
var bDiff = pixels[p1 + 2] - pixels[p2 + 2];
/* if (isNaN(rDiff) || isNaN(gDiff) || isNaN(bDiff)) {
console.log(p1, pixels[p1]);
console.log(p2, pixels[p2]);
} */
return Math.sqrt(rDiff * rDiff + gDiff * gDiff + bDiff * bDiff);
}
function updateVerticalGradient(seamCoordinates, pixels, w, h, oldGradient) {
var newGradient = new Array(w * h);
for (var i = 0; i < seamCoordinates.length; i++) {
var coord = seamCoordinates[i];
var y = coord[1];
//update gradient for that row
for (var x = 0; x < w; x++) {
var currIndex = y * w + x;
//gradient before do not change
if (x < coord[0] - 1) {
newGradient[currIndex] = oldGradient[y * (w + 1) + x];
}
else if (x == coord[0] - 1 || x == coord[0]) {
newGradient[currIndex] = colorDistance(pixels,
x == w - 1? currIndex * 4 : (currIndex + 1) * 4 ,
x == 0? currIndex * 4 : (currIndex - 1) * 4);
}
else {
newGradient[currIndex] = oldGradient[y * (w + 1) + x + 1];
}
if (isNaN(newGradient[currIndex]))
console.log(x, y);
}
}
return newGradient;
}
function updateHorizontalGradient(seamCoordinates, pixels, w, h, oldGradient) {
var newGradient = new Array(w * h);
for (var i = 0; i < seamCoordinates.length; i++) {
var coord = seamCoordinates[i];
var x = coord[0];
//update gradient for that row
for (var y = 0; y < h; y++) {
var currIndex = y * w + x;
//gradient before do not change
if (y < coord[1] - 1) {
newGradient[currIndex] = oldGradient[y * w + x];
}
else if (y == coord[1] - 1 || y == coord[1]) {
newGradient[currIndex] = colorDistance(pixels,
y == h - 1? currIndex * 4 : (currIndex + w) * 4 ,
y == 0? currIndex * 4 : (currIndex - w) * 4);
}
else {
newGradient[currIndex] = oldGradient[y * w + x + 1];
}
if (isNaN(newGradient[currIndex]))
console.log(x, y);
}
}
return newGradient;
}
function computeGradient(pixels, w, h) {
var gradients = [];
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
var index = (y * w + x) * 4;
//horizontal gradient
var horizontal = colorDistance(pixels,
x == w - 1? index : index + 4,
x == 0? index : index - 4);
//vertical gradient
var vertical = colorDistance(pixels,
y == h - 1? index : index + w * 4,
y == 0? index : index - w * 4);
if (isNaN(horizontal) || isNaN(vertical)) {
console.log(x, y);
console.log(horizontal);
console.log(vertical);
}
gradients.push( (horizontal + vertical) );
}
}
return gradients;
}
function drawGradient(ctx, w, h, gradients) {
var maxGradient = Math.max.apply(null, gradients);
var imgData = ctx.createImageData(w, h);
console.log(gradients.length);
console.log(maxGradient);
for (var i = 0; i < gradients.length; i++) {
var whiteFactor = (gradients[i] / maxGradient) * 255;
for (var j = i * 4; j < i * 4 + 3; j++)
imgData.data[j] = whiteFactor;
imgData.data[j] = 255;
}
ctx.putImageData(imgData, 0, 0);
}