feat(pngdec): implement the PNG decoder #175

Merged
Qznad merged 2 commits from upstream-pr/175 into main 2026-09-30 18:12:57 +00:00
Qznad commented 2026-09-30 10:15:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Qznad on 2026-09-30T10:15:58Z.


What

Implements the 5 libScePngDec entry points (previously NotImplemented_nid_no_patch) and adds core/Decoder/Png, the PNG counterpart of core/Decoder/Jpeg requested in #146: a shared encoder/decoder with no PRX dependency, so the relinker can use it for the executable icon.

core/Decoder/Png (static library decoder_png):

  • Decoder::Png::Decode(png) returns RGBA8 for every PNG colour type and bit depth (palette, tRNS, 1-16 bit, Adam7), and Decoder::Png::Encode(pixels, width, height, channels) writes 1-4 channel PNGs. Both use the existing nothings/stb submodule (STBI_ONLY_PNG, no stdio, static stb symbols, hidden visibility).
  • Decoder::Png::ParseHeader(png) reads IHDR and walks the chunks up to IDAT for tRNS, because stb does not expose colour type, bit depth, interlace or transparency. Chunk lengths are bounds-checked against the buffer.

libScePngDec:

  • The existing structs and signatures in SceTypes.hpp match shadPS4's reverse engineering and are unchanged; sizes are now checked with static_assert. Error codes 0x80690001-0x80690012 and the NIDs follow shadPS4.
  • QueryMemorySize / Create / Delete: the handle lives in the caller's memory, aligned to 8, and points to itself; Delete clears it, so stale and foreign handles return INVALID_HANDLE.
  • ParseHeader maps the PNG header to ImageInfo (colour space 2/3/4/18/19, Adam7 and tRNS flags).
  • Decode writes R8G8B8A8 or B8G8R8A8 rows at image_pitch (0 means packed), fills alpha with alpha_value when the image has no alpha or tRNS, reduces 16-bit to 8-bit, and returns (width << 16) | height, or 0 when either is above 32767, as shadPS4 does.

Differences from shadPS4 and assumptions (no documentation available):

  • QueryMemorySize returns 0x20; shadPS4 returns the size of its own libpng handle. Memory smaller than that returns INVALID_WORK_MEMORY.
  • The output buffer is checked: (height - 1) * pitch + width * 4 above image_mem_size returns INVALID_SIZE, a pitch below width * 4 returns INVALID_PARAM. shadPS4 writes without checking.
  • An attribute above 1 returns INVALID_PARAM from QueryMemorySize as from Create (shadPS4 returns INVALID_ADDR there only).
  • A zero PNG size returns INVALID_SIZE, a null image_info in ParseHeader returns INVALID_ADDR, an unreadable header INVALID_DATA, a failed decode DECODE_ERROR.
  • max_image_width is validated at creation but not enforced at decode.

Not supported: the 16-bit output attribute throws std::runtime_error when the PNG really is 16-bit; 8-bit images still decode with it.

Tested

Linux (GCC 15.2, BUILD_TESTING=ON): full build with no warnings in the changed files, ctest 66/66 pass, including the 2 new tests:

  • decoder_png: exact round trips for 1-4 channels, Pillow-made fixtures (2-bit palette with tRNS, 16-bit gray, 1-bit gray), hand-built headers (interlace, invalid colour type and bit depth, zero width, bad signature, oversized chunk length, truncation).
  • guest_png_dec: every error path, stale and misaligned handles, RGBA and BGRA output with padded rows, alpha fill, palette transparency, 16-bit input, the packed return value, and the 16-bit output attribute.

Also checked:

  • Sources compile without warnings under -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wsign-conversion; both tests run clean under ASan and UBSan.
  • 10 Pillow-made PNGs (gray 1/8/16-bit, gray+alpha, RGB, RGB with tRNS, RGBA, palette with and without tRNS, and a hand-built Adam7 interlaced RGB image) decoded through scePngDecDecode as B8G8R8A8 with a padded pitch match Pillow pixel for pixel, with the padding untouched.
  • After NID patching, libScePngDec.prx exports exactly m0uW+8pFyaw, WC216DD3El4, QbD+eENEwo8, U6h4e5JRPaQ and -6srIGbLTIU.

Not tested with a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR (libScePngEnc on Decoder::Png::Encode follows separately)
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt (16-bit output throws; no new silent stubs)
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source (none added; reuses nothings/stb)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4689138590 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/175). Originally posted by [Qznad](https://github.com/Qznad) on 2026-09-30T10:15:58Z. --- ### What Implements the 5 `libScePngDec` entry points (previously `NotImplemented_nid_no_patch`) and adds `core/Decoder/Png`, the PNG counterpart of `core/Decoder/Jpeg` requested in #146: a shared encoder/decoder with no PRX dependency, so the relinker can use it for the executable icon. `core/Decoder/Png` (static library `decoder_png`): - `Decoder::Png::Decode(png)` returns RGBA8 for every PNG colour type and bit depth (palette, `tRNS`, 1-16 bit, Adam7), and `Decoder::Png::Encode(pixels, width, height, channels)` writes 1-4 channel PNGs. Both use the existing `nothings/stb` submodule (`STBI_ONLY_PNG`, no stdio, static stb symbols, hidden visibility). - `Decoder::Png::ParseHeader(png)` reads IHDR and walks the chunks up to `IDAT` for `tRNS`, because stb does not expose colour type, bit depth, interlace or transparency. Chunk lengths are bounds-checked against the buffer. `libScePngDec`: - The existing structs and signatures in `SceTypes.hpp` match shadPS4's reverse engineering and are unchanged; sizes are now checked with `static_assert`. Error codes `0x80690001`-`0x80690012` and the NIDs follow shadPS4. - `QueryMemorySize` / `Create` / `Delete`: the handle lives in the caller's memory, aligned to 8, and points to itself; `Delete` clears it, so stale and foreign handles return `INVALID_HANDLE`. - `ParseHeader` maps the PNG header to `ImageInfo` (colour space 2/3/4/18/19, Adam7 and `tRNS` flags). - `Decode` writes R8G8B8A8 or B8G8R8A8 rows at `image_pitch` (0 means packed), fills alpha with `alpha_value` when the image has no alpha or `tRNS`, reduces 16-bit to 8-bit, and returns `(width << 16) | height`, or 0 when either is above 32767, as shadPS4 does. Differences from shadPS4 and assumptions (no documentation available): - `QueryMemorySize` returns 0x20; shadPS4 returns the size of its own libpng handle. Memory smaller than that returns `INVALID_WORK_MEMORY`. - The output buffer is checked: `(height - 1) * pitch + width * 4` above `image_mem_size` returns `INVALID_SIZE`, a pitch below `width * 4` returns `INVALID_PARAM`. shadPS4 writes without checking. - An attribute above 1 returns `INVALID_PARAM` from `QueryMemorySize` as from `Create` (shadPS4 returns `INVALID_ADDR` there only). - A zero PNG size returns `INVALID_SIZE`, a null `image_info` in `ParseHeader` returns `INVALID_ADDR`, an unreadable header `INVALID_DATA`, a failed decode `DECODE_ERROR`. - `max_image_width` is validated at creation but not enforced at decode. Not supported: the 16-bit output attribute throws `std::runtime_error` when the PNG really is 16-bit; 8-bit images still decode with it. ### Tested Linux (GCC 15.2, `BUILD_TESTING=ON`): full build with no warnings in the changed files, `ctest` 66/66 pass, including the 2 new tests: - `decoder_png`: exact round trips for 1-4 channels, Pillow-made fixtures (2-bit palette with `tRNS`, 16-bit gray, 1-bit gray), hand-built headers (interlace, invalid colour type and bit depth, zero width, bad signature, oversized chunk length, truncation). - `guest_png_dec`: every error path, stale and misaligned handles, RGBA and BGRA output with padded rows, alpha fill, palette transparency, 16-bit input, the packed return value, and the 16-bit output attribute. Also checked: - Sources compile without warnings under `-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wsign-conversion`; both tests run clean under ASan and UBSan. - 10 Pillow-made PNGs (gray 1/8/16-bit, gray+alpha, RGB, RGB with `tRNS`, RGBA, palette with and without `tRNS`, and a hand-built Adam7 interlaced RGB image) decoded through `scePngDecDecode` as B8G8R8A8 with a padded pitch match Pillow pixel for pixel, with the padding untouched. - After NID patching, `libScePngDec.prx` exports exactly `m0uW+8pFyaw`, `WC216DD3El4`, `QbD+eENEwo8`, `U6h4e5JRPaQ` and `-6srIGbLTIU`. Not tested with a title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/docs/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR (`libScePngEnc` on `Decoder::Png::Encode` follows separately) - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/TechnicalDebt.md#silent-stubs) (16-bit output throws; no new silent stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source (none added; reuses `nothings/stb`) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-09-30 10:16:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-09-30T10:16:23Z.


Progress report (fc05962 → a0e2f88)

📈 System libraries: 74.8% (+0.17%, +5 implemented functions)

✅ 5 implemented
Library Name
libScePngDec scePngDecCreate
libScePngDec scePngDecDecode
libScePngDec scePngDecDelete
libScePngDec scePngDecParseHeader
libScePngDec scePngDecQueryMemorySize
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5909213419 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/175#issuecomment-5909213419). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-09-30T10:16:23Z. --- <!-- progress-report --> ### Progress report (fc05962 → a0e2f88) 📈 **System libraries**: 74.8% (+0.17%, +5 implemented functions) <details> <summary>✅ 5 implemented</summary> | Library | Name | | - | - | | libScePngDec | `scePngDecCreate` | | libScePngDec | `scePngDecDecode` | | libScePngDec | `scePngDecDelete` | | libScePngDec | `scePngDecParseHeader` | | libScePngDec | `scePngDecQueryMemorySize` | </details>
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!175
No description provided.