Skip to content

fix(imaging): decode Apple CgBI ("iPhone-optimised") PNGs - #19

Merged
charpy4n6 merged 1 commit into
mainfrom
fix/cgbi-png-decoding
Sep 3, 2026
Merged

charpy4n6 merged 1 commit into
mainfrom
fix/cgbi-png-decoding

Conversation

@charpy4n6

Copy link
Copy Markdown
Collaborator

PNGs bundled inside iOS .app packages are rewritten by Xcode's asset pipeline into a non-standard variant that Pillow and Chromium/WebView2 render as a black or empty image: a CgBI chunk before IHDR, a raw-DEFLATE IDAT (no zlib header), byte-swapped BGRA channels, premultiplied alpha, and optionally an iDOT chunk splitting the IDAT into two streams. A real Project VIC set can be ~30% these (5,929 of 20,705 PNGs in one iOS case).

  • imaging.py: is_cgbi_png() + _decode_cgbi_png() (manual chunk parse, raw inflate, PNG unfiltering incl. Adam7 de-interlace, channel swap, un-premultiply). load_any() uses it, so thumbnails, hashing, screening and metadata all get the real pixels. New to_web_png() keeps alpha.
  • media.py: thumbnails composite transparency onto white so a mostly transparent asset no longer thumbnails to black.
  • web/app.py: /view transcodes a CgBI PNG to a real PNG (cached) instead of serving bytes no browser can render.

Verified against a real 29k-file iOS case: all 5,929 CgBI PNGs decode (85 interlaced included), 0 failures.

Claude-Session: https://claude.ai/code/session_01VcEhYD7XkWcxocYPALdcEb

PNGs bundled inside iOS .app packages are rewritten by Xcode's asset
pipeline into a non-standard variant that Pillow and Chromium/WebView2
render as a black or empty image: a CgBI chunk before IHDR, a raw-DEFLATE
IDAT (no zlib header), byte-swapped BGRA channels, premultiplied alpha,
and optionally an iDOT chunk splitting the IDAT into two streams. A real
Project VIC set can be ~30% these (5,929 of 20,705 PNGs in one iOS case).

- imaging.py: is_cgbi_png() + _decode_cgbi_png() (manual chunk parse, raw
  inflate, PNG unfiltering incl. Adam7 de-interlace, channel swap,
  un-premultiply). load_any() uses it, so thumbnails, hashing, screening
  and metadata all get the real pixels. New to_web_png() keeps alpha.
- media.py: thumbnails composite transparency onto white so a mostly
  transparent asset no longer thumbnails to black.
- web/app.py: /view transcodes a CgBI PNG to a real PNG (cached) instead
  of serving bytes no browser can render.

Verified against a real 29k-file iOS case: all 5,929 CgBI PNGs decode
(85 interlaced included), 0 failures.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VcEhYD7XkWcxocYPALdcEb
@charpy4n6
charpy4n6 merged commit 9f4c32f into main Sep 3, 2026
8 checks passed
@charpy4n6
charpy4n6 deleted the fix/cgbi-png-decoding branch September 3, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant