@mizchi/vlmkit-core 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/a11y-semantic.d.mts +34 -0
- package/dist/a11y-semantic.mjs +261 -0
- package/dist/authored-style-capture.d.mts +42 -0
- package/dist/authored-style-capture.mjs +117 -0
- package/dist/authored-style-diff.d.mts +15 -0
- package/dist/authored-style-diff.mjs +16 -0
- package/dist/cli-args.d.mts +11 -0
- package/dist/cli-args.mjs +21 -0
- package/dist/cli-error.d.mts +4 -0
- package/dist/cli-error.mjs +59 -0
- package/dist/computed-style-capture.d.mts +41 -0
- package/dist/computed-style-capture.mjs +589 -0
- package/dist/computed-style-diff.d.mts +83 -0
- package/dist/computed-style-diff.mjs +123 -0
- package/dist/diff-regions.d.mts +8 -0
- package/dist/diff-regions.mjs +52 -0
- package/dist/dom-equivalence.d.mts +51 -0
- package/dist/dom-equivalence.mjs +91 -0
- package/dist/dom-position-styles.d.mts +166 -0
- package/dist/dom-position-styles.mjs +296 -0
- package/dist/element-compare.d.mts +71 -0
- package/dist/element-compare.mjs +269 -0
- package/dist/heatmap-regions.d.mts +57 -0
- package/dist/heatmap-regions.mjs +158 -0
- package/dist/heatmap.d.mts +86 -0
- package/dist/heatmap.mjs +510 -0
- package/dist/image-resize.d.mts +34 -0
- package/dist/image-resize.mjs +92 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +14 -0
- package/dist/mask.d.mts +18 -0
- package/dist/mask.mjs +27 -0
- package/dist/png-utils.d.mts +30 -0
- package/dist/png-utils.mjs +80 -0
- package/dist/region-shift.d.mts +20 -0
- package/dist/region-shift.mjs +128 -0
- package/dist/run-ledger.d.mts +14 -0
- package/dist/run-ledger.mjs +31 -0
- package/dist/terminal-colors.d.mts +12 -0
- package/dist/terminal-colors.mjs +14 -0
- package/dist/text-rows.d.mts +121 -0
- package/dist/text-rows.mjs +246 -0
- package/{src/types.ts → dist/types.d.mts} +88 -206
- package/dist/types.mjs +1 -0
- package/package.json +17 -5
- package/src/a11y-semantic.ts +0 -384
- package/src/authored-style-capture.ts +0 -185
- package/src/authored-style-diff.ts +0 -32
- package/src/cli-args.ts +0 -29
- package/src/cli-error.ts +0 -85
- package/src/computed-style-capture.ts +0 -717
- package/src/computed-style-diff.ts +0 -192
- package/src/diff-regions.ts +0 -70
- package/src/dom-equivalence.ts +0 -158
- package/src/dom-position-styles.ts +0 -457
- package/src/element-compare.ts +0 -354
- package/src/heatmap-regions.ts +0 -245
- package/src/heatmap.ts +0 -670
- package/src/image-resize.ts +0 -112
- package/src/index.ts +0 -105
- package/src/mask.ts +0 -41
- package/src/png-utils.ts +0 -86
- package/src/region-shift.ts +0 -173
- package/src/terminal-colors.ts +0 -13
- package/src/text-rows.ts +0 -412
package/src/heatmap.ts
DELETED
|
@@ -1,670 +0,0 @@
|
|
|
1
|
-
import type { VrtDiff, VrtSnapshot, DiffRegion, DiffRegionType, DiffReport, ShiftRegion, DiffRegionColor, DiffRegionColorSample, DiffRegionColorPair } from "./types.ts";
|
|
2
|
-
import { type PngData, cropImage, decodePng, encodePng } from "./png-utils.ts";
|
|
3
|
-
import { estimateRegionShift } from "./region-shift.ts";
|
|
4
|
-
|
|
5
|
-
// ---- Shared diff pipeline ----
|
|
6
|
-
|
|
7
|
-
interface PixelDiffResult {
|
|
8
|
-
diffOutput: Uint8Array;
|
|
9
|
-
width: number;
|
|
10
|
-
height: number;
|
|
11
|
-
diffPixels: number;
|
|
12
|
-
totalPixels: number;
|
|
13
|
-
threshold: number;
|
|
14
|
-
resizedBaseline: PngData;
|
|
15
|
-
resizedCurrent: PngData;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async function runPixelDiff(
|
|
19
|
-
baselinePath: string,
|
|
20
|
-
screenshotPath: string,
|
|
21
|
-
testId: string,
|
|
22
|
-
opts: { threshold?: number; outputDir?: string; skipHeatmap?: boolean },
|
|
23
|
-
): Promise<PixelDiffResult & { heatmapPath?: string }> {
|
|
24
|
-
const pixelmatch = (await import("pixelmatch")).default;
|
|
25
|
-
const baseline = await decodePng(baselinePath);
|
|
26
|
-
const current = await decodePng(screenshotPath);
|
|
27
|
-
|
|
28
|
-
let resizedBaseline = baseline;
|
|
29
|
-
let resizedCurrent = current;
|
|
30
|
-
let overflowPixels = 0;
|
|
31
|
-
|
|
32
|
-
if (baseline.width !== current.width || baseline.height !== current.height) {
|
|
33
|
-
const commonW = Math.min(baseline.width, current.width);
|
|
34
|
-
const commonH = Math.min(baseline.height, current.height);
|
|
35
|
-
overflowPixels =
|
|
36
|
-
Math.max(baseline.width, current.width) * Math.max(baseline.height, current.height)
|
|
37
|
-
- commonW * commonH;
|
|
38
|
-
resizedBaseline = cropImage(baseline, commonW, commonH);
|
|
39
|
-
resizedCurrent = cropImage(current, commonW, commonH);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const width = resizedBaseline.width;
|
|
43
|
-
const height = resizedBaseline.height;
|
|
44
|
-
const totalPixels = width * height + overflowPixels;
|
|
45
|
-
const diffOutput = new Uint8Array(width * height * 4);
|
|
46
|
-
const threshold = opts.threshold ?? 0.1;
|
|
47
|
-
|
|
48
|
-
const diffPixels = overflowPixels + pixelmatch(
|
|
49
|
-
resizedBaseline.data, resizedCurrent.data, diffOutput, width, height, { threshold },
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
let heatmapPath: string | undefined;
|
|
53
|
-
if (opts.outputDir && diffPixels > 0 && !opts.skipHeatmap) {
|
|
54
|
-
const safeName = testId.replace(/[/\\:]/g, "_");
|
|
55
|
-
heatmapPath = `${opts.outputDir}/${safeName}_heatmap.png`;
|
|
56
|
-
await encodePng(heatmapPath, { width, height, data: diffOutput });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return { diffOutput, width, height, diffPixels, totalPixels, threshold, resizedBaseline, resizedCurrent, heatmapPath };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// ---- Public API ----
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Compare two screenshots pixel-by-pixel and generate a diff heatmap.
|
|
66
|
-
*/
|
|
67
|
-
export async function compareScreenshots(
|
|
68
|
-
snapshot: VrtSnapshot,
|
|
69
|
-
opts: {
|
|
70
|
-
threshold?: number;
|
|
71
|
-
outputDir?: string;
|
|
72
|
-
skipHeatmap?: boolean;
|
|
73
|
-
} = {}
|
|
74
|
-
): Promise<VrtDiff | null> {
|
|
75
|
-
if (!snapshot.baselinePath) return null;
|
|
76
|
-
|
|
77
|
-
const r = await runPixelDiff(snapshot.baselinePath, snapshot.screenshotPath, snapshot.testId, opts);
|
|
78
|
-
const regions = detectDiffRegions(r.diffOutput, r.width, r.height);
|
|
79
|
-
attachRegionColorSamples(regions, r.resizedBaseline, r.resizedCurrent);
|
|
80
|
-
attachRegionShiftEstimates(regions, r.resizedBaseline, r.resizedCurrent);
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
snapshot,
|
|
84
|
-
diffPixels: r.diffPixels,
|
|
85
|
-
totalPixels: r.totalPixels,
|
|
86
|
-
diffRatio: r.diffPixels / r.totalPixels,
|
|
87
|
-
heatmapPath: r.heatmapPath,
|
|
88
|
-
regions,
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Grid-based diff region detection.
|
|
94
|
-
* Splits image into cells and clusters cells exceeding the diff threshold.
|
|
95
|
-
*/
|
|
96
|
-
function detectDiffRegions(
|
|
97
|
-
diffData: Uint8Array,
|
|
98
|
-
width: number,
|
|
99
|
-
height: number,
|
|
100
|
-
cellSize: number = 32
|
|
101
|
-
): DiffRegion[] {
|
|
102
|
-
const cols = Math.ceil(width / cellSize);
|
|
103
|
-
const rows = Math.ceil(height / cellSize);
|
|
104
|
-
const grid = new Uint32Array(cols * rows);
|
|
105
|
-
|
|
106
|
-
// Count diff pixels per cell
|
|
107
|
-
for (let y = 0; y < height; y++) {
|
|
108
|
-
for (let x = 0; x < width; x++) {
|
|
109
|
-
const idx = (y * width + x) * 4;
|
|
110
|
-
// pixelmatch diff: changed = red (R=255,G=0), unchanged = white (R=255,G=255)
|
|
111
|
-
if (diffData[idx + 1] < 128) {
|
|
112
|
-
const col = Math.floor(x / cellSize);
|
|
113
|
-
const row = Math.floor(y / cellSize);
|
|
114
|
-
grid[row * cols + col]++;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Merge adjacent diff cells into bounding rectangles
|
|
120
|
-
const visited = new Uint8Array(cols * rows);
|
|
121
|
-
const regions: DiffRegion[] = [];
|
|
122
|
-
const minPixels = 4; // noise threshold
|
|
123
|
-
|
|
124
|
-
for (let r = 0; r < rows; r++) {
|
|
125
|
-
for (let c = 0; c < cols; c++) {
|
|
126
|
-
const i = r * cols + c;
|
|
127
|
-
if (visited[i] || grid[i] < minPixels) continue;
|
|
128
|
-
|
|
129
|
-
// BFS cluster detection
|
|
130
|
-
let minC = c,
|
|
131
|
-
maxC = c,
|
|
132
|
-
minR = r,
|
|
133
|
-
maxR = r;
|
|
134
|
-
let totalDiff = 0;
|
|
135
|
-
const queue = [i];
|
|
136
|
-
visited[i] = 1;
|
|
137
|
-
|
|
138
|
-
while (queue.length > 0) {
|
|
139
|
-
const ci = queue.shift()!;
|
|
140
|
-
const cr = Math.floor(ci / cols);
|
|
141
|
-
const cc = ci % cols;
|
|
142
|
-
totalDiff += grid[ci];
|
|
143
|
-
minC = Math.min(minC, cc);
|
|
144
|
-
maxC = Math.max(maxC, cc);
|
|
145
|
-
minR = Math.min(minR, cr);
|
|
146
|
-
maxR = Math.max(maxR, cr);
|
|
147
|
-
|
|
148
|
-
// 4-connected neighbors
|
|
149
|
-
for (const [dr, dc] of [
|
|
150
|
-
[-1, 0],
|
|
151
|
-
[1, 0],
|
|
152
|
-
[0, -1],
|
|
153
|
-
[0, 1],
|
|
154
|
-
]) {
|
|
155
|
-
const nr = cr + dr;
|
|
156
|
-
const nc = cc + dc;
|
|
157
|
-
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols) continue;
|
|
158
|
-
const ni = nr * cols + nc;
|
|
159
|
-
if (!visited[ni] && grid[ni] >= minPixels) {
|
|
160
|
-
visited[ni] = 1;
|
|
161
|
-
queue.push(ni);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const regionWidth = (maxC - minC + 1) * cellSize;
|
|
167
|
-
const regionHeight = (maxR - minR + 1) * cellSize;
|
|
168
|
-
regions.push({
|
|
169
|
-
x: minC * cellSize,
|
|
170
|
-
y: minR * cellSize,
|
|
171
|
-
width: regionWidth,
|
|
172
|
-
height: regionHeight,
|
|
173
|
-
diffPixelCount: totalDiff,
|
|
174
|
-
regionType: classifyRegion(regionWidth, regionHeight, width),
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return regions;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function attachRegionColorSamples(
|
|
183
|
-
regions: DiffRegion[],
|
|
184
|
-
baseline: PngData,
|
|
185
|
-
current: PngData,
|
|
186
|
-
): void {
|
|
187
|
-
for (const region of regions) {
|
|
188
|
-
const sample = sampleRegionColorSample(baseline, current, region);
|
|
189
|
-
if (sample) region.colorSample = sample;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function attachRegionShiftEstimates(
|
|
194
|
-
regions: DiffRegion[],
|
|
195
|
-
baseline: PngData,
|
|
196
|
-
current: PngData,
|
|
197
|
-
): void {
|
|
198
|
-
for (const region of regions) {
|
|
199
|
-
if (region.regionType === "edge") continue;
|
|
200
|
-
const shift = estimateRegionShift(baseline, current, region);
|
|
201
|
-
if (shift) region.shift = shift;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/** Fraction of a region's opaque pixels below which the change counts as
|
|
206
|
-
* "sparse" (thin glyphs, a minority recolor) and a peak pixel is worth
|
|
207
|
-
* surfacing alongside the mean. */
|
|
208
|
-
const SPARSE_CHANGE_RATIO = 0.5;
|
|
209
|
-
/** Per-channel mean delta above which a pixel counts as actually changed,
|
|
210
|
-
* matching `diff region`'s sampleBboxColorPair gate. */
|
|
211
|
-
const CHANGED_PIXEL_DELTA = 1;
|
|
212
|
-
|
|
213
|
-
interface ColorAccumulator {
|
|
214
|
-
br: number;
|
|
215
|
-
bg: number;
|
|
216
|
-
bb: number;
|
|
217
|
-
cr: number;
|
|
218
|
-
cg: number;
|
|
219
|
-
cb: number;
|
|
220
|
-
n: number;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
function makeColorAccumulator(): ColorAccumulator {
|
|
224
|
-
return { br: 0, bg: 0, bb: 0, cr: 0, cg: 0, cb: 0, n: 0 };
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function meanPair(acc: ColorAccumulator): DiffRegionColorPair {
|
|
228
|
-
const baseline = roundColor(acc.br / acc.n, acc.bg / acc.n, acc.bb / acc.n);
|
|
229
|
-
const current = roundColor(acc.cr / acc.n, acc.cg / acc.n, acc.cb / acc.n);
|
|
230
|
-
return { baseline, current, distance: Math.round(rgbDistance(baseline, current)) };
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
function roundColor(r: number, g: number, b: number): DiffRegionColor {
|
|
234
|
-
const ri = Math.round(r);
|
|
235
|
-
const gi = Math.round(g);
|
|
236
|
-
const bi = Math.round(b);
|
|
237
|
-
return { r: ri, g: gi, b: bi, hex: toHex(ri, gi, bi) };
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Sample a region's color pair from the pixels that actually differ.
|
|
242
|
-
*
|
|
243
|
-
* A region-wide median (the old behaviour) reflects the unchanged majority
|
|
244
|
-
* and reads as "no change" on exactly the regions that changed in a minority
|
|
245
|
-
* of their pixels — e.g. a dark background recolor hidden behind dominant
|
|
246
|
-
* white form inputs (A/B v3 draft 10). We accumulate only pixels whose
|
|
247
|
-
* baseline↔current per-channel delta clears CHANGED_PIXEL_DELTA, falling back
|
|
248
|
-
* to every opaque pixel when nothing clears the gate.
|
|
249
|
-
*
|
|
250
|
-
* When the changed pixels are sparse, the mean is still dominated by
|
|
251
|
-
* antialiasing blends, so we additionally surface the single highest-delta
|
|
252
|
-
* pixel pair as `peak` (draft 11).
|
|
253
|
-
*/
|
|
254
|
-
export function sampleRegionColorSample(
|
|
255
|
-
baseline: PngData,
|
|
256
|
-
current: PngData,
|
|
257
|
-
region: { x: number; y: number; width: number; height: number },
|
|
258
|
-
): DiffRegionColorSample | undefined {
|
|
259
|
-
const left = Math.max(0, region.x);
|
|
260
|
-
const top = Math.max(0, region.y);
|
|
261
|
-
const right = Math.min(baseline.width, current.width, region.x + region.width);
|
|
262
|
-
const bottom = Math.min(baseline.height, current.height, region.y + region.height);
|
|
263
|
-
if (right <= left || bottom <= top) return undefined;
|
|
264
|
-
|
|
265
|
-
const all = makeColorAccumulator();
|
|
266
|
-
const changed = makeColorAccumulator();
|
|
267
|
-
let peakDelta = -1;
|
|
268
|
-
let peakBaseline: DiffRegionColor | undefined;
|
|
269
|
-
let peakCurrent: DiffRegionColor | undefined;
|
|
270
|
-
|
|
271
|
-
for (let y = top; y < bottom; y++) {
|
|
272
|
-
for (let x = left; x < right; x++) {
|
|
273
|
-
const bi = (y * baseline.width + x) * 4;
|
|
274
|
-
const ci = (y * current.width + x) * 4;
|
|
275
|
-
if (baseline.data[bi + 3]! === 0 || current.data[ci + 3]! === 0) continue;
|
|
276
|
-
const br = baseline.data[bi]!;
|
|
277
|
-
const bg = baseline.data[bi + 1]!;
|
|
278
|
-
const bb = baseline.data[bi + 2]!;
|
|
279
|
-
const cr = current.data[ci]!;
|
|
280
|
-
const cg = current.data[ci + 1]!;
|
|
281
|
-
const cb = current.data[ci + 2]!;
|
|
282
|
-
all.br += br; all.bg += bg; all.bb += bb;
|
|
283
|
-
all.cr += cr; all.cg += cg; all.cb += cb;
|
|
284
|
-
all.n++;
|
|
285
|
-
|
|
286
|
-
const delta = (Math.abs(br - cr) + Math.abs(bg - cg) + Math.abs(bb - cb)) / 3;
|
|
287
|
-
if (delta > CHANGED_PIXEL_DELTA) {
|
|
288
|
-
changed.br += br; changed.bg += bg; changed.bb += bb;
|
|
289
|
-
changed.cr += cr; changed.cg += cg; changed.cb += cb;
|
|
290
|
-
changed.n++;
|
|
291
|
-
if (delta > peakDelta) {
|
|
292
|
-
peakDelta = delta;
|
|
293
|
-
peakBaseline = { r: br, g: bg, b: bb, hex: toHex(br, bg, bb) };
|
|
294
|
-
peakCurrent = { r: cr, g: cg, b: cb, hex: toHex(cr, cg, cb) };
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
if (all.n === 0) return undefined;
|
|
301
|
-
|
|
302
|
-
const source = changed.n > 0 ? changed : all;
|
|
303
|
-
const sample: DiffRegionColorSample = meanPair(source);
|
|
304
|
-
|
|
305
|
-
// The peak only adds signal when the mean is unreliable — i.e. the change
|
|
306
|
-
// is sparse and the peak names a different color than the averaged-out mean.
|
|
307
|
-
if (
|
|
308
|
-
changed.n > 0
|
|
309
|
-
&& peakBaseline
|
|
310
|
-
&& peakCurrent
|
|
311
|
-
&& changed.n < all.n * SPARSE_CHANGE_RATIO
|
|
312
|
-
&& (peakBaseline.hex !== sample.baseline.hex || peakCurrent.hex !== sample.current.hex)
|
|
313
|
-
) {
|
|
314
|
-
sample.peak = {
|
|
315
|
-
baseline: peakBaseline,
|
|
316
|
-
current: peakCurrent,
|
|
317
|
-
distance: Math.round(rgbDistance(peakBaseline, peakCurrent)),
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
return sample;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
function toHex(r: number, g: number, b: number): string {
|
|
325
|
-
const hex = (value: number) => value.toString(16).padStart(2, "0");
|
|
326
|
-
return `#${hex(r)}${hex(g)}${hex(b)}`;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
function rgbDistance(a: DiffRegionColor, b: DiffRegionColor): number {
|
|
330
|
-
return Math.sqrt((a.r - b.r) ** 2 + (a.g - b.g) ** 2 + (a.b - b.b) ** 2);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Classify a diff region based on its shape relative to the image width.
|
|
335
|
-
* - "edge": thin lines (height <= 2 or width <= 2)
|
|
336
|
-
* - "shift": wide horizontal bands (width/height > 3 and width > 80% image width)
|
|
337
|
-
* - "content": localized changes
|
|
338
|
-
*/
|
|
339
|
-
function classifyRegion(regionWidth: number, regionHeight: number, imageWidth: number): DiffRegionType {
|
|
340
|
-
if (regionHeight <= 2 || regionWidth <= 2) return "edge";
|
|
341
|
-
if (regionWidth / regionHeight > 3 && regionWidth > imageWidth * 0.8) return "shift";
|
|
342
|
-
return "content";
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Compute luminance profile (average brightness per row).
|
|
347
|
-
*/
|
|
348
|
-
function luminanceProfile(data: Uint8Array, width: number, height: number): Float64Array {
|
|
349
|
-
const profile = new Float64Array(height);
|
|
350
|
-
for (let y = 0; y < height; y++) {
|
|
351
|
-
let sum = 0;
|
|
352
|
-
for (let x = 0; x < width; x++) {
|
|
353
|
-
const idx = (y * width + x) * 4;
|
|
354
|
-
sum += 0.299 * data[idx] + 0.587 * data[idx + 1] + 0.114 * data[idx + 2];
|
|
355
|
-
}
|
|
356
|
-
profile[y] = sum / width;
|
|
357
|
-
}
|
|
358
|
-
return profile;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Detect global vertical shift between two images using cross-correlation.
|
|
363
|
-
* Returns the offset in pixels (positive = img2 shifted down).
|
|
364
|
-
*/
|
|
365
|
-
function detectGlobalShift(
|
|
366
|
-
baseline: { data: Uint8Array; width: number; height: number },
|
|
367
|
-
current: { data: Uint8Array; width: number; height: number },
|
|
368
|
-
maxShift?: number,
|
|
369
|
-
): number {
|
|
370
|
-
const height = Math.min(baseline.height, current.height);
|
|
371
|
-
const width = Math.min(baseline.width, current.width);
|
|
372
|
-
const limit = maxShift ?? Math.min(Math.floor(height / 4), 500);
|
|
373
|
-
|
|
374
|
-
const profile1 = luminanceProfile(baseline.data, width, height);
|
|
375
|
-
const profile2 = luminanceProfile(current.data, width, height);
|
|
376
|
-
|
|
377
|
-
let bestOffset = 0;
|
|
378
|
-
let bestCorr = -Infinity;
|
|
379
|
-
|
|
380
|
-
for (let offset = -limit; offset <= limit; offset++) {
|
|
381
|
-
let sum = 0;
|
|
382
|
-
let count = 0;
|
|
383
|
-
for (let y = 0; y < height; y++) {
|
|
384
|
-
const y2 = y + offset;
|
|
385
|
-
if (y2 >= 0 && y2 < height) {
|
|
386
|
-
sum += profile1[y] * profile2[y2];
|
|
387
|
-
count++;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
const corr = count > 0 ? sum / count : 0;
|
|
391
|
-
if (corr > bestCorr) {
|
|
392
|
-
bestCorr = corr;
|
|
393
|
-
bestOffset = offset;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
return bestOffset;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Per-band shift detection — split the image into horizontal bands and run
|
|
401
|
-
* the same cross-correlation per band. Lets us report "section A shifted
|
|
402
|
-
* +8px, section B shifted +20px" instead of a single global average that
|
|
403
|
-
* loses localization.
|
|
404
|
-
*
|
|
405
|
-
* Bands with low luminance variance (e.g. a flat white footer) are dropped
|
|
406
|
-
* because cross-correlation can't lock onto them and the result is noise.
|
|
407
|
-
*
|
|
408
|
-
* Exported for unit testing.
|
|
409
|
-
*/
|
|
410
|
-
export function detectBandShifts(
|
|
411
|
-
baseline: { data: Uint8Array; width: number; height: number },
|
|
412
|
-
current: { data: Uint8Array; width: number; height: number },
|
|
413
|
-
options: { bandHeight?: number; minConfidence?: number; maxShift?: number } = {},
|
|
414
|
-
): ShiftRegion[] {
|
|
415
|
-
const height = Math.min(baseline.height, current.height);
|
|
416
|
-
const width = Math.min(baseline.width, current.width);
|
|
417
|
-
const bandHeight = Math.max(50, options.bandHeight ?? 240);
|
|
418
|
-
const minConfidence = options.minConfidence ?? 0.02;
|
|
419
|
-
const limit = options.maxShift ?? Math.min(Math.floor(bandHeight / 2), 400);
|
|
420
|
-
|
|
421
|
-
if (height < bandHeight * 2) return [];
|
|
422
|
-
|
|
423
|
-
const profile1 = luminanceProfile(baseline.data, width, height);
|
|
424
|
-
const profile2 = luminanceProfile(current.data, width, height);
|
|
425
|
-
|
|
426
|
-
const regions: ShiftRegion[] = [];
|
|
427
|
-
const bandCount = Math.floor(height / bandHeight);
|
|
428
|
-
for (let b = 0; b < bandCount; b++) {
|
|
429
|
-
const yStart = b * bandHeight;
|
|
430
|
-
const yEnd = b === bandCount - 1 ? height : yStart + bandHeight;
|
|
431
|
-
|
|
432
|
-
// Variance check — flat bands have no signal.
|
|
433
|
-
let mean = 0;
|
|
434
|
-
for (let y = yStart; y < yEnd; y++) mean += profile1[y]!;
|
|
435
|
-
mean /= (yEnd - yStart);
|
|
436
|
-
let variance = 0;
|
|
437
|
-
for (let y = yStart; y < yEnd; y++) {
|
|
438
|
-
const d = profile1[y]! - mean;
|
|
439
|
-
variance += d * d;
|
|
440
|
-
}
|
|
441
|
-
variance /= (yEnd - yStart);
|
|
442
|
-
if (variance < 5) continue; // empirically: flat background
|
|
443
|
-
|
|
444
|
-
// Mean-subtract both profiles so the cross-correlation magnitude scales
|
|
445
|
-
// with actual structure rather than background brightness.
|
|
446
|
-
let mean2 = 0;
|
|
447
|
-
for (let y = yStart; y < yEnd; y++) mean2 += profile2[y]!;
|
|
448
|
-
mean2 /= (yEnd - yStart);
|
|
449
|
-
|
|
450
|
-
let bestOffset = 0;
|
|
451
|
-
let bestCorr = -Infinity;
|
|
452
|
-
let secondBestCorr = -Infinity;
|
|
453
|
-
let zeroOffsetCorr = 0;
|
|
454
|
-
for (let offset = -limit; offset <= limit; offset++) {
|
|
455
|
-
let sum = 0;
|
|
456
|
-
let count = 0;
|
|
457
|
-
for (let y = yStart; y < yEnd; y++) {
|
|
458
|
-
const y2 = y + offset;
|
|
459
|
-
if (y2 >= 0 && y2 < height) {
|
|
460
|
-
sum += (profile1[y]! - mean) * (profile2[y2]! - mean2);
|
|
461
|
-
count++;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
const corr = count > 0 ? sum / count : 0;
|
|
465
|
-
if (offset === 0) zeroOffsetCorr = corr;
|
|
466
|
-
if (corr > bestCorr) {
|
|
467
|
-
secondBestCorr = bestCorr;
|
|
468
|
-
bestCorr = corr;
|
|
469
|
-
bestOffset = offset;
|
|
470
|
-
} else if (corr > secondBestCorr) {
|
|
471
|
-
secondBestCorr = corr;
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
// Confidence = peak sharpness relative to the second-best alignment.
|
|
476
|
-
// A clean shift has best ≫ second-best; noise has best ≈ second-best.
|
|
477
|
-
const confidence = bestCorr > 0 && secondBestCorr > 0
|
|
478
|
-
? Math.min(1, Math.max(0, (bestCorr - secondBestCorr) / bestCorr))
|
|
479
|
-
: 0;
|
|
480
|
-
|
|
481
|
-
if (bestOffset !== 0 && bestCorr > zeroOffsetCorr && confidence >= minConfidence) {
|
|
482
|
-
regions.push({ yStart, yEnd, shift: bestOffset, confidence: Number(confidence.toFixed(3)) });
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
return regions;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
/**
|
|
490
|
-
* Count diff pixels after compensating for vertical shift.
|
|
491
|
-
*/
|
|
492
|
-
function compensatedDiffCount(
|
|
493
|
-
baselineData: Uint8Array,
|
|
494
|
-
currentData: Uint8Array,
|
|
495
|
-
width: number,
|
|
496
|
-
height: number,
|
|
497
|
-
shift: number,
|
|
498
|
-
threshold: number,
|
|
499
|
-
): number {
|
|
500
|
-
let count = 0;
|
|
501
|
-
for (let y = 0; y < height; y++) {
|
|
502
|
-
const y2 = y + shift;
|
|
503
|
-
if (y2 < 0 || y2 >= height) {
|
|
504
|
-
count += width;
|
|
505
|
-
continue;
|
|
506
|
-
}
|
|
507
|
-
for (let x = 0; x < width; x++) {
|
|
508
|
-
const idx1 = (y * width + x) * 4;
|
|
509
|
-
const idx2 = (y2 * width + x) * 4;
|
|
510
|
-
const dr = Math.abs(baselineData[idx1] - currentData[idx2]);
|
|
511
|
-
const dg = Math.abs(baselineData[idx1 + 1] - currentData[idx2 + 1]);
|
|
512
|
-
const db = Math.abs(baselineData[idx1 + 2] - currentData[idx2 + 2]);
|
|
513
|
-
if ((dr + dg + db) / 3 > threshold * 255) count++;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
return count;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* Generate a compact 10x10 ASCII heatmap of diff distribution.
|
|
521
|
-
*/
|
|
522
|
-
function generateCompact(
|
|
523
|
-
diffData: Uint8Array,
|
|
524
|
-
width: number,
|
|
525
|
-
height: number,
|
|
526
|
-
diffPixels: number,
|
|
527
|
-
totalPixels: number,
|
|
528
|
-
regions: DiffRegion[],
|
|
529
|
-
): string {
|
|
530
|
-
const gridSize = 10;
|
|
531
|
-
const cellW = Math.ceil(width / gridSize);
|
|
532
|
-
const cellH = Math.ceil(height / gridSize);
|
|
533
|
-
const grid: number[][] = Array.from({ length: gridSize }, () => new Array(gridSize).fill(0));
|
|
534
|
-
|
|
535
|
-
for (let y = 0; y < height; y++) {
|
|
536
|
-
for (let x = 0; x < width; x++) {
|
|
537
|
-
const idx = (y * width + x) * 4;
|
|
538
|
-
// pixelmatch diff output: changed pixels are red (R=255,G=0,B=0),
|
|
539
|
-
// unchanged pixels are white (R=255,G=255,B=255). Check G channel.
|
|
540
|
-
if (diffData[idx + 1] < 128) {
|
|
541
|
-
const gx = Math.min(Math.floor(x / cellW), gridSize - 1);
|
|
542
|
-
const gy = Math.min(Math.floor(y / cellH), gridSize - 1);
|
|
543
|
-
grid[gy][gx]++;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
const matchPct = ((1 - diffPixels / totalPixels) * 100).toFixed(0);
|
|
549
|
-
const lines = [`diff:${diffPixels}/${totalPixels}(${matchPct}%match)`];
|
|
550
|
-
const cellTotal = cellW * cellH;
|
|
551
|
-
for (let gy = 0; gy < gridSize; gy++) {
|
|
552
|
-
lines.push(grid[gy].map((v) => (v > cellTotal * 0.05 ? "X" : ".")).join(""));
|
|
553
|
-
}
|
|
554
|
-
if (regions.length > 0) {
|
|
555
|
-
const regionStrs = regions.map((r) => `${r.regionType || "content"}:${r.x},${r.y},${r.width}x${r.height}`);
|
|
556
|
-
lines.push(`regions:${regionStrs.join(";")}`);
|
|
557
|
-
}
|
|
558
|
-
return lines.join("\n");
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* Generate a comprehensive diff report including clustering, shift detection,
|
|
563
|
-
* and region classification. Compatible with mizchi/pixelmatch 0.5.0 DiffReport format.
|
|
564
|
-
*/
|
|
565
|
-
export async function generateDiffReport(
|
|
566
|
-
snapshot: VrtSnapshot,
|
|
567
|
-
opts: {
|
|
568
|
-
threshold?: number;
|
|
569
|
-
outputDir?: string;
|
|
570
|
-
skipHeatmap?: boolean;
|
|
571
|
-
detectShift?: boolean;
|
|
572
|
-
} = {},
|
|
573
|
-
): Promise<DiffReport | null> {
|
|
574
|
-
if (!snapshot.baselinePath) return null;
|
|
575
|
-
|
|
576
|
-
const r = await runPixelDiff(snapshot.baselinePath, snapshot.screenshotPath, snapshot.testId, opts);
|
|
577
|
-
const regions = detectDiffRegions(r.diffOutput, r.width, r.height);
|
|
578
|
-
|
|
579
|
-
// Shift detection
|
|
580
|
-
let globalShift = 0;
|
|
581
|
-
let shiftRegions: ShiftRegion[] = [];
|
|
582
|
-
let compensated = r.diffPixels;
|
|
583
|
-
|
|
584
|
-
if (opts.detectShift !== false && r.height > 4) {
|
|
585
|
-
globalShift = detectGlobalShift(r.resizedBaseline, r.resizedCurrent);
|
|
586
|
-
if (globalShift !== 0) {
|
|
587
|
-
compensated = compensatedDiffCount(
|
|
588
|
-
r.resizedBaseline.data, r.resizedCurrent.data,
|
|
589
|
-
r.width, r.height, globalShift, r.threshold,
|
|
590
|
-
);
|
|
591
|
-
// Prefer per-band shifts when they reveal locally-varying offsets.
|
|
592
|
-
// Falls back to the global single-band region if no band locks on.
|
|
593
|
-
const bands = detectBandShifts(r.resizedBaseline, r.resizedCurrent);
|
|
594
|
-
shiftRegions = bands.length > 0
|
|
595
|
-
? bands
|
|
596
|
-
: [{ yStart: 0, yEnd: r.height, shift: globalShift }];
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
const shiftOnly = regions.length > 0 && regions.every((rg) => rg.regionType === "shift" || rg.regionType === "edge");
|
|
601
|
-
const contentChangeCount = regions.filter((rg) => rg.regionType === "content").length;
|
|
602
|
-
const compact = generateCompact(r.diffOutput, r.width, r.height, r.diffPixels, r.totalPixels, regions);
|
|
603
|
-
|
|
604
|
-
return {
|
|
605
|
-
diffPixels: r.diffPixels,
|
|
606
|
-
totalPixels: r.totalPixels,
|
|
607
|
-
diffRatio: r.diffPixels / r.totalPixels,
|
|
608
|
-
regions,
|
|
609
|
-
shiftOnly,
|
|
610
|
-
contentChangeCount,
|
|
611
|
-
globalShift,
|
|
612
|
-
shiftRegions,
|
|
613
|
-
compensatedDiffCount: compensated,
|
|
614
|
-
compact,
|
|
615
|
-
};
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
/**
|
|
619
|
-
* Whiteout detection: checks if most of the image is white (or a single color).
|
|
620
|
-
*/
|
|
621
|
-
export function detectWhiteout(
|
|
622
|
-
data: PngData,
|
|
623
|
-
opts: { threshold?: number } = {}
|
|
624
|
-
): { isWhiteout: boolean; whiteRatio: number } {
|
|
625
|
-
const threshold = opts.threshold ?? 0.95;
|
|
626
|
-
const { width, height, data: pixels } = data;
|
|
627
|
-
const total = width * height;
|
|
628
|
-
let whiteCount = 0;
|
|
629
|
-
|
|
630
|
-
for (let i = 0; i < total; i++) {
|
|
631
|
-
const offset = i * 4;
|
|
632
|
-
const r = pixels[offset];
|
|
633
|
-
const g = pixels[offset + 1];
|
|
634
|
-
const b = pixels[offset + 2];
|
|
635
|
-
// Treat (250+, 250+, 250+) as white
|
|
636
|
-
if (r >= 250 && g >= 250 && b >= 250) {
|
|
637
|
-
whiteCount++;
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
const whiteRatio = whiteCount / total;
|
|
642
|
-
return { isWhiteout: whiteRatio >= threshold, whiteRatio };
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
* Empty content detection: checks if the image has low entropy.
|
|
647
|
-
*/
|
|
648
|
-
export function detectEmptyContent(
|
|
649
|
-
data: PngData,
|
|
650
|
-
opts: { threshold?: number } = {}
|
|
651
|
-
): { isEmpty: boolean; uniqueColors: number } {
|
|
652
|
-
const threshold = opts.threshold ?? 8;
|
|
653
|
-
const colorSet = new Set<number>();
|
|
654
|
-
const { width, height, data: pixels } = data;
|
|
655
|
-
const total = width * height;
|
|
656
|
-
|
|
657
|
-
// Sampling with stride (full scan is too expensive)
|
|
658
|
-
const stride = Math.max(1, Math.floor(total / 10000));
|
|
659
|
-
for (let i = 0; i < total; i += stride) {
|
|
660
|
-
const offset = i * 4;
|
|
661
|
-
const color =
|
|
662
|
-
(pixels[offset] << 16) | (pixels[offset + 1] << 8) | pixels[offset + 2];
|
|
663
|
-
colorSet.add(color);
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
return {
|
|
667
|
-
isEmpty: colorSet.size <= threshold,
|
|
668
|
-
uniqueColors: colorSet.size,
|
|
669
|
-
};
|
|
670
|
-
}
|