MCPcopy
hub / github.com/coder/mux / computeDiffRatio

Function computeDiffRatio

scripts/reproWorkspaceSwitchTearWeb.ts:362–378  ·  view source on GitHub ↗
(leftPng: Buffer, rightPng: Buffer)

Source from the content-addressed store, hash-verified

360}
361
362async function computeDiffRatio(leftPng: Buffer, rightPng: Buffer): Promise<number> {
363 const [left, right] = await Promise.all([
364 sharp(leftPng).ensureAlpha().raw().toBuffer({ resolveWithObject: true }),
365 sharp(rightPng).ensureAlpha().raw().toBuffer({ resolveWithObject: true }),
366 ]);
367 if (left.info.width !== right.info.width || left.info.height !== right.info.height) return 1;
368 let differentPixels = 0;
369 const totalPixels = left.info.width * left.info.height;
370 for (let offset = 0; offset < left.data.length; offset += 4) {
371 const delta =
372 Math.abs(left.data[offset] - right.data[offset]) +
373 Math.abs(left.data[offset + 1] - right.data[offset + 1]) +
374 Math.abs(left.data[offset + 2] - right.data[offset + 2]);
375 if (delta > 30) differentPixels += 1;
376 }
377 return differentPixels / totalPixels;
378}
379
380async function detectVisualInstability(frames: SwitchFrameSample[]) {
381 const anchorIndex = frames.findIndex((frame) => frame.containsTargetMarker);

Callers 1

detectVisualInstabilityFunction · 0.85

Calls 1

absMethod · 0.80

Tested by

no test coverage detected