# API and CLI

Version0.1.0, identity `work:png-row-filter-desk:v1`. `core.mjs` is an original dependency-free ES module using standard CompressionStream, Blob/Response and WebCrypto APIs. It encodes a small original numeric RGBA grid; it never parses an uploaded PNG or trains a model.

```javascript
import {parseRequest, encodePNG, rowsCSV, fromHex} from './core.mjs';
const req = parseRequest('{"width":1,"height":1,"rgba":[17,201,129,0]}');
const report = await encodePNG(req);
const pngBytes = fromHex(report.strategies[5].png_hex);
const rowCSV = rowsCSV(report, 'heuristic');
```

`parseRequest(text)` requires JSON text at most20,000 UTF-16 units. Ordinary JSON duplicate-name last-value semantics apply; it is not a duplicate-key validator. `validateRequest(object)` accepts exactly own keys width/height/rgba on a plain object. Width and height must be numeric integers1..32. `rgba` must be an ordinary dense Array of exactly4wh numeric integers0..255. Sparse arrays, typed arrays, null/boolean/string/nonfinite/fractional/out-of-range values and extra fields are rejected. The validated snapshot is copied and deeply frozen before any asynchronous hashing/compression. Source is unpremultiplied RGBA8, row-major, without color-space inference.

`encodePNG(request,{signal}={})` returns a deeply frozen complete report. Cancellation via optional AbortSignal is cooperative before/after platform compression and between strategies; it does not promise interrupting a platform operation mid-instruction. A missing CompressionStream implementation or a platform error rejects the whole operation. No partial successful report is returned. The browser runs this operation in its own module worker and terminates that worker on a newer intent; a generation guard also prevents obsolete completion/error from rendering.

Top-level report contains schema `png-row-filter-report-v1`, identity/version, full request, dimensions, source byte count/hex/SHA-256, explicit profile/numerical limits, six complete strategies and comparison. Strategy order is none,sub,up,average,paeth,heuristic. Every strategy includes:

- Every row: ordinal, chosen type/name, all five scores, chosen score, all byte traces and complete filtered/reconstructed row hex. A byte trace has row_byte/column/channel/raw/left/up/upper_left/predictor/filtered/signed/reconstructed. Reconstruction uses already decoded neighbors.
- Full row-type-prefixed scanline hex/SHA/length, reconstructed RGBA hex/SHA/byte-equality flag; actual compressed IDAT hex/SHA/length; complete PNG hex/SHA/length; all three chunks' offsets, type, data length and CRC fields.
- Total selected row score and PNG byte difference versus fixed None. The difference is an actual complete-file size measurement, not the score difference or a new compression run.

Comparison reports smallest actual PNG byte count, all tied best strategy IDs in fixed order, heuristic gap versus best, its tied-best boolean and all-roundtrip equality. It preserves every strategy, losing heuristic cases and ties. Six platform zlib encodings occur during one complete call. Exporting the same report again or showing a second file view does not perform six more encodings. A PNG has exactly signature+IHDR+one IDAT+IEND, RGBA8/no interlace/no ancillary chunks. Chunks and original alpha bytes remain available for independent decoding. No compressed byte identity across engines or exact visual/color-management identity is promised.

`rowsCSV(report,strategy)` accepts a supported strategy and emits header plus every selected strategy row, with all five row scores/chosen score/filter name/type/filtered/reconstructed hex. All fields are quoted; records have CRLF terminators. CSV is not the full byte report. `fromHex` accepts lowercase even hexadecimal only. `paeth`, `crc32`, `deepFreeze`, DEFAULT and STRATEGIES are also exported for inspection/controls. Low-level predictor and CRC functions do not validate arbitrary external objects; use the typed report path.

## CLI

`node cli.mjs INPUT NEW_OUTPUT` reads a regular final nonsymlink file at most20,000 actual bytes, then applies the same text/type bounds. Output uses exclusive `wx`, mode0600, refusing existing files, final output symlinks and dangling symlinks. JSON includes complete PNGs as lowercase hex; the command does not separately overwrite PNG files. Error prints a short message and exits nonzero. Parent symlink bans, hostile-path race protection, atomicity, signatures and fsync are not claimed.
