Skip to content

Ignore sub-pixel glyph position drift in screenshot comparison #16

Closed

Description

@sentry-junior

Replaces #15 with a clearer description. Junior cannot edit or close #15.

Summary

Frameshift reports screenshots as changed when they look the same. Chrome sometimes draws a glyph a fraction of a pixel to one side between two runs. This changes the colour of the glyph edge pixels a lot, so the colour threshold reports a change. No threshold or region size can separate this noise from real changes. We need a separate check that recognises this kind of drift and marks the screenshot as unchanged.

Example: getsentry/junior PR #2140 does not touch the dashboard, but 12 of its 96 screenshots are marked as changed (report).

The technical problem

What changes in the pixels

  • Chrome puts glyphs at fractional x positions, and it draws each glyph for a small set of fractional positions. The measured shifts fit steps of about a quarter pixel.
  • In some runs, the text origin is a tiny amount to one side. Glyphs near a step boundary then go to the next step, and Chrome draws them again with different edge shades. Glyphs that do not go to the next step stay byte-identical.
  • Proof from #2140: in the monospace placeholder Search automations, only the glyphs at indexes 2, 7, 12 and 17 changed. With an advance of about 7.8px, every 5th glyph has the same fractional position, so these glyphs all went to the next step together. The ink centres of the changed glyphs moved 0.12 to 0.21px. The other glyphs did not change.
  • With LCD text, one edge pixel can change by up to ~70 per channel. But the total ink, the colour and the shape of each glyph stay the same. People cannot see the change.

How it occurs

  • It is all or nothing per test (per page). In #2140, every screenshot from 3 tests changed, and every screenshot from the other tests was byte-identical to the baseline. The CI runner image and Chrome version were the same.
  • The other version is stable. The same changed bytes come back in other PRs. Two main-branch baselines in our sample (f04b34f8, f7968ff3) were also captured in the other version. When the baseline is the odd one, every PR based on it gets flagged.
  • We do not know what makes Chrome choose a different text origin. A fractional margin-left on body did not reproduce the changed bytes locally. So we cannot reliably fix this in the app under test. It must be handled in the comparison.

Why the current settings do not help

  • Honeydiff's anti-aliasing filter suppresses 0 pixels on these images.
  • A threshold of 3.5 (1427d57) still flags all 12. A threshold of 12 still flags them.
  • One changed area reaches 209px. A minimum region size that large would also hide real changes.

Suggested solution

Add a second check after Honeydiff. Run it only when Honeydiff reports a change, both images have the same size, and no height alignment was used. If every changed area passes the check, report the screenshot as unchanged and delete the diff image. If one area fails, keep the current result.

Areas to check

  1. Each Honeydiff region (difference.regions[].spatial.bounds), with 3px of padding.
  2. Groups of regions: join boxes that are 3px or less apart, then add 3px of padding. Honeydiff often splits one glyph into several small regions, so a group covers whole glyphs.

Fail the check (real change) if any area:

  • Has a pixel that is not fully opaque in either image.
  • Changes a flat pixel: a channel changes by more than 8, and in both images no channel varies by more than 6 in the 3x3 block around the pixel. Moved glyphs only change pixels next to an edge.
  • Changes its ink. Use the median colour of the area border in the baseline as the background. Ink is the distance from that background in linear light (sRGB decoded), per channel, because moving a glyph keeps its coverage in linear light. Fail if:
    • total ink changes by more than 17%
    • the share of ink in any one channel changes by more than 0.05
    • the ink centre moves more than 0.45px in x or more than 0.16px in y
    • the ink share of any row changes by more than 0.35 times the share of the fullest row
  • (Groups only.) Is explained by a whole-pixel move. For each shift from -2 to +2px in x and y (not 0,0), compare the shifted baseline with the candidate. Fail if a shift makes the summed absolute RGB difference less than half of the unshifted difference. The factor of 2 is needed because long straight strokes match themselves when moved along their length.

Where the limits come from

Check Largest drift seen Limit
Total ink change 0.131 0.17
Channel share change 0.025 0.05
Ink centre move x 0.399px 0.45px
Ink centre move y 0.109px 0.16px
Row profile change 0.245 0.35
Whole-pixel move gain 0.35 2
Flat pixels changed 0 0

All real changes in the sample were at least 8x past a limit.

Validation

  • 147 changed screenshots from 30 getsentry/junior reports: all 71 drift-only screenshots become unchanged, and all 76 real changes stay changed. I checked borderline cases by eye.
  • Main-branch baselines compared in pairs: the check hid only the 15 screenshots caused by the two drifted baselines.
  • #2140 goes from 12 changed to 0.
  • Synthetic edits on a real screenshot stay changed: a 1px move in x or y, a swapped digit, a swapped colour channel on text, text with 10% less contrast, a removed status dot, a mirrored icon.
  • The cost is about 15 to 270ms per changed screenshot.

Limits and risks

  • A real change that keeps a glyph's ink, colour and shape and moves it less than half a pixel will be hidden. A person cannot see such a change either.
  • The limits come from one project (Chrome on Linux, a dark UI, LCD text). Other fonts or light themes may need more data. The limits are constants in one module, so they are easy to change.
  • The report does not show which screenshots the check hid. If we want that, the report format needs a new field.

Implementation

A working change is ready (commit cc00a0a on b08384b; the patch is in the Slack thread linked below):

  • packages/action/src/glyph-drift.mjs: new module that exports isGlyphPositionDrift(baseline, candidate, boxes). It takes decoded pngjs images and the Honeydiff region boxes.
  • packages/action/src/compare.mjs: comparePair calls it when analysis.different is true, and treats true as unchanged.
  • packages/action/src/compare.test.mjs: draws glyph-like strokes at fractional positions. It tests that a 0.25px shift is unchanged, and that a 1px move, a new ink colour and a different letter are changed.
  • pnpm check and pnpm action:smoke pass. Rebuild dist/index.mjs with pnpm action:build.

After the release, getsentry/junior must move its pin from v0 (b730732) to the new release.

via David Cramer.

--

View Junior Session [Sentry]

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions