-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
324 lines (298 loc) · 9.44 KB
/
index.html
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<!DOCTYPE html>
<html>
<head>
<title>Image Convolution GPU.js</title>
</head>
<body>
<script src="js/gpu.js?nocache"></script>
Sequence of filtering function: GrayScale -> Sobel -> Blur -> Sharpen -> Emboss -> Camera ->Animation
<hr>
An attempt has been made to improve performance of 3x3 blur filtering, but has deproved performances instead.
<hr>
GPU<input id="r1"type="radio" name="GPUCPU" checked="checked" >
CPU<input id="r2"type="radio" name="GPUCPU" >
<hr>
<input type="button" value="GrayScale Disabled" onclick="return toggleGrayScale(this);" />
<input type="button" value="Sobel Disabled" onclick="return toggleSobel(this);" />
<input type="button" value="Blur Disabled" onclick="return toggleBlur(this);" />
Choose blur filtering method:
<select id="filterMethod" onchange="changeFilter(this);">
<option value="0" selected="selected">unoptimized 3x3</option>
<option value="1">optimized 3x3</option>
<option value="2">unoptimized 5x5</option>
</select>
<input type="button" value="Sharpen Disabled" onclick="return toggleSharp(this);" />
<input type="button" value="Emboss Disabled" onclick="return toggleEmboss(this);" />
<input type="button" value="Blink Disabled" onclick="return toggleTV(this);" />
<input type="button" value="Animation Disabled" onclick="return changeAnimation(this);" />
<hr>
<i>FPS:</i><div id="fps"></div>
<table align="center">
<tr><td>
<img id="1" src="images/image2.jpg" onload="loadImage(this.id)" width="800" height="600" style="display: none;">
<canvas id="c1" width="800" height="600">
</td></tr>
</table>
<script>
var loadedImage = false;
var arr1 = [];
var filtering = 0;
var animation = false;
var firsttime = true;
var selection = 0;
//filter variables
var gray = false;
var sharp = false;
var sobel = false;
var blur = false;
var emboss = false;
var tv = false;
var filterMethod = 0;
//calculate fps function
var fps = { startTime : 0, frameNumber : 0,
getFPS : function() {
this.frameNumber++;
var d = new Date().getTime(), currentTime = ( d - this.startTime ) / 1000, result = Math.floor( ( (this.frameNumber*10) / currentTime ) );
if( currentTime > 1 ) {
this.startTime = new Date().getTime();
this.frameNumber = 0;
}
return result/10.0;
}
};
//change method for blur filtering
function changeFilter(el){
filterMethod = el.value;
}
/*start of toggle enabled/disabled filtering function */
function toggleGrayScale(el) {
if ( el.value === "GrayScale Disabled" ) {
gray = true;
el.value = "GrayScale Enabled";
} else {
gray = false;
el.value = "GrayScale Disabled";
}
}
function toggleSharp(el) {
if ( el.value === "Sharpen Disabled" ) {
sharp = true;
el.value = "Sharpen Enabled";
} else {
sharp = false;
el.value = "Sharpen Disabled";
}
}
function toggleTV(el) {
if ( el.value === "Blink Disabled" ) {
tv = true;
el.value = "Blink Enabled";
} else {
tv = false;
el.value = "Blink Disabled";
}
}
function toggleSobel( el ) {
if ( el.value === "Sobel Disabled" ) {
sobel = true;
el.value = "Sobel Enabled";
} else {
sobel = false;
el.value = "Sobel Disabled";
}
}
function toggleBlur( el ) {
if ( el.value === "Blur Disabled" ) {
blur = true;
el.value = "Blur Enabled";
} else {
blur = false;
el.value = "Blur Disabled";
}
}
function toggleEmboss( el ) {
if ( el.value === "Emboss Disabled" ) {
emboss = true;
el.value = "Emboss Enabled";
} else {
emboss = false;
el.value = "Emboss Disabled";
}
}
function changeAnimation( el ) {
if ( el.value === "Animation Disabled" ) {
animation = true
el.value = "Animation Enabled";
} else {
animation = false;
el.value = "Animation Disabled";
}
}
/*end of toggle enabled/disabled filtering function */
//load the image an array
function loadImage(n) {
//get canvas and image
var canvas = document.getElementById("c"+n);
var ctx = canvas.getContext('2d');
var imag = document.getElementById(n);
//draw image on new canvas
ctx.drawImage(imag, 0, 0);
//get image data
var imageData = ctx.getImageData(0, 0, 800, 600);
//initialize array [RGBA][Y][X]
for (var channel=0; channel<4; channel++) {
window["arr"+n].push([]);
for (var y=0; y<600; y++) {
window["arr"+n][channel].push([]);
}
}
//fill array with converted data
var pointer = 0;
for (var y=0; y<600; y++) {
for (var x=0; x<800; x++) {
window["arr"+n][0][600-y-1][x] = imageData.data[pointer++]/256;
window["arr"+n][1][600-y-1][x] = imageData.data[pointer++]/256;
window["arr"+n][2][600-y-1][x] = imageData.data[pointer++]/256;
window["arr"+n][3][600-y-1][x] = imageData.data[pointer++]/256;
}
}
var pointer = 0;
loadedImage = true;
}
</script>
<script src="kerneldefs.js?nocache"></script>
<script>
//make animation function
var myCodeAnim = makeAnim("cpu");
//make filtering function
/*filtering function for cpu mode*/
var cpuGray = makeGrayScalelFilter("cpu");
var cpuSharp = makeSharpFilter("cpu");
var cpuSobel = makeSobelFilter("cpu");
var cpuBlur = makeBlurFilter("cpu");
var cpuBlur5x5 = makeBlurFilter5x5("cpu");
var cpuEmboss = makeEmbossFilter("cpu");
var cpuTV = makeTVFilter("cpu");
var marquee2 = makeAnimator("cpu");
//cpu optimized version for blur
var cpuBlur2 = makeBlurFilterOptimized("cpu");
var cpuPart2 = makeBlurFilterOptimized_2("cpu");
var cpuBlur5x5_2 = makeBlurFilter5x5Optimized("cpu");
/*filtering function for gpu mode*/
var myKernelAnim = makeAnim("gpu");
var gpuGray = makeGrayScalelFilter("gpu");
var gpuSharp = makeSharpFilter("gpu");
var gpuSobel = makeSobelFilter("gpu");
var gpuBlur = makeBlurFilter("gpu");
var gpuBlur5x5 = makeBlurFilter5x5("cpu");
var gpuEmboss = makeEmbossFilter("gpu");
var gpuTV = makeTVFilter("gpu");
var marquee = makeAnimator("gpu");
//cpu optimized version for blur
var gpuBlur2 = makeBlurFilterOptimized("gpu");
var gpuPart2 = makeBlurFilterOptimized_2("gpu");
var gpuBlur5x5_2 = makeBlurFilter5x5Optimized("gpu");
//for orignal image
var orig;
//variable for animation effect
var animIndex = 0;
//variable for fps element
var f = document.querySelector("#fps");
//Rendering Loop
function renderLoop() {
f.innerHTML = fps.getFPS();
/*if cpu mode selected*/
if (document.getElementById("r2").checked) {
if (loadedImage){
orig = myCodeAnim(arr1);
loadedImage=false;
}
/*if Animation effect selected*/
if (animation) {
var C = marquee2(orig,animIndex);
animIndex = (animIndex+1)%800;
} else {
var C = orig;
}
/*if GrayScale selected*/
if(gray)
var C = cpuGray(C);
/*if Sobel selected*/
if (sobel)
var C = cpuSobel(C);
/*if Blur selected*/
if(blur){
//check which blur method to apply
if(filterMethod ==0)
var C = cpuBlur(C);
else if(filterMethod == 1){
B = cpuBlur2(C);
C =cpuPart2(B);
}
else
var C=cpuBlur5x5(C);
}
/*if Sharp selected*/
if(sharp)
var C = cpuSharp(C);
/*if Emboss selected*/
if(emboss)
var C = cpuEmboss(C);
/*if Camera effect selected*/
if(tv){
C= cpuTV(C,Math.random()*2);
}
}
/*else gpu mode selected*/
/*everything here is similar to cpu mode, using gpu filtering function instead*/
else {
if (loadedImage){
orig = myKernelAnim(arr1);
loadedImage=false;
}
if (animation) {
var C = marquee(orig,animIndex);
animIndex = (animIndex+1)%800;
} else {
var C = orig;
}
if(gray)
var C = gpuGray(C);
if (sobel)
var C = gpuSobel(C);
if(blur){
if(filterMethod ==0)
var C = gpuBlur(C);
else if(filterMethod == 1){
B = gpuBlur2(C);
C =gpuPart2(B);
B.delete();
}
else {
var C=gpuBlur5x5(C);
}
}
if(sharp)
var C = gpuSharp(C);
if(emboss)
var C = gpuEmboss(C);
if(tv){
C= gpuTV(C,Math.random()*2);
}
}
//render the image to graphical output
toimg1(C);
//first time rendered, replace canvas element
if (firsttime) {
firsttime = false;
var cv1 = document.getElementsByTagName("canvas")[0];
var newCanvas1 = toimg1.getCanvas();
newCanvas1.id = cv1.id;
cv1.parentNode.replaceChild(newCanvas1, cv1);
}
requestAnimationFrame(renderLoop);
}
window.onload = renderLoop;
</script>
</body>
</html>