forked from jimrandomh/faceclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.ts
More file actions
314 lines (285 loc) · 9.61 KB
/
Copy pathimage.ts
File metadata and controls
314 lines (285 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { BdfFont, Glyph } from "./bdffont";
import { wrapText } from "./textwrap";
export const G2_LENS_WIDTH = 640;
export const G2_LENS_HEIGHT = 480;
const DEFAULT_CORNER_RADIUS = 8;
export function imageFromAsciiArt(lines: readonly string[], value = 255): GrayImage {
const width = Math.max(0, ...lines.map((line) => line.length));
const image = new GrayImage(width, lines.length, 0);
const fill = clampByte(value);
for (let y = 0; y < lines.length; y++) {
const line = lines[y]!;
for (let x = 0; x < line.length; x++) {
const pixel = line[x];
if (pixel && pixel !== " " && pixel !== ".") {
image.pixels[y * width + x] = fill;
}
}
}
return image;
}
export class GrayImage {
readonly width: number;
readonly height: number;
readonly pixels: Uint8Array;
constructor(width: number, height: number, fill = 0) {
this.width = width;
this.height = height;
this.pixels = new Uint8Array(width * height);
this.clear(fill);
}
clone(): GrayImage {
const clone = new GrayImage(this.width, this.height, 0);
clone.pixels.set(this.pixels);
return clone;
}
clear(value = 0): void {
this.pixels.fill(clampByte(value));
}
setPixel(x: number, y: number, value: number): void {
this.setPixelUnchecked(x, y, clampByte(value));
}
private setPixelUnchecked(x: number, y: number, value: number): void {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
this.pixels[y * this.width + x] = value;
}
getPixel(x: number, y: number): number {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return 0;
return this.pixels[y * this.width + x] ?? 0;
}
fillRect(x: number, y: number, width: number, height: number, value: number): void {
const left = Math.max(0, x | 0);
const top = Math.max(0, y | 0);
const right = Math.min(this.width, (x + width) | 0);
const bottom = Math.min(this.height, (y + height) | 0);
const fill = clampByte(value);
for (let row = top; row < bottom; row++) {
const offset = row * this.width;
for (let col = left; col < right; col++) {
this.pixels[offset + col] = fill;
}
}
}
drawRect(x: number, y: number, width: number, height: number, value: number): void {
this.drawLine(x, y, x + width - 1, y, value);
this.drawLine(x, y, x, y + height - 1, value);
this.drawLine(x + width - 1, y, x + width - 1, y + height - 1, value);
this.drawLine(x, y + height - 1, x + width - 1, y + height - 1, value);
}
drawLine(x0: number, y0: number, x1: number, y1: number, value: number): void {
let ax = Math.round(x0);
let ay = Math.round(y0);
const bx = Math.round(x1);
const by = Math.round(y1);
const stroke = clampByte(value);
const dx = Math.abs(bx - ax);
const sx = ax < bx ? 1 : -1;
const dy = -Math.abs(by - ay);
const sy = ay < by ? 1 : -1;
let err = dx + dy;
while (true) {
this.setPixelUnchecked(ax, ay, stroke);
if (ax === bx && ay === by) break;
const e2 = 2 * err;
if (e2 >= dy) {
err += dy;
ax += sx;
}
if (e2 <= dx) {
err += dx;
ay += sy;
}
}
}
drawText(font: BdfFont, x: number, y: number, text: string, value: number): void {
let cursorX = x;
const fill = clampByte(value);
for (const char of text) {
if (char === "\n") {
cursorX = x;
y += font.lineHeight;
continue;
}
const glyph = font.getGlyph(char.codePointAt(0) ?? 32);
if (!glyph) continue;
this.drawGlyph(font, glyph, cursorX, y, fill);
cursorX += glyph.dwidthX;
}
}
drawTextWrapped({font, x, y, width, text, value }: {
font: BdfFont;
x: number;
y: number;
width: number;
text: string;
value: number;
}): void {
const lines = wrapText(font, text, width);
for (let i = 0; i < lines.length; i++) {
this.drawText(font, x, y + i * font.lineHeight, lines[i]!, value);
}
}
bitBlt(
source: GrayImage,
dx: number,
dy: number,
opts: {
sx?: number;
sy?: number;
width?: number;
height?: number;
/** Skip source pixels with value 0 (color-key transparency). */
transparentZero?: boolean;
} = {},
): void {
const srcX = Math.max(0, (opts.sx ?? 0) | 0);
const srcY = Math.max(0, (opts.sy ?? 0) | 0);
const copyWidth = Math.max(0, (opts.width ?? (source.width - srcX)) | 0);
const copyHeight = Math.max(0, (opts.height ?? (source.height - srcY)) | 0);
const destX = dx | 0;
const destY = dy | 0;
for (let row = 0; row < copyHeight; row++) {
const sy = srcY + row;
const ty = destY + row;
if (sy < 0 || sy >= source.height || ty < 0 || ty >= this.height) continue;
for (let col = 0; col < copyWidth; col++) {
const sx = srcX + col;
const tx = destX + col;
if (sx < 0 || sx >= source.width || tx < 0 || tx >= this.width) continue;
const value = source.pixels[sy * source.width + sx] ?? 0;
if (opts.transparentZero && value === 0) continue;
this.pixels[ty * this.width + tx] = value;
}
}
}
fillRoundedRect(
x: number,
y: number,
width: number,
height: number,
value: number,
radius = DEFAULT_CORNER_RADIUS,
): void {
const left = x | 0;
const top = y | 0;
const rectWidth = width | 0;
const rectHeight = height | 0;
const bottom = top + rectHeight;
const fill = clampByte(value);
for (let row = top; row < bottom; row++) {
if (row < 0 || row >= this.height) continue;
const span = roundedRectRowSpan(row, left, top, rectWidth, rectHeight, radius);
if (!span) continue;
const start = Math.max(0, span.left);
const end = Math.min(this.width, span.right);
const offset = row * this.width;
for (let col = start; col < end; col++) {
this.pixels[offset + col] = fill;
}
}
}
drawRoundedRect(
x: number,
y: number,
width: number,
height: number,
value: number,
radius = DEFAULT_CORNER_RADIUS,
): void {
const left = x | 0;
const top = y | 0;
const rectWidth = width | 0;
const rectHeight = height | 0;
const bottom = top + rectHeight;
const stroke = clampByte(value);
const innerLeft = left + 1;
const innerTop = top + 1;
const innerWidth = rectWidth - 2;
const innerHeight = rectHeight - 2;
for (let row = top; row < bottom; row++) {
if (row < 0 || row >= this.height) continue;
const outer = roundedRectRowSpan(row, left, top, rectWidth, rectHeight, radius);
if (!outer) continue;
const inner =
innerWidth > 0 && innerHeight > 0
? roundedRectRowSpan(row, innerLeft, innerTop, innerWidth, innerHeight, Math.max(0, radius - 1))
: undefined;
const offset = row * this.width;
const outerLeft = Math.max(0, outer.left);
const outerRight = Math.min(this.width, outer.right);
if (!inner) {
for (let col = outerLeft; col < outerRight; col++) {
this.pixels[offset + col] = stroke;
}
continue;
}
const innerLeftClamped = Math.max(outerLeft, Math.min(outerRight, inner.left));
const innerRightClamped = Math.max(outerLeft, Math.min(outerRight, inner.right));
for (let col = outerLeft; col < innerLeftClamped; col++) {
this.pixels[offset + col] = stroke;
}
for (let col = innerRightClamped; col < outerRight; col++) {
this.pixels[offset + col] = stroke;
}
}
}
fingerprint(): string {
let hash = 2166136261;
for (let i = 0; i < this.pixels.length; i++) {
hash ^= this.pixels[i]!;
hash = Math.imul(hash, 16777619);
}
return `fnv:${(hash >>> 0).toString(16)}`;
}
/**
* Snapshot of the full image as a single 8-bit-per-pixel grayscale buffer,
* row-major and top-to-bottom. The Java side turns this into whatever wire
* format the firmware currently expects (today: a 4bpp BMP), so all framing
* concerns stay on one side of the bridge.
*/
to8bppBuffer(): Uint8Array {
return Uint8Array.from(this.pixels);
}
private drawGlyph(font: BdfFont, glyph: Glyph, x: number, y: number, value: number): void {
const baselineY = y + font.ascent;
const top = baselineY - (glyph.bbxHeight + glyph.bbxY);
const left = x + glyph.bbxX;
const rowBitWidth = ((glyph.bbxWidth + 7) >> 3) << 3;
for (let row = 0; row < glyph.bbxHeight; row++) {
const bits = glyph.bitmapRows[row] ?? 0;
for (let col = 0; col < glyph.bbxWidth; col++) {
const shift = rowBitWidth - 1 - col;
if (((bits >> shift) & 1) !== 0) {
this.setPixelUnchecked(left + col, top + row, value);
}
}
}
}
}
function clampByte(value: number): number {
return Math.max(0, Math.min(255, Math.round(value)));
}
function roundedRectRowSpan(
row: number,
x: number,
y: number,
width: number,
height: number,
radius: number,
): { left: number; right: number } | undefined {
if (width <= 0 || height <= 0 || row < y || row >= y + height) return undefined;
const r = Math.max(0, Math.min(radius | 0, (width / 2) | 0, (height / 2) | 0));
if (r === 0) return { left: x, right: x + width };
const cy = row + 0.5;
const topCornerBottom = y + r;
const bottomCornerTop = y + height - r;
if (cy >= topCornerBottom && cy < bottomCornerTop) {
return { left: x, right: x + width };
}
const cornerY = cy < topCornerBottom ? topCornerBottom : bottomCornerTop;
const dy = cy - cornerY;
const dxSquared = r * r - dy * dy;
if (dxSquared < 0) return undefined;
const inset = (r - Math.sqrt(dxSquared) + 0.5) | 0;
return { left: x + inset, right: x + width - inset };
}