@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.
Files changed (65) hide show
  1. package/dist/a11y-semantic.d.mts +34 -0
  2. package/dist/a11y-semantic.mjs +261 -0
  3. package/dist/authored-style-capture.d.mts +42 -0
  4. package/dist/authored-style-capture.mjs +117 -0
  5. package/dist/authored-style-diff.d.mts +15 -0
  6. package/dist/authored-style-diff.mjs +16 -0
  7. package/dist/cli-args.d.mts +11 -0
  8. package/dist/cli-args.mjs +21 -0
  9. package/dist/cli-error.d.mts +4 -0
  10. package/dist/cli-error.mjs +59 -0
  11. package/dist/computed-style-capture.d.mts +41 -0
  12. package/dist/computed-style-capture.mjs +589 -0
  13. package/dist/computed-style-diff.d.mts +83 -0
  14. package/dist/computed-style-diff.mjs +123 -0
  15. package/dist/diff-regions.d.mts +8 -0
  16. package/dist/diff-regions.mjs +52 -0
  17. package/dist/dom-equivalence.d.mts +51 -0
  18. package/dist/dom-equivalence.mjs +91 -0
  19. package/dist/dom-position-styles.d.mts +166 -0
  20. package/dist/dom-position-styles.mjs +296 -0
  21. package/dist/element-compare.d.mts +71 -0
  22. package/dist/element-compare.mjs +269 -0
  23. package/dist/heatmap-regions.d.mts +57 -0
  24. package/dist/heatmap-regions.mjs +158 -0
  25. package/dist/heatmap.d.mts +86 -0
  26. package/dist/heatmap.mjs +510 -0
  27. package/dist/image-resize.d.mts +34 -0
  28. package/dist/image-resize.mjs +92 -0
  29. package/dist/index.d.mts +15 -0
  30. package/dist/index.mjs +14 -0
  31. package/dist/mask.d.mts +18 -0
  32. package/dist/mask.mjs +27 -0
  33. package/dist/png-utils.d.mts +30 -0
  34. package/dist/png-utils.mjs +80 -0
  35. package/dist/region-shift.d.mts +20 -0
  36. package/dist/region-shift.mjs +128 -0
  37. package/dist/run-ledger.d.mts +14 -0
  38. package/dist/run-ledger.mjs +31 -0
  39. package/dist/terminal-colors.d.mts +12 -0
  40. package/dist/terminal-colors.mjs +14 -0
  41. package/dist/text-rows.d.mts +121 -0
  42. package/dist/text-rows.mjs +246 -0
  43. package/{src/types.ts → dist/types.d.mts} +88 -206
  44. package/dist/types.mjs +1 -0
  45. package/package.json +17 -5
  46. package/src/a11y-semantic.ts +0 -384
  47. package/src/authored-style-capture.ts +0 -185
  48. package/src/authored-style-diff.ts +0 -32
  49. package/src/cli-args.ts +0 -29
  50. package/src/cli-error.ts +0 -85
  51. package/src/computed-style-capture.ts +0 -717
  52. package/src/computed-style-diff.ts +0 -192
  53. package/src/diff-regions.ts +0 -70
  54. package/src/dom-equivalence.ts +0 -158
  55. package/src/dom-position-styles.ts +0 -457
  56. package/src/element-compare.ts +0 -354
  57. package/src/heatmap-regions.ts +0 -245
  58. package/src/heatmap.ts +0 -670
  59. package/src/image-resize.ts +0 -112
  60. package/src/index.ts +0 -105
  61. package/src/mask.ts +0 -41
  62. package/src/png-utils.ts +0 -86
  63. package/src/region-shift.ts +0 -173
  64. package/src/terminal-colors.ts +0 -13
  65. package/src/text-rows.ts +0 -412
@@ -0,0 +1,158 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { PNG } from "pngjs";
3
+ //#region src/heatmap-regions.ts
4
+ /**
5
+ * Heatmap region clustering.
6
+ *
7
+ * `detectBandShifts` (see `heatmap.ts`) reports per-band Y-shifts —
8
+ * useful for global translations but loses horizontal localization.
9
+ * Subagent F wished for "heatmap region clustering": group connected
10
+ * hot pixels in `*_heatmap.png` into clusters with rectangular
11
+ * bounding boxes, so "a text run at y=420 shifted up 4px" becomes
12
+ * one row instead of being smeared across a tall band.
13
+ *
14
+ * Reuses the connected-component algorithm from `component-bbox.ts`
15
+ * — the input is just a different mask (foreground = "hot pixel").
16
+ *
17
+ * Pixelmatch heatmaps use a fixed palette: bright red for "diff
18
+ * pixel", original (resized) baseline tinted for context. We treat
19
+ * any pixel whose red channel is strongly elevated relative to the
20
+ * other two channels as a hot pixel.
21
+ */
22
+ const DEFAULT_MIN_AREA = 80;
23
+ const DEFAULT_TOP_N = 8;
24
+ const DEFAULT_RED_OVER = 60;
25
+ const DEFAULT_MAX_REGION_FRACTION = .8;
26
+ function buildHotMask(data, width, height, redOver) {
27
+ const mask = new Uint8Array(width * height);
28
+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
29
+ const idx = (y * width + x) * 4;
30
+ const r = data[idx], g = data[idx + 1], b = data[idx + 2];
31
+ if (r - Math.max(g, b) >= redOver) mask[y * width + x] = 1;
32
+ }
33
+ return mask;
34
+ }
35
+ function labelComponents(mask, width, height) {
36
+ const labels = new Int32Array(width * height);
37
+ const parent = [0];
38
+ const find = (x) => {
39
+ while (parent[x] !== x) {
40
+ parent[x] = parent[parent[x]];
41
+ x = parent[x];
42
+ }
43
+ return x;
44
+ };
45
+ const union = (a, b) => {
46
+ const ra = find(a), rb = find(b);
47
+ if (ra !== rb) parent[Math.max(ra, rb)] = Math.min(ra, rb);
48
+ };
49
+ let next = 1;
50
+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
51
+ const idx = y * width + x;
52
+ if (!mask[idx]) continue;
53
+ const up = y > 0 ? labels[idx - width] : 0;
54
+ const left = x > 0 ? labels[idx - 1] : 0;
55
+ if (up && left) {
56
+ labels[idx] = Math.min(up, left);
57
+ if (up !== left) union(up, left);
58
+ } else if (up) labels[idx] = up;
59
+ else if (left) labels[idx] = left;
60
+ else {
61
+ labels[idx] = next;
62
+ parent[next] = next;
63
+ next++;
64
+ }
65
+ }
66
+ const byRoot = /* @__PURE__ */ new Map();
67
+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
68
+ const l = labels[y * width + x];
69
+ if (!l) continue;
70
+ const root = find(l);
71
+ let s = byRoot.get(root);
72
+ if (!s) {
73
+ s = {
74
+ minX: x,
75
+ minY: y,
76
+ maxX: x,
77
+ maxY: y,
78
+ area: 0
79
+ };
80
+ byRoot.set(root, s);
81
+ }
82
+ if (x < s.minX) s.minX = x;
83
+ if (x > s.maxX) s.maxX = x;
84
+ if (y < s.minY) s.minY = y;
85
+ if (y > s.maxY) s.maxY = y;
86
+ s.area++;
87
+ }
88
+ const out = [];
89
+ for (const s of byRoot.values()) out.push({
90
+ top: s.minY,
91
+ left: s.minX,
92
+ width: s.maxX - s.minX + 1,
93
+ height: s.maxY - s.minY + 1,
94
+ area: s.area
95
+ });
96
+ return out;
97
+ }
98
+ function findHeatmapRegionsFromRgba(data, width, height, options = {}) {
99
+ const minArea = options.minArea ?? DEFAULT_MIN_AREA;
100
+ const topN = options.topN ?? DEFAULT_TOP_N;
101
+ const redOver = options.redOverGreenBlue ?? DEFAULT_RED_OVER;
102
+ const maxFraction = options.maxRegionFraction ?? DEFAULT_MAX_REGION_FRACTION;
103
+ if (width <= 0 || height <= 0) return [];
104
+ const mask = buildHotMask(data, width, height, redOver);
105
+ const imageArea = width * height;
106
+ return labelComponents(mask, width, height).filter((r) => r.area >= minArea).filter((r) => r.width * r.height / imageArea < maxFraction).sort((a, b) => b.area - a.area).slice(0, topN);
107
+ }
108
+ function toHex(r, g, b) {
109
+ const c = (n) => n.toString(16).padStart(2, "0");
110
+ return `#${c(r)}${c(g)}${c(b)}`;
111
+ }
112
+ /** Sample the dominant color inside `region` from the source image data. */
113
+ function sampleRegionColor(sourceData, sourceWidth, sourceHeight, region) {
114
+ const inset = 2;
115
+ const x0 = Math.max(0, region.left + inset);
116
+ const x1 = Math.min(sourceWidth, region.left + region.width - inset);
117
+ const y0 = Math.max(0, region.top + inset);
118
+ const y1 = Math.min(sourceHeight, region.top + region.height - inset);
119
+ if (x1 <= x0 || y1 <= y0) return void 0;
120
+ const stepX = Math.max(1, Math.floor((x1 - x0) / 5));
121
+ const stepY = Math.max(1, Math.floor((y1 - y0) / 5));
122
+ const rs = [], gs = [], bs = [];
123
+ for (let y = y0; y < y1; y += stepY) for (let x = x0; x < x1; x += stepX) {
124
+ const i = (y * sourceWidth + x) * 4;
125
+ if (sourceData[i + 3] === 0) continue;
126
+ rs.push(sourceData[i]);
127
+ gs.push(sourceData[i + 1]);
128
+ bs.push(sourceData[i + 2]);
129
+ }
130
+ if (rs.length === 0) return void 0;
131
+ rs.sort((a, b) => a - b);
132
+ gs.sort((a, b) => a - b);
133
+ bs.sort((a, b) => a - b);
134
+ const mid = rs.length >> 1;
135
+ const r = rs[mid], g = gs[mid], b = bs[mid];
136
+ return {
137
+ r,
138
+ g,
139
+ b,
140
+ hex: toHex(r, g, b)
141
+ };
142
+ }
143
+ async function findHeatmapRegionsFromFile(path, options = {}, sourceImagePath) {
144
+ const buf = await readFile(path);
145
+ const png = PNG.sync.read(buf);
146
+ const regions = findHeatmapRegionsFromRgba(png.data, png.width, png.height, options);
147
+ if (sourceImagePath) try {
148
+ const sourceBuf = await readFile(sourceImagePath);
149
+ const sourcePng = PNG.sync.read(sourceBuf);
150
+ for (const region of regions) {
151
+ const color = sampleRegionColor(sourcePng.data, sourcePng.width, sourcePng.height, region);
152
+ if (color) region.dominantColor = color;
153
+ }
154
+ } catch {}
155
+ return regions;
156
+ }
157
+ //#endregion
158
+ export { findHeatmapRegionsFromFile, findHeatmapRegionsFromRgba };
@@ -0,0 +1,86 @@
1
+ import { DiffRegionColorSample, DiffReport, ShiftRegion, VrtDiff, VrtSnapshot } from "./types.mjs";
2
+ import { PngData } from "./png-utils.mjs";
3
+
4
+ //#region src/heatmap.d.ts
5
+ /**
6
+ * Compare two screenshots pixel-by-pixel and generate a diff heatmap.
7
+ */
8
+ declare function compareScreenshots(snapshot: VrtSnapshot, opts?: {
9
+ threshold?: number;
10
+ outputDir?: string;
11
+ skipHeatmap?: boolean;
12
+ }): Promise<VrtDiff | null>;
13
+ /**
14
+ * Sample a region's color pair from the pixels that actually differ.
15
+ *
16
+ * A region-wide median (the old behaviour) reflects the unchanged majority
17
+ * and reads as "no change" on exactly the regions that changed in a minority
18
+ * of their pixels — e.g. a dark background recolor hidden behind dominant
19
+ * white form inputs (A/B v3 draft 10). We accumulate only pixels whose
20
+ * baseline↔current per-channel delta clears CHANGED_PIXEL_DELTA, falling back
21
+ * to every opaque pixel when nothing clears the gate.
22
+ *
23
+ * When the changed pixels are sparse, the mean is still dominated by
24
+ * antialiasing blends, so we additionally surface the single highest-delta
25
+ * pixel pair as `peak` (draft 11).
26
+ */
27
+ declare function sampleRegionColorSample(baseline: PngData, current: PngData, region: {
28
+ x: number;
29
+ y: number;
30
+ width: number;
31
+ height: number;
32
+ }): DiffRegionColorSample | undefined;
33
+ /**
34
+ * Per-band shift detection — split the image into horizontal bands and run
35
+ * the same cross-correlation per band. Lets us report "section A shifted
36
+ * +8px, section B shifted +20px" instead of a single global average that
37
+ * loses localization.
38
+ *
39
+ * Bands with low luminance variance (e.g. a flat white footer) are dropped
40
+ * because cross-correlation can't lock onto them and the result is noise.
41
+ *
42
+ * Exported for unit testing.
43
+ */
44
+ declare function detectBandShifts(baseline: {
45
+ data: Uint8Array;
46
+ width: number;
47
+ height: number;
48
+ }, current: {
49
+ data: Uint8Array;
50
+ width: number;
51
+ height: number;
52
+ }, options?: {
53
+ bandHeight?: number;
54
+ minConfidence?: number;
55
+ maxShift?: number;
56
+ }): ShiftRegion[];
57
+ /**
58
+ * Generate a comprehensive diff report including clustering, shift detection,
59
+ * and region classification. Compatible with mizchi/pixelmatch 0.5.0 DiffReport format.
60
+ */
61
+ declare function generateDiffReport(snapshot: VrtSnapshot, opts?: {
62
+ threshold?: number;
63
+ outputDir?: string;
64
+ skipHeatmap?: boolean;
65
+ detectShift?: boolean;
66
+ }): Promise<DiffReport | null>;
67
+ /**
68
+ * Whiteout detection: checks if most of the image is white (or a single color).
69
+ */
70
+ declare function detectWhiteout(data: PngData, opts?: {
71
+ threshold?: number;
72
+ }): {
73
+ isWhiteout: boolean;
74
+ whiteRatio: number;
75
+ };
76
+ /**
77
+ * Empty content detection: checks if the image has low entropy.
78
+ */
79
+ declare function detectEmptyContent(data: PngData, opts?: {
80
+ threshold?: number;
81
+ }): {
82
+ isEmpty: boolean;
83
+ uniqueColors: number;
84
+ };
85
+ //#endregion
86
+ export { compareScreenshots, detectBandShifts, detectEmptyContent, detectWhiteout, generateDiffReport, sampleRegionColorSample };