|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { stripSdrPngColourTags } from "./pngColourTags"; |
| 3 | + |
| 4 | +const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); |
| 5 | + |
| 6 | +function chunk(type: string, data: Buffer): Buffer { |
| 7 | + const length = Buffer.alloc(4); |
| 8 | + length.writeUInt32BE(data.length); |
| 9 | + // The helper never reads a CRC, so a filler is enough to tell the chunks apart. |
| 10 | + return Buffer.concat([length, Buffer.from(type, "latin1"), data, Buffer.alloc(4, 0xcc)]); |
| 11 | +} |
| 12 | + |
| 13 | +const IDAT = chunk("IDAT", Buffer.from([1, 2, 3])); |
| 14 | +const png = (...before: Buffer[]) => |
| 15 | + Buffer.concat([ |
| 16 | + SIGNATURE, |
| 17 | + chunk("IHDR", Buffer.alloc(13)), |
| 18 | + ...before, |
| 19 | + IDAT, |
| 20 | + chunk("IEND", Buffer.alloc(0)), |
| 21 | + ]); |
| 22 | +const cicp = (transfer: number) => chunk("cICP", Buffer.from([1, transfer, 0, 1])); |
| 23 | + |
| 24 | +describe("stripSdrPngColourTags", () => { |
| 25 | + it.each([ |
| 26 | + ["a BT.709 cICP", cicp(1)], |
| 27 | + ["an sRGB cICP", cicp(13)], |
| 28 | + ["a gAMA", chunk("gAMA", Buffer.alloc(4))], |
| 29 | + ["a cHRM", chunk("cHRM", Buffer.alloc(32))], |
| 30 | + ])("drops %s and nothing else", (_name, tag) => { |
| 31 | + expect(stripSdrPngColourTags(png(tag))?.equals(png())).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it("keeps the chunks that already match the file's values", () => { |
| 35 | + const kept = [chunk("sRGB", Buffer.from([0])), chunk("iCCP", Buffer.alloc(8))]; |
| 36 | + expect(stripSdrPngColourTags(png(...kept, cicp(1)))?.equals(png(...kept))).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it.each([ |
| 40 | + ["has no colour tag", png()], |
| 41 | + ["carries a PQ cICP", png(cicp(16), chunk("gAMA", Buffer.alloc(4)))], |
| 42 | + ["carries an HLG cICP", png(cicp(18))], |
| 43 | + ["is cut short", png(cicp(1)).subarray(0, 40)], |
| 44 | + ])("leaves a PNG that %s alone", (_name, bytes) => { |
| 45 | + expect(stripSdrPngColourTags(bytes)).toBeNull(); |
| 46 | + }); |
| 47 | +}); |
0 commit comments