-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
91 lines (68 loc) · 2.07 KB
/
test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from contextlib import ContextDecorator
from patlas import AtlasPacker, load, TextureFormat
from timeit import default_timer
import pathlib
# stress
N = 100; dim = 2 ** 13
# non-stress
#N = 4; dim = 2 ** 11
try:
pth = __file__
except NameError:
pth = '.'
class timer(ContextDecorator):
def __init__(self, strval):
self.sv = strval
def __enter__(self):
self.t0 = default_timer()
return self
def __exit__(self, *exc):
t1 = default_timer() - self.t0
print(f'{self.sv} took {t1:.3f} seconds.')
pth = pathlib.Path(pth).parent.resolve()
ims = [str(pth / 'images' / x) for x in ['alex.png', 'kazoo.jpg']]
x = AtlasPacker(dim, pad = 1)
with timer('Multi'):
for i in range(N):
x.pack(ims)
# Pack all at once (with OpenMP, should be much faster)
z = AtlasPacker(dim, pad = 1)
with timer('Single'):
z.pack(ims*N)
with timer('Pickle'):
x.save(str(pth / 'foo'))
print(f'Pickle size: {(pth / "foo.patlas").stat().st_size * 1e-6:.3f} MB')
with timer('Load'):
loaded_atlas, loaded_locs = load(str(pth / 'foo.patlas'))
assert bytes(loaded_atlas) == bytes(x.atlas)
# DXT5
w = AtlasPacker(dim, pad=1, texture_format=TextureFormat.DXT5)
with timer('DXT5'):
w.pack(ims*N)
with timer('DXT5 retrieval'):
tmp = w.atlas
with timer('DXT5 retrieval (cached)'):
tmp = w.atlas
with timer('DXT5 pickle'):
w.save(str(pth / 'foo'))
print(f'DXT5 pickle size: {(pth / "foo.patlas").stat().st_size * 1e-6:.3f} MB')
with timer('DXT5 pickle load'):
dxt_atlas, dxt_meta = load(str(pth / 'foo.patlas'))
assert bytes(dxt_atlas) == bytes(w.atlas)
if False:
import matplotlib.pyplot as plt
plt.imshow(x.atlas, origin='lower')
plt.show()
plt.imshow(z.atlas, origin='lower')
plt.show()
plt.imshow(w.atlas, origin='lower')
plt.show()
if False:
from PIL import Image
import numpy as np
im = Image.fromarray(np.array(x.atlas))
with timer('PIL save PNG'):
im.save('test.png')
with timer('PIL load PNG'):
with Image.open('test.png') as im:
a = np.asarray(im)