mirror of
https://github.com/Nexoniarz/YAPF.git
synced 2026-11-03 04:54:26 +00:00
Language bindings, KDE thumbnails, yapf compare, in-memory encoding
- yapf_encode() / yapf_free_buffer(): encode to memory; yapf_save uses it - yapf.c also compiles as C++ - `yapf compare`: size and decode speed of the original format vs YAPF, with a lossless check - bindings with tested examples: C++ (header-only), C# (P/Invoke), Rust (crate, builds the C code), Go (cgo), Java (FFM API, Java 22+), Zig (@cImport), LuaJIT (FFI), TypeScript types for the JS port; Python now encodes in memory - docs/LANGUAGES.md and examples/ with measured results - KDE Dolphin thumbnail plugin (KF6, CMake) - README: honest status for Photoshop / VS Code / Figma (not run in the apps yet) and macOS (cross-compiled only), platform table Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2ca726e3af
commit
d7f494a0c9
52 changed files with 1926 additions and 106 deletions
|
|
@ -21,3 +21,5 @@ It uses the C library through `ctypes` when it finds one:
|
|||
Without the library it falls back to a pure-Python decoder that needs
|
||||
`numpy` (decoding only; about 1.4 s for a 4K image). `yapf.backend()`
|
||||
tells you which one is in use.
|
||||
|
||||
Example: `YAPF_LIBRARY=../../build/libyapf.so python3 example.py ../../other/YAPF.YAPF`
|
||||
|
|
|
|||
33
bindings/python/example.py
Normal file
33
bindings/python/example.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
"""
|
||||
example.py — YAPF from Python.
|
||||
|
||||
make # builds build/libyapf.so (or get it from Releases)
|
||||
YAPF_LIBRARY=../../build/libyapf.so python3 example.py ../../other/YAPF.YAPF
|
||||
"""
|
||||
import sys
|
||||
import time
|
||||
|
||||
import yapf
|
||||
|
||||
path = sys.argv[1] if len(sys.argv) > 1 else "../../other/YAPF.YAPF"
|
||||
data = open(path, "rb").read()
|
||||
|
||||
img = yapf.decode(data)
|
||||
ms = float("inf") # best of 10, as in a running program
|
||||
for _ in range(10):
|
||||
t = time.perf_counter()
|
||||
img = yapf.decode(data)
|
||||
ms = min(ms, (time.perf_counter() - t) * 1000)
|
||||
raw = img.width * img.height * img.channels
|
||||
print("%s: %dx%d, %d channels, decoded in %.2f ms (%s backend)"
|
||||
% (path, img.width, img.height, img.channels, ms, yapf.backend()))
|
||||
print("file is %d bytes, %.1f%% of the raw pixels" % (len(data), 100.0 * len(data) / raw))
|
||||
|
||||
if yapf.backend() == "c":
|
||||
again = yapf.encode(img)
|
||||
print("re-encoded:", len(again), "bytes, identical:", again == data)
|
||||
|
||||
w = h = 128 # your own pixels → .yapf
|
||||
rgb = bytes(v for y in range(h) for x in range(w) for v in (x * 2, y * 2, 128))
|
||||
yapf.save("gradient.yapf", yapf.Image(w, h, 3, rgb))
|
||||
print("wrote gradient.yapf")
|
||||
|
|
@ -94,6 +94,9 @@ def _find_library():
|
|||
lib.yapf_load_memory_mt.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.c_int]
|
||||
lib.yapf_free.argtypes = [ctypes.POINTER(_CImage)]
|
||||
lib.yapf_save.argtypes = [ctypes.c_char_p, ctypes.POINTER(_CImage)]
|
||||
lib.yapf_encode.argtypes = [ctypes.POINTER(_CImage), ctypes.POINTER(ctypes.c_void_p),
|
||||
ctypes.POINTER(ctypes.c_size_t)]
|
||||
lib.yapf_free_buffer.argtypes = [ctypes.c_void_p]
|
||||
return lib
|
||||
except (OSError, AttributeError):
|
||||
continue
|
||||
|
|
@ -124,15 +127,20 @@ def _c_decode(data):
|
|||
_lib.yapf_free(p)
|
||||
|
||||
|
||||
def _c_save(path, img):
|
||||
def _c_encode(img):
|
||||
bufs = [ctypes.create_string_buffer(m, len(m)) for m in img.mips]
|
||||
ptrs = (ctypes.POINTER(ctypes.c_uint8) * len(bufs))(
|
||||
*[ctypes.cast(b, ctypes.POINTER(ctypes.c_uint8)) for b in bufs])
|
||||
c = _CImage(img.width, img.height, img.channels, img.gpu_format, img.flags,
|
||||
len(bufs), ptrs[0], ptrs)
|
||||
rc = _lib.yapf_save(os.fsencode(path), ctypes.byref(c))
|
||||
data, size = ctypes.c_void_p(), ctypes.c_size_t()
|
||||
rc = _lib.yapf_encode(ctypes.byref(c), ctypes.byref(data), ctypes.byref(size))
|
||||
if rc != 0:
|
||||
raise YapfError("yapf_save failed with code %d" % rc)
|
||||
raise YapfError("yapf_encode failed with code %d" % rc)
|
||||
try:
|
||||
return ctypes.string_at(data, size.value)
|
||||
finally:
|
||||
_lib.yapf_free_buffer(data)
|
||||
|
||||
|
||||
# ── pure-Python fallback decoder (numpy) ──────────────────────────────
|
||||
|
|
@ -280,9 +288,9 @@ def load(path):
|
|||
return decode(f.read())
|
||||
|
||||
|
||||
def save(path, img):
|
||||
"""Encode an Image (or anything with width/height/channels/pixels) to a
|
||||
.yapf file. Needs the C library."""
|
||||
def encode(img):
|
||||
"""Encode an Image (or anything with width/height/channels/pixels) to
|
||||
YAPF bytes. Needs the C library."""
|
||||
if not _lib:
|
||||
raise YapfError("saving .yapf needs the YAPF native library, which was not found "
|
||||
"for this platform; see https://github.com/Nexoniarz/YAPF#python")
|
||||
|
|
@ -291,20 +299,14 @@ def save(path, img):
|
|||
expected = img.width * img.height * img.channels
|
||||
if len(img.pixels) != expected:
|
||||
raise YapfError("expected %d pixel bytes, got %d" % (expected, len(img.pixels)))
|
||||
_c_save(path, img)
|
||||
return _c_encode(img)
|
||||
|
||||
|
||||
def encode(img):
|
||||
"""Encode an Image to YAPF bytes. Needs the C library."""
|
||||
import tempfile
|
||||
fd, tmp = tempfile.mkstemp(suffix=".yapf")
|
||||
os.close(fd)
|
||||
try:
|
||||
save(tmp, img)
|
||||
with open(tmp, "rb") as f:
|
||||
return f.read()
|
||||
finally:
|
||||
os.remove(tmp)
|
||||
def save(path, img):
|
||||
"""Encode an Image to a .yapf file. Needs the C library."""
|
||||
data = encode(img)
|
||||
with open(path, "wb") as f:
|
||||
f.write(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Reference in a new issue