fix(ssim-cie94): further tune SSIM-CIE94 to fight wk artifacts (#19370)
This patch adds a grid border around the image so that the SSIM resolution doesn't drop for the border pixels. We also add a test with WebKit rendering artifacts to make sure new approach helps to fight this.
This commit is contained in:
parent
465278a54f
commit
526bc3f252
|
|
@ -37,22 +37,34 @@ export function compare(actual: Buffer, expected: Buffer, diff: Buffer, width: n
|
|||
const {
|
||||
maxColorDeltaE94
|
||||
} = options;
|
||||
const [r1, g1, b1] = ImageChannel.intoRGB(width, height, expected);
|
||||
const [r2, g2, b2] = ImageChannel.intoRGB(width, height, actual);
|
||||
|
||||
const drawRedPixel = (x: number, y: number) => drawPixel(width, diff, x, y, 255, 0, 0);
|
||||
const drawYellowPixel = (x: number, y: number) => drawPixel(width, diff, x, y, 255, 255, 0);
|
||||
const paddingSize = Math.max(VARIANCE_WINDOW_RADIUS, SSIM_WINDOW_RADIUS);
|
||||
const paddingColorEven = [255, 0, 255];
|
||||
const paddingColorOdd = [0, 255, 0];
|
||||
const [r1, g1, b1] = ImageChannel.intoRGB(width, height, expected, {
|
||||
paddingSize,
|
||||
paddingColorEven,
|
||||
paddingColorOdd,
|
||||
});
|
||||
const [r2, g2, b2] = ImageChannel.intoRGB(width, height, actual, {
|
||||
paddingSize,
|
||||
paddingColorEven,
|
||||
paddingColorOdd,
|
||||
});
|
||||
|
||||
const drawRedPixel = (x: number, y: number) => drawPixel(width, diff, x - paddingSize, y - paddingSize, 255, 0, 0);
|
||||
const drawYellowPixel = (x: number, y: number) => drawPixel(width, diff, x - paddingSize, y - paddingSize, 255, 255, 0);
|
||||
const drawGrayPixel = (x: number, y: number) => {
|
||||
const gray = rgb2gray(r1.get(x, y), g1.get(x, y), b1.get(x, y));
|
||||
const value = blendWithWhite(gray, 0.1);
|
||||
drawPixel(width, diff, x, y, value, value, value);
|
||||
drawPixel(width, diff, x - paddingSize, y - paddingSize, value, value, value);
|
||||
};
|
||||
|
||||
let fastR, fastG, fastB;
|
||||
|
||||
let diffCount = 0;
|
||||
for (let y = 0; y < height; ++y){
|
||||
for (let x = 0; x < width; ++x) {
|
||||
for (let y = paddingSize; y < r1.height - paddingSize; ++y){
|
||||
for (let x = paddingSize; x < r1.width - paddingSize; ++x) {
|
||||
// Fast-path: equal pixels.
|
||||
if (r1.get(x, y) === r2.get(x, y) && g1.get(x, y) === g2.get(x, y) && b1.get(x, y) === b2.get(x, y)) {
|
||||
drawGrayPixel(x, y);
|
||||
|
|
|
|||
|
|
@ -16,29 +16,49 @@
|
|||
|
||||
import { blendWithWhite } from './colorUtils';
|
||||
|
||||
export type PaddingOptions = {
|
||||
paddingSize?: number,
|
||||
paddingColorOdd?: number[],
|
||||
paddingColorEven?: number[],
|
||||
};
|
||||
|
||||
export class ImageChannel {
|
||||
data: Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
|
||||
static intoRGB(width: number, height: number, data: Buffer): ImageChannel[] {
|
||||
const r = new Uint8Array(width * height);
|
||||
const g = new Uint8Array(width * height);
|
||||
const b = new Uint8Array(width * height);
|
||||
for (let y = 0; y < height; ++y) {
|
||||
for (let x = 0; x < width; ++x) {
|
||||
const index = y * width + x;
|
||||
const offset = index * 4;
|
||||
static intoRGB(width: number, height: number, data: Buffer, options: PaddingOptions = {}): ImageChannel[] {
|
||||
const {
|
||||
paddingSize = 0,
|
||||
paddingColorOdd = [255, 0, 255],
|
||||
paddingColorEven = [0, 255, 0],
|
||||
} = options;
|
||||
const newWidth = width + 2 * paddingSize;
|
||||
const newHeight = height + 2 * paddingSize;
|
||||
const r = new Uint8Array(newWidth * newHeight);
|
||||
const g = new Uint8Array(newWidth * newHeight);
|
||||
const b = new Uint8Array(newWidth * newHeight);
|
||||
for (let y = 0; y < newHeight; ++y) {
|
||||
for (let x = 0; x < newWidth; ++x) {
|
||||
const index = y * newWidth + x;
|
||||
if (y >= paddingSize && y < newHeight - paddingSize && x >= paddingSize && x < newWidth - paddingSize) {
|
||||
const offset = ((y - paddingSize) * width + (x - paddingSize)) * 4;
|
||||
const alpha = data[offset + 3] === 255 ? 1 : data[offset + 3] / 255;
|
||||
r[index] = blendWithWhite(data[offset], alpha);
|
||||
g[index] = blendWithWhite(data[offset + 1], alpha);
|
||||
b[index] = blendWithWhite(data[offset + 2], alpha);
|
||||
} else {
|
||||
const color = (y + x) % 2 === 0 ? paddingColorEven : paddingColorOdd;
|
||||
r[index] = color[0];
|
||||
g[index] = color[1];
|
||||
b[index] = color[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
return [
|
||||
new ImageChannel(width, height, r),
|
||||
new ImageChannel(width, height, g),
|
||||
new ImageChannel(width, height, b),
|
||||
new ImageChannel(newWidth, newHeight, r),
|
||||
new ImageChannel(newWidth, newHeight, g),
|
||||
new ImageChannel(newWidth, newHeight, b),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
Binary file not shown.
|
After Width: | Height: | Size: 2.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.1 KiB |
Loading…
Reference in a new issue