MCPcopy Create free account
hub / github.com/Snapchat/Valdi / generateDiffImage

Function generateDiffImage

apps/snapshot_tests/src/DiffImage.ts:4–90  ·  view source on GitHub ↗
(baseline: IBitmap, actual: IBitmap, tolerance: number)

Source from the content-addressed store, hash-verified

2import { BitmapAlphaType, BitmapColorType, IBitmap, ImageEncoding } from 'drawing/src/IBitmap';
3
4export function generateDiffImage(baseline: IBitmap, actual: IBitmap, tolerance: number): ArrayBuffer {
5 const baseInfo = baseline.getInfo();
6 const actInfo = actual.getInfo();
7 const width = baseInfo.width;
8 const height = baseInfo.height;
9 const baseRowBytes = baseInfo.rowBytes;
10 const actRowBytes = actInfo.rowBytes;
11 const panelWidth = width * 3;
12 const bytesPerPixel = 4;
13 const diffRowBytes = panelWidth * bytesPerPixel;
14 const buffer = new ArrayBuffer(diffRowBytes * height);
15
16 const diffBitmap = createBitmapWithBuffer(
17 {
18 width: panelWidth,
19 height,
20 colorType: BitmapColorType.RGBA8888,
21 alphaType: BitmapAlphaType.Premul,
22 rowBytes: diffRowBytes,
23 },
24 buffer,
25 );
26
27 try {
28 baseline.accessPixels(baselineView => {
29 actual.accessPixels(actualView => {
30 diffBitmap.accessPixels(diffView => {
31 for (let y = 0; y < height; y++) {
32 for (let x = 0; x < width; x++) {
33 const baseOffset = y * baseRowBytes + x * bytesPerPixel;
34 const actOffset = y * actRowBytes + x * bytesPerPixel;
35 const baseR = baselineView.getUint8(baseOffset);
36 const baseG = baselineView.getUint8(baseOffset + 1);
37 const baseB = baselineView.getUint8(baseOffset + 2);
38 const baseA = baselineView.getUint8(baseOffset + 3);
39 const actR = actualView.getUint8(actOffset);
40 const actG = actualView.getUint8(actOffset + 1);
41 const actB = actualView.getUint8(actOffset + 2);
42 const actA = actualView.getUint8(actOffset + 3);
43
44 const diffRowOffset = y * diffRowBytes;
45
46 // Left panel: baseline
47 const leftOffset = diffRowOffset + x * bytesPerPixel;
48 diffView.setUint8(leftOffset, baseR);
49 diffView.setUint8(leftOffset + 1, baseG);
50 diffView.setUint8(leftOffset + 2, baseB);
51 diffView.setUint8(leftOffset + 3, baseA);
52
53 // Center panel: actual
54 const centerOffset = diffRowOffset + (width + x) * bytesPerPixel;
55 diffView.setUint8(centerOffset, actR);
56 diffView.setUint8(centerOffset + 1, actG);
57 diffView.setUint8(centerOffset + 2, actB);
58 diffView.setUint8(centerOffset + 3, actA);
59
60 // Right panel: diff overlay
61 const rightOffset = diffRowOffset + (width * 2 + x) * bytesPerPixel;

Callers 1

compareSnapshotsFunction · 0.90

Calls 5

createBitmapWithBufferFunction · 0.90
getInfoMethod · 0.65
accessPixelsMethod · 0.65
encodeMethod · 0.65
disposeMethod · 0.65

Tested by

no test coverage detected