dds_png: compress scanlines without staging the entire image #2249

Merged
leonida/mandy
kirigayakazima merged 1 commit from codex/dds-png-bounded-scanlines into main 2026-11-28 19:31:39 +00:00

Pull request successfully merged and closed

kirigayakazima commented 2026-11-26 10:23:59 +00:00

Claimed in the coordination issue as: DDS-PNG-BOUNDED-SCANLINES

What and why

Feed PNG filter bytes and RGBA rows to one zlib compressor incrementally. A 1024×1024 zero-filled synthetic texture on Windows reduced peak allocations measured by tracemalloc after source allocation from 4,579,376 to 384,418 bytes. Existing tests also decode and compare the image data.

Tested

Windows, Python 3.14.7. The regression failed on the parent commit and passed with this change.

  • python -I tests/test_dds_png.py
  • python -I tests/test_dds_subresources.py
  • python -I tests/test_dds_png_dx10.py
  • git diff --check and the changed-path no-game-data scan passed.
  • Shared main baseline: 498 semantic test files, 6,351 tests passed.

GPUs tested: N/A (synthetic CPU tests).

AI-assisted: Yes.

Checklist

  • Based on current main and merges cleanly with the tested base.
  • One topic.
  • No game data, keys or decryption.
  • Translator contract table: N/A.
Claimed in the coordination issue as: [DDS-PNG-BOUNDED-SCANLINES](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7189018014) ### What and why Feed PNG filter bytes and RGBA rows to one zlib compressor incrementally. A 1024×1024 zero-filled synthetic texture on Windows reduced peak allocations measured by tracemalloc after source allocation from 4,579,376 to 384,418 bytes. Existing tests also decode and compare the image data. ### Tested Windows, Python 3.14.7. The regression failed on the parent commit and passed with this change. - `python -I tests/test_dds_png.py` - `python -I tests/test_dds_subresources.py` - `python -I tests/test_dds_png_dx10.py` - `git diff --check` and the changed-path no-game-data scan passed. - Shared main baseline: 498 semantic test files, 6,351 tests passed. GPUs tested: N/A (synthetic CPU tests). AI-assisted: Yes. ### Checklist - [x] Based on current main and merges cleanly with the tested base. - [x] One topic. - [x] No game data, keys or decryption. - Translator contract table: N/A.
DeadlyChirp commented 2026-11-26 10:42:32 +00:00

Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

  • tests/test_dds_png.py: 101 passed, 143 subtests passed (main: 100 passed).

(AI-assisted test run; output checked by hand.)

Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. - `tests/test_dds_png.py`: 101 passed, 143 subtests passed (main: 100 passed). (AI-assisted test run; output checked by hand.)
lixonik reviewed 2026-11-26 10:58:29 +00:00
lixonik left a comment
No description provided.
Windows 11: 101/101 DDS/PNG tests passed. I compared synthetic 2048×2048 zeros, 1024×1024 seeded random RGBA, an odd rectangle, and a narrow image against the base. Each PNG was byte-identical, and inflating IDAT recovered the input RGBA. Tracemalloc peak after allocating the source fell from 16.29 to 0.30 MiB for the zero image. The random case went from 16.01 to 16.25 MiB, consistent with keeping the entire compressed IDAT in memory. No correctness findings from these checks. AI-assisted.
wavehs reviewed 2026-11-26 19:50:27 +00:00
wavehs left a comment
No description provided.
**macOS arm64 check of #2249** (dds_png scanline streaming). A small Python encoder change; runs here with the standard library only. Tested head `a9ec081f1` merged onto `main` `bdbc7e923` as merge `7ed2ab4f4` (clean, no conflicts). Environment: Apple M3 Pro arm64, macOS 27.2.1, CPython 3.15.2 (zlib from the system CPython build). **On the merge** - `python -I tests/test_dds_png.py`: 101 tests OK, including the new `test_compressible_png_has_bounded_scanline_storage` (subprocess, `tracemalloc` peak < 1 MB for a 1024x1024 RGBA source). - I also round-tripped a hand-built 3x5 image through `encode_png`: walking the chunks, the IDAT decompresses to one filter byte `0` per scanline and the exact RGBA bytes, so the streaming is spec-compliant and not losing the row separators. - Output bytes need not be identical to the old one-shot `zlib.compress(raw)` (chunked deflate may differ); no test depends on that, and the PNG decodes to the same pixels. **Negative control (new test kept, `tools/dds_png.py` reverted to `main`)** - `FAILED (failures=1)` - the memory test, because the old code allocates `height * (stride + 1)` (~4 MB here) before compressing. Restored: OK. **Nits** - The new test asserts an absolute peak (`< 1024 * 1024` bytes) across a subprocess. With a 4 MB source and the streaming encoder the measured peak is far below, so there is room, but on a CPython/zlib build with a much larger compressor or `tracemalloc` overhead this could be brittle. A relative bound (e.g. `< len(rgba) / 2`) would keep the intent ("does not stage the image") without pinning a number that depends on the compression library. - `compressor.compress(b"\x00")` followed by the row is clear; `compressed.extend` on a `bytearray` is fine. The whole compressed stream is still held in memory (necessary for one IDAT), which the comment on the test could say so nobody reads it as "constant memory". **Not run**: the DDS side of the tool (BC decode/encode paths) beyond what the existing 101 tests cover, and no GPU; this change is PNG-only. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
tiguan26 approved these changes 2026-11-27 04:34:06 +00:00
tiguan26 left a comment

Approving a9ec081f1.

Scanlines go to a streaming zlib compressor one by one with their filter byte, instead of being copied into a second image-sized buffer first; the decompressed data is the same, so the PNG is too.

Merged onto main d1dff7949 in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. tests/test_dds_png.py: 101 passed (main: 100). AI-assisted (Claude Code, Claude Opus 5.5).

Approving a9ec081f1. Scanlines go to a streaming `zlib` compressor one by one with their filter byte, instead of being copied into a second image-sized buffer first; the decompressed data is the same, so the PNG is too. Merged onto `main` `d1dff7949` in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. `tests/test_dds_png.py`: 101 passed (main: 100). AI-assisted (Claude Code, Claude Opus 5.5).
MoonEyes commented 2026-11-28 19:31:35 +00:00

Red/green: tests/test_dds_png.py fails on main (4579376 bytes of scanline storage, not below 1048576) and passes with the PR merged, on GitHub Actions Linux and natively on Windows.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main 7daa4a4a5 in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: tests/test_dds_png.py fails on main (4579376 bytes of scanline storage, not below 1048576) and passes with the PR merged, on GitHub Actions Linux and natively on Windows. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main 7daa4a4a5 in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.