Something went wrong. Try again.
A live media wall for your Bluesky follows
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import { rgbaToThumbHash, thumbHashToDataURL } from 'thumbhash';import { extractMediaPalette, type MediaPixels } from './media-pixels.js';
self.onmessage = async ( event: MessageEvent<{ id: number; pixels?: MediaPixels; blob?: Blob; width?: number; height?: number; }>) => { const { id, blob } = event.data; let pixels = event.data.pixels; let bitmap: ImageBitmap | undefined; try { if (!pixels && blob) { const ratio = (event.data.width || 1) / (event.data.height || 1); const width = Math.max(1, Math.round(100 * Math.min(1, ratio))); const height = Math.max(1, Math.round(100 / Math.max(1, ratio))); bitmap = await createImageBitmap(blob, { resizeWidth: width, resizeHeight: height, resizeQuality: 'low' }); const canvas = new OffscreenCanvas(width, height); const context = canvas.getContext('2d', { willReadFrequently: true }); if (!context) throw new Error('Canvas unavailable'); context.drawImage(bitmap, 0, 0, width, height); pixels = { width, height, rgba: context.getImageData(0, 0, width, height).data }; } if (!pixels) throw new Error('Missing pixels'); if ( !Number.isInteger(pixels.width) || !Number.isInteger(pixels.height) || pixels.width < 1 || pixels.height < 1 || pixels.width > 100 || pixels.height > 100 || pixels.rgba.length !== pixels.width * pixels.height * 4 ) throw new Error('Invalid pixels'); const hash = rgbaToThumbHash(pixels.width, pixels.height, pixels.rgba); self.postMessage({ id, hash, placeholder: thumbHashToDataURL(hash), palette: extractMediaPalette(pixels.rgba, pixels.width, pixels.height) }); } catch { self.postMessage({ id, error: true }); } finally { bitmap?.close(); }};