forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_texture.py
More file actions
90 lines (65 loc) · 2.73 KB
/
test_texture.py
File metadata and controls
90 lines (65 loc) · 2.73 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
from panda3d.core import Texture, PNMImage
from array import array
def image_from_stored_pixel(component_type, format, data):
""" Creates a 1-pixel texture with the given settings and pixel data,
then returns a PNMImage as result of calling texture.store(). """
tex = Texture("")
tex.setup_1d_texture(1, component_type, format)
tex.set_ram_image(data)
img = PNMImage()
assert tex.store(img)
return img
def test_texture_store_unsigned_byte():
data = array('B', (2, 1, 0, 0xff))
img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_rgba, data)
assert img.maxval == 0xff
pix = img.get_pixel(0, 0)
assert tuple(pix) == (0, 1, 2, 0xff)
def test_texture_store_unsigned_short():
data = array('H', (2, 1, 0, 0xffff))
img = image_from_stored_pixel(Texture.T_unsigned_short, Texture.F_rgba, data)
assert img.maxval == 0xffff
pix = img.get_pixel(0, 0)
assert tuple(pix) == (0, 1, 2, 0xffff)
def test_texture_store_unsigned_int():
data = array('I', (0x30000, 2, 0, 0xffffffff))
img = image_from_stored_pixel(Texture.T_unsigned_int, Texture.F_rgba, data)
assert img.maxval == 0xffff
pix = img.get_pixel(0, 0)
assert tuple(pix) == (0, 0, 3, 0xffff)
def test_texture_store_float():
data = array('f', (0.5, 0.0, -2.0, 10000.0))
img = image_from_stored_pixel(Texture.T_float, Texture.F_rgba, data)
assert img.maxval == 0xffff
col = img.get_xel_a(0, 0)
assert col.almost_equal((0.0, 0.0, 0.5, 1.0), 1 / 255.0)
def test_texture_store_half():
# Python's array class doesn't support half floats, so we hardcode the
# binary representation of these numbers:
data = array('H', (
# -[exp][mantissa]
0b0011110000000000, # 1.0
0b1100000000000000, # -2.0
0b0111101111111111, # 65504.0
0b0011010101010101, # 0.333251953125
))
img = image_from_stored_pixel(Texture.T_half_float, Texture.F_rgba, data)
assert img.maxval == 0xffff
col = img.get_xel_a(0, 0)
assert col.almost_equal((1.0, 0.0, 1.0, 0.333251953125), 1 / 255.0)
def test_texture_store_srgb():
# 188 = roughly middle gray
data = array('B', [188, 188, 188])
img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_srgb, data)
# We allow some imprecision.
assert img.maxval == 0xff
col = img.get_xel(0, 0)
assert col.almost_equal((0.5, 0.5, 0.5), 1 / 255.0)
def test_texture_store_srgb_alpha():
# 188 = middle gray
data = array('B', [188, 188, 188, 188])
img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_srgb_alpha, data)
# We allow some imprecision.
assert img.maxval == 0xff
col = img.get_xel_a(0, 0)
assert col.almost_equal((0.5, 0.5, 0.5, 188 / 255.0), 1 / 255.0)