@blackbelt-technology/pi-dashboard-deck3d 0.8.0 → 0.10.0
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/.pi/skills/deck3d/SKILL.agent.md +78 -0
- package/.pi/skills/deck3d/SKILL.md +68 -0
- package/.pi/skills/deck3d/reference/effects.md +185 -2
- package/.pi/skills/deck3d/reference/ir-fields.md +26 -3
- package/.pi/skills/deck3d-from-pptx/SKILL.md +134 -0
- package/.pi/skills/deck3d-from-pptx/reference/steps.md +72 -0
- package/README.md +30 -2
- package/assets/preview/stub-chrome.png +0 -0
- package/assets/preview/stub-icon.json +59 -0
- package/assets/preview/stub-layout.json +195 -0
- package/assets/preview/stub-outline.json +48 -0
- package/dist/cli.js +11780 -5934
- package/dist/harvest/harness.js +4 -4
- package/dist/runtime.js +5916 -4007
- package/fixtures/business-2031/assets/stub-chrome.png +0 -0
- package/fixtures/business-2031/assets/stub-icon.json +59 -0
- package/fixtures/business-2031/assets/stub-layout.json +195 -0
- package/fixtures/business-2031/assets/stub-outline.json +48 -0
- package/fixtures/business-2031/build-overrides.mjs +58 -13
- package/fixtures/business-2031/deck.agent.md +60 -0
- package/fixtures/business-2031/deck.json +133 -8
- package/fixtures/business-2031/overrides.json +133 -8
- package/package.json +8 -4
- package/scripts/gen-stub-assets.ts +31 -0
- package/src/art/cutout.ts +97 -0
- package/src/art/decode.ts +169 -0
- package/src/art/icon.ts +380 -0
- package/src/art/png.ts +232 -0
- package/src/art/trace.ts +288 -0
- package/src/check/rules.ts +13 -8
- package/src/cli.ts +313 -51
- package/src/compare/index.ts +131 -0
- package/src/compare/ssim.ts +91 -0
- package/src/fx/art-geometry.ts +137 -0
- package/src/fx/art3d.meta.json +30 -0
- package/src/fx/art3d.ts +131 -0
- package/src/fx/bank.meta.json +26 -0
- package/src/fx/bank.ts +8 -0
- package/src/fx/compose.ts +5 -4
- package/src/fx/crane.meta.json +26 -0
- package/src/fx/crane.ts +8 -0
- package/src/fx/dolly.ts +5 -1
- package/src/fx/fade.ts +5 -1
- package/src/fx/flythrough.ts +5 -1
- package/src/fx/frame-chrome.meta.json +17 -0
- package/src/fx/frame-chrome.ts +134 -0
- package/src/fx/haze.meta.json +19 -0
- package/src/fx/haze.ts +91 -0
- package/src/fx/icon3d.meta.json +21 -0
- package/src/fx/icon3d.ts +167 -0
- package/src/fx/index.ts +96 -66
- package/src/fx/iris.ts +5 -1
- package/src/fx/meta.schema.json +10 -2
- package/src/fx/path.meta.json +25 -0
- package/src/fx/path.ts +8 -0
- package/src/fx/persistent.ts +11 -0
- package/src/fx/revolve.meta.json +26 -0
- package/src/fx/revolve.ts +8 -0
- package/src/fx/slide-layer.meta.json +26 -0
- package/src/fx/slide-layer.ts +557 -0
- package/src/fx/slide-space.ts +142 -0
- package/src/fx/stub-assets.ts +117 -0
- package/src/fx/swing.meta.json +26 -0
- package/src/fx/swing.ts +8 -0
- package/src/fx/types.ts +63 -2
- package/src/ir/merge.ts +5 -2
- package/src/ir/schema.json +140 -3
- package/src/ir/types.ts +54 -0
- package/src/ir/validate.ts +91 -3
- package/src/parse/harvest/index.ts +4 -1
- package/src/parse/markdown.ts +38 -7
- package/src/pptx/baseline.ts +183 -0
- package/src/pptx/extract.ts +161 -0
- package/src/pptx/fallback.ts +70 -0
- package/src/pptx/import.ts +256 -0
- package/src/pptx/master.ts +135 -0
- package/src/pptx/media.ts +81 -0
- package/src/pptx/package.ts +109 -0
- package/src/pptx/ref.ts +100 -0
- package/src/pptx/shapes.ts +276 -0
- package/src/pptx/stage.ts +82 -0
- package/src/pptx/text.ts +202 -0
- package/src/pptx/theme.ts +194 -0
- package/src/pptx/types.ts +159 -0
- package/src/pptx/xfrm.ts +93 -0
- package/src/pptx/xml.ts +106 -0
- package/src/pptx/zip.ts +239 -0
- package/src/render/assets.ts +57 -0
- package/src/render/build.ts +75 -0
- package/src/render/index.ts +7 -2
- package/src/render/template.html +1 -1
- package/src/runtime/assets.ts +75 -0
- package/src/runtime/fade.ts +110 -0
- package/src/runtime/hud.ts +37 -9
- package/src/runtime/index.ts +431 -67
- package/src/runtime/post.ts +11 -5
- package/src/runtime/scene.ts +25 -2
- package/src/runtime/transition.ts +222 -0
- package/src/runtime/types.ts +13 -2
- package/src/serve/index.ts +199 -16
- package/src/util/cleanup.ts +13 -0
- package/src/util/confine.ts +15 -0
- package/src/variants/pick.ts +282 -0
- package/src/variants/render.ts +179 -0
- package/src/variants/schema.json +48 -0
- package/src/variants/style.ts +318 -0
package/src/art/png.ts
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure-TS PNG codec on RGBA buffers (node `zlib`, no native image deps).
|
|
3
|
+
*
|
|
4
|
+
* The encoder writes 8-bit RGBA, filter 0 — deterministic for identical
|
|
5
|
+
* pixels. The decoder reads 8-bit non-interlaced greyscale, RGB, palette,
|
|
6
|
+
* grey+alpha and RGBA, which covers chromium screenshots and the files this
|
|
7
|
+
* package writes; anything else is decoded in the sandboxed page instead.
|
|
8
|
+
*/
|
|
9
|
+
import { deflateSync, inflateSync } from "node:zlib";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Decoded-size cap for untrusted PNGs (4096×4096 = 64 MiB of RGBA). A few KB
|
|
13
|
+
* of valid PNG can inflate to hundreds of MB, so the pixel count comes from the
|
|
14
|
+
* header, BEFORE anything is inflated, and inflation itself is bounded too.
|
|
15
|
+
*/
|
|
16
|
+
export const PNG_MAX_PIXELS = 4096 * 4096;
|
|
17
|
+
|
|
18
|
+
/** A PNG over the decoded-size cap: no other decoder (the sandbox) should be asked to try. */
|
|
19
|
+
export class PngLimitError extends Error {
|
|
20
|
+
constructor(message: string) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "PngLimitError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RgbaImage {
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
/** Row-major RGBA, 4 bytes per pixel. */
|
|
30
|
+
data: Uint8Array;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const SIG = [137, 80, 78, 71, 13, 10, 26, 10];
|
|
34
|
+
|
|
35
|
+
const CRC_TABLE = (() => {
|
|
36
|
+
const t = new Uint32Array(256);
|
|
37
|
+
for (let n = 0; n < 256; n++) {
|
|
38
|
+
let c = n;
|
|
39
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
40
|
+
t[n] = c >>> 0;
|
|
41
|
+
}
|
|
42
|
+
return t;
|
|
43
|
+
})();
|
|
44
|
+
|
|
45
|
+
function crc32(bytes: Uint8Array): number {
|
|
46
|
+
let c = 0xffffffff;
|
|
47
|
+
for (let i = 0; i < bytes.length; i++) c = CRC_TABLE[(c ^ bytes[i]) & 0xff] ^ (c >>> 8);
|
|
48
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function chunk(type: string, data: Uint8Array): Uint8Array {
|
|
52
|
+
const out = new Uint8Array(12 + data.length);
|
|
53
|
+
const view = new DataView(out.buffer);
|
|
54
|
+
view.setUint32(0, data.length);
|
|
55
|
+
for (let i = 0; i < 4; i++) out[4 + i] = type.charCodeAt(i);
|
|
56
|
+
out.set(data, 8);
|
|
57
|
+
view.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length)));
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function encodePng(img: RgbaImage): Uint8Array {
|
|
62
|
+
const { width, height, data } = img;
|
|
63
|
+
const ihdr = new Uint8Array(13);
|
|
64
|
+
const v = new DataView(ihdr.buffer);
|
|
65
|
+
v.setUint32(0, width);
|
|
66
|
+
v.setUint32(4, height);
|
|
67
|
+
ihdr[8] = 8; // bit depth
|
|
68
|
+
ihdr[9] = 6; // RGBA
|
|
69
|
+
const raw = new Uint8Array((width * 4 + 1) * height);
|
|
70
|
+
for (let y = 0; y < height; y++) {
|
|
71
|
+
raw[y * (width * 4 + 1)] = 0;
|
|
72
|
+
raw.set(data.subarray(y * width * 4, (y + 1) * width * 4), y * (width * 4 + 1) + 1);
|
|
73
|
+
}
|
|
74
|
+
const idat = deflateSync(raw, { level: 9 });
|
|
75
|
+
const parts = [new Uint8Array(SIG), chunk("IHDR", ihdr), chunk("IDAT", new Uint8Array(idat)), chunk("IEND", new Uint8Array(0))];
|
|
76
|
+
const total = parts.reduce((a, p) => a + p.length, 0);
|
|
77
|
+
const out = new Uint8Array(total);
|
|
78
|
+
let o = 0;
|
|
79
|
+
for (const p of parts) {
|
|
80
|
+
out.set(p, o);
|
|
81
|
+
o += p.length;
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function isPng(bytes: Uint8Array): boolean {
|
|
87
|
+
return SIG.every((b, i) => bytes[i] === b);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function paeth(a: number, b: number, c: number): number {
|
|
91
|
+
const p = a + b - c;
|
|
92
|
+
const pa = Math.abs(p - a);
|
|
93
|
+
const pb = Math.abs(p - b);
|
|
94
|
+
const pc = Math.abs(p - c);
|
|
95
|
+
return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function decodePng(bytes: Uint8Array, maxPixels = PNG_MAX_PIXELS): RgbaImage {
|
|
99
|
+
if (!isPng(bytes)) throw new Error("not a PNG");
|
|
100
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
101
|
+
let p = 8;
|
|
102
|
+
let width = 0;
|
|
103
|
+
let height = 0;
|
|
104
|
+
let depth = 0;
|
|
105
|
+
let type = 0;
|
|
106
|
+
let interlace = 0;
|
|
107
|
+
let palette: Uint8Array | undefined;
|
|
108
|
+
let trns: Uint8Array | undefined;
|
|
109
|
+
const idat: Uint8Array[] = [];
|
|
110
|
+
while (p + 8 <= bytes.length) {
|
|
111
|
+
const len = view.getUint32(p);
|
|
112
|
+
const t = String.fromCharCode(bytes[p + 4], bytes[p + 5], bytes[p + 6], bytes[p + 7]);
|
|
113
|
+
const data = bytes.subarray(p + 8, p + 8 + len);
|
|
114
|
+
if (t === "IHDR") {
|
|
115
|
+
width = view.getUint32(p + 8);
|
|
116
|
+
height = view.getUint32(p + 12);
|
|
117
|
+
depth = data[8];
|
|
118
|
+
type = data[9];
|
|
119
|
+
interlace = data[12];
|
|
120
|
+
} else if (t === "PLTE") palette = data;
|
|
121
|
+
else if (t === "tRNS") trns = data;
|
|
122
|
+
else if (t === "IDAT") idat.push(data);
|
|
123
|
+
else if (t === "IEND") break;
|
|
124
|
+
p += 12 + len;
|
|
125
|
+
}
|
|
126
|
+
if (depth !== 8 || interlace !== 0) throw new Error(`unsupported PNG (bit depth ${depth}, interlace ${interlace})`);
|
|
127
|
+
const channels = ({ 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 } as Record<number, number>)[type];
|
|
128
|
+
if (!channels) throw new Error(`unsupported PNG colour type ${type}`);
|
|
129
|
+
if (width * height > maxPixels) {
|
|
130
|
+
throw new PngLimitError(`image is ${width}×${height} (${((width * height) / 1e6).toFixed(1)} megapixels; limit ${(maxPixels / 1e6).toFixed(1)} megapixels)`);
|
|
131
|
+
}
|
|
132
|
+
const total = idat.reduce((a, c) => a + c.length, 0);
|
|
133
|
+
const joined = new Uint8Array(total);
|
|
134
|
+
let o = 0;
|
|
135
|
+
for (const c of idat) {
|
|
136
|
+
joined.set(c, o);
|
|
137
|
+
o += c.length;
|
|
138
|
+
}
|
|
139
|
+
// The header promises exactly this many bytes; inflating more is a lie or a bomb.
|
|
140
|
+
const raw = new Uint8Array(inflateSync(joined, { maxOutputLength: (width * channels + 1) * height }));
|
|
141
|
+
const stride = width * channels;
|
|
142
|
+
const pixels = new Uint8Array(stride * height);
|
|
143
|
+
for (let y = 0; y < height; y++) {
|
|
144
|
+
const f = raw[y * (stride + 1)];
|
|
145
|
+
const src = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
|
|
146
|
+
const row = y * stride;
|
|
147
|
+
for (let x = 0; x < stride; x++) {
|
|
148
|
+
const a = x >= channels ? pixels[row + x - channels] : 0;
|
|
149
|
+
const b = y > 0 ? pixels[row - stride + x] : 0;
|
|
150
|
+
const c = x >= channels && y > 0 ? pixels[row - stride + x - channels] : 0;
|
|
151
|
+
let v = src[x];
|
|
152
|
+
if (f === 1) v += a;
|
|
153
|
+
else if (f === 2) v += b;
|
|
154
|
+
else if (f === 3) v += (a + b) >> 1;
|
|
155
|
+
else if (f === 4) v += paeth(a, b, c);
|
|
156
|
+
pixels[row + x] = v & 255;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const out = new Uint8Array(width * height * 4);
|
|
160
|
+
for (let i = 0; i < width * height; i++) {
|
|
161
|
+
const s = i * channels;
|
|
162
|
+
let r: number;
|
|
163
|
+
let g: number;
|
|
164
|
+
let b: number;
|
|
165
|
+
let al = 255;
|
|
166
|
+
if (type === 0) {
|
|
167
|
+
r = g = b = pixels[s];
|
|
168
|
+
} else if (type === 2) {
|
|
169
|
+
[r, g, b] = [pixels[s], pixels[s + 1], pixels[s + 2]];
|
|
170
|
+
} else if (type === 3) {
|
|
171
|
+
const idx = pixels[s];
|
|
172
|
+
[r, g, b] = [palette?.[idx * 3] ?? 0, palette?.[idx * 3 + 1] ?? 0, palette?.[idx * 3 + 2] ?? 0];
|
|
173
|
+
al = trns && idx < trns.length ? trns[idx] : 255;
|
|
174
|
+
} else if (type === 4) {
|
|
175
|
+
r = g = b = pixels[s];
|
|
176
|
+
al = pixels[s + 1];
|
|
177
|
+
} else {
|
|
178
|
+
[r, g, b, al] = [pixels[s], pixels[s + 1], pixels[s + 2], pixels[s + 3]];
|
|
179
|
+
}
|
|
180
|
+
out.set([r, g, b, al], i * 4);
|
|
181
|
+
}
|
|
182
|
+
return { width, height, data: out };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Copy a sub-rectangle (clamped to the image; pixels outside are transparent). */
|
|
186
|
+
export function cropRgba(img: RgbaImage, x: number, y: number, w: number, h: number): RgbaImage {
|
|
187
|
+
const W = Math.max(1, Math.round(w));
|
|
188
|
+
const H = Math.max(1, Math.round(h));
|
|
189
|
+
const X = Math.round(x);
|
|
190
|
+
const Y = Math.round(y);
|
|
191
|
+
const out = new Uint8Array(W * H * 4);
|
|
192
|
+
for (let row = 0; row < H; row++) {
|
|
193
|
+
const sy = Y + row;
|
|
194
|
+
if (sy < 0 || sy >= img.height) continue;
|
|
195
|
+
for (let col = 0; col < W; col++) {
|
|
196
|
+
const sx = X + col;
|
|
197
|
+
if (sx < 0 || sx >= img.width) continue;
|
|
198
|
+
const si = (sy * img.width + sx) * 4;
|
|
199
|
+
out.set(img.data.subarray(si, si + 4), (row * W + col) * 4);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return { width: W, height: H, data: out };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Bilinear resize to `w` × `h`. */
|
|
206
|
+
export function resizeRgba(img: RgbaImage, w: number, h: number): RgbaImage {
|
|
207
|
+
const W = Math.max(1, Math.round(w));
|
|
208
|
+
const H = Math.max(1, Math.round(h));
|
|
209
|
+
const out = new Uint8Array(W * H * 4);
|
|
210
|
+
const sx = img.width / W;
|
|
211
|
+
const sy = img.height / H;
|
|
212
|
+
for (let y = 0; y < H; y++) {
|
|
213
|
+
const fy = Math.min(img.height - 1, Math.max(0, (y + 0.5) * sy - 0.5));
|
|
214
|
+
const y0 = Math.floor(fy);
|
|
215
|
+
const y1 = Math.min(img.height - 1, y0 + 1);
|
|
216
|
+
const ty = fy - y0;
|
|
217
|
+
for (let x = 0; x < W; x++) {
|
|
218
|
+
const fx = Math.min(img.width - 1, Math.max(0, (x + 0.5) * sx - 0.5));
|
|
219
|
+
const x0 = Math.floor(fx);
|
|
220
|
+
const x1 = Math.min(img.width - 1, x0 + 1);
|
|
221
|
+
const tx = fx - x0;
|
|
222
|
+
for (let c = 0; c < 4; c++) {
|
|
223
|
+
const a = img.data[(y0 * img.width + x0) * 4 + c];
|
|
224
|
+
const b = img.data[(y0 * img.width + x1) * 4 + c];
|
|
225
|
+
const d = img.data[(y1 * img.width + x0) * 4 + c];
|
|
226
|
+
const e = img.data[(y1 * img.width + x1) * 4 + c];
|
|
227
|
+
out[(y * W + x) * 4 + c] = Math.round((a * (1 - tx) + b * tx) * (1 - ty) + (d * (1 - tx) + e * tx) * ty);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return { width: W, height: H, data: out };
|
|
232
|
+
}
|
package/src/art/trace.ts
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `art trace` core (design D2): RGBA buffer → coloured polygons with holes.
|
|
3
|
+
* Plain TS, no browser — unit-tested on synthetic buffers.
|
|
4
|
+
*
|
|
5
|
+
* 1. Classify pixels: background (transparent, or nearest to the corner
|
|
6
|
+
* colour of an opaque image) vs palette classes (nearest `--palette`
|
|
7
|
+
* entry; one foreground class when no palette is given).
|
|
8
|
+
* 2. 4-connected components per class (iterative flood fill).
|
|
9
|
+
* 3. Boundary loops by crack following along pixel edges, each pixel's
|
|
10
|
+
* edges walked clockwise on screen: the outer loop comes out with a
|
|
11
|
+
* positive shoelace area in image coordinates, every hole negative.
|
|
12
|
+
* 4. Collinear staircase merge + Ramer–Douglas–Peucker at `--tolerance`.
|
|
13
|
+
*
|
|
14
|
+
* Output is a pure function of (pixels, flags): byte-identical JSON.
|
|
15
|
+
*/
|
|
16
|
+
import type { RgbaImage } from "./png.js";
|
|
17
|
+
|
|
18
|
+
export type Pt = [number, number];
|
|
19
|
+
|
|
20
|
+
export interface TracedPolygon {
|
|
21
|
+
color: string;
|
|
22
|
+
/** Outer ring, positive shoelace area in image coordinates. */
|
|
23
|
+
outer: Pt[];
|
|
24
|
+
/** Hole rings, negative shoelace area, each inside `outer`. */
|
|
25
|
+
holes: Pt[][];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Outline {
|
|
29
|
+
version: 1;
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
polygons: TracedPolygon[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface TraceOptions {
|
|
36
|
+
palette?: string[];
|
|
37
|
+
/** Regions (and holes) with fewer pixels are dropped. Default 16. */
|
|
38
|
+
minArea?: number;
|
|
39
|
+
/** RDP tolerance in px. Default 1. */
|
|
40
|
+
tolerance?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type Rgb = [number, number, number];
|
|
44
|
+
|
|
45
|
+
export function parseHex(hex: string): Rgb {
|
|
46
|
+
const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
|
|
47
|
+
if (!m) throw new Error(`bad colour '${hex}' (expected #rrggbb)`);
|
|
48
|
+
const n = Number.parseInt(m[1], 16);
|
|
49
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function toHex([r, g, b]: Rgb): string {
|
|
53
|
+
return `#${[r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("")}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const d2 = (a: Rgb, b: Rgb) => (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2;
|
|
57
|
+
|
|
58
|
+
/** Signed shoelace area in image coordinates. */
|
|
59
|
+
export function signedArea(ring: Pt[]): number {
|
|
60
|
+
let a = 0;
|
|
61
|
+
for (let i = 0; i < ring.length; i++) {
|
|
62
|
+
const [x1, y1] = ring[i];
|
|
63
|
+
const [x2, y2] = ring[(i + 1) % ring.length];
|
|
64
|
+
a += x1 * y2 - x2 * y1;
|
|
65
|
+
}
|
|
66
|
+
return a / 2;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Class per pixel: -1 background, else palette index (0 without a palette). */
|
|
70
|
+
function classify(img: RgbaImage, palette: Rgb[]): Int16Array {
|
|
71
|
+
const { width, height, data } = img;
|
|
72
|
+
const n = width * height;
|
|
73
|
+
const cls = new Int16Array(n);
|
|
74
|
+
let transparent = false;
|
|
75
|
+
for (let i = 0; i < n; i++) if (data[i * 4 + 3] < 250) transparent = true;
|
|
76
|
+
// Opaque image: the commonest corner colour is the background.
|
|
77
|
+
let bg: Rgb | undefined;
|
|
78
|
+
if (!transparent) {
|
|
79
|
+
const corners = [0, width - 1, (height - 1) * width, n - 1].map((i) => [data[i * 4], data[i * 4 + 1], data[i * 4 + 2]] as Rgb);
|
|
80
|
+
const counts = corners.map((c) => corners.filter((o) => d2(o, c) < 300).length);
|
|
81
|
+
bg = corners[counts.indexOf(Math.max(...counts))];
|
|
82
|
+
}
|
|
83
|
+
for (let i = 0; i < n; i++) {
|
|
84
|
+
const a = data[i * 4 + 3];
|
|
85
|
+
const c: Rgb = [data[i * 4], data[i * 4 + 1], data[i * 4 + 2]];
|
|
86
|
+
if (transparent ? a < 128 : false) {
|
|
87
|
+
cls[i] = -1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (palette.length) {
|
|
91
|
+
let best = -1;
|
|
92
|
+
let bestD = bg ? d2(c, bg) : Number.POSITIVE_INFINITY;
|
|
93
|
+
palette.forEach((p, k) => {
|
|
94
|
+
const d = d2(c, p);
|
|
95
|
+
if (d < bestD) {
|
|
96
|
+
bestD = d;
|
|
97
|
+
best = k;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
cls[i] = best;
|
|
101
|
+
} else {
|
|
102
|
+
cls[i] = bg && d2(c, bg) < 48 * 48 * 3 ? -1 : 0;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return cls;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 4-connected components of one class, in scan order of their first pixel. */
|
|
109
|
+
function components(cls: Int16Array, width: number, height: number): Array<{ cls: number; pixels: number[] }> {
|
|
110
|
+
const seen = new Uint8Array(cls.length);
|
|
111
|
+
const out: Array<{ cls: number; pixels: number[] }> = [];
|
|
112
|
+
const stack: number[] = [];
|
|
113
|
+
for (let start = 0; start < cls.length; start++) {
|
|
114
|
+
if (seen[start] || cls[start] < 0) continue;
|
|
115
|
+
const c = cls[start];
|
|
116
|
+
const pixels: number[] = [];
|
|
117
|
+
stack.push(start);
|
|
118
|
+
seen[start] = 1;
|
|
119
|
+
while (stack.length) {
|
|
120
|
+
const p = stack.pop() as number;
|
|
121
|
+
pixels.push(p);
|
|
122
|
+
const x = p % width;
|
|
123
|
+
const y = (p / width) | 0;
|
|
124
|
+
const nb = [x > 0 ? p - 1 : -1, x < width - 1 ? p + 1 : -1, y > 0 ? p - width : -1, y < height - 1 ? p + width : -1];
|
|
125
|
+
for (const q of nb) {
|
|
126
|
+
if (q >= 0 && !seen[q] && cls[q] === c) {
|
|
127
|
+
seen[q] = 1;
|
|
128
|
+
stack.push(q);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
out.push({ cls: c, pixels });
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Boundary loops of a pixel set by crack following. Each member pixel
|
|
139
|
+
* contributes its sides facing a non-member, directed clockwise on screen;
|
|
140
|
+
* at a saddle vertex the walk turns right first, which keeps diagonal
|
|
141
|
+
* neighbours apart (4-connectivity).
|
|
142
|
+
*/
|
|
143
|
+
function boundaryLoops(member: (x: number, y: number) => boolean, pixels: number[], width: number): Pt[][] {
|
|
144
|
+
// Edge key: start vertex → list of [endX, endY, dir].
|
|
145
|
+
const out = new Map<number, Array<[number, number, number]>>();
|
|
146
|
+
const W = width + 1;
|
|
147
|
+
const add = (x1: number, y1: number, x2: number, y2: number, dir: number) => {
|
|
148
|
+
const k = y1 * W + x1;
|
|
149
|
+
const list = out.get(k);
|
|
150
|
+
if (list) list.push([x2, y2, dir]);
|
|
151
|
+
else out.set(k, [[x2, y2, dir]]);
|
|
152
|
+
};
|
|
153
|
+
// dir: 0 right, 1 down, 2 left, 3 up (screen).
|
|
154
|
+
for (const p of pixels) {
|
|
155
|
+
const x = p % width;
|
|
156
|
+
const y = (p / width) | 0;
|
|
157
|
+
if (!member(x, y - 1)) add(x, y, x + 1, y, 0);
|
|
158
|
+
if (!member(x + 1, y)) add(x + 1, y, x + 1, y + 1, 1);
|
|
159
|
+
if (!member(x, y + 1)) add(x + 1, y + 1, x, y + 1, 2);
|
|
160
|
+
if (!member(x - 1, y)) add(x, y + 1, x, y, 3);
|
|
161
|
+
}
|
|
162
|
+
const loops: Pt[][] = [];
|
|
163
|
+
const keys = [...out.keys()].sort((a, b) => a - b);
|
|
164
|
+
for (const startKey of keys) {
|
|
165
|
+
while ((out.get(startKey)?.length ?? 0) > 0) {
|
|
166
|
+
const sx = startKey % W;
|
|
167
|
+
const sy = (startKey / W) | 0;
|
|
168
|
+
const ring: Pt[] = [[sx, sy]];
|
|
169
|
+
let key = startKey;
|
|
170
|
+
let dirIn = -1;
|
|
171
|
+
for (;;) {
|
|
172
|
+
const list = out.get(key) as Array<[number, number, number]>;
|
|
173
|
+
// Prefer a right turn, then straight, then left.
|
|
174
|
+
let pick = 0;
|
|
175
|
+
if (list.length > 1 && dirIn >= 0) {
|
|
176
|
+
for (const d of [(dirIn + 1) % 4, dirIn, (dirIn + 3) % 4]) {
|
|
177
|
+
const i = list.findIndex((e) => e[2] === d);
|
|
178
|
+
if (i >= 0) {
|
|
179
|
+
pick = i;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const [ex, ey, dir] = list.splice(pick, 1)[0];
|
|
185
|
+
if (!list.length) out.delete(key);
|
|
186
|
+
dirIn = dir;
|
|
187
|
+
key = ey * W + ex;
|
|
188
|
+
if (key === startKey) break;
|
|
189
|
+
ring.push([ex, ey]);
|
|
190
|
+
}
|
|
191
|
+
loops.push(ring);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return loops;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Drop vertices on a straight run (the pixel staircase's collinear points). */
|
|
198
|
+
function mergeCollinear(ring: Pt[]): Pt[] {
|
|
199
|
+
const out: Pt[] = [];
|
|
200
|
+
for (let i = 0; i < ring.length; i++) {
|
|
201
|
+
const a = ring[(i - 1 + ring.length) % ring.length];
|
|
202
|
+
const b = ring[i];
|
|
203
|
+
const c = ring[(i + 1) % ring.length];
|
|
204
|
+
if ((b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]) !== 0) out.push(b);
|
|
205
|
+
}
|
|
206
|
+
return out.length >= 3 ? out : ring;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function rdp(points: Pt[], tol: number): Pt[] {
|
|
210
|
+
if (points.length < 3) return points;
|
|
211
|
+
const [ax, ay] = points[0];
|
|
212
|
+
const [bx, by] = points[points.length - 1];
|
|
213
|
+
const len = Math.hypot(bx - ax, by - ay);
|
|
214
|
+
let maxD = -1;
|
|
215
|
+
let idx = 0;
|
|
216
|
+
for (let i = 1; i < points.length - 1; i++) {
|
|
217
|
+
const [px, py] = points[i];
|
|
218
|
+
const d = len === 0 ? Math.hypot(px - ax, py - ay) : Math.abs((bx - ax) * (ay - py) - (ax - px) * (by - ay)) / len;
|
|
219
|
+
if (d > maxD) {
|
|
220
|
+
maxD = d;
|
|
221
|
+
idx = i;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (maxD <= tol) return [points[0], points[points.length - 1]];
|
|
225
|
+
return [...rdp(points.slice(0, idx + 1), tol).slice(0, -1), ...rdp(points.slice(idx), tol)];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** RDP on a closed ring: split at the vertex farthest from the first. */
|
|
229
|
+
function simplifyRing(ring: Pt[], tol: number): Pt[] {
|
|
230
|
+
const r = mergeCollinear(ring);
|
|
231
|
+
if (r.length <= 4 || tol <= 0) return r;
|
|
232
|
+
let far = 0;
|
|
233
|
+
let farD = -1;
|
|
234
|
+
for (let i = 1; i < r.length; i++) {
|
|
235
|
+
const d = Math.hypot(r[i][0] - r[0][0], r[i][1] - r[0][1]);
|
|
236
|
+
if (d > farD) {
|
|
237
|
+
farD = d;
|
|
238
|
+
far = i;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const a = rdp(r.slice(0, far + 1), tol);
|
|
242
|
+
const b = rdp([...r.slice(far), r[0]], tol);
|
|
243
|
+
const out = [...a.slice(0, -1), ...b.slice(0, -1)];
|
|
244
|
+
return out.length >= 3 ? out : r;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function dominantColor(img: RgbaImage, pixels: number[]): Rgb {
|
|
248
|
+
const counts = new Map<number, number>();
|
|
249
|
+
for (const p of pixels) {
|
|
250
|
+
const k = (img.data[p * 4] << 16) | (img.data[p * 4 + 1] << 8) | img.data[p * 4 + 2];
|
|
251
|
+
counts.set(k, (counts.get(k) ?? 0) + 1);
|
|
252
|
+
}
|
|
253
|
+
let best = 0;
|
|
254
|
+
let bestN = -1;
|
|
255
|
+
for (const [k, n] of [...counts.entries()].sort((a, b) => a[0] - b[0])) {
|
|
256
|
+
if (n > bestN) {
|
|
257
|
+
bestN = n;
|
|
258
|
+
best = k;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return [(best >> 16) & 255, (best >> 8) & 255, best & 255];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function traceImage(img: RgbaImage, opts: TraceOptions = {}): Outline {
|
|
265
|
+
const palette = (opts.palette ?? []).map(parseHex);
|
|
266
|
+
const minArea = opts.minArea ?? 16;
|
|
267
|
+
const tol = opts.tolerance ?? 1;
|
|
268
|
+
const { width, height } = img;
|
|
269
|
+
const cls = classify(img, palette);
|
|
270
|
+
const polygons: TracedPolygon[] = [];
|
|
271
|
+
for (const comp of components(cls, width, height)) {
|
|
272
|
+
if (comp.pixels.length < minArea) continue;
|
|
273
|
+
const set = new Uint8Array(width * height);
|
|
274
|
+
for (const p of comp.pixels) set[p] = 1;
|
|
275
|
+
const member = (x: number, y: number) => x >= 0 && y >= 0 && x < width && y < height && set[y * width + x] === 1;
|
|
276
|
+
const loops = boundaryLoops(member, comp.pixels, width).map((ring) => ({ ring, area: signedArea(ring) }));
|
|
277
|
+
const outer = loops.filter((l) => l.area > 0).sort((a, b) => b.area - a.area)[0];
|
|
278
|
+
if (!outer) continue;
|
|
279
|
+
const holes = loops.filter((l) => l.area < 0 && -l.area >= minArea).map((l) => simplifyRing(l.ring, tol));
|
|
280
|
+
const color = palette.length ? palette[comp.cls] : dominantColor(img, comp.pixels);
|
|
281
|
+
polygons.push({
|
|
282
|
+
color: toHex(color),
|
|
283
|
+
outer: simplifyRing(outer.ring, tol),
|
|
284
|
+
holes: holes.filter((h) => signedArea(h) < 0),
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
return { version: 1, width, height, polygons };
|
|
288
|
+
}
|
package/src/check/rules.ts
CHANGED
|
@@ -76,7 +76,7 @@ export interface Finding {
|
|
|
76
76
|
const px = (n: number): string => `${Math.round(n)}px`;
|
|
77
77
|
|
|
78
78
|
function isText(m: Measurement): boolean {
|
|
79
|
-
return (m.kind === "label" || m.kind === "title") && m.text.length > 0;
|
|
79
|
+
return (m.kind === "label" || m.kind === "title" || m.kind === "layer-text") && m.text.length > 0;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/** Overrides grammar key for a slide-level knob. */
|
|
@@ -103,22 +103,27 @@ export function fitFindings(measurements: Measurement[], viewport: { w: number;
|
|
|
103
103
|
const minY = Math.min(...usable.map((m) => m.rect.y));
|
|
104
104
|
const maxY = Math.max(...usable.map((m) => m.rect.y + m.rect.h));
|
|
105
105
|
const out: Finding[] = [];
|
|
106
|
-
|
|
106
|
+
// Imported layer text names the item past the edge: the fix is per item
|
|
107
|
+
// (`widen`/`retext`), not the slide-wide diagram scale.
|
|
108
|
+
const add = (detail: string, measured: number, threshold: number, extreme: Measurement | undefined): void => {
|
|
109
|
+
const layer = extreme?.kind === "layer-text" ? extreme : undefined;
|
|
107
110
|
out.push({
|
|
108
111
|
severity: "error",
|
|
109
112
|
rule: "fit",
|
|
110
113
|
slide: slide.id,
|
|
111
114
|
slideIndex: slide.index,
|
|
115
|
+
...(layer ? { id: layer.id, text: layer.text } : {}),
|
|
112
116
|
measured: px(measured),
|
|
113
117
|
threshold: px(threshold),
|
|
114
|
-
detail,
|
|
115
|
-
suggest: slideKnob(slide.id, "diagram.scale"),
|
|
118
|
+
detail: layer ? `${detail} (${layer.id})` : detail,
|
|
119
|
+
suggest: slideKnob(slide.id, layer ? "effects" : "diagram.scale"),
|
|
116
120
|
});
|
|
117
121
|
};
|
|
118
|
-
|
|
119
|
-
if (
|
|
120
|
-
if (
|
|
121
|
-
if (
|
|
122
|
+
const at = (pick: (m: Measurement) => number, v: number) => usable.find((m) => pick(m) === v);
|
|
123
|
+
if (minX < left) add(`left ${px(minX)} < ${px(left)}`, minX, left, at((m) => m.rect.x, minX));
|
|
124
|
+
if (maxX > right) add(`right ${px(maxX)} > ${px(right)}`, maxX, right, at((m) => m.rect.x + m.rect.w, maxX));
|
|
125
|
+
if (minY < top) add(`top ${px(minY)} < ${px(top)}`, minY, top, at((m) => m.rect.y, minY));
|
|
126
|
+
if (maxY > bottom) add(`bottom ${px(maxY)} > ${px(bottom)}`, maxY, bottom, at((m) => m.rect.y + m.rect.h, maxY));
|
|
122
127
|
return out;
|
|
123
128
|
}
|
|
124
129
|
|