Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement a new image comparison function #19166

Merged
merged 5 commits into from
Dec 2, 2022

Conversation

aslushnikov
Copy link
Contributor

@aslushnikov aslushnikov commented Nov 30, 2022

This patch implements a new image comparison function, codenamed
"ssim-cie94". The goal of the new comparison function is to cancel out
browser non-determenistic rendering.

To use the new comparison function:

await expect(page).toHaveScreenshot({
  comparator: 'ssim-cie94',
});

As of Nov 30, 2022, we identified the following sources of
non-determenistic rendering for Chromium:

  • Anti-aliasing for certain shapes might be different due to the
    way skia rasterizes certain shapes.
  • Color blending might be different on x86 and aarch64 architectures.

The new function employs a few heuristics to fight these
differences.

Consider two non-equal image pixels (r1, g1, b1) and (r2, g2, b2):

  1. If the CIE94 metric is less then 1.0, then we consider these pixels
    EQUAL. (The value 1.0 is the just-noticeable difference for
    CIE94.). Otherwise, proceed to next step.
  2. If all the 8 neighbors of the first pixel match its color, or
    if the 8 neighbors of the second pixel match its color, then these
    pixels are DIFFERENT. (In case of anti-aliasing, some of the
    direct neighbors have to be blended up or down.) Otherwise, proceed
    to next step.
  3. If SSIM in some locality around the different pixels is more than
    0.99, then consider this pixels to be EQUAL. Otherwise, mark them
    as DIFFERENT. (Local SSIM for anti-aliased pixels turns out to be
    very close to 1.0).

@pastelsky
Copy link
Contributor

This is amazing! It might be worth also exploring multi-scale SSIM —
https://ece.uwaterloo.ca/~z70wang/publications/msssim.pdf
and ssimulacra2 —
https://github.com/cloudinary/ssimulacra2

as further improvements to SSIM.

@aslushnikov aslushnikov force-pushed the introduce-image-tools branch 2 times, most recently from 68c1972 to f9893b1 Compare November 30, 2022 21:17
@aslushnikov
Copy link
Contributor Author

@pastelsky thank you for the recommendation! I'm familiar with MS-SSIM, but we don't need it here since we're only inspecting a small locality of the image around the anti-aliased pixel.

I haven't seen ssimulacra2 before - thank you for pointing it out. I'll explore it.

This patch implements a new image comparison function, codenamed
'ssim_v1'. The goal of the new comparison function is to cancel out
browser non-determenistic rendering.

To use the new comparison function:

```ts
await expect(page).toHaveScreenshot({
  comparator: 'ssim_v1',
});
```

As of Nov 30, 2022, we identified the following sources of
non-determenistic rendering for Chromium:
- Anti-aliasing for certain shapes might be different due to the
  way skia rasterizes certain shapes.
- Color blending might be different on `x86` and `aarch64` architectures.

The new function employes a few heuristics to fight these
differences.

Consider two non-equal image pixels `(r1, g1, b1)` and `(r2, g2, b2)`:
1. If the [CIE94] metric is less then 1.0, then we consider these pixels
   **EQUAL**. (The value `1.0` is the [just-noticeable difference] for
   [CIE94].). Otherwise, proceed to next step.
1. If all the 8 neighbours of the first pixel match its color, or
   if the 8 neighbours of the second pixel match its color, then these
   pixels are **DIFFERENT**. (In case of anti-aliasing, some of the
   direct neighbours have to be blended up or down.) Otherwise, proceed
   to next step.
1. If SSIM in some locality around the different pixels is more than
   0.99, then consider this pixels to be **EQUAL**. Otherwise, mark them
   as **DIFFERENT**. (Local SSIM for anti-aliased pixels turns out to be
   very close to 1.0).

[CIE94]: https://en.wikipedia.org/wiki/Color_difference#CIE94
[just-noticeable difference]: https://en.wikipedia.org/wiki/Just-noticeable_difference
@aslushnikov aslushnikov merged commit 6d82460 into microsoft:main Dec 2, 2022
@aslushnikov aslushnikov deleted the introduce-image-tools branch December 2, 2022 23:22
@kolodny
Copy link

kolodny commented Jan 26, 2023

Was this released in 1.30.0? I'm still not seeing the comparator option after pulling in the latest stable release

@Jovan27
Copy link

Jovan27 commented Mar 29, 2023

@aslushnikov Can we get this included in some of the newer alpha versions or it's going to be available only with @playwright/test@1.29.0-alpha-dec-9-2022 ?

@aslushnikov
Copy link
Contributor Author

@Jovan27 it is available as a _comparator option in the latest 1.32 version. It would help a ton if you can let us know if it works / doesn't work for you.

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.

5 participants