/** Keeps every segment through the SOS header and drops the scan data. */ export function jpegWithoutScanData(jpeg: Buffer): Buffer { let offset = 2; for (;;) { const marker = jpeg[offset + 1]; offset += 2 + jpeg.readUInt16BE(offset + 2); if (marker === 0xda) break; } return Buffer.concat([jpeg.subarray(0, offset), Buffer.from([0xff, 0xd9])]); } /** Inserts `count` APP13 segments of ~64 KiB right after SOI (Photoshop-style metadata). */ export function jpegWithMetadata(jpeg: Buffer, count: number): Buffer { const payload = Buffer.alloc(65_533, 0x20); const segment = Buffer.concat([ Buffer.from([0xff, 0xed, 0xff, 0xff]), payload, ]); return Buffer.concat([ jpeg.subarray(0, 2), ...Array.from({ length: count }, () => segment), jpeg.subarray(2), ]); }