Why inspect a PNG one row at a time?

A PNG can preserve every pixel byte while storing those bytes in different ways. Before compression, a row filter predicts a byte from its neighbors and stores a reversible difference. Repeated patterns in those differences may compress well. A quick row score helps choose a filter, but the final compressed size is still something to measure.

I kept two small counterexamples in this desk. In the frozen local runs, the white4×3 grid produced a78-byte PNG with the row-score heuristic and a72-byte PNG with None. The vertical ramp produced82 bytes with the heuristic and79 with fixed Sub. Those figures describe the measured runtimes; the same input can produce different compressed bytes in another engine. The complete RGBA input, all six encodings and their losing results stay visible.

Try an original example, choose a strategy and follow a row from its input bytes through the prediction and residual to the reconstructed bytes. Download the full report or an actual PNG and check the decoded RGBA bytes. Fully transparent pixels still have RGB bytes; a preview alone cannot prove that those bytes survived.

This is a browser-local teaching tool for small numeric RGBA grids. It does not accept arbitrary image uploads, train a model, or claim a universally smallest PNG. The technical notes below describe the original frozen candidate and its bounded encoding profile; current hosting and release acceptance are recorded separately.

为什么把一张PNG拆成一行行来看?

PNG可以完整保留每个像素的字节,同时换一种方式存下它们。压缩之前,行过滤器会用相邻字节预测当前字节,存储可逆的差值。这些差值里的重复规律可能更容易压缩;用来挑选过滤器的行分数只是一个判断依据,最终文件多大还得实际测量。

我在工具里保留了两个很小的反例。冻结版本的本地实测中,4×3白色网格使用行分数启发式得到78字节,使用None得到72字节;纵向渐变分别是82字节和固定Sub的79字节。这些数字属于当时的运行环境,换一个引擎,压缩字节可能变化。完整RGBA输入、六种编码和输掉的结果都可以检查。

可以先载入一个原始样例,选择一种策略,沿着一行查看原字节、预测值、差值和重建字节,再下载完整报告或真正的PNG核对。完全透明的像素仍有RGB字节,只看预览图无法证明这些字节被保留下来。

这是处理小型数字RGBA网格的本地教学工具,不接收任意图片上传,没有模型训练,也不保证生成最小PNG。以下技术正文保留原冻结候选的说明和处理范围;托管与发布验收另行记录。

PNG Row Filter Desk

Encode the same small RGBA grid six ways and inspect every byte. This desk exposes PNG's five predictors and a signed-residual row heuristic, then actually compresses each scanline stream and assembles a downloadable PNG. Every strategy reconstructs the exact supplied RGBA bytes, including invisible RGB values at alpha0.

I built it to make a specific distinction visible: a smaller row score does not guarantee a smaller compressed file. On the original4×3 white example, the heuristic scores less but produces78 bytes versus72 for None in the measured runtimes. On the original8×4 vertical ramp, the heuristic produces82 bytes versus79 for fixed Sub. Both examples were fixed before the compression measurements. All six outputs, ties and counterexamples remain available.

Original source, code and fixtures by Renchao Wu. Identity work:png-row-filter-desk:v1, version0.1.0, Apache-2.0. This is a local candidate; hosted availability, independent acceptance and publication are separate states.

Use the desk

Load one of eight original examples or edit {width,height,rgba}. Dimensions are integer1..32; the flat row-major array contains exactly four integer bytes R,G,B,A per pixel. Alpha is unpremultiplied. The desk accepts numeric grid data, not uploaded image files or remote URLs.

The page actually encodes None/Sub/Up/Average/Paeth and the row-score heuristic. Select an encoding to inspect its three chunks/CRCs, all row scores and every predictor/residual/reconstructed byte. Row/channel filters and64-row byte pages affect only the display. Download full JSON for all six complete traces and encoded files, the selected PNG for actual image bytes, or CSV for all rows of the selected encoding. CSV is explicitly a row-summary subset.

The source and PNG previews reflect browser decoding, color handling and alpha compositing. Their appearance is not the byte-equality test. Independent decoding checks the filtered stream and all original RGBA bytes, including RGB hidden by transparency. No gamma/profile metadata is invented.

The fixed encoding profile

This small inspection tool writes PNG signature, IHDR, one IDAT and IEND. IHDR declares RGBA color type6, bit depth8, compression/filter method0, no interlace. The platform's CompressionStream('deflate') supplies zlib-wrapped DEFLATE; this is not a custom DEFLATE implementation. The wire has57 structure bytes plus its actual IDAT payload. Each chunk CRC32 covers type+data, excluding its length field.

For RGBA8 the left neighbor is four bytes earlier in the same channel. Up and upper-left refer to the previous row. Missing neighbors are zero. Average uses floor of a wide (left+up)/2. Paeth chooses the neighbor closest to left+up-upperLeft, with ties left, up, then upper-left. Residuals wrap modulo256. The heuristic scores each of the five candidate rows as the sum of absolute signed residual bytes and chooses the lowest score, with lowest type breaking ties. Its score excludes the type prefix. The complete type-prefixed scanline stream is then compressed.

All six strategies are encodings of one source image, not six new works or model fits. There is no training. This bounded tool does not parse arbitrary PNGs, convert palette/16-bit/interlaced/APNG images, choose color types, provide a general image optimizer or certify complete PNG conformance/security. Actual sizes and compressed hashes can vary between engines and versions; I assert decoded-byte identity, not universal compressed-byte identity or a universal best strategy.

Run locally

Serve the directory with python3 -m http.server 8000 --bind 127.0.0.1 and open the local page in a modern browser with module workers, CompressionStream and WebCrypto. Encoding runs in its own static worker; edit/Clear/new run terminates obsolete work. The page fetches only its own worker/modules/example inventory. Example success/failure does not replace the current input or results. No image upload, external inference, account, tracking or persistent storage is used.

The Node CLI uses the same mathematical core:

node cli.mjs request.json NEW_REPORT.json

It requires a regular final nonsymlink input of at most20,000 bytes and creates a new exclusive mode0600 report. It does not promise hostile-filesystem race protection or atomic transactions. See STATIC-API.md.

Run python3 run_checks.py in a complete writable copy for the original expected fixtures, new full reports, independent PNG/CRC/zlib reconstruction, API/CLI and saved native evidence checks. It refuses a frozen directory containing MANIFEST.json. Python reference checks need only the standard library; Node24.19.0 and Python3.14.7 were used. Saved native replay is not a new GUI execution. See VALIDATION.md, CONTRACT.md and SOURCES.md.