Files
image-similarity/web/worker.js
T
mark 729071817e
continuous-integration/drone/push Build is passing
phash updates!
2026-07-04 01:10:33 +02:00

38 lines
966 B
JavaScript

import init, { hash_all, resize_preview, dct_coefficients, dct_masks, phash_lowfreq, mutate } from './pkg/image_similarity.js';
const ready = init();
onmessage = async (e) => {
const { id, op, args } = e.data;
await ready;
try {
const bytes = new Uint8Array(args.buffer);
let result;
switch (op) {
case 'pipeline':
result = {
resize8: resize_preview(bytes, 8),
resize32: resize_preview(bytes, 32),
coefficients: dct_coefficients(bytes),
masks: dct_masks(bytes),
lowfreq: phash_lowfreq(bytes),
hashes: hash_all(bytes),
};
break;
case 'hashes':
result = { hashes: hash_all(bytes) };
break;
case 'mutate': {
const png = mutate(bytes, args.kind, args.amount);
result = { png, hashes: hash_all(png) };
break;
}
default:
throw new Error(`unknown op ${op}`);
}
postMessage({ id, ok: true, result });
} catch (err) {
postMessage({ id, ok: false, error: String(err) });
}
};