Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/byte-codec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ To run a single test file, pass its path to vitest directly, e.g. `pnpm exec vit
| `bytes/flate` | `deflate`, `inflate`, `inflateTolerant` (fflate-backed DEFLATE compression/decompression with a safety cap) |
| `image/png-encode` | `encodePng` (raw RGB/RGBA pixels → PNG bytes; a truecolour source image whose pixels reduce to 256 or fewer distinct colours is encoded both as indexed colour, PNG colour type 3 with a PLTE and, where needed, a tRNS chunk, and as plain truecolour, and whichever comes out smaller is returned — indexed colour wins for the large flat-colour images typical of diagrams and screenshots, but the PLTE/tRNS chunk overhead can make it larger for small images, so the encoder measures rather than assumes, at the cost of a full second encode for every eligible image; throws if either `width` or `height` is not a positive integer no greater than the PNG spec's own IHDR limit, or if their product exceeds a practical per-call pixel-count ceiling, since PNG's IHDR chunk has no valid encoding for zero, negative, fractional, or non-finite dimensions and this bounds an otherwise-unbounded encode to a fixed worst case) |
| `image/png-decode` | `decodePng` (PNG bytes → raw pixels, handling every PNG colour type including indexed/palette via its own PLTE/tRNS lookup), `RawImage` |
| `image/png-filter` | `filterScanlines`, `unfilterScanlines` (the five PNG scanline filters) |
| `image/png-filter` | `filterScanlines`, `unfilterScanlines` (the five PNG scanline filters), `filterScanlineRows` (`filterScanlines` reading each row from a callback, so a caller whose pixels are not laid out as PNG rows writes each row as it is filtered rather than building an interleaved copy of the whole image first; `encodePng` filters its colour and alpha planes this way) |
| `image/jpeg-info` | `readJpegInfo` (JPEG header reader: dimensions, components, progressive flag — no sample decoding) |
| `text/decode` | `decodeText`, `tryDecodeText`, `detectByteOrderMark`, `isProbablyText`, `UndecodableTextError`, `TEXT_DECODE_CHUNK_CODE_UNITS` (bytes → text with the character encoding worked out from the bytes; see [Text decoding](#text-decoding) below) |

Expand Down
65 changes: 65 additions & 0 deletions packages/byte-codec/src/image/png-encode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,68 @@ describe("encodePng and decodePng known answers over seeded images", () => {
},
);
});

// The edge cases the row-at-a-time truecolour encoder and the two-pass palette search must reproduce byte for byte: a raster and alpha plane shorter than the image (each missing sample reads as 0), a short grey raster, and a palette with alpha. Each SHA-256 was recorded from the encoder as it was before either change, which built one interleaved copy of the image and stored palette indices as it searched.
describe("encodePng reproduces its earlier bytes for short planes and a palette with alpha", () => {
const pixels = GOLDEN_WIDTH * GOLDEN_HEIGHT;
const fewColours = seededBytes(pixels, 7).map((value) => (value % 4) * 60);
const images: Record<string, RawImage> = {
short3a: {
width: GOLDEN_WIDTH,
height: GOLDEN_HEIGHT,
channels: 3,
data: seededBytes(pixels * 3 - 100, 8),
alpha: seededBytes(pixels - 50, 9),
},
short1: {
width: GOLDEN_WIDTH,
height: GOLDEN_HEIGHT,
channels: 1,
data: seededBytes(pixels - 77, 10),
},
palettedAlpha: {
width: GOLDEN_WIDTH,
height: GOLDEN_HEIGHT,
channels: 3,
data: Uint8Array.from(
{ length: pixels * 3 },
(_unused, index) => fewColours[Math.floor(index / 3)]!,
),
alpha: fewColours.map((value) => (value === 60 ? 128 : 255)),
},
};
it.each([
[
"short3a",
"adaptive",
"0ca94aa2c9200e70112786d538ea7160bd26a3822a33165adeea7bbc6d6bfffd",
],
[
"short3a",
"none",
"bcb89064f49ed3f0de54cefc3ff28c35a38f748c579f9d6a206019164e5828e9",
],
[
"short1",
"adaptive",
"e8683d85b61ad2a5facf843805d03eb1f8c12f8c77a9d78f58c6315954e819d9",
],
[
"short1",
"none",
"af560fde86b7a1ddc4bd793bd91d9b4def6023f62d3db67463f14add0ee8d261",
],
[
"palettedAlpha",
"adaptive",
"e611d66702ba9ed26b6cb2a28ef631c8b4f1327539bc1dd56f0918dfef3453b8",
],
[
"palettedAlpha",
"none",
"a4218aafe1d8f832f55ec19c340664e464bdbcab3fa66dcd6fc22db4fc805603",
],
] as const)("%s under the %s filter", (name, filter, png) => {
expect(sha256Hex(encodePng(images[name]!, { filter }))).toBe(png);
});
});
Loading
Loading