import zlib from 'node:zlib'; import { RAW_PREVIEW_EDGE } from './config.mjs'; let sharpModule = null; let sharpFailed = false; /** sharp gives us JPEG encoding and resizing; without it we fall back to PNG. */ export async function loadSharp() { if (sharpModule) return sharpModule; if (sharpFailed) return null; try { const mod = await import('sharp'); sharpModule = mod.default; return sharpModule; } catch { sharpFailed = true; return null; } } const CRC_TABLE = (() => { const table = new Int32Array(256); for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; table[n] = c; } return table; })(); function crc32(buf) { let c = 0xffffffff; for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; } function chunk(type, data) { const out = Buffer.alloc(8 + data.length + 4); out.writeUInt32BE(data.length, 0); out.write(type, 4, 'ascii'); data.copy(out, 8); out.writeUInt32BE(crc32(Buffer.concat([Buffer.from(type, 'ascii'), data])), 8 + data.length); return out; } /** Minimal dependency-free 8-bit RGB PNG encoder (used only when sharp is absent). */ export function encodePng(rgb, width, height) { const stride = width * 3; const raw = Buffer.alloc((stride + 1) * height); for (let y = 0; y < height; y++) { raw[y * (stride + 1)] = 0; Buffer.from(rgb.buffer, rgb.byteOffset + y * stride, stride).copy(raw, y * (stride + 1) + 1); } const idat = zlib.deflateSync(raw, { level: 6 }); const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(width, 0); ihdr.writeUInt32BE(height, 4); ihdr[8] = 8; ihdr[9] = 2; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0; return Buffer.concat([ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr), chunk('IDAT', idat), chunk('IEND', Buffer.alloc(0)), ]); } /** Encode an interleaved RGB byte buffer as JPEG (sharp) or PNG (fallback). */ export async function encodeRgb(rgb, width, height, quality = 92, maxEdge = 0) { const sharp = await loadSharp(); if (!sharp) { return { data: encodePng(rgb, width, height), ext: 'png', width, height }; } let img = sharp(Buffer.from(rgb.buffer, rgb.byteOffset, rgb.byteLength), { raw: { width, height, channels: 3 }, }); if (maxEdge > 0 && Math.max(width, height) > maxEdge) img = img.resize({ width: width >= height ? maxEdge : null, height: height > width ? maxEdge : null, fit: 'inside' }); const data = await img.jpeg({ quality, chromaSubsampling: '4:4:4' }).toBuffer(); const meta = await sharp(data).metadata(); return { data, ext: 'jpg', width: Number(meta.width ?? width), height: Number(meta.height ?? height) }; } export async function jpegInfo(buffer) { const sharp = await loadSharp(); if (!sharp) return null; try { const meta = await sharp(buffer).metadata(); return { width: Number(meta.width ?? 0), height: Number(meta.height ?? 0), format: meta.format }; } catch { return null; } } /** Re-encode any decodable image to a normalised JPEG proxy. */ export async function toJpegProxy(buffer, maxEdge = RAW_PREVIEW_EDGE) { const sharp = await loadSharp(); if (!sharp) return null; try { let img = sharp(buffer, { failOn: 'none' }).rotate(); const meta = await img.metadata(); const w = Number(meta.width ?? 0); const h = Number(meta.height ?? 0); if (!w || !h) return null; if (maxEdge > 0 && Math.max(w, h) > maxEdge) img = img.resize({ width: maxEdge, height: maxEdge, fit: 'inside' }); const data = await img.jpeg({ quality: 92, chromaSubsampling: '4:4:4' }).toBuffer(); const outMeta = await sharp(data).metadata(); return { data, width: Number(outMeta.width ?? w), height: Number(outMeta.height ?? h), ext: 'jpg' }; } catch { return null; } } export async function makeThumb(buffer, edge = 512) { const sharp = await loadSharp(); if (!sharp) return null; try { const data = await sharp(buffer, { failOn: 'none' }) .rotate() .resize({ width: edge, height: edge, fit: 'inside', withoutEnlargement: true }) .jpeg({ quality: 82 }) .toBuffer(); return data; } catch { return null; } }