-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge_scale.html
More file actions
executable file
·176 lines (147 loc) · 5.7 KB
/
edge_scale.html
File metadata and controls
executable file
·176 lines (147 loc) · 5.7 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Video Frames</title>
<link rel="stylesheet" href="style.css">
<style>
#movieclip {
display: none;
}
</style>
</head>
<body>
<header>Avoid cross domain security error. $ http-server -p 8000 </header>
<canvas id="canvas" width="640" height="360"></canvas>
<input id="mono" type="checkbox" name="mono"/>
<input id="invert" type="checkbox" name="invert"/>
<input id="r_weight" type="range" value="0.4" min="0.0" max="3" step="0.1"/>
<input id="g_weight" type="range" value="1.0" min="0.0" max="3" step="0.1"/>
<input id="b_weight" type="range" value="1.0" min="0.0" max="3" step="0.1"/>
<video id="movieclip" width="640" height="360" autoplay>
<source src="./assets/movieclip.mp4" type="video/mp4"/>
<source src="./assets/movieclip.webm" type="video/webm"/>
<source src="./assets/movieclip.ogv" type="video/ogg"/>
</video>
<script src="utils.js"></script>
<script src="Stats.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
copyCanvas = createCopyCanvas(canvas),
copyContext = copyCanvas.getContext('2d'),
video = document.getElementById('movieclip'),
stats = Stat.initStats();
function createCopyCanvas (canvas) {
var copy = document.createElement('canvas');
copy.width = canvas.width/1.2;
copy.height = canvas.height/1.2;
return copy;
}
function edge (imageData, copiedImageData, mono, invert, rWeightNumerator, gWeightNumerator, bWeightNumerator) {
var pixels = imageData.data,
pixelsCopy = copiedImageData.data,
h = imageData.height,
w = imageData.width,
w4 = w*4,
c = -1/8,
rWeight = rWeightNumerator/c,
gWeight = gWeightNumerator/c,
bWeight = bWeightNumerator/c;
for (var y = h; y > 0; y--) {
var offsetY = (y-1)*w4;
var nextY = (y == h) ? y - 1 : y;
var prevY = (y == 1) ? 0 : y-2;
var offsetYPrev = prevY*w*4;
var offsetYNext = nextY*w*4;
for (var x = w; x > 0; x--) {
var offset = offsetY + (x*4-4);
var offsetPrev = offsetYPrev + ((x == 1) ? 0 : x-2) * 4;
var offsetNext = offsetYNext + ((x == w) ? x-1 : x) * 4;
var r = ((pixelsCopy[offsetPrev-4]
+ pixelsCopy[offsetPrev]
+ pixelsCopy[offsetPrev+4]
+ pixelsCopy[offset-4]
+ pixelsCopy[offset+4]
+ pixelsCopy[offsetNext-4]
+ pixelsCopy[offsetNext]
+ pixelsCopy[offsetNext+4]) * c
+ pixelsCopy[offset]
)
* rWeight;
var g = ((pixelsCopy[offsetPrev-3]
+ pixelsCopy[offsetPrev+1]
+ pixelsCopy[offsetPrev+5]
+ pixelsCopy[offset-3]
+ pixelsCopy[offset+5]
+ pixelsCopy[offsetNext-3]
+ pixelsCopy[offsetNext+1]
+ pixelsCopy[offsetNext+5]) * c
+ pixelsCopy[offset+1])
* gWeight;
var b = ((pixelsCopy[offsetPrev-2]
+ pixelsCopy[offsetPrev+2]
+ pixelsCopy[offsetPrev+6]
+ pixelsCopy[offset-2]
+ pixelsCopy[offset+6]
+ pixelsCopy[offsetNext-2]
+ pixelsCopy[offsetNext+2]
+ pixelsCopy[offsetNext+6]) * c
+ pixelsCopy[offset+2])
* bWeight;
if (mono) {
var brightness = (r*0.3 + g*0.59 + b*0.11)||0;
if (invert) brightness = 255 - brightness;
if (brightness < 0 ) brightness = 0;
if (brightness > 255 ) brightness = 255;
r = g = b = brightness;
} else {
if (invert) {
r = 255 - r;
g = 255 - g;
b = 255 - b;
}
if (r < 0 ) r = 0;
if (g < 0 ) g = 0;
if (b < 0 ) b = 0;
if (r > 255 ) r = 255;
if (g > 255 ) g = 255;
if (b > 255 ) b = 255;
}
pixels[offset] = r;
pixels[offset+1] = g;
pixels[offset+2] = b;
}
}
}
var callCount = 0;
(function drawFrame () {
stats.begin();
window.requestAnimationFrame(drawFrame, canvas);
copyContext.drawImage(
video,
0, 0, canvas.width, canvas.height,
0, 0, copyCanvas.width, copyCanvas.height
);
var imageData = copyContext.getImageData(0, 0, copyCanvas.width, copyCanvas.height),
copiedImageData = copyContext.getImageData(0, 0, copyCanvas.width, copyCanvas.height),
mono = document.getElementById('mono').checked,
invert = document.getElementById('invert').checked,
rWeight = parseFloat(document.getElementById('r_weight').value, 10) || 0.0,
gWeight = parseFloat(document.getElementById('g_weight').value, 10) || 0.0,
bWeight = parseFloat(document.getElementById('b_weight').value, 10) || 0.0;
edge(imageData, copiedImageData, mono, invert, rWeight, gWeight, bWeight);
context.clearRect(0,0,canvas.width,canvas.height);
copyContext.putImageData(imageData, 0, 0);
context.drawImage(
copyCanvas,
0, 0, copyCanvas.width, copyCanvas.height,
0, 0, canvas.width, canvas.height
);
stats.end();
}());
};
</script>
</body>
</html>