-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_syntax.py
More file actions
54 lines (42 loc) · 1.82 KB
/
binary_syntax.py
File metadata and controls
54 lines (42 loc) · 1.82 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
# MIT License
# Copyright (c) 2020 Maurice Quach
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# Modified: Rodrigo Borba Pinheiro
# Copyright (c) 2010-2024, InterDigital
# All rights reserved.
# See LICENSE under the root folder.
import numpy as np
def to_bytes(x, dtype):
iinfo = np.iinfo(dtype)
x = np.array(x, dtype=dtype)
assert np.all(x <= iinfo.max), f'Overflow {x} {iinfo}'
assert np.all(iinfo.min <= x), f'Underflow {x} {iinfo}'
return x.tobytes()
def scalar_to_bytes(x, dtype):
return to_bytes([x], dtype)
def read_from_buffer(f, n, dtype):
return np.frombuffer(f.read(int(np.dtype(dtype).itemsize * n)), dtype=dtype)
def save_compressed_file(strings):
"""Saves the attributes of a point cloud and the hyperprior an unified bitstream"""
ret = b''
for string in strings:
n_bytes_b = scalar_to_bytes(len(string[0]), np.uint32)
ret += n_bytes_b + string[0]
return ret
def load_compressed_file(f):
"""Loads the unified bitstream from an encoded point cloud"""
strings = []
for _ in range(2):
n_bytes = read_from_buffer(f, 1, np.uint32)[0]
string = f.read(int(n_bytes))
strings.append([string])
file_end = f.read()
assert file_end == b'', f'File not read completely file_end {file_end}'
return strings