File size: 4,379 Bytes
a39b890 | 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | /**
* @aegismark/wasm-decoder v1.0.0
* Native WebAssembly Blind Watermark Decoder for Arbitrary Local Screen Crops
*
* Features:
* - Zero external dependencies; embeds compiled 14 KB WebAssembly binary (decoder_core.c)
* - Recovers arbitrary UTF-8 watermarks from ANY local screenshot crop at ANY (x0, y0) offset
* - 2D Toroidal Pilot Cross-Correlation over Z_64 x Z_64 + sub-cell phase search
* - Soft-Decision LLR Spread-Spectrum Despreading + GF(256) RS(32,20) Algebraic Berlekamp-Massey
* + Chien + Forney + OSD-2 Soft Bit Flipping + Keyed CRC-16 Verification
*/
const DEFAULT_SECRET_KEY = "aegismark-overlay-2026";
export class AegisMarkWasmDecoder {
/**
* @param {WebAssembly.Instance} instance
*/
constructor(instance) {
this._instance = instance;
this._exports = instance.exports;
this._memory = this._exports.memory;
}
/**
* Instantiate the native WebAssembly blind watermark decoder.
* @param {{ wasmBinary?: Uint8Array | ArrayBuffer, wasmUrl?: string }} [options]
* @returns {Promise<AegisMarkWasmDecoder>}
*/
static async create(options = {}) {
let wasmBytes = options.wasmBinary;
if (!wasmBytes) {
if (typeof process !== "undefined" && process.versions && process.versions.node) {
const fs = await import("node:fs");
const path = await import("node:path");
const url = await import("node:url");
const dir = path.dirname(url.fileURLToPath(import.meta.url));
wasmBytes = fs.readFileSync(path.join(dir, "aegismark_decoder.wasm"));
} else {
const wasmUrl = options.wasmUrl || new URL("./aegismark_decoder.wasm", import.meta.url).href;
const resp = await fetch(wasmUrl);
wasmBytes = await resp.arrayBuffer();
}
}
const { instance } = await WebAssembly.instantiate(wasmBytes, {});
return new AegisMarkWasmDecoder(instance);
}
/**
* Blindly decode an arbitrary local screenshot crop (RGBA buffer) without knowing crop (x0, y0).
*
* @param {Uint8Array | Uint8ClampedArray} rgbaData - Raw RGBA pixel buffer (width * height * 4 bytes)
* @param {number} width - Crop width in pixels
* @param {number} height - Crop height in pixels
* @param {{ secretKey?: string, chipSize?: number }} [options]
*/
decodeRGBA(rgbaData, width, height, options = {}) {
const t0 = typeof performance !== "undefined" ? performance.now() : Date.now();
const secretKey = options.secretKey || DEFAULT_SECRET_KEY;
const preferredChipSize = options.chipSize || 2;
const ex = this._exports;
const memU8 = new Uint8Array(this._memory.buffer);
const rgbaPtr = ex.wasm_get_rgba_buffer();
const keyPtr = ex.wasm_get_key_buffer();
const payloadPtr = ex.wasm_get_payload_buffer();
const byteLen = width * height * 4;
if (rgbaData.length < byteLen) {
throw new Error(`Invalid RGBA buffer length: expected ${byteLen}, got ${rgbaData.length}`);
}
memU8.set(rgbaData.subarray(0, byteLen), rgbaPtr);
const keyBytes = new TextEncoder().encode(String(secretKey));
const keyLen = Math.min(keyBytes.length, 255);
memU8.set(keyBytes.subarray(0, keyLen), keyPtr);
const ok = ex.wasm_decode_rgba(width | 0, height | 0, keyLen | 0, preferredChipSize | 0);
const verified = ok === 1 && ex.wasm_get_verified() === 1;
const payloadLen = verified ? ex.wasm_get_payload_length() : 0;
const payloadBytes = verified
? memU8.slice(payloadPtr, payloadPtr + payloadLen)
: new Uint8Array(0);
let text = "";
if (verified && payloadLen > 0) {
text = new TextDecoder("utf-8", { fatal: false }).decode(payloadBytes);
}
const elapsedMs = (typeof performance !== "undefined" ? performance.now() : Date.now()) - t0;
return {
verified,
text,
payloadBytes,
syncOffset: {
dy: ex.wasm_get_sync_dy(),
dx: ex.wasm_get_sync_dx(),
subY: ex.wasm_get_sub_y(),
subX: ex.wasm_get_sub_x(),
},
chipSize: ex.wasm_get_chip_size(),
rsErrorsCorrected: ex.wasm_get_rs_errors(),
bitAgreement: Number(ex.wasm_get_bit_agreement().toFixed(4)),
confidence: Number(ex.wasm_get_confidence().toFixed(4)),
pilotZScore: Number(ex.wasm_get_pilot_zscore().toFixed(2)),
elapsedMs: Number(elapsedMs.toFixed(2)),
};
}
}
export default AegisMarkWasmDecoder;
|