qrdx 0.0.45 → 0.0.47
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/LICENSE +21 -0
- package/README.md +353 -300
- package/dist/index.js +27 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -7
- package/dist/index.mjs.map +1 -1
- package/dist/qr-code-canvas.js +12 -0
- package/dist/qr-code-canvas.js.map +1 -0
- package/dist/qr-code-canvas.mjs +12 -0
- package/dist/qr-code-canvas.mjs.map +1 -0
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/art-trace.d.ts +46 -0
- package/dist/src/art-trace.d.ts.map +1 -0
- package/dist/src/art.d.ts +138 -0
- package/dist/src/art.d.ts.map +1 -0
- package/dist/src/body-patterns.d.ts +34 -0
- package/dist/src/body-patterns.d.ts.map +1 -0
- package/dist/src/codegen.d.ts +5 -5
- package/dist/src/codegen.d.ts.map +1 -1
- package/dist/src/codeword-map.d.ts +40 -0
- package/dist/src/codeword-map.d.ts.map +1 -0
- package/dist/src/constants.d.ts +4 -7
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/corner-dot.d.ts +3 -1
- package/dist/src/corner-dot.d.ts.map +1 -1
- package/dist/src/corner-eye.d.ts +5 -2
- package/dist/src/corner-eye.d.ts.map +1 -1
- package/dist/src/detection.d.ts.map +1 -1
- package/dist/src/download.d.ts +22 -5
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/eps.d.ts +62 -0
- package/dist/src/eps.d.ts.map +1 -0
- package/dist/src/frame-icons.d.ts +11 -0
- package/dist/src/frame-icons.d.ts.map +1 -0
- package/dist/src/frame-import.d.ts +61 -0
- package/dist/src/frame-import.d.ts.map +1 -0
- package/dist/src/frame-spec.d.ts +24 -0
- package/dist/src/frame-spec.d.ts.map +1 -0
- package/dist/src/frame-styles.d.ts +50 -0
- package/dist/src/frame-styles.d.ts.map +1 -0
- package/dist/src/frame.d.ts +126 -0
- package/dist/src/frame.d.ts.map +1 -0
- package/dist/src/frames/builtin.d.ts +8 -0
- package/dist/src/frames/builtin.d.ts.map +1 -0
- package/dist/src/frames/official.d.ts +18 -0
- package/dist/src/frames/official.d.ts.map +1 -0
- package/dist/src/helpers.d.ts +15 -2
- package/dist/src/helpers.d.ts.map +1 -1
- package/dist/src/index.d.ts +24 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/logo-budget.d.ts +54 -0
- package/dist/src/logo-budget.d.ts.map +1 -0
- package/dist/src/logo-cleanup.d.ts +25 -0
- package/dist/src/logo-cleanup.d.ts.map +1 -0
- package/dist/src/logo.d.ts +28 -0
- package/dist/src/logo.d.ts.map +1 -0
- package/dist/src/print.d.ts +73 -0
- package/dist/src/print.d.ts.map +1 -0
- package/dist/src/qr-code-canvas.d.ts +13 -0
- package/dist/src/qr-code-canvas.d.ts.map +1 -0
- package/dist/src/qr-code.d.ts +4 -2
- package/dist/src/qr-code.d.ts.map +1 -1
- package/dist/src/qr-outline.d.ts +41 -0
- package/dist/src/qr-outline.d.ts.map +1 -0
- package/dist/src/qr-shape.d.ts +72 -0
- package/dist/src/qr-shape.d.ts.map +1 -0
- package/dist/src/scan-risk.d.ts +40 -0
- package/dist/src/scan-risk.d.ts.map +1 -0
- package/dist/src/scan-score.d.ts +154 -0
- package/dist/src/scan-score.d.ts.map +1 -0
- package/dist/src/shapes.d.ts +36 -0
- package/dist/src/shapes.d.ts.map +1 -0
- package/dist/src/silhouette.d.ts +28 -0
- package/dist/src/silhouette.d.ts.map +1 -0
- package/dist/src/utils/color.d.ts.map +1 -1
- package/dist/src/utils/fmt.d.ts +24 -0
- package/dist/src/utils/fmt.d.ts.map +1 -0
- package/dist/src/utils/paint.d.ts +80 -0
- package/dist/src/utils/paint.d.ts.map +1 -0
- package/dist/src/utils/parse-color.d.ts +21 -0
- package/dist/src/utils/parse-color.d.ts.map +1 -0
- package/dist/src/utils/path-data.d.ts +24 -0
- package/dist/src/utils/path-data.d.ts.map +1 -0
- package/dist/src/utils/png.d.ts +16 -0
- package/dist/src/utils/png.d.ts.map +1 -0
- package/dist/src/utils/polygon-clip.d.ts +26 -0
- package/dist/src/utils/polygon-clip.d.ts.map +1 -0
- package/dist/src/utils/print-shared.d.ts +20 -0
- package/dist/src/utils/print-shared.d.ts.map +1 -0
- package/dist/src/utils/svg-geometry.d.ts +49 -0
- package/dist/src/utils/svg-geometry.d.ts.map +1 -0
- package/dist/src/utils/svg-gradient.d.ts +59 -0
- package/dist/src/utils/svg-gradient.d.ts.map +1 -0
- package/dist/src/utils/svg-painter.d.ts +152 -0
- package/dist/src/utils/svg-painter.d.ts.map +1 -0
- package/dist/src/utils.d.ts +18 -2
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/types/art.d.ts +64 -0
- package/dist/types/art.d.ts.map +1 -0
- package/dist/types/color.d.ts +125 -7
- package/dist/types/color.d.ts.map +1 -1
- package/dist/types/corner-dot.d.ts +52 -2
- package/dist/types/corner-dot.d.ts.map +1 -1
- package/dist/types/corner-eye.d.ts +43 -2
- package/dist/types/corner-eye.d.ts.map +1 -1
- package/dist/types/frame-spec.d.ts +3509 -0
- package/dist/types/frame-spec.d.ts.map +1 -0
- package/dist/types/frame.d.ts +68 -0
- package/dist/types/frame.d.ts.map +1 -0
- package/dist/types/image-pattern.d.ts +15 -1
- package/dist/types/image-pattern.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -9
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +6 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/qr.d.ts +1684 -4
- package/dist/types/qr.d.ts.map +1 -1
- package/dist/types/template.d.ts +2 -2
- package/dist/types/template.d.ts.map +1 -1
- package/package.json +63 -20
- package/src/api.tsx +67 -17
- package/src/art-trace.ts +554 -0
- package/src/art.tsx +2226 -0
- package/src/body-patterns.tsx +389 -0
- package/src/codegen.ts +10 -5
- package/src/codeword-map.ts +265 -0
- package/src/constants.ts +4 -8
- package/src/corner-dot.tsx +209 -33
- package/src/corner-eye.ts +328 -60
- package/src/detection.ts +5 -11
- package/src/download.tsx +123 -52
- package/src/eps.ts +701 -0
- package/src/frame-icons.ts +325 -0
- package/src/frame-import.ts +1198 -0
- package/src/frame-spec.ts +518 -0
- package/src/frame-styles.ts +1095 -0
- package/src/frame.tsx +630 -0
- package/src/frames/builtin/banner-bottom.json +58 -0
- package/src/frames/builtin/circle.json +81 -0
- package/src/frames/builtin/rounded-border.json +60 -0
- package/src/frames/builtin/ticket.json +51 -0
- package/src/frames/builtin.ts +20 -0
- package/src/frames/official/attendance.json +110 -0
- package/src/frames/official/audio-guide.json +95 -0
- package/src/frames/official/book-now.json +103 -0
- package/src/frames/official/check-in.json +110 -0
- package/src/frames/official/christmas.json +131 -0
- package/src/frames/official/donate.json +77 -0
- package/src/frames/official/event-ticket.json +77 -0
- package/src/frames/official/feedback-bubble.json +73 -0
- package/src/frames/official/follow-us.json +83 -0
- package/src/frames/official/get-the-app.json +107 -0
- package/src/frames/official/halloween.json +80 -0
- package/src/frames/official/new-year.json +94 -0
- package/src/frames/official/order-at-table.json +86 -0
- package/src/frames/official/product-info.json +119 -0
- package/src/frames/official/register-product.json +77 -0
- package/src/frames/official/review-us.json +127 -0
- package/src/frames/official/rsvp-card.json +112 -0
- package/src/frames/official/save-contact.json +117 -0
- package/src/frames/official/scan-to-pay.json +94 -0
- package/src/frames/official/shop-window.json +81 -0
- package/src/frames/official/sign-rider.json +81 -0
- package/src/frames/official/table-tent.json +75 -0
- package/src/frames/official/wifi-card.json +89 -0
- package/src/frames/official/winter.json +142 -0
- package/src/frames/official.ts +77 -0
- package/src/helpers.ts +168 -11
- package/src/index.tsx +141 -172
- package/src/logo-budget.ts +226 -0
- package/src/logo-cleanup.ts +146 -0
- package/src/logo.tsx +137 -0
- package/src/print.ts +794 -0
- package/src/qr-code-canvas.tsx +241 -0
- package/src/qr-code.tsx +51 -16
- package/src/qr-outline.ts +511 -0
- package/src/qr-shape.ts +255 -0
- package/src/scan-risk.ts +197 -0
- package/src/scan-score.ts +1629 -0
- package/src/shapes.ts +169 -0
- package/src/silhouette.ts +334 -0
- package/src/utils/color.ts +10 -1
- package/src/utils/fmt.ts +59 -0
- package/src/utils/paint.ts +317 -0
- package/src/utils/parse-color.ts +135 -0
- package/src/utils/path-data.ts +182 -0
- package/src/utils/png.ts +175 -0
- package/src/utils/polygon-clip.ts +163 -0
- package/src/utils/print-shared.ts +74 -0
- package/src/utils/svg-geometry.ts +838 -0
- package/src/utils/svg-gradient.ts +407 -0
- package/src/utils/svg-painter.ts +946 -0
- package/src/utils.tsx +794 -385
- package/types/art.ts +74 -0
- package/types/color.ts +132 -12
- package/types/corner-dot.ts +41 -4
- package/types/corner-eye.ts +31 -4
- package/types/frame-spec.ts +748 -0
- package/types/frame.ts +67 -0
- package/types/image-pattern.ts +15 -0
- package/types/index.ts +106 -4
- package/types/qr.ts +180 -2
- package/types/template.ts +2 -2
- package/dist/cli.js +0 -4231
package/src/art-trace.ts
ADDED
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: palette and contour constants */
|
|
2
|
+
// Vectorizes the small RGBA logo bitmap stored in `art.image` into flat-color
|
|
3
|
+
// layers, so Logo QR draws a crisp logo (smooth vector edges at any export
|
|
4
|
+
// size) instead of a blocky color grid. Everything stays plain filled paths,
|
|
5
|
+
// which the SVG, PNG, PDF and EPS outputs all draw the same way.
|
|
6
|
+
//
|
|
7
|
+
// 1. Palette: the logo's main flat colors (a histogram of opaque pixels).
|
|
8
|
+
// 2. Soft membership: each pixel's share of its nearest palette colors, so
|
|
9
|
+
// anti-aliased edges keep their sub-pixel position.
|
|
10
|
+
// 3. Stacked layers: largest color at the bottom; layer k covers every pixel
|
|
11
|
+
// whose colors from k upwards add up to at least half, so neighbouring
|
|
12
|
+
// colors meet without seams.
|
|
13
|
+
// 4. Marching squares at the 0.5 level, then Douglas–Peucker simplification
|
|
14
|
+
// and gently rounded corners.
|
|
15
|
+
|
|
16
|
+
import { roundTo } from "./utils/fmt";
|
|
17
|
+
|
|
18
|
+
/** sRGB color, channels 0–1 (gamma encoded) */
|
|
19
|
+
export type RGB = [number, number, number];
|
|
20
|
+
|
|
21
|
+
export interface ArtBitmap {
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
data: Uint8Array;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface PaletteColor {
|
|
28
|
+
color: RGB;
|
|
29
|
+
/** Share of the logo's opaque area, 0–1 */
|
|
30
|
+
area: number;
|
|
31
|
+
/** Area-weighted center, in image pixels */
|
|
32
|
+
cx: number;
|
|
33
|
+
cy: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface TracedLogo {
|
|
37
|
+
width: number;
|
|
38
|
+
height: number;
|
|
39
|
+
/** Layers bottom to top; `d` is SVG path data in image pixels */
|
|
40
|
+
layers: { color: RGB; d: string }[];
|
|
41
|
+
/** Largest first */
|
|
42
|
+
palette: PaletteColor[];
|
|
43
|
+
/**
|
|
44
|
+
* True when the image is photographic (many colors, no flat regions): the
|
|
45
|
+
* logo is then drawn as a fine pixel mosaic instead of traced shapes.
|
|
46
|
+
*/
|
|
47
|
+
photographic: boolean;
|
|
48
|
+
/** Opaque logo pixels' coverage of the image box, 0–1 */
|
|
49
|
+
coverage: number;
|
|
50
|
+
/** Per-pixel opacity after backdrop removal, 0–1 */
|
|
51
|
+
alpha: Float32Array;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const srgb = (byte: number) => byte / 255;
|
|
55
|
+
|
|
56
|
+
const distance2 = (a: RGB, b: RGB) =>
|
|
57
|
+
(a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2;
|
|
58
|
+
|
|
59
|
+
/** Luma of a gamma-encoded color, as camera binarizers see it. */
|
|
60
|
+
export const luma = (c: RGB) => 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Logos saved as JPEG (or with a solid backdrop) have no transparency: when
|
|
64
|
+
* the four corners share one opaque color, treat that color as background.
|
|
65
|
+
*/
|
|
66
|
+
export function backdropColor(image: ArtBitmap): RGB | null {
|
|
67
|
+
const { width, height, data } = image;
|
|
68
|
+
const corners = [0, width - 1, (height - 1) * width, height * width - 1].map(
|
|
69
|
+
(index) => {
|
|
70
|
+
const i = index * 4;
|
|
71
|
+
return {
|
|
72
|
+
color: [srgb(data[i]), srgb(data[i + 1]), srgb(data[i + 2])] as RGB,
|
|
73
|
+
alpha: data[i + 3] / 255,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
if (corners.some((corner) => corner.alpha < 0.9)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const first = corners[0].color;
|
|
81
|
+
return corners.every(({ color }) => distance2(color, first) < 0.006)
|
|
82
|
+
? first
|
|
83
|
+
: null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Opacity per pixel, with a detected solid backdrop made transparent. */
|
|
87
|
+
function opacity(image: ArtBitmap): Float32Array {
|
|
88
|
+
const { width, height, data } = image;
|
|
89
|
+
const backdrop = backdropColor(image);
|
|
90
|
+
const alpha = new Float32Array(width * height);
|
|
91
|
+
for (let p = 0; p < alpha.length; p++) {
|
|
92
|
+
const i = p * 4;
|
|
93
|
+
let a = data[i + 3] / 255;
|
|
94
|
+
if (backdrop && a > 0) {
|
|
95
|
+
const d = Math.sqrt(
|
|
96
|
+
distance2(
|
|
97
|
+
[srgb(data[i]), srgb(data[i + 1]), srgb(data[i + 2])],
|
|
98
|
+
backdrop
|
|
99
|
+
)
|
|
100
|
+
);
|
|
101
|
+
// Fully backdrop below 0.06, fully logo above 0.16 (soft edge between)
|
|
102
|
+
a *= Math.min(1, Math.max(0, (d - 0.06) / 0.1));
|
|
103
|
+
}
|
|
104
|
+
alpha[p] = a;
|
|
105
|
+
}
|
|
106
|
+
return alpha;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const MAX_COLORS = 8;
|
|
110
|
+
/** Palette colors closer than this (sRGB distance) merge */
|
|
111
|
+
const MERGE_DISTANCE = 0.14;
|
|
112
|
+
/** Smallest share of the logo a palette color needs */
|
|
113
|
+
const MIN_SHARE = 0.006;
|
|
114
|
+
|
|
115
|
+
function extractPalette(
|
|
116
|
+
image: ArtBitmap,
|
|
117
|
+
alpha: Float32Array
|
|
118
|
+
): { palette: PaletteColor[]; error: number } {
|
|
119
|
+
const { width, height, data } = image;
|
|
120
|
+
// 4 bits per channel histogram of solid pixels
|
|
121
|
+
const count = new Float64Array(4096);
|
|
122
|
+
const sums = new Float64Array(4096 * 3);
|
|
123
|
+
let total = 0;
|
|
124
|
+
for (let p = 0; p < alpha.length; p++) {
|
|
125
|
+
if (alpha[p] < 0.9) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const i = p * 4;
|
|
129
|
+
const bin =
|
|
130
|
+
Math.floor(data[i] / 16) * 256 +
|
|
131
|
+
Math.floor(data[i + 1] / 16) * 16 +
|
|
132
|
+
Math.floor(data[i + 2] / 16);
|
|
133
|
+
count[bin] += 1;
|
|
134
|
+
sums[bin * 3] += srgb(data[i]);
|
|
135
|
+
sums[bin * 3 + 1] += srgb(data[i + 1]);
|
|
136
|
+
sums[bin * 3 + 2] += srgb(data[i + 2]);
|
|
137
|
+
total++;
|
|
138
|
+
}
|
|
139
|
+
if (total === 0) {
|
|
140
|
+
return { palette: [], error: 0 };
|
|
141
|
+
}
|
|
142
|
+
const bins = [...count.keys()]
|
|
143
|
+
.filter((bin) => count[bin] > 0)
|
|
144
|
+
.sort((a, b) => count[b] - count[a]);
|
|
145
|
+
const centers: RGB[] = [];
|
|
146
|
+
for (const bin of bins) {
|
|
147
|
+
if (count[bin] / total < MIN_SHARE / 2 || centers.length >= MAX_COLORS) {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
const color: RGB = [
|
|
151
|
+
sums[bin * 3] / count[bin],
|
|
152
|
+
sums[bin * 3 + 1] / count[bin],
|
|
153
|
+
sums[bin * 3 + 2] / count[bin],
|
|
154
|
+
];
|
|
155
|
+
if (centers.every((c) => distance2(c, color) >= MERGE_DISTANCE ** 2)) {
|
|
156
|
+
centers.push(color);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Two k-means rounds over solid pixels to settle the colors
|
|
160
|
+
let stats: {
|
|
161
|
+
n: number;
|
|
162
|
+
r: number;
|
|
163
|
+
g: number;
|
|
164
|
+
b: number;
|
|
165
|
+
x: number;
|
|
166
|
+
y: number;
|
|
167
|
+
}[] = [];
|
|
168
|
+
let error = 0;
|
|
169
|
+
for (let round = 0; round < 3; round++) {
|
|
170
|
+
stats = centers.map(() => ({ n: 0, r: 0, g: 0, b: 0, x: 0, y: 0 }));
|
|
171
|
+
error = 0;
|
|
172
|
+
for (let p = 0; p < alpha.length; p++) {
|
|
173
|
+
if (alpha[p] < 0.9) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const i = p * 4;
|
|
177
|
+
const color: RGB = [srgb(data[i]), srgb(data[i + 1]), srgb(data[i + 2])];
|
|
178
|
+
let best = 0;
|
|
179
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
180
|
+
for (const [k, center] of centers.entries()) {
|
|
181
|
+
const d = distance2(color, center);
|
|
182
|
+
if (d < bestDistance) {
|
|
183
|
+
bestDistance = d;
|
|
184
|
+
best = k;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const s = stats[best];
|
|
188
|
+
s.n++;
|
|
189
|
+
s.r += color[0];
|
|
190
|
+
s.g += color[1];
|
|
191
|
+
s.b += color[2];
|
|
192
|
+
s.x += p % width;
|
|
193
|
+
s.y += Math.floor(p / width);
|
|
194
|
+
error += Math.sqrt(bestDistance);
|
|
195
|
+
}
|
|
196
|
+
for (const [k, s] of stats.entries()) {
|
|
197
|
+
if (s.n > 0) {
|
|
198
|
+
centers[k] = [s.r / s.n, s.g / s.n, s.b / s.n];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const palette = centers
|
|
203
|
+
.map((color, k) => ({
|
|
204
|
+
color,
|
|
205
|
+
area: stats[k].n / total,
|
|
206
|
+
cx: stats[k].n ? stats[k].x / stats[k].n + 0.5 : width / 2,
|
|
207
|
+
cy: stats[k].n ? stats[k].y / stats[k].n + 0.5 : height / 2,
|
|
208
|
+
}))
|
|
209
|
+
.filter((entry) => entry.area >= MIN_SHARE)
|
|
210
|
+
.sort((a, b) => b.area - a.area);
|
|
211
|
+
return { palette, error: error / total };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ── Contours ───────────────────────────────────────────────────────────────
|
|
215
|
+
|
|
216
|
+
type Point = [number, number];
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Iso-contours of `field` (width × height samples at pixel centers) at 0.5,
|
|
220
|
+
* as closed loops in image pixel coordinates; inside is where field ≥ 0.5.
|
|
221
|
+
*/
|
|
222
|
+
function contours(
|
|
223
|
+
field: Float32Array,
|
|
224
|
+
width: number,
|
|
225
|
+
height: number
|
|
226
|
+
): Point[][] {
|
|
227
|
+
// Padded grid so every loop closes inside it
|
|
228
|
+
const w = width + 2;
|
|
229
|
+
const h = height + 2;
|
|
230
|
+
const value = (x: number, y: number) =>
|
|
231
|
+
x <= 0 || y <= 0 || x > width || y > height
|
|
232
|
+
? 0
|
|
233
|
+
: field[(y - 1) * width + (x - 1)];
|
|
234
|
+
// Edge crossing ids: horizontal edge (x,y)-(x+1,y) → 2·(y·w+x),
|
|
235
|
+
// vertical edge (x,y)-(x,y+1) → 2·(y·w+x)+1.
|
|
236
|
+
const next = new Map<number, number>();
|
|
237
|
+
const points = new Map<number, Point>();
|
|
238
|
+
const crossing = (id: number): Point => {
|
|
239
|
+
const cached = points.get(id);
|
|
240
|
+
if (cached) {
|
|
241
|
+
return cached;
|
|
242
|
+
}
|
|
243
|
+
const cell = Math.floor(id / 2);
|
|
244
|
+
const x = cell % w;
|
|
245
|
+
const y = Math.floor(cell / w);
|
|
246
|
+
const a = value(x, y);
|
|
247
|
+
const vertical = id % 2 === 1;
|
|
248
|
+
const b = vertical ? value(x, y + 1) : value(x + 1, y);
|
|
249
|
+
const t = a === b ? 0.5 : Math.min(1, Math.max(0, (0.5 - a) / (b - a)));
|
|
250
|
+
// Sample (x, y) sits at pixel center (x − 0.5, y − 0.5) in image space.
|
|
251
|
+
const point: Point = vertical
|
|
252
|
+
? [x - 0.5, y - 0.5 + t]
|
|
253
|
+
: [x - 0.5 + t, y - 0.5];
|
|
254
|
+
points.set(id, point);
|
|
255
|
+
return point;
|
|
256
|
+
};
|
|
257
|
+
const link = (from: number, to: number) => {
|
|
258
|
+
next.set(from, to);
|
|
259
|
+
};
|
|
260
|
+
for (let y = 0; y < h - 1; y++) {
|
|
261
|
+
for (let x = 0; x < w - 1; x++) {
|
|
262
|
+
const tl = value(x, y) >= 0.5;
|
|
263
|
+
const tr = value(x + 1, y) >= 0.5;
|
|
264
|
+
const br = value(x + 1, y + 1) >= 0.5;
|
|
265
|
+
const bl = value(x, y + 1) >= 0.5;
|
|
266
|
+
const top = 2 * (y * w + x);
|
|
267
|
+
const bottom = 2 * ((y + 1) * w + x);
|
|
268
|
+
const left = 2 * (y * w + x) + 1;
|
|
269
|
+
const right = 2 * (y * w + x + 1) + 1;
|
|
270
|
+
// Segments keep the inside on their left (y-down space), so loops are
|
|
271
|
+
// consistent; `link(a, b)` walks from edge a to edge b.
|
|
272
|
+
const code = (tl ? 8 : 0) + (tr ? 4 : 0) + (br ? 2 : 0) + (bl ? 1 : 0);
|
|
273
|
+
switch (code) {
|
|
274
|
+
case 1:
|
|
275
|
+
link(bottom, left);
|
|
276
|
+
break;
|
|
277
|
+
case 2:
|
|
278
|
+
link(right, bottom);
|
|
279
|
+
break;
|
|
280
|
+
case 3:
|
|
281
|
+
link(right, left);
|
|
282
|
+
break;
|
|
283
|
+
case 4:
|
|
284
|
+
link(top, right);
|
|
285
|
+
break;
|
|
286
|
+
case 5: {
|
|
287
|
+
const center =
|
|
288
|
+
(value(x, y) +
|
|
289
|
+
value(x + 1, y) +
|
|
290
|
+
value(x + 1, y + 1) +
|
|
291
|
+
value(x, y + 1)) /
|
|
292
|
+
4;
|
|
293
|
+
if (center >= 0.5) {
|
|
294
|
+
link(top, left);
|
|
295
|
+
link(bottom, right);
|
|
296
|
+
} else {
|
|
297
|
+
link(top, right);
|
|
298
|
+
link(bottom, left);
|
|
299
|
+
}
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
case 6:
|
|
303
|
+
link(top, bottom);
|
|
304
|
+
break;
|
|
305
|
+
case 7:
|
|
306
|
+
link(top, left);
|
|
307
|
+
break;
|
|
308
|
+
case 8:
|
|
309
|
+
link(left, top);
|
|
310
|
+
break;
|
|
311
|
+
case 9:
|
|
312
|
+
link(bottom, top);
|
|
313
|
+
break;
|
|
314
|
+
case 10: {
|
|
315
|
+
const center =
|
|
316
|
+
(value(x, y) +
|
|
317
|
+
value(x + 1, y) +
|
|
318
|
+
value(x + 1, y + 1) +
|
|
319
|
+
value(x, y + 1)) /
|
|
320
|
+
4;
|
|
321
|
+
if (center >= 0.5) {
|
|
322
|
+
link(left, bottom);
|
|
323
|
+
link(right, top);
|
|
324
|
+
} else {
|
|
325
|
+
link(left, top);
|
|
326
|
+
link(right, bottom);
|
|
327
|
+
}
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
case 11:
|
|
331
|
+
link(right, top);
|
|
332
|
+
break;
|
|
333
|
+
case 12:
|
|
334
|
+
link(left, right);
|
|
335
|
+
break;
|
|
336
|
+
case 13:
|
|
337
|
+
link(bottom, right);
|
|
338
|
+
break;
|
|
339
|
+
case 14:
|
|
340
|
+
link(left, bottom);
|
|
341
|
+
break;
|
|
342
|
+
default:
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const loops: Point[][] = [];
|
|
348
|
+
for (const start of next.keys()) {
|
|
349
|
+
if (!next.has(start)) {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
const loop: Point[] = [];
|
|
353
|
+
let id: number | undefined = start;
|
|
354
|
+
while (id !== undefined && next.has(id)) {
|
|
355
|
+
loop.push(crossing(id));
|
|
356
|
+
const following: number | undefined = next.get(id);
|
|
357
|
+
next.delete(id);
|
|
358
|
+
id = following;
|
|
359
|
+
}
|
|
360
|
+
if (loop.length >= 3) {
|
|
361
|
+
loops.push(loop);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return loops;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function signedArea(loop: Point[]): number {
|
|
368
|
+
let area = 0;
|
|
369
|
+
for (let i = 0; i < loop.length; i++) {
|
|
370
|
+
const a = loop[i];
|
|
371
|
+
const b = loop[(i + 1) % loop.length];
|
|
372
|
+
area += a[0] * b[1] - b[0] * a[1];
|
|
373
|
+
}
|
|
374
|
+
return area / 2;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Douglas–Peucker on an open polyline (keeps both ends). */
|
|
378
|
+
function simplifyOpen(points: Point[], epsilon: number): Point[] {
|
|
379
|
+
if (points.length <= 2) {
|
|
380
|
+
return points;
|
|
381
|
+
}
|
|
382
|
+
const keep = new Uint8Array(points.length);
|
|
383
|
+
keep[0] = 1;
|
|
384
|
+
keep[points.length - 1] = 1;
|
|
385
|
+
const stack: [number, number][] = [[0, points.length - 1]];
|
|
386
|
+
while (stack.length > 0) {
|
|
387
|
+
const [from, to] = stack.pop() as [number, number];
|
|
388
|
+
const a = points[from];
|
|
389
|
+
const b = points[to];
|
|
390
|
+
const dx = b[0] - a[0];
|
|
391
|
+
const dy = b[1] - a[1];
|
|
392
|
+
const length = Math.hypot(dx, dy) || 1;
|
|
393
|
+
let farthest = -1;
|
|
394
|
+
let farthestDistance = epsilon;
|
|
395
|
+
for (let i = from + 1; i < to; i++) {
|
|
396
|
+
const p = points[i];
|
|
397
|
+
const d = Math.abs(dy * (p[0] - a[0]) - dx * (p[1] - a[1])) / length;
|
|
398
|
+
if (d > farthestDistance) {
|
|
399
|
+
farthestDistance = d;
|
|
400
|
+
farthest = i;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (farthest >= 0) {
|
|
404
|
+
keep[farthest] = 1;
|
|
405
|
+
stack.push([from, farthest], [farthest, to]);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return points.filter((_, i) => keep[i] === 1);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function simplifyLoop(loop: Point[], epsilon: number): Point[] {
|
|
412
|
+
// Split at the point farthest from the first, simplify both halves.
|
|
413
|
+
let far = 0;
|
|
414
|
+
let farDistance = -1;
|
|
415
|
+
for (const [i, p] of loop.entries()) {
|
|
416
|
+
const d = (p[0] - loop[0][0]) ** 2 + (p[1] - loop[0][1]) ** 2;
|
|
417
|
+
if (d > farDistance) {
|
|
418
|
+
farDistance = d;
|
|
419
|
+
far = i;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const first = simplifyOpen(loop.slice(0, far + 1), epsilon);
|
|
423
|
+
const second = simplifyOpen([...loop.slice(far), loop[0]], epsilon);
|
|
424
|
+
return [...first.slice(0, -1), ...second.slice(0, -1)];
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Logo outlines are in sample pixels, drawn scaled down: 0.01 is plenty
|
|
428
|
+
const r2 = (n: number) => roundTo(n, 2);
|
|
429
|
+
|
|
430
|
+
/** Closed path through `loop` with corners rounded by at most `radius`. */
|
|
431
|
+
function smoothPath(loop: Point[], radius: number): string {
|
|
432
|
+
const n = loop.length;
|
|
433
|
+
const toward = (from: Point, to: Point): Point => {
|
|
434
|
+
const length = Math.hypot(to[0] - from[0], to[1] - from[1]) || 1;
|
|
435
|
+
const t = Math.min(0.5, radius / length);
|
|
436
|
+
return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
|
|
437
|
+
};
|
|
438
|
+
let d = "";
|
|
439
|
+
for (let i = 0; i < n; i++) {
|
|
440
|
+
const p = loop[i];
|
|
441
|
+
const a = toward(p, loop[(i - 1 + n) % n]);
|
|
442
|
+
const b = toward(p, loop[(i + 1) % n]);
|
|
443
|
+
d += i === 0 ? `M${r2(a[0])} ${r2(a[1])}` : `L${r2(a[0])} ${r2(a[1])}`;
|
|
444
|
+
d += `Q${r2(p[0])} ${r2(p[1])} ${r2(b[0])} ${r2(b[1])}`;
|
|
445
|
+
}
|
|
446
|
+
return `${d}Z`;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function layerPath(field: Float32Array, width: number, height: number) {
|
|
450
|
+
let d = "";
|
|
451
|
+
for (const loop of contours(field, width, height)) {
|
|
452
|
+
if (Math.abs(signedArea(loop)) < 0.8) {
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
const simple = simplifyLoop(loop, 0.18);
|
|
456
|
+
if (simple.length >= 3) {
|
|
457
|
+
d += smoothPath(simple, 0.9);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return d;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** Mean distance (sRGB) above which an image counts as photographic. */
|
|
464
|
+
const PHOTO_ERROR = 0.075;
|
|
465
|
+
|
|
466
|
+
/** Traces a decoded art bitmap (see the file comment). */
|
|
467
|
+
export function traceLogo(image: ArtBitmap): TracedLogo {
|
|
468
|
+
const { width, height, data } = image;
|
|
469
|
+
const alpha = opacity(image);
|
|
470
|
+
let opaque = 0;
|
|
471
|
+
for (const a of alpha) {
|
|
472
|
+
opaque += a;
|
|
473
|
+
}
|
|
474
|
+
const { palette, error } = extractPalette(image, alpha);
|
|
475
|
+
const base = {
|
|
476
|
+
width,
|
|
477
|
+
height,
|
|
478
|
+
palette,
|
|
479
|
+
alpha,
|
|
480
|
+
coverage: opaque / (width * height),
|
|
481
|
+
};
|
|
482
|
+
if (palette.length === 0 || error > PHOTO_ERROR) {
|
|
483
|
+
return { ...base, layers: [], photographic: palette.length > 0 };
|
|
484
|
+
}
|
|
485
|
+
const k = palette.length;
|
|
486
|
+
// fields[c] = share of palette color c at each pixel (times opacity)
|
|
487
|
+
const fields = Array.from(
|
|
488
|
+
{ length: k },
|
|
489
|
+
() => new Float32Array(width * height)
|
|
490
|
+
);
|
|
491
|
+
const colors = palette.map((entry) => entry.color);
|
|
492
|
+
for (let p = 0; p < alpha.length; p++) {
|
|
493
|
+
const a = alpha[p];
|
|
494
|
+
if (a <= 0) {
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
const i = p * 4;
|
|
498
|
+
const color: RGB = [srgb(data[i]), srgb(data[i + 1]), srgb(data[i + 2])];
|
|
499
|
+
let first = 0;
|
|
500
|
+
let second = -1;
|
|
501
|
+
let d1 = Number.POSITIVE_INFINITY;
|
|
502
|
+
let d2 = Number.POSITIVE_INFINITY;
|
|
503
|
+
for (let c = 0; c < k; c++) {
|
|
504
|
+
const d = distance2(color, colors[c]);
|
|
505
|
+
if (d < d1) {
|
|
506
|
+
second = first;
|
|
507
|
+
d2 = d1;
|
|
508
|
+
first = c;
|
|
509
|
+
d1 = d;
|
|
510
|
+
} else if (d < d2) {
|
|
511
|
+
second = c;
|
|
512
|
+
d2 = d;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
if (second < 0 || d1 < 1e-6) {
|
|
516
|
+
fields[first][p] = a;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
// Position of the color between its two nearest palette colors
|
|
520
|
+
const pa = colors[first];
|
|
521
|
+
const pb = colors[second];
|
|
522
|
+
const ab: RGB = [pb[0] - pa[0], pb[1] - pa[1], pb[2] - pa[2]];
|
|
523
|
+
const t = Math.min(
|
|
524
|
+
1,
|
|
525
|
+
Math.max(
|
|
526
|
+
0,
|
|
527
|
+
((color[0] - pa[0]) * ab[0] +
|
|
528
|
+
(color[1] - pa[1]) * ab[1] +
|
|
529
|
+
(color[2] - pa[2]) * ab[2]) /
|
|
530
|
+
(ab[0] ** 2 + ab[1] ** 2 + ab[2] ** 2)
|
|
531
|
+
)
|
|
532
|
+
);
|
|
533
|
+
fields[first][p] = a * (1 - t);
|
|
534
|
+
fields[second][p] = a * t;
|
|
535
|
+
}
|
|
536
|
+
// Stacked layers: layer c covers where colors c…k−1 add up to ≥ 0.5.
|
|
537
|
+
const layers: { color: RGB; d: string }[] = [];
|
|
538
|
+
const cumulative = new Float32Array(width * height);
|
|
539
|
+
const stacked: Float32Array[] = new Array(k);
|
|
540
|
+
for (let c = k - 1; c >= 0; c--) {
|
|
541
|
+
const field = fields[c];
|
|
542
|
+
for (let p = 0; p < cumulative.length; p++) {
|
|
543
|
+
cumulative[p] += field[p];
|
|
544
|
+
}
|
|
545
|
+
stacked[c] = cumulative.slice();
|
|
546
|
+
}
|
|
547
|
+
for (let c = 0; c < k; c++) {
|
|
548
|
+
const d = layerPath(stacked[c], width, height);
|
|
549
|
+
if (d) {
|
|
550
|
+
layers.push({ color: colors[c], d });
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return { ...base, layers, photographic: false };
|
|
554
|
+
}
|