This repository was archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtools.py
More file actions
50 lines (46 loc) · 1.47 KB
/
Copy pathtools.py
File metadata and controls
50 lines (46 loc) · 1.47 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
import ctypes
import numpy as np
import OpenGL.GL as gl
# only load libraries if they are needed
class LazyFuncCaller:
libs = {}
def __init__(self, lib_name, func_name):
self.lib_name = lib_name
self.func_name = func_name
def __call__(self, *args, **kwargs):
if self.lib_name not in LazyFuncCaller.libs:
LazyFuncCaller.libs[self.lib_name] = ctypes.CDLL(self.lib_name)
func = getattr(LazyFuncCaller.libs[self.lib_name], self.func_name)
return func(*args, **kwargs)
# provide rollback capability to classes
class TransactionMixin:
def __init__(self):
self.rollback_cbs = []
def rollback(self):
for cb in reversed(self.rollback_cbs):
cb()
self.rollback_cbs = []
def add_rollback_cb(self, cb):
self.rollback_cbs += [ cb ]
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.rollback()
PPM_HEADER = """\
P6
%(width)s %(height)s
255
"""
def write_ppm(filename, width, height):
img_buf = gl.glReadPixels(0, 0, width, height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
# GL_RGB => 3 components per pixel
img = np.frombuffer(img_buf, np.uint8).reshape(height, width, 3)[::-1]
bin_data = img.tobytes('C')
header = PPM_HEADER % dict(
width = width,
height = height
)
with open(filename, "wb") as f:
f.write(header.encode('ASCII'))
f.write(bin_data)
print("image generated: " + filename)