| 14 | AUTO = "auto" |
| 15 | |
| 16 | class Encoder: |
| 17 | |
| 18 | def __init__(self, backend=EncoderBackend.AUTO): |
| 19 | self.backend = backend |
| 20 | self._native = None |
| 21 | self._wasm = None |
| 22 | self.backend_name = None |
| 23 | |
| 24 | # ------------------------------------------------------------------ |
| 25 | # Try native first (AUTO or NATIVE modes) |
| 26 | # ------------------------------------------------------------------ |
| 27 | if backend in (EncoderBackend.AUTO, EncoderBackend.NATIVE): |
| 28 | try: |
| 29 | import basisu_py.basisu_python as native_encoder |
| 30 | native_encoder.init() |
| 31 | |
| 32 | self._native = native_encoder |
| 33 | self._wasm = None |
| 34 | self.backend_name = "NATIVE" |
| 35 | |
| 36 | print("[Encoder] Using native backend") |
| 37 | return |
| 38 | |
| 39 | except Exception as e: |
| 40 | if backend == EncoderBackend.NATIVE: |
| 41 | raise RuntimeError( |
| 42 | f"[Encoder] Native backend requested but unavailable: {e}" |
| 43 | ) |
| 44 | print("[Encoder] Native unavailable; falling back to WASM:", e) |
| 45 | |
| 46 | # ------------------------------------------------------------------ |
| 47 | # Fallback to WASM (AUTO or explicitly WASM) |
| 48 | # ------------------------------------------------------------------ |
| 49 | try: |
| 50 | from basisu_py.wasm.wasm_encoder import BasisuWasmEncoder |
| 51 | except Exception as e: |
| 52 | raise RuntimeError( |
| 53 | f"[Encoder] WASM backend cannot be imported: {e}\n" |
| 54 | "Make sure wasmtime is installed and basisu_py/wasm/*.wasm exist." |
| 55 | ) |
| 56 | |
| 57 | wasm_path = Path(__file__).parent / "wasm" / "basisu_module_st.wasm" |
| 58 | self._wasm = BasisuWasmEncoder(str(wasm_path)) |
| 59 | self._wasm.load() |
| 60 | self._native = None |
| 61 | self.backend_name = "WASM" |
| 62 | |
| 63 | print("[Encoder] Using WASM backend") |
| 64 | |
| 65 | |
| 66 | # ------------------------------------------------------ |
| 67 | # Public API |
| 68 | # ------------------------------------------------------ |
| 69 | def compress(self, |
| 70 | image, |
| 71 | format=-1, |
| 72 | quality=BasisQuality.MAX, |
| 73 | effort=BasisEffort.DEFAULT, |
no outgoing calls