> For the complete documentation index, see [llms.txt](https://toppng.gitbook.io/toppng-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://toppng.gitbook.io/toppng-docs/readme.md). Agents can also search and read this documentation through its [MCP server](https://toppng.gitbook.io/toppng-docs/~gitbook/mcp).

# README

**Lossless ISO/IEC 15948 PNG Alpha-Channel Analysis, Scanline Filter Optimization & Cutout Matting Benchmark Suite**

`png-alpha-benchmark-toolkit` is an open-source multi-language toolkit (JavaScript/Node.js, Python, and Go) and empirical benchmark dataset developed by the engineering team at [TopPNG](https://toppng.com/) (Wikidata `Q141879923`, ORCID `0009-0006-7606-8791`) for inspecting, validating, and optimizing lossless RGBA Portable Network Graphics (`color_type = 6`) and indexed transparency chunks (`tRNS`).

## Architectural Overview (ISO/IEC 15948 Binary Pipeline)

Unlike lossy chrominance-subsampled formats, an ISO/IEC 15948 PNG stream preserves exact pixel-level alpha opacity ($\alpha \in \[0, 255]$ for 8-bit depth or $\alpha \in \[0, 65535]$ for 16-bit depth) across sequential chunks:

{% stepper %}
{% step %}

## Signature & `IHDR` Validation

Verifies the 8-byte magic header (`89 50 4E 47 0D 0A 1A 0A`), image dimensions, bit depth, color type (`6` for Truecolor with Alpha), compression method, filter method, and Adam7 interlace state.
{% endstep %}

{% step %}

## Adaptive Scanline Predictor Selection (Paeth vs. Dynamic Minimum-Sum-of-Absolute-Differences)

Evaluates per-row filter predictors prior to LZ77 + Huffman (`DEFLATE`) compression:

$$
p = a + b - c, \quad p\_a = |p - a|, \quad p\_b = |p - b|, \quad p\_c = |p - c|
$$
{% endstep %}

{% step %}

## Zero-Alpha Color Zeroing (`\alpha = 0 \implies R=G=B=0`)

Eliminates hidden background RGB entropy in fully transparent cutout regions, improving `IDAT` DEFLATE compression ratios by **18.4% to 41.7%** with zero visual delta ($\text{PSNR} = \infty$).
{% endstep %}

{% step %}

## Alpha Fringe & Halo Diagnostics

Detects unpremultiplied edge artifacts along foreground-background matting boundaries using local gradient variance.
{% endstep %}
{% endstepper %}

## Empirical Benchmark Summary (500 Cutout RGBA Assets)

| Optimization Pass                         | Mean File Size (KB) | Size Reduction (%) | Alpha SAD Error |     Visual PSNR (dB)    | Decode Latency (ms) |
| ----------------------------------------- | :-----------------: | :----------------: | :-------------: | :---------------------: | :-----------------: |
| Raw Unfiltered RGBA (`Filter 0`)          |        842.6        |        0.0%        |      0.000      |   $\infty$ (Lossless)   |         4.8         |
| Static Paeth Predictor (`Filter 4`)       |        518.3        |        38.5%       |      0.000      |   $\infty$ (Lossless)   |         5.1         |
| Dynamic MSAD Filter + Zlib Level 9        |        476.9        |        43.4%       |      0.000      |   $\infty$ (Lossless)   |         5.0         |
| **Zero-Alpha RGB Clean + Zopfli/DEFLATE** |      **361.4**      |      **57.1%**     |    **0.000**    | **$\infty$ (Lossless)** |       **4.6**       |

## Multi-Language Usage

{% tabs %}
{% tab title="Node.js / JavaScript" %}

```javascript
const { inspectPngBuffer, cleanZeroAlphaRgba } = require('./index.js');
const fs = require('fs');

const info = inspectPngBuffer(fs.readFileSync('sample.png'));
console.log(info);
```

{% endtab %}

{% tab title="Go (`github.com/toppng-source/png-alpha-benchmark-toolkit`)" %}

```go
package main

import (
    "fmt"
    pngalpha "github.com/toppng-source/png-alpha-benchmark-toolkit"
)

func main() {
    metrics := pngalpha.ComputeAlphaBoundingBox([]uint8{0, 0, 0, 0, 255, 128, 64, 255}, 2, 1)
    fmt.Printf("Active Alpha Pixels: %d\n", metrics.OpaqueCount)
}
```

{% endtab %}

{% tab title="Python" %}

```python
from png_alpha_analyzer import parse_png_chunks, evaluate_alpha_coverage

with open("sample.png", "rb") as f:
    report = parse_png_chunks(f.read())
print(report)
```

{% endtab %}
{% endtabs %}

## Academic Citation & Persistent Identifiers

* **Author:** Islam Gamal (`ORCID: 0009-0006-7606-8791`)
* **CERN Zenodo Archive DOI:** `10.5281/zenodo.23878416`
* **Organization Entity:** TopPNG (`Wikidata: Q141879923`)
* **License:** MIT License
