Clock photo renders as a black screen: jpegDataURL re-encode path ships a solid-black JPEG for every bundled photo > 350 KB #3

Closed
opened 2026-10-14 09:40:25 +00:00 by xrisk · 1 comment
Owner

Clock photo renders as a solid black screen (re-encode path produces a black JPEG)

Summary

A photo-backed clock shows a black screen instead of the photo whenever the shuffle
lands on a bundled photo larger than 350 KB. The photo is not missing — the app ships a
solid-black JPEG as the photo's data URL, which WebKit then paints full-bleed. The next
photo (≤ 350 KB, shipped verbatim) renders fine, so the symptom looks intermittent.

  • Severity: high — the kiosk's hero widget goes black for ~31% of shuffles.
  • Environment: macOS 27.0.1 (26A434), MacBookPro18,3 (arm64), io.rishav.tvd built from
    main @ 880d318. Config under test: one clock cell, photoSet: "new-york",
    shuffleSeconds: 300.

Repro

  1. Run the kiosk with a clock photo cell (photoSet: "new-york").
  2. Wait for (or force, via the admin next-photo control) a shuffle onto any photo whose
    bundled file is > 350,000 bytes — 158 of the 518 new-york photos qualify.
  3. The cell paints black (clock text/caption over pure black) for a full shuffle interval.

Deterministic check (no kiosk needed): resolve one such photo through the real code path,
e.g. ClockPhotoLibrary.image(setID: "new-york", index: 229)
(new-york/dl-047dbb39d4dd401e.jpg, 506,502 bytes). The returned src decodes to a
solid-black 1280×960 JPEG:

src jpeg bytes: 20193
decoded 1280x960 — ImageMagick: mean=0,0,0  min=0  max=0  std=0     (solid black)
original file  — ImageMagick: mean=107,101,88  min=1  max=255     (a real photo)

Photos ≤ 350 KB are base64'd verbatim (dataURL(for:) fast path) and render correctly —
which is why the next image after a black one looks fine.

Root cause

ClockPhotoLibrary.jpegDataURL(_:maxSide:)
(Sources/TVDashboard/Clock/ClockPhotoLibrary.swift:222-240) re-encodes oversized photos
into an NSBitmapImageRep configured as 3 samples, no alpha (24-bit packed RGB):

let rep = NSBitmapImageRep(
    bitmapDataPlanes: nil, pixelsWide: w, pixelsHigh: h,
    bitsPerSample: 8, samplesPerPixel: 3, hasAlpha: false, isPlanar: false,
    colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)

On macOS 27, NSGraphicsContext(bitmapImageRep:) for that pixel format fails to create
a CG context
:

CGBitmapContextCreate: unsupported parameter combination:
    RGB | 8 bits/component, integer | 192 bytes/row.
    kCGImageAlphaNone | kCGImageByteOrderDefault | kCGImagePixelFormatPacked

Verified in the running app: for a 3-sample/no-alpha rep,
NSGraphicsContext(bitmapImageRep:)?.cgContext == nil, so image.draw(in:) silently
paints nothing; the zeroed rep is then JPEG-encoded and shipped as the photo.
No error is logged — dataURL(for:) returns a non-nil (black) data URL, so
ClockPhotoLibrary.image succeeds and the failure is invisible Swift-side.

The sibling draw paths already use the working configuration —
ScriptingMusicSource.pngDataURL (ScriptingMusicSource.swift:329-333) and
AlbumArtPalette.downsampledRGBA (AlbumArtPalette.swift:47-51) both use
samplesPerPixel: 4, hasAlpha: true.

Verified fix direction

Same draw, rep changed to samplesPerPixel: 4, hasAlpha: true (RGBA; JPEG encoding drops
alpha):

B) 4-sample rep: cgContext valid = true
B) 4-sample rep draw: meanRGB=(105,100,84)     (original: 107,101,88 — correct image)

Note: samplesPerPixel: 4, hasAlpha: false (RGBX) is rejected by the
NSBitmapImageRep initializer ("Inconsistent set of values to create NSBitmapImageRep"),
so the fix is hasAlpha: true (or an explicit bitsPerPixel: 32).

Secondary issues found while debugging (same function)

  1. Point-size vs pixel-size: jpegDataURL computes the downscale target from
    NSImage.size (points, DPI-scaled), not pixel dimensions. A 1280 px-wide photo with
    300 DPI metadata gets a ~307 px target — squashed to ~24% resolution. 79 of the 158
    affected new-york photos carry non-72 DPI density. (pngDataURL has the same
    point-size logic.) Fix: size from representations.first?.pixelsWide/pixelsHigh
    (or cgImage.width/height).
  2. CMYK photo in the pool: new-york/dl-12597627ac0c4d63.jpg ("Radio City Music Hall",
    index 86) is a 4-component CMYK JPEG. scripts/fetch-clock-photos.sh re-encodes
    downloads with sips -Z 1280 -s format jpeg, which preserves CMYK (verified), so
    it stays CMYK in the pool and the shared download cache. It is > 350 KB so it takes the
    re-encode path today; a ≤ 350 KB CMYK file would be shipped verbatim to WebKit.
    Consider normalizing to RGB (or rejecting non-RGB) at fetch/build time.
  3. Silent failure mode: when the bitmap context is invalid, the draw no-ops and a black
    JPEG ships with no log line. A cgContext == nil check (log + return nil) would turn
    this into a visible, diagnosable failure.

Why the tests didn't catch it

SelfTest only asserts that an on-demand photo resolves to a data URL with a caption
(SelfTest.swift:261-263) — it never decodes or inspects pixels. The selftest's own
bitmap-rep usage (SelfTest.swift:336) uses the working 4-sample config and asserts
nothing about pixel content.

Suggested fix

  • In ClockPhotoLibrary.jpegDataURL, create the rep with samplesPerPixel: 4, hasAlpha: true (the config the music paths already use), and bail out with a log line
    if NSGraphicsContext(bitmapImageRep:)?.cgContext == nil instead of shipping a black
    JPEG.
  • Size the target from pixel dimensions, not image.size points.
  • Add a selftest that resolves one > 350 KB photo and asserts the data URL decodes to a
    non-uniform image (mean/stddev within sane bounds of the source file).
  • Optional: normalize/reject CMYK in scripts/fetch-clock-photos.sh.
# Clock photo renders as a solid black screen (re-encode path produces a black JPEG) ## Summary A photo-backed clock shows a **black screen instead of the photo** whenever the shuffle lands on a bundled photo larger than 350 KB. The photo is not missing — the app ships a **solid-black JPEG** as the photo's data URL, which WebKit then paints full-bleed. The next photo (≤ 350 KB, shipped verbatim) renders fine, so the symptom looks intermittent. - **Severity:** high — the kiosk's hero widget goes black for ~31% of shuffles. - **Environment:** macOS 27.0.1 (26A434), MacBookPro18,3 (arm64), `io.rishav.tvd` built from `main` @ `880d318`. Config under test: one clock cell, `photoSet: "new-york"`, `shuffleSeconds: 300`. ## Repro 1. Run the kiosk with a clock photo cell (`photoSet: "new-york"`). 2. Wait for (or force, via the admin next-photo control) a shuffle onto any photo whose bundled file is **> 350,000 bytes** — 158 of the 518 `new-york` photos qualify. 3. The cell paints black (clock text/caption over pure black) for a full shuffle interval. Deterministic check (no kiosk needed): resolve one such photo through the real code path, e.g. `ClockPhotoLibrary.image(setID: "new-york", index: 229)` (`new-york/dl-047dbb39d4dd401e.jpg`, 506,502 bytes). The returned `src` decodes to a **solid-black 1280×960 JPEG**: ``` src jpeg bytes: 20193 decoded 1280x960 — ImageMagick: mean=0,0,0 min=0 max=0 std=0 (solid black) original file — ImageMagick: mean=107,101,88 min=1 max=255 (a real photo) ``` Photos ≤ 350 KB are base64'd verbatim (`dataURL(for:)` fast path) and render correctly — which is why the *next* image after a black one looks fine. ## Root cause `ClockPhotoLibrary.jpegDataURL(_:maxSide:)` (`Sources/TVDashboard/Clock/ClockPhotoLibrary.swift:222-240`) re-encodes oversized photos into an `NSBitmapImageRep` configured as **3 samples, no alpha** (24-bit packed RGB): ```swift let rep = NSBitmapImageRep( bitmapDataPlanes: nil, pixelsWide: w, pixelsHigh: h, bitsPerSample: 8, samplesPerPixel: 3, hasAlpha: false, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0) ``` On macOS 27, `NSGraphicsContext(bitmapImageRep:)` for that pixel format **fails to create a CG context**: ``` CGBitmapContextCreate: unsupported parameter combination: RGB | 8 bits/component, integer | 192 bytes/row. kCGImageAlphaNone | kCGImageByteOrderDefault | kCGImagePixelFormatPacked ``` Verified in the running app: for a 3-sample/no-alpha rep, `NSGraphicsContext(bitmapImageRep:)?.cgContext == nil`, so `image.draw(in:)` silently paints **nothing**; the zeroed rep is then JPEG-encoded and shipped as the photo. No error is logged — `dataURL(for:)` returns a non-nil (black) data URL, so `ClockPhotoLibrary.image` succeeds and the failure is invisible Swift-side. The sibling draw paths already use the working configuration — `ScriptingMusicSource.pngDataURL` (`ScriptingMusicSource.swift:329-333`) and `AlbumArtPalette.downsampledRGBA` (`AlbumArtPalette.swift:47-51`) both use `samplesPerPixel: 4, hasAlpha: true`. ### Verified fix direction Same draw, rep changed to `samplesPerPixel: 4, hasAlpha: true` (RGBA; JPEG encoding drops alpha): ``` B) 4-sample rep: cgContext valid = true B) 4-sample rep draw: meanRGB=(105,100,84) (original: 107,101,88 — correct image) ``` Note: `samplesPerPixel: 4, hasAlpha: false` (RGBX) is rejected by the `NSBitmapImageRep` initializer ("Inconsistent set of values to create NSBitmapImageRep"), so the fix is `hasAlpha: true` (or an explicit `bitsPerPixel: 32`). ## Secondary issues found while debugging (same function) 1. **Point-size vs pixel-size:** `jpegDataURL` computes the downscale target from `NSImage.size` (points, DPI-scaled), not pixel dimensions. A 1280 px-wide photo with 300 DPI metadata gets a ~307 px target — squashed to ~24% resolution. 79 of the 158 affected `new-york` photos carry non-72 DPI density. (`pngDataURL` has the same point-size logic.) Fix: size from `representations.first?.pixelsWide/pixelsHigh` (or `cgImage.width/height`). 2. **CMYK photo in the pool:** `new-york/dl-12597627ac0c4d63.jpg` ("Radio City Music Hall", index 86) is a 4-component CMYK JPEG. `scripts/fetch-clock-photos.sh` re-encodes downloads with `sips -Z 1280 -s format jpeg`, which **preserves CMYK** (verified), so it stays CMYK in the pool and the shared download cache. It is > 350 KB so it takes the re-encode path today; a ≤ 350 KB CMYK file would be shipped verbatim to WebKit. Consider normalizing to RGB (or rejecting non-RGB) at fetch/build time. 3. **Silent failure mode:** when the bitmap context is invalid, the draw no-ops and a black JPEG ships with no log line. A `cgContext == nil` check (log + return nil) would turn this into a visible, diagnosable failure. ## Why the tests didn't catch it `SelfTest` only asserts that an on-demand photo resolves to a data URL with a caption (`SelfTest.swift:261-263`) — it never decodes or inspects pixels. The selftest's own bitmap-rep usage (`SelfTest.swift:336`) uses the working 4-sample config and asserts nothing about pixel content. ## Suggested fix - In `ClockPhotoLibrary.jpegDataURL`, create the rep with `samplesPerPixel: 4, hasAlpha: true` (the config the music paths already use), and bail out with a log line if `NSGraphicsContext(bitmapImageRep:)?.cgContext == nil` instead of shipping a black JPEG. - Size the target from pixel dimensions, not `image.size` points. - Add a selftest that resolves one > 350 KB photo and asserts the data URL decodes to a non-uniform image (mean/stddev within sane bounds of the source file). - Optional: normalize/reject CMYK in `scripts/fetch-clock-photos.sh`.
Author
Owner

Fix implemented and pushed in PR #4 (branch implement_tea_issues_three).

  • aacddb2 switches clock-photo JPEG rendering to a four-sample RGBA bitmap, uses true pixel dimensions for photo/artwork scaling, guards graphics-context creation, normalizes downloaded/CMYK cached photos to sRGB, and adds pixel-content and dimension regression checks.
  • Review follow-up e4b81a5 makes both CMYK conversion paths publish shared-cache updates via a temporary file and rename, preventing concurrent worktree builds from reading partially written JPEGs. The cache-write rule is documented in the agent wiki.

Validation: make test passed (Swift selftests, Reminders selftests, and JS runtime checks). Targeted script checks passed for both the existing-local-photo and shared-cache-hit conversion paths. AirPlay rendering was not manually verified.

The fix is on PR #4; it has not been merged into main yet.

Fix implemented and pushed in PR #4 (branch `implement_tea_issues_three`). - `aacddb2` switches clock-photo JPEG rendering to a four-sample RGBA bitmap, uses true pixel dimensions for photo/artwork scaling, guards graphics-context creation, normalizes downloaded/CMYK cached photos to sRGB, and adds pixel-content and dimension regression checks. - Review follow-up `e4b81a5` makes both CMYK conversion paths publish shared-cache updates via a temporary file and rename, preventing concurrent worktree builds from reading partially written JPEGs. The cache-write rule is documented in the agent wiki. Validation: `make test` passed (Swift selftests, Reminders selftests, and JS runtime checks). Targeted script checks passed for both the existing-local-photo and shared-cache-hit conversion paths. AirPlay rendering was not manually verified. The fix is on PR #4; it has not been merged into `main` yet.
xrisk closed this issue 2026-10-14 10:41:07 +00:00
Sign in to join this conversation.
No labels
No milestone
No project
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.

Dependencies

No dependencies set

Reference
xrisk/tvd#3
No description provided.