-
Notifications
You must be signed in to change notification settings - Fork 0
/
texfunc.py
68 lines (57 loc) · 1.66 KB
/
texfunc.py
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
from numba import jit
import numpy as np
from PIL import Image
# bilinear interpolation
@jit
def texture2D(img, uv):
h, w = img.shape[:2]
img_size = np.array([w, h])
texel = np.array([uv[0], 1.0 - uv[1]]) * img_size - 0.5
lxy = np.floor(texel)
dx, dy = texel - lxy
x0, y0 = lxy
x1, y1 = lxy + 1
if len(img.shape) > 2:
f00 = img[int(y0), int(x0), :]
f10 = img[int(y0), int(x1), :]
f01 = img[int(y1), int(x0), :]
f11 = img[int(y1), int(x1), :]
else:
f00 = img[int(y0), int(x0)]
f10 = img[int(y0), int(x1)]
f01 = img[int(y1), int(x0)]
f11 = img[int(y1), int(x1)]
return f00 * (1 - dx) * (1 - dy) + f10 * dx * (1 - dy)\
+ f01 * (1 - dx) * dy + f11 * dx * dy
@jit
def texture2D_px(img, xy):
h, w = img.shape[:2]
img_size = np.array([w, h])
lxy = np.floor(xy)
dx, dy = xy - lxy
x0, y0 = lxy
x1, y1 = lxy + 1
if len(img.shape) > 2:
f00 = img[int(y0), int(x0), :]
f10 = img[int(y0), int(x1), :]
f01 = img[int(y1), int(x0), :]
f11 = img[int(y1), int(x1), :]
else:
f00 = img[int(y0), int(x0)]
f10 = img[int(y0), int(x1)]
f01 = img[int(y1), int(x0)]
f11 = img[int(y1), int(x1)]
return f00 * (1 - dx) * (1 - dy) + f10 * dx * (1 - dy)\
+ f01 * (1 - dx) * dy + f11 * dx * dy
@jit
def texture2D_nn(img, uv):
h, w = img.shape[:2]
img_size = np.array([w, h])
texel = uv * img_size - 0.5
xy = np.round(texel)
if len(img.shape) > 2:
return img[int(xy[1]), int(xy[0]), :]
else:
return img[int(xy[1]), int(xy[0])]
if __name__ == '__main__':
pass