perf(screenshot): read the crop region instead of decoding the whole capture

Merged
TH
2mo ago
+1488-29

Summary

screenshot --crop-on <selector> decoded the whole capture to RGBA, copied the frame's rows, and re-encoded it. One PNG worker job now turns captured bytes into cropped bytes:

  • a region reader that reconstructs pixels only down to the crop box's last row and allocates only the box's pixels;
  • a truecolor writer that drops alpha when the cropped pixels carry none, so an opaque crop is RGB and one with transparency stays RGBA.

The reader claims the 8-bit non-interlaced truecolor layout iOS simulator and Android emulator captures arrive in. Palette, grayscale, interlaced, 16-bit and structurally untrusted files fall through to the general PNG reader, which keeps owning the canonical decode error and the previous RGBA output.

agent-device screenshot card.png --crop-on 'label="Apple Account"'

What the region reader does not claim. A deflate stream cannot be cut short, so the new path inflates the whole compressed capture into a buffer sized for every filtered row, exactly as the old path did; what stops at the box is the pixel work, not the read. And the reader only acts on a file it can vouch for: the IHDR and every chunk checksum are verified, an unrecognised critical chunk name is a decline, every row's filter byte is read whether or not the crop reaches that row, and IEND has to be the end of the buffer, because bytes after it are content no conforming reader reaches. A box covering the whole image is reported as "your file is already the answer" only after the general reader has decoded the file cleanly. A truncated capture, a bad checksum, an unknown critical chunk or an unreadable filter below the box therefore fails the same way it did before, on both paths.

Why the reader and the writer together, and not one of them. Measured on the same bytes with only one half of the change in place (main thread, median of 7, one 20%-height crop box; full table in a comment below):

pipeline245 kB iOS UI3.0 MB iOS photo1.4 MB Android screencap
old reader + old writer55.2ms, 93 kB99.7ms, 1030 kB67.2ms, 744 kB
old reader + new writer36.4ms, 30 kB89.3ms, 441 kB85.5ms, 317 kB
new reader + old writer28.1ms, 60 kB55.2ms, 384 kB42.8ms, 202 kB
both (shipped)13.5ms, 30 kB44.1ms, 441 kB59.1ms, 317 kB

Each half alone buys one thing. The writer alone buys the artifact (93 kB to 30 kB) and costs time on noisy captures; the reader alone buys the time (55.2ms to 28.1ms) and leaves an RGBA artifact bigger than the one the writer produces. Only together do they get 4.1x faster and 3.1x smaller on the flat capture where --crop-on is actually used. The reader cannot avoid the writer, because an RGBA-only answer gives back part of what the region read won; the writer cannot avoid the reader, because re-encoding from a full RGBA bitmap is the cost the reader removes.

18 files, 1,488 gross lines: 574 production across seven modules (largest 251 lines), 913 mirrored tests and fixtures, one docs line. Budget: asking for an explicit exception to the 1,000-line budget rather than a split: the 570 production lines are one reader, the writer it feeds, and the format module both of them read, and the 895 remaining lines are their mirrored tests, which by repo convention land with the source. A split would ship a PNG writer with no reader feeding it, or a reader with no writer, and every intermediate layer would be dead code on main. Say split and it lands as three layers the same day; say exception and this line is the record.

Validation

Validated at this layer's head 71a4386108, and at the stack head 03baaa59c3 whose only additions over it are scripts/png-crop-benchmark and its gate registration.

  • pnpm check:affected --run: 368 test files / 2,350 tests pass, including the eager-closure budget suite (590 checks), plus format, oxlint, typecheck, layering, di-seams, check:fallow --base origin/main and command-doc coverage. Two gates fail and both fail identically at origin/main in this worktree: mutation-model, and production-exports (63 findings, 63 at the merge base, none in png-*). One provider-integration scenario timed out under full-suite load and passes in isolation (2.4s of a 5s budget).
  • Cropped bytes vs ImageMagick's crop over a real iOS and a real Android capture, across RGB, RGBA, grayscale, palette, 16-bit, interlaced and translucent sources: max channel delta 0; declared color type is RGB for opaque, RGBA otherwise.
  • iPhone 17 Pro: 370x90 Apple Account crop, 8-bit RGB, correct frame.
  • Android, live on a Pixel 7 CI emulator after pnpm build && pnpm build:android: snapshot -i --json reports androidSnapshot.backend: android-helper, a 1080x2400 Settings capture crops to 436x71 (label="Network & internet") and 585x51 (label="Assistant, recent apps, default apps"), and magick compare -metric AE against ImageMagick's own crop of that same capture reports 0 differing pixels. Both artifacts declare IHDR 8-bit, colorType 2, non-interlaced, so the device capture loses its unused alpha. Session closed; emulator left as found.
  • Crop stage, measured at the worker boundary both paths use, on three real captures: 2.6x to 5.6x faster and 1.67x to 2.16x smaller on a 245 kB iOS UI capture, 1.78x to 2.52x faster and 1.04x to 1.31x smaller on a 3 MB iOS photo capture. On a noisy 1.4 MB Android screencap it is a wash on time and the crop can come out ~13% larger, because inflating and reconstructing that much entropy dominates and the None-filter writer cannot beat the general writer's filter search there. The benchmark layer documents how to reproduce that.