(leftPng: Buffer, rightPng: Buffer)
| 360 | } |
| 361 | |
| 362 | async 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 | |
| 380 | async function detectVisualInstability(frames: SwitchFrameSample[]) { |
| 381 | const anchorIndex = frames.findIndex((frame) => frame.containsTargetMarker); |
no test coverage detected