-
Notifications
You must be signed in to change notification settings - Fork 98
Add sz using ctypes #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add sz using ctypes #422
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bb9a2da
Add sz using ctypes
martindurant 64ec7f6
Update numcodecs/sz.py
martindurant b71611b
Update numcodecs/sz.py
martindurant 834b28f
one type
martindurant 24ae913
Update numcodecs/sz.py
martindurant 868f907
Update numcodecs/sz.py
martindurant 7d1bbbc
Update numcodecs/sz.py
martindurant 0e09b41
Add docs and tests
martindurant a06fcbf
Skip without libaec
martindurant 12d40d0
lint and add ilbaec to linux ci
martindurant 0dcb23d
better skip
martindurant 00d301b
typo
martindurant 4aa7af0
skip reason
martindurant 90fea47
coverage
martindurant 3220437
Apply suggestions from code review
joshmoore 2b5f092
Mark makes flake happy
joshmoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import ctypes | ||
from numcodecs.abc import Codec | ||
|
||
|
||
class Params(ctypes.Structure): | ||
joshmoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_fields_ = [ | ||
("options_mask", ctypes.c_int), | ||
("bits_per_pixel", ctypes.c_int), | ||
("pixels_per_block", ctypes.c_int), | ||
("pixels_per_scanline", ctypes.c_int) | ||
] | ||
|
||
|
||
libsz = False | ||
for ext in [".so", ".dylib", ".dll"]: | ||
try: | ||
libsz = ctypes.cdll.LoadLibrary("libsz" + ext) | ||
except OSError: | ||
pass | ||
|
||
if libsz: | ||
BufftoBuffCompress = libsz.SZ_BufftoBuffCompress | ||
BufftoBuffCompress.restype = ctypes.c_int | ||
BufftoBuffCompress.argtypes = [ | ||
ctypes.c_void_p, ctypes.POINTER(ctypes.c_size_t), ctypes.c_void_p, ctypes.c_size_t, | ||
ctypes.POINTER(Params) | ||
] | ||
|
||
BufftoBuffDecompress = libsz.SZ_BufftoBuffDecompress | ||
BufftoBuffCompress = libsz.SZ_BufftoBuffCompress | ||
BufftoBuffCompress.restype = ctypes.c_int | ||
BufftoBuffCompress.argtypes = [ | ||
ctypes.c_void_p, ctypes.POINTER(ctypes.c_size_t), ctypes.c_void_p, ctypes.c_size_t, | ||
ctypes.POINTER(Params) | ||
] | ||
martindurant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def check_sz(): | ||
if not libsz: | ||
raise ImportError("libsz could not be loaded") | ||
|
||
|
||
class HdfSzipCodec(Codec): | ||
joshmoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
codec_id = "hdf_szip" | ||
joshmoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __init__(self, mask, bits_per_pixel, pix_per_block, pix_per_scanline): | ||
joshmoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.mask = mask | ||
self.pix_per_block = pix_per_block | ||
self.bits_per_pixel = bits_per_pixel | ||
self.pix_per_scanline = pix_per_scanline | ||
joshmoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def decode(self, buf, out=None): | ||
check_sz() | ||
buf = memoryview(buf) | ||
param = Params(self.mask, self.bits_per_pixel, self.pix_per_block, self.pix_per_scanline) | ||
lout = int.from_bytes(buf[:4], "little") | ||
dest_len = ctypes.c_int(lout) | ||
p_dest_len = ctypes.pointer(dest_len) | ||
if out is None: | ||
out = ctypes.create_string_buffer(lout) | ||
buf2 = ctypes.c_void_p.from_buffer_copy(buf, 4) # shouldn't need copy, but wants writable | ||
martindurant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ok = BufftoBuffDecompress( | ||
out, p_dest_len, buf2, buf.nbytes - 4, param | ||
) | ||
assert dest_len == lout # that we got the expected number of bytes | ||
martindurant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert ok == 0 | ||
return out | ||
|
||
def encode(self, buf): | ||
check_sz() | ||
raise NotImplementedError | ||
martindurant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def testme(): | ||
sample_buffer = b'\x00\x02\x00\x00\x15UUUUUUUQUUUUUUUU\x15UUUUUUUQUUUUUUUU\x15UUUUUUUQUUUUUUUU\x15UUUUUUUQUUUUUUUU' | ||
# pl = h5obj.id.get_create_plist().get_filter(0) | ||
# mask, pix_per_block, bits_per_pixel, pix_per_scanline = pl[2] | ||
# (141, 32, 16, 256) | ||
# lout = 512 | ||
|
||
codec = HdfSzipCodec(mask=141, pix_per_block=32, bits_per_pixel=16, pix_per_scanline=256) | ||
codec.decode(sample_buffer) # Bus Error |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.