Skip to content

fix(web): pasted 16-bit PNG photos upload at 8 bits per channel - #40949

Open
emmanuelchucks wants to merge 4 commits into
pingdotgg:mainfrom
emmanuelchucks:fix/web-16-bit-png-attachments
Open

emmanuelchucks wants to merge 4 commits into
pingdotgg:mainfrom
emmanuelchucks:fix/web-16-bit-png-attachments

Conversation

@emmanuelchucks

Copy link
Copy Markdown

Problem

On a Mac, copying a photo on an iPhone and pasting it into the composer (Universal Clipboard) gives the browser a 16-bit RGBA PNG with a Display P3 profile. Mine was 1042×1389 and 8,711,193 bytes, from a 4032×3024 HDR JPEG. compressImageToByteLimit only redraws an image whose longest edge is over 2000px, so this one uploads byte for byte.

Over my upload link (Starlink, about 0.1–0.4 MB/s) the server's AttachmentUpload store spans for these pastes took 20.7 s and 76.6 s. I also gave up on a few pastes that looked stuck and pasted again; their traces have issueUrl and deletePending but no store.

Change

  • fitImageToMaxEdge (now fitImageForAttachment) also redraws a PNG whose IHDR bit depth is 16, at its own size. It goes through the same canvas path as the over-2000px case, so it stays a PNG, now at 8 bits per channel, in its own colour space (Display P3 is kept by readImageColorSpace).
  • The canvas always writes RGBA, so a 16-bit greyscale PNG could come out larger. When the redraw isn't smaller than the original, the original is kept, so no upload gets bigger than it is now.
  • 8-bit PNGs and JPEGs under the limit still pass through without a decode.

Scope

A small fix for an obvious bug, so no issue. #20106 hit the same gap (images under 2000px are never re-encoded) when large images broke mid-turn Claude requests. It closed when Claude Code fixed its side, which left the upload size as it was.

Left out on purpose:

  • An 8-bit photo pasted as a PNG is still large (the photo above is 2.4 MB after this change; JPEG would be about 0.23 MB). Re-encoding PNGs lossily would change what screenshots become, so I didn't.
  • Mobile's fitComposerImage has the same edge-only rule. I didn't check whether iOS hands the app a 16-bit PNG, so it's unchanged.

Verification

  • New case in imageCompression.browser.test.ts: a Display P3 PNG drawn on a float16 canvas (Chromium writes it as 16-bit) comes back as an 8-bit PNG of the same size with the same colours, raw and colour-managed. On main it fails with expected undefined to deeply equal { width: 300, height: 400 }, because the file passes through untouched.

  • New case in imageCompression.test.ts: a 16-bit PNG whose redraw comes out larger is kept as is.

  • I ran prepareImageForAttachment in the Chromium test runner on two real pastes:

    Paste main This branch
    1042×1389, 16-bit, P3 8,711,193 B, unchanged 2,380,654 B, 8-bit PNG
    1039×1385, 16-bit, P3 8,531,038 B, unchanged 2,556,134 B, 8-bit PNG

    Preparing took about 180 ms. For the first one I compared every pixel with the original: none differ, both colour-managed in P3 and raw, and the output carries the P3 profile.

  • vp run check:changed passes.

  • Not checked: Safari and Firefox, and the mobile app.

Code written with Claude Code (Claude Opus 5.5).

@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Nov 22, 2026
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The attachment preparation path now handles 16-bit PNGs when scaling is not required. It declines encoded output if encoding fails or does not reduce the file size. Tests cover retaining the original file and preparing a color-preserving 8-bit PNG.

Changes: PNG attachment preparation

Layer / File(s) Summary
Image fitting and validation
apps/web/src/lib/imageCanvas.ts, apps/web/src/lib/imageCompression.ts, apps/web/src/lib/imageCompression.worker.ts, apps/web/src/lib/imageCompression.test.ts, apps/web/src/lib/imageCompression.browser.test.ts
The fitting function now accepts 16-bit PNGs when scaling is not required and declines output when encoding fails or does not produce a smaller blob. The main-thread and worker paths use the updated fitting function. Tests check that the original file is retained when redraw would increase its size, and that browser preparation outputs an 8-bit PNG with the original dimensions and colors.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

Suggested reviewers: juliusmarminge

Merge Risk: Minimal · up to cf11b

Pasted 16-bit PNG photos below the resize threshold are now redrawn at 8 bits per channel, and the original file is kept if the redraw is not smaller. No specific merge-blocking risk was found. Safari, Firefox, and the mobile app were not checked.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the actual change: pasted 16-bit PNG photos are now uploaded at 8 bits per channel. It follows the conventional commit form and matches the code changes, which redraw 16-bit PNGs via f…
Description check Passed The description follows the template. It covers Problem, Change, Scope, and Verification. It explains the 16-bit PNG upload issue with measured sizes and timings, and it states the fallback rule (keep…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the actual change: pasted 16-bit PNG photos are now uploaded at 8 bits per channel. It follows the conventional commit form and matches the code changes, which redraw 16-bit PNGs via fitImageForAttachment and add tests for that path.

Full details: Description check

Explanation

The description follows the template. It covers Problem, Change, Scope, and Verification. It explains the 16-bit PNG upload issue with measured sizes and timings, and it states the fallback rule (keep the original when the redraw is not smaller). Scope explains why no issue is linked and why 8-bit PNGs and mobile are excluded. Verification names the new tests, real-paste measurements, and what was not checked (Safari, Firefox, mobile). It also states the model used, as the template asks. Minor gap: the Scope section does not link an issue, but it gives a reason for the omission, which the template allows for small obvious bugs.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c8a7527 and cf11b5e.

Files selected for processing: 5

  • apps/web/src/lib/imageCanvas.ts
  • apps/web/src/lib/imageCompression.browser.test.ts
  • apps/web/src/lib/imageCompression.test.ts
  • apps/web/src/lib/imageCompression.ts
  • apps/web/src/lib/imageCompression.worker.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@juliusmarminge juliusmarminge added the takeover:in-progress A takeover lead is working on this PR label Nov 22, 2026
@github-actions github-actions Bot added size:S 10-29 changed lines (additions + deletions). and removed size:M 30-99 changed lines (additions + deletions). labels Nov 22, 2026
@juliusmarminge

Copy link
Copy Markdown
Member

The automated pipeline stopped on this pull request and will not merge it:

the fix is written and reviewed, but proving it needs something no worker has:

  • Permanent blocker: macOS with iPhone Universal Clipboard is required to measure the reported interaction on its original platform. This worker is Linux, macOS access outside the device pool is unavailable, and the brief prohibits substituting another platform. Upload medians, spread, and preservation of dimensions, PNG format, and Display P3 colours remain unverified on both commits.

It stays open for a person to finish or close.

@juliusmarminge juliusmarminge added takeover:blocked Takeover blocked; reason in the PR body and removed takeover:in-progress A takeover lead is working on this PR labels Nov 22, 2026
@github-actions github-actions Bot added size:M 30-99 changed lines (additions + deletions). and removed size:S 10-29 changed lines (additions + deletions). labels Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). takeover:blocked Takeover blocked; reason in the PR body vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants