forked from mbanquiero/TallerAlgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bilinear.html
98 lines (75 loc) · 1.8 KB
/
bilinear.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
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var canvas;
var ctx;
//
// Ejemplo de bilinear sampling
//
var diff_map = [];
function CreateTexture()
{
for(var i=0;i<4;++i)
for(var j=0;j<4;++j)
diff_map[i+4*j] = {r:128 , g:128, b:128};
diff_map[1+4*1] = {r:255 , g:0, b:0};
diff_map[2+4*1] = {r:0 , g:255, b:0};
diff_map[1+4*2] = {r:0 , g:0, b:255};
diff_map[2+4*2] = {r:255 , g:255, b:255};
}
function tex2d(u, v)
{
var du = 4;
var dv = 4;
u -= 0.5/du;
v -= 0.5/dv;
var r = u*du;
var s = v*dv;
var i = r<0 ? du-1 : r|0
var j = s<0 ? dv-1 : s|0
var ri = r-i;
var rj = s-j;
var i1 = i<du-1?i+1:0;
var j1 = j<dv-1?j+1:0;
if(ri<0)
ri+=du;
if(rj<0)
rj+=dv;
var c00 = diff_map[i+dv*j];
var c01 = diff_map[i+dv*j1];
var c11 = diff_map[i1+dv*j1];
var c10 = diff_map[i1+dv*j];
var red = (c00.r * (1-ri)*(1-rj) + c10.r*ri*(1-rj) + c11.r*ri*rj + c01.r*(1-ri)*rj) | 0;
var green = (c00.g * (1-ri)*(1-rj) + c10.g*ri*(1-rj) + c11.g*ri*rj + c01.g*(1-ri)*rj) | 0;
var blue = (c00.b * (1-ri)*(1-rj) + c10.b*ri*(1-rj) + c11.b*ri*rj + c01.b*(1-ri)*rj) | 0;
return 'rgba(' + red.toString() + ',' + green.toString() + ',' + blue.toString() + ',255)';
}
function draw()
{
if (canvas.getContext)
{
ctx.fillStyle = 'rgba(240,240,240,255)';
ctx.fillRect(0,0,1000,800);
var k = 5;
for(var i=0;i<=125;++i)
for(var j=0;j<=125;++j)
{
ctx.fillStyle = tex2d(i/125,j/125);
ctx.fillRect(10+i*k,10+j*k,k,k);
}
}
}
function animate()
{
canvas = document.getElementById('mycanvas');
ctx = canvas.getContext('2d');
CreateTexture();
draw();
}
</script>
</head>
<body onload="animate();">
<canvas id="mycanvas" width="1000" height="700"></canvas>
</body>
</html>