imagescript 3.7.0 → 3.8.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.
@@ -0,0 +1,939 @@
1
+ /*!
2
+ * SVG filter effects on premultiplied RGBA pixels, ported from resvg (https://github.com/linebender/resvg,
3
+ * crates/resvg/src/filter: mod.rs, box_blur.rs (itself based on https://github.com/fschutt/fastblur), iir_blur.rs,
4
+ * color_matrix.rs, component_transfer.rs, composite.rs, morphology.rs, convolve_matrix.rs, turbulence.rs (itself from
5
+ * the reference implementation in the SVG 1.1 specification), lighting.rs, displacement_map.rs; Copyright 2017 the Resvg
6
+ * Authors, MIT License) and the blend modes of tiny-skia (https://github.com/linebender/tiny-skia,
7
+ * src/pipeline/highp.rs; Copyright 2011 Google Inc., 2020 Yevhenii Reizner, BSD-3-Clause License,
8
+ * itself from Skia). The IIR blur
9
+ * follows Pascal Getreuer's gaussianiir (http://www.getreuer.info/home/gaussianiir; Simplified BSD License), an
10
+ * implementation of Alvarez and Mazorra's fast Gaussian convolution. See THIRD_PARTY_NOTICES.md
11
+ */
12
+ const TO_LINEAR = new Uint8Array(256), TO_SRGB = new Uint8Array(256);
13
+ for (let i = 0; i < 256; i++) {
14
+ const c = i / 255;
15
+ TO_LINEAR[i] = Math.round((c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4) * 255);
16
+ TO_SRGB[i] = Math.round((c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055) * 255);
17
+ }
18
+ const u8 = (v) => v >= 255 ? 255 : v > 0 ? Math.trunc(v) : 0;
19
+ const clamp = (min, v, max) => v > max ? max : v >= min ? v : min;
20
+ const create = (width, height) => ({ width, height, data: new Uint8Array(width * height * 4) });
21
+ const clone = (p) => ({ width: p.width, height: p.height, data: p.data.slice() });
22
+ function demultiply(d) {
23
+ for (let i = 0; i < d.length; i += 4) {
24
+ const a = d[i + 3] / 255;
25
+ d[i] = u8(d[i] / a + 0.5);
26
+ d[i + 1] = u8(d[i + 1] / a + 0.5);
27
+ d[i + 2] = u8(d[i + 2] / a + 0.5);
28
+ }
29
+ }
30
+ function multiply(d) {
31
+ for (let i = 0; i < d.length; i += 4) {
32
+ const a = d[i + 3] / 255;
33
+ d[i] = u8(d[i] * a + 0.5);
34
+ d[i + 1] = u8(d[i + 1] * a + 0.5);
35
+ d[i + 2] = u8(d[i + 2] * a + 0.5);
36
+ }
37
+ }
38
+ function convert(d, table) {
39
+ demultiply(d);
40
+ for (let i = 0; i < d.length; i += 4) {
41
+ d[i] = table[d[i]];
42
+ d[i + 1] = table[d[i + 1]];
43
+ d[i + 2] = table[d[i + 2]];
44
+ }
45
+ multiply(d);
46
+ }
47
+ function inColorSpace(result, linear) {
48
+ if (result.linear === linear)
49
+ return result;
50
+ const pixmap = clone(result.pixmap);
51
+ convert(pixmap.data, linear ? TO_LINEAR : TO_SRGB);
52
+ return { pixmap, region: result.region, linear };
53
+ }
54
+ function draw(dst, src, dx, dy, mode = 'over') {
55
+ const s = src.data, d = dst.data;
56
+ if (mode === 'over') {
57
+ for (let y = Math.max(0, dy); y < Math.min(dst.height, src.height + dy); y++) {
58
+ const x0 = Math.max(0, dx), x1 = Math.min(dst.width, src.width + dx);
59
+ for (let i = (y * dst.width + x0) * 4, j = ((y - dy) * src.width + x0 - dx) * 4, end = (y * dst.width + x1) * 4; i < end; i += 4, j += 4) {
60
+ const sa = s[j + 3];
61
+ if (sa === 0)
62
+ continue;
63
+ if (sa === 255) {
64
+ d[i] = s[j];
65
+ d[i + 1] = s[j + 1];
66
+ d[i + 2] = s[j + 2];
67
+ d[i + 3] = 255;
68
+ continue;
69
+ }
70
+ const fd = 255 - sa;
71
+ d[i] = Math.round((s[j] * 255 + d[i] * fd) / 255);
72
+ d[i + 1] = Math.round((s[j + 1] * 255 + d[i + 1] * fd) / 255);
73
+ d[i + 2] = Math.round((s[j + 2] * 255 + d[i + 2] * fd) / 255);
74
+ d[i + 3] = Math.round((sa * 255 + d[i + 3] * fd) / 255);
75
+ }
76
+ }
77
+ return;
78
+ }
79
+ for (let y = Math.max(0, dy); y < Math.min(dst.height, src.height + dy); y++) {
80
+ for (let x = Math.max(0, dx); x < Math.min(dst.width, src.width + dx); x++) {
81
+ const i = (y * dst.width + x) * 4, j = ((y - dy) * src.width + x - dx) * 4;
82
+ const sa = s[j + 3], da = d[i + 3];
83
+ let fs, fd;
84
+ switch (mode) {
85
+ case 'in':
86
+ fs = da;
87
+ fd = 0;
88
+ break;
89
+ case 'out':
90
+ fs = 255 - da;
91
+ fd = 0;
92
+ break;
93
+ case 'atop':
94
+ fs = da;
95
+ fd = 255 - sa;
96
+ break;
97
+ case 'xor':
98
+ fs = 255 - da;
99
+ fd = 255 - sa;
100
+ break;
101
+ case 'lighter':
102
+ fs = 255;
103
+ fd = 255;
104
+ break;
105
+ default:
106
+ fs = 255;
107
+ fd = 255 - sa;
108
+ }
109
+ for (let c = 0; c < 4; c++)
110
+ d[i + c] = u8(Math.round((s[j + c] * fs + d[i + c] * fd) / 255));
111
+ }
112
+ }
113
+ }
114
+ function clearOutside(p, r) {
115
+ const x0 = clamp(0, r.x, p.width), x1 = clamp(0, r.x + r.width, p.width), y0 = clamp(0, r.y, p.height), y1 = clamp(0, r.y + r.height, p.height);
116
+ const row = p.width * 4;
117
+ p.data.fill(0, 0, y0 * row);
118
+ p.data.fill(0, y1 * row);
119
+ for (let y = y0; y < y1; y++) {
120
+ p.data.fill(0, y * row, (y * p.width + x0) * 4);
121
+ p.data.fill(0, (y * p.width + x1) * 4, (y + 1) * row);
122
+ }
123
+ }
124
+ function intRect(r, m) {
125
+ const xs = [r.x, r.x + r.width], ys = [r.y, r.y + r.height];
126
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
127
+ for (const x of xs)
128
+ for (const y of ys) {
129
+ const tx = m[0] * x + m[2] * y + m[4], ty = m[1] * x + m[3] * y + m[5];
130
+ x0 = Math.min(x0, tx);
131
+ y0 = Math.min(y0, ty);
132
+ x1 = Math.max(x1, tx);
133
+ y1 = Math.max(y1, ty);
134
+ }
135
+ return { x: Math.floor(x0), y: Math.floor(y0), width: Math.max(1, Math.ceil(x1 - x0)), height: Math.max(1, Math.ceil(y1 - y0)) };
136
+ }
137
+ const sameRect = (a, b) => a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
138
+ const scaleOf = (m) => [Math.hypot(m[0], m[1]), Math.hypot(m[2], m[3])];
139
+ export function applyFilter(filter, ts, source) {
140
+ const result = filterResult(filter, ts, source);
141
+ source.data.fill(0);
142
+ if (!result)
143
+ return;
144
+ const { image, region } = result;
145
+ draw(source, inColorSpace(image, false).pixmap, region.x, region.y);
146
+ }
147
+ function filterResult(filter, ts, full) {
148
+ const r = intRect(filter.rect, ts);
149
+ const x0 = Math.max(r.x, 0), y0 = Math.max(r.y, 0), x1 = Math.min(r.x + r.width, full.width), y1 = Math.min(r.y + r.height, full.height);
150
+ if (x1 <= x0 || y1 <= y0)
151
+ return null;
152
+ const outputRegion = { x: x0, y: y0, width: x1 - x0, height: y1 - y0 };
153
+ let source = full;
154
+ if (!sameRect(outputRegion, { x: 0, y: 0, width: full.width, height: full.height })) {
155
+ source = create(outputRegion.width, outputRegion.height);
156
+ for (let y = 0; y < source.height; y++)
157
+ source.data.set(full.data.subarray(((y + y0) * full.width + x0) * 4, ((y + y0) * full.width + x1) * 4), y * source.width * 4);
158
+ }
159
+ const matrix = [ts[0], ts[1], ts[2], ts[3], ts[4] - x0, ts[5] - y0];
160
+ const region = { x: 0, y: 0, width: outputRegion.width, height: outputRegion.height };
161
+ const results = [];
162
+ const input = (value) => {
163
+ if (value === 'SourceGraphic' || value === 'SourceAlpha') {
164
+ const pixmap = clone(source);
165
+ if (value === 'SourceAlpha')
166
+ for (let i = 0; i < pixmap.data.length; i += 4)
167
+ pixmap.data[i] = pixmap.data[i + 1] = pixmap.data[i + 2] = 0;
168
+ return { pixmap, region, linear: false };
169
+ }
170
+ for (let i = results.length - 1; i >= 0; i--)
171
+ if (results[i].name === value.result)
172
+ return results[i].image;
173
+ return input('SourceGraphic');
174
+ };
175
+ for (const primitive of filter.primitives) {
176
+ const { kind } = primitive;
177
+ let subregion = intRect(primitive.rect, matrix);
178
+ if (kind.type === 'offset' && typeof kind.input === 'object') {
179
+ const name = kind.input.result;
180
+ for (let i = results.length - 1; i >= 0; i--)
181
+ if (results[i].name === name) {
182
+ subregion = results[i].image.region;
183
+ break;
184
+ }
185
+ }
186
+ let result = apply(kind, primitive.linear, matrix, region, subregion, input);
187
+ if (!sameRect(region, subregion)) {
188
+ const pixmap = clone(result.pixmap);
189
+ if (kind.type !== 'offset')
190
+ clearOutside(pixmap, subregion);
191
+ result = { pixmap, region: subregion, linear: result.linear };
192
+ }
193
+ results.push({ name: primitive.result, image: result });
194
+ }
195
+ const last = results.pop();
196
+ return last ? { image: last.image, region: outputRegion } : null;
197
+ }
198
+ function apply(kind, linear, ts, region, subregion, input) {
199
+ const [scaleX, scaleY] = scaleOf(ts);
200
+ const own = (pixmap, space = linear) => ({ pixmap, region: { x: 0, y: 0, width: pixmap.width, height: pixmap.height }, linear: space });
201
+ switch (kind.type) {
202
+ case 'blur': {
203
+ const source = input(kind.input);
204
+ const sigma = stdDeviation(kind.sx * scaleX, kind.sy * scaleY);
205
+ if (!sigma)
206
+ return source;
207
+ const pixmap = clone(inColorSpace(source, linear).pixmap);
208
+ blur(pixmap, sigma);
209
+ return own(pixmap);
210
+ }
211
+ case 'offset': {
212
+ const source = input(kind.input);
213
+ const dx = kind.dx * scaleX, dy = kind.dy * scaleY;
214
+ if (Math.abs(dx) < 1e-6 && Math.abs(dy) < 1e-6)
215
+ return source;
216
+ const pixmap = create(source.pixmap.width, source.pixmap.height);
217
+ draw(pixmap, source.pixmap, Math.trunc(dx), Math.trunc(dy));
218
+ return own(pixmap, source.linear);
219
+ }
220
+ case 'flood': {
221
+ const pixmap = create(region.width, region.height);
222
+ const a = Math.round(kind.opacity * 255) / 255;
223
+ const color = [u8(kind.color[0] * a + 0.5), u8(kind.color[1] * a + 0.5), u8(kind.color[2] * a + 0.5), Math.round(a * 255)];
224
+ for (let i = 0; i < pixmap.data.length; i += 4)
225
+ pixmap.data.set(color, i);
226
+ return own(pixmap, false);
227
+ }
228
+ case 'composite': {
229
+ const a = inColorSpace(input(kind.input), linear).pixmap, b = inColorSpace(input(kind.input2), linear).pixmap;
230
+ const pixmap = create(region.width, region.height);
231
+ if (kind.operator === 'arithmetic') {
232
+ const [k1, k2, k3, k4] = kind.k, d = pixmap.data;
233
+ const calc = (i1, i2, max) => {
234
+ const x = i1 / 255, y = i2 / 255;
235
+ return clamp(0, k1 * x * y + k2 * x + k3 * y + k4, max);
236
+ };
237
+ for (let i = 0; i < d.length; i += 4) {
238
+ const alpha = calc(a.data[i + 3], b.data[i + 3], 1);
239
+ if (Math.abs(alpha) < 1e-6)
240
+ continue;
241
+ for (let c = 0; c < 3; c++)
242
+ d[i + c] = u8(calc(a.data[i + c], b.data[i + c], alpha) * 255);
243
+ d[i + 3] = u8(alpha * 255);
244
+ }
245
+ return own(pixmap);
246
+ }
247
+ draw(pixmap, b, 0, 0);
248
+ draw(pixmap, a, 0, 0, kind.operator);
249
+ return own(pixmap);
250
+ }
251
+ case 'merge': {
252
+ const pixmap = create(region.width, region.height);
253
+ for (const value of kind.inputs)
254
+ draw(pixmap, inColorSpace(input(value), linear).pixmap, 0, 0);
255
+ return own(pixmap);
256
+ }
257
+ case 'dropShadow': {
258
+ const source = inColorSpace(input(kind.input), linear).pixmap;
259
+ const dx = kind.dx * scaleX, dy = kind.dy * scaleY;
260
+ const pixmap = create(source.width, source.height);
261
+ const shadow = clone(source);
262
+ const sigma = stdDeviation(kind.sx * scaleX, kind.sy * scaleY);
263
+ if (sigma)
264
+ blur(shadow, sigma);
265
+ const opacity = Math.round(kind.opacity * 255) / 255, d = shadow.data;
266
+ for (let i = 0; i < d.length; i += 4) {
267
+ const a = opacity * d[i + 3] / 255;
268
+ d[i] = u8(kind.color[0] * a + 0.5);
269
+ d[i + 1] = u8(kind.color[1] * a + 0.5);
270
+ d[i + 2] = u8(kind.color[2] * a + 0.5);
271
+ d[i + 3] = u8(a * 255 + 0.5);
272
+ }
273
+ convert(d, linear ? TO_LINEAR : TO_SRGB);
274
+ draw(pixmap, shadow, Math.trunc(dx), Math.trunc(dy));
275
+ draw(pixmap, source, 0, 0);
276
+ return own(pixmap);
277
+ }
278
+ case 'colorMatrix': {
279
+ const pixmap = clone(inColorSpace(input(kind.input), linear).pixmap);
280
+ demultiply(pixmap.data);
281
+ colorMatrix(kind.matrix, pixmap.data);
282
+ multiply(pixmap.data);
283
+ return own(pixmap);
284
+ }
285
+ case 'componentTransfer': {
286
+ const pixmap = clone(inColorSpace(input(kind.input), linear).pixmap);
287
+ demultiply(pixmap.data);
288
+ const d = pixmap.data;
289
+ for (let c = 0; c < 4; c++) {
290
+ const f = kind.functions[c];
291
+ if (!f || ((f.type === 'table' || f.type === 'discrete') && !f.values.length))
292
+ continue;
293
+ const table = new Uint8Array(256);
294
+ for (let v = 0; v < 256; v++)
295
+ table[v] = transfer(f, v);
296
+ for (let i = c; i < d.length; i += 4)
297
+ d[i] = table[d[i]];
298
+ }
299
+ multiply(pixmap.data);
300
+ return own(pixmap);
301
+ }
302
+ case 'blend': {
303
+ const a = inColorSpace(input(kind.input), linear).pixmap, b = inColorSpace(input(kind.input2), linear).pixmap;
304
+ const pixmap = create(region.width, region.height);
305
+ draw(pixmap, b, 0, 0);
306
+ blend(pixmap, a, kind.mode);
307
+ return own(pixmap);
308
+ }
309
+ case 'morphology': {
310
+ const pixmap = clone(inColorSpace(input(kind.input), linear).pixmap);
311
+ const rx = kind.rx * scaleX, ry = kind.ry * scaleY;
312
+ if (!(rx > 0 && ry > 0))
313
+ pixmap.data.fill(0);
314
+ else
315
+ morphology(pixmap, kind.dilate, rx, ry);
316
+ return own(pixmap);
317
+ }
318
+ case 'convolve': {
319
+ const pixmap = clone(inColorSpace(input(kind.input), linear).pixmap);
320
+ if (kind.preserveAlpha)
321
+ demultiply(pixmap.data);
322
+ convolve(pixmap, kind);
323
+ return own(pixmap);
324
+ }
325
+ case 'turbulence': {
326
+ const pixmap = create(region.width, region.height);
327
+ if (Math.abs(scaleX) < 1e-6 || Math.abs(scaleY) < 1e-6)
328
+ return own(pixmap);
329
+ turbulence(pixmap, region.x - ts[4], region.y - ts[5], scaleX, scaleY, kind);
330
+ multiply(pixmap.data);
331
+ return own(pixmap);
332
+ }
333
+ case 'displacement': {
334
+ const src = inColorSpace(input(kind.input), linear).pixmap, map = inColorSpace(input(kind.input2), linear).pixmap;
335
+ const pixmap = create(region.width, region.height);
336
+ const sx = kind.scale * scaleX, sy = kind.scale * scaleY, w = src.width, h = src.height;
337
+ const round = (v) => Math.sign(v) * Math.round(Math.abs(v));
338
+ for (let y = 0; y < Math.min(h, map.height); y++)
339
+ for (let x = 0; x < Math.min(w, map.width); x++) {
340
+ const m = (y * map.width + x) * 4;
341
+ const ox = round(x + (map.data[m + kind.xChannel] / 255 - 0.5) * sx * kind.scale);
342
+ const oy = round(y + (map.data[m + kind.yChannel] / 255 - 0.5) * sy * kind.scale);
343
+ if (ox < 0 || ox >= w || oy < 0 || oy >= h)
344
+ continue;
345
+ pixmap.data.set(src.data.subarray((oy * w + ox) * 4, (oy * w + ox) * 4 + 4), (y * pixmap.width + x) * 4);
346
+ }
347
+ return own(pixmap);
348
+ }
349
+ case 'image': {
350
+ const pixmap = kind.draw([scaleX, 0, 0, scaleY, subregion.x, subregion.y], region.width, region.height) || create(region.width, region.height);
351
+ return own(pixmap, false);
352
+ }
353
+ case 'lighting': {
354
+ const pixmap = create(region.width, region.height);
355
+ lighting(kind, transformLight(kind.light, region, ts), input(kind.input).pixmap, pixmap);
356
+ return own(pixmap);
357
+ }
358
+ case 'tile': {
359
+ const source = input(kind.input);
360
+ const r = source.region, pixmap = create(region.width, region.height);
361
+ if (r.width <= 0 || r.height <= 0)
362
+ return own(pixmap, false);
363
+ const s = source.pixmap.data, d = pixmap.data;
364
+ for (let y = 0; y < pixmap.height; y++) {
365
+ const sy = r.y + (((y - r.y) % r.height) + r.height) % r.height;
366
+ for (let x = 0; x < pixmap.width; x++) {
367
+ const sx = r.x + (((x - r.x) % r.width) + r.width) % r.width;
368
+ if (sx < 0 || sy < 0 || sx >= source.pixmap.width || sy >= source.pixmap.height)
369
+ continue;
370
+ const j = (sy * source.pixmap.width + sx) * 4, i = (y * pixmap.width + x) * 4;
371
+ d[i] = s[j];
372
+ d[i + 1] = s[j + 1];
373
+ d[i + 2] = s[j + 2];
374
+ d[i + 3] = s[j + 3];
375
+ }
376
+ }
377
+ return own(pixmap, false);
378
+ }
379
+ }
380
+ }
381
+ function stdDeviation(sx, sy) {
382
+ if (Math.abs(sx) < 1e-6 && Math.abs(sy) < 1e-6)
383
+ return null;
384
+ if (sx < 0.05)
385
+ sx = 0;
386
+ if (sy < 0.05)
387
+ sy = 0;
388
+ return { sx, sy, box: sx >= 2 || sy >= 2 };
389
+ }
390
+ function blur(p, { sx, sy, box }) {
391
+ if (box)
392
+ boxBlur(p, sx, sy);
393
+ else
394
+ iirBlur(p, sx, sy);
395
+ }
396
+ const roundF32 = (x) => Math.fround(Math.fround(x + 12582912) - 12582912);
397
+ function boxSizes(sigma) {
398
+ if (!(sigma > 0))
399
+ return [1, 1, 1, 1, 1];
400
+ const n = 5, f = Math.fround;
401
+ const ideal = f(Math.sqrt(f(f(12 * sigma * sigma) / n)) + 1);
402
+ let wl = Math.floor(ideal);
403
+ if (wl % 2 === 0)
404
+ wl--;
405
+ const wu = wl + 2;
406
+ const m = Math.round(f(f(12 * sigma * sigma - n * wl * wl - 4 * n * wl - 3 * n) / f(-4 * wl - 4)));
407
+ return Array.from({ length: n }, (_, i) => i < m ? wl : wu);
408
+ }
409
+ function boxRows(src, dst, width, height, r) {
410
+ if (r === 0) {
411
+ dst.set(src);
412
+ return;
413
+ }
414
+ const scale = Math.fround(1 / (2 * r + 1));
415
+ for (let y = 0; y < height; y++) {
416
+ const o = y * width * 4;
417
+ let s0 = 0, s1 = 0, s2 = 0, s3 = 0;
418
+ for (let k = 0; k < Math.min(r, width); k++) {
419
+ const i = o + k * 4;
420
+ s0 += src[i];
421
+ s1 += src[i + 1];
422
+ s2 += src[i + 2];
423
+ s3 += src[i + 3];
424
+ }
425
+ for (let t = 0; t < width; t++) {
426
+ if (t + r < width) {
427
+ const i = o + (t + r) * 4;
428
+ s0 += src[i];
429
+ s1 += src[i + 1];
430
+ s2 += src[i + 2];
431
+ s3 += src[i + 3];
432
+ }
433
+ if (t > r) {
434
+ const i = o + (t - r - 1) * 4;
435
+ s0 -= src[i];
436
+ s1 -= src[i + 1];
437
+ s2 -= src[i + 2];
438
+ s3 -= src[i + 3];
439
+ }
440
+ const i = o + t * 4;
441
+ dst[i] = roundF32(Math.fround(s0 * scale));
442
+ dst[i + 1] = roundF32(Math.fround(s1 * scale));
443
+ dst[i + 2] = roundF32(Math.fround(s2 * scale));
444
+ dst[i + 3] = roundF32(Math.fround(s3 * scale));
445
+ }
446
+ }
447
+ }
448
+ function boxColumns(src, dst, width, height, r) {
449
+ if (r === 0) {
450
+ dst.set(src);
451
+ return;
452
+ }
453
+ const scale = Math.fround(1 / (2 * r + 1)), row = width * 4;
454
+ const sums = new Int32Array(row);
455
+ for (let k = 0; k < Math.min(r, height); k++)
456
+ for (let i = 0; i < row; i++)
457
+ sums[i] += src[k * row + i];
458
+ for (let t = 0; t < height; t++) {
459
+ if (t + r < height)
460
+ for (let i = 0, o = (t + r) * row; i < row; i++)
461
+ sums[i] += src[o + i];
462
+ if (t > r)
463
+ for (let i = 0, o = (t - r - 1) * row; i < row; i++)
464
+ sums[i] -= src[o + i];
465
+ for (let i = 0, o = t * row; i < row; i++)
466
+ dst[o + i] = roundF32(Math.fround(sums[i] * scale));
467
+ }
468
+ }
469
+ function boxBlur(p, sx, sy) {
470
+ const { width, height, data } = p;
471
+ const horizontal = boxSizes(sx), vertical = boxSizes(sy);
472
+ const back = new Uint8Array(data.length);
473
+ for (let i = 0; i < 5; i++) {
474
+ boxColumns(data, back, width, height, (vertical[i] - 1) >> 1);
475
+ boxRows(back, data, width, height, (horizontal[i] - 1) >> 1);
476
+ }
477
+ }
478
+ function iirBlur(p, sx, sy) {
479
+ const { width, height, data } = p, steps = 4;
480
+ const buf = new Float64Array(width * height);
481
+ const coefficients = (sigma) => {
482
+ const lambda = sigma * sigma / (2 * steps);
483
+ return [lambda, (1 + 2 * lambda - Math.sqrt(1 + 4 * lambda)) / (2 * lambda)];
484
+ };
485
+ for (let c = 0; c < 4; c++) {
486
+ for (let i = 0; i < buf.length; i++)
487
+ buf[i] = data[i * 4 + c] / 255;
488
+ let lambdaX = 1, nuX = 1, lambdaY = 1, nuY = 1;
489
+ if (sx > 0) {
490
+ [lambdaX, nuX] = coefficients(sx);
491
+ for (let y = 0; y < height; y++) {
492
+ const o = y * width;
493
+ for (let s = 0; s < steps; s++) {
494
+ for (let x = 1; x < width; x++)
495
+ buf[o + x] += nuX * buf[o + x - 1];
496
+ for (let x = width - 1; x > 0; x--)
497
+ buf[o + x - 1] += nuX * buf[o + x];
498
+ }
499
+ }
500
+ }
501
+ if (sy > 0) {
502
+ [lambdaY, nuY] = coefficients(sy);
503
+ for (let x = 0; x < width; x++)
504
+ for (let s = 0; s < steps; s++) {
505
+ for (let y = width; y < buf.length; y += width)
506
+ buf[x + y] += nuY * buf[x + y - width];
507
+ for (let y = buf.length - width; y > 0; y -= width)
508
+ buf[x + y - width] += nuY * buf[x + y];
509
+ }
510
+ }
511
+ const post = (Math.sqrt(nuX * nuY) / Math.sqrt(lambdaX * lambdaY)) ** (2 * steps);
512
+ for (let i = 0; i < buf.length; i++)
513
+ data[i * 4 + c] = u8(buf[i] * post * 255);
514
+ }
515
+ }
516
+ function colorMatrix(matrix, d) {
517
+ const out = (v) => u8(clamp(0, v, 1) * 255);
518
+ if (matrix.type === 'luminanceToAlpha') {
519
+ for (let i = 0; i < d.length; i += 4) {
520
+ const a = d[i] / 255 * 0.2125 + d[i + 1] / 255 * 0.7154 + d[i + 2] / 255 * 0.0721;
521
+ d[i] = d[i + 1] = d[i + 2] = 0;
522
+ d[i + 3] = out(a);
523
+ }
524
+ return;
525
+ }
526
+ if (matrix.type === 'matrix') {
527
+ const m = matrix.values;
528
+ for (let i = 0; i < d.length; i += 4) {
529
+ const r = d[i] / 255, g = d[i + 1] / 255, b = d[i + 2] / 255, a = d[i + 3] / 255;
530
+ d[i] = out(r * m[0] + g * m[1] + b * m[2] + a * m[3] + m[4]);
531
+ d[i + 1] = out(r * m[5] + g * m[6] + b * m[7] + a * m[8] + m[9]);
532
+ d[i + 2] = out(r * m[10] + g * m[11] + b * m[12] + a * m[13] + m[14]);
533
+ d[i + 3] = out(r * m[15] + g * m[16] + b * m[17] + a * m[18] + m[19]);
534
+ }
535
+ return;
536
+ }
537
+ let m;
538
+ if (matrix.type === 'saturate') {
539
+ const v = Math.max(0, matrix.value);
540
+ m = [0.213 + 0.787 * v, 0.715 - 0.715 * v, 0.072 - 0.072 * v, 0.213 - 0.213 * v, 0.715 + 0.285 * v, 0.072 - 0.072 * v,
541
+ 0.213 - 0.213 * v, 0.715 - 0.715 * v, 0.072 + 0.928 * v];
542
+ }
543
+ else {
544
+ const angle = matrix.angle * Math.PI / 180, a1 = Math.cos(angle), a2 = Math.sin(angle);
545
+ m = [0.213 + 0.787 * a1 - 0.213 * a2, 0.715 - 0.715 * a1 - 0.715 * a2, 0.072 - 0.072 * a1 + 0.928 * a2,
546
+ 0.213 - 0.213 * a1 + 0.143 * a2, 0.715 + 0.285 * a1 + 0.140 * a2, 0.072 - 0.072 * a1 - 0.283 * a2,
547
+ 0.213 - 0.213 * a1 - 0.787 * a2, 0.715 - 0.715 * a1 + 0.715 * a2, 0.072 + 0.928 * a1 + 0.072 * a2];
548
+ }
549
+ for (let i = 0; i < d.length; i += 4) {
550
+ const r = d[i] / 255, g = d[i + 1] / 255, b = d[i + 2] / 255;
551
+ d[i] = out(r * m[0] + g * m[1] + b * m[2]);
552
+ d[i + 1] = out(r * m[3] + g * m[4] + b * m[5]);
553
+ d[i + 2] = out(r * m[6] + g * m[7] + b * m[8]);
554
+ }
555
+ }
556
+ function transfer(f, value) {
557
+ const c = value / 255;
558
+ let v;
559
+ if (f.type === 'linear')
560
+ v = f.slope * c + f.intercept;
561
+ else if (f.type === 'gamma')
562
+ v = f.amplitude * c ** f.exponent + f.offset;
563
+ else if (f.type === 'table') {
564
+ const n = f.values.length - 1, k = Math.min(Math.floor(c * n), n);
565
+ v = k === n ? f.values[k] : f.values[k] + (c - k / n) * n * (f.values[k + 1] - f.values[k]);
566
+ }
567
+ else {
568
+ const n = f.values.length;
569
+ v = f.values[Math.min(Math.floor(c * n), n - 1)];
570
+ }
571
+ return u8(clamp(0, v, 1) * 255);
572
+ }
573
+ function blend(dst, src, mode) {
574
+ const s = src.data, d = dst.data;
575
+ const inv = (v) => 1 - v;
576
+ const lum = (r, g, b) => r * 0.3 + g * 0.59 + b * 0.11;
577
+ const sat = (r, g, b) => Math.max(r, g, b) - Math.min(r, g, b);
578
+ const setSat = (c, value) => {
579
+ const mn = Math.min(c[0], c[1], c[2]), mx = Math.max(c[0], c[1], c[2]), range = mx - mn;
580
+ for (let k = 0; k < 3; k++)
581
+ c[k] = range === 0 ? 0 : (c[k] - mn) * value / range;
582
+ };
583
+ const setLum = (c, l) => {
584
+ const diff = l - lum(c[0], c[1], c[2]);
585
+ for (let k = 0; k < 3; k++)
586
+ c[k] += diff;
587
+ };
588
+ const clipColor = (c, a) => {
589
+ const mn = Math.min(c[0], c[1], c[2]), mx = Math.max(c[0], c[1], c[2]), l = lum(c[0], c[1], c[2]);
590
+ for (let k = 0; k < 3; k++) {
591
+ let v = c[k];
592
+ if (!(mx >= 0))
593
+ v = l + (v - l) * l / (l - mn);
594
+ if (mx > a)
595
+ v = l + (v - l) * (a - l) / (mx - l);
596
+ c[k] = Math.max(v, 0);
597
+ }
598
+ };
599
+ const separable = (sc, dc, sa, da) => {
600
+ switch (mode) {
601
+ case 'multiply': return sc * inv(da) + dc * inv(sa) + sc * dc;
602
+ case 'screen': return sc + dc - sc * dc;
603
+ case 'darken': return sc + dc - Math.max(sc * da, dc * sa);
604
+ case 'lighten': return sc + dc - Math.min(sc * da, dc * sa);
605
+ case 'difference': return sc + dc - 2 * Math.min(sc * da, dc * sa);
606
+ case 'exclusion': return sc + dc - 2 * sc * dc;
607
+ case 'color-burn':
608
+ return dc === da ? dc + sc * inv(da) : sc === 0 ? dc * inv(sa) : sa * (da - Math.min(da, (da - dc) * sa / sc)) + sc * inv(da) + dc * inv(sa);
609
+ case 'color-dodge':
610
+ return dc === 0 ? sc * inv(da) : sc === sa ? sc + dc * inv(sa) : sa * Math.min(da, dc * sa / (sa - sc)) + sc * inv(da) + dc * inv(sa);
611
+ case 'hard-light':
612
+ return sc * inv(da) + dc * inv(sa) + (2 * sc <= sa ? 2 * sc * dc : sa * da - 2 * (da - dc) * (sa - sc));
613
+ case 'overlay':
614
+ return sc * inv(da) + dc * inv(sa) + (2 * dc <= da ? 2 * sc * dc : sa * da - 2 * (da - dc) * (sa - sc));
615
+ case 'soft-light': {
616
+ const m = da > 0 ? dc / da : 0, s2 = 2 * sc, m4 = 4 * m;
617
+ const darkSrc = dc * (sa + (s2 - sa) * (1 - m));
618
+ const darkDst = (m4 * m4 + m4) * (m - 1) + 7 * m;
619
+ const liteDst = Math.sqrt(m) - m;
620
+ const liteSrc = dc * sa + da * (s2 - sa) * (4 * dc <= da ? darkDst : liteDst);
621
+ return sc * inv(da) + dc * inv(sa) + (s2 <= sa ? darkSrc : liteSrc);
622
+ }
623
+ default: return sc + dc * inv(sa);
624
+ }
625
+ };
626
+ const c = [0, 0, 0];
627
+ for (let i = 0; i < d.length; i += 4) {
628
+ const r = s[i] / 255, g = s[i + 1] / 255, b = s[i + 2] / 255, a = s[i + 3] / 255;
629
+ const dr = d[i] / 255, dg = d[i + 1] / 255, db = d[i + 2] / 255, da = d[i + 3] / 255;
630
+ let out;
631
+ if (mode === 'hue' || mode === 'saturation' || mode === 'color' || mode === 'luminosity') {
632
+ if (mode === 'hue') {
633
+ c[0] = r * a;
634
+ c[1] = g * a;
635
+ c[2] = b * a;
636
+ setSat(c, sat(dr, dg, db) * a);
637
+ setLum(c, lum(dr, dg, db) * a);
638
+ }
639
+ else if (mode === 'saturation') {
640
+ c[0] = dr * a;
641
+ c[1] = dg * a;
642
+ c[2] = db * a;
643
+ setSat(c, sat(r, g, b) * da);
644
+ setLum(c, lum(dr, dg, db) * a);
645
+ }
646
+ else if (mode === 'color') {
647
+ c[0] = r * da;
648
+ c[1] = g * da;
649
+ c[2] = b * da;
650
+ setLum(c, lum(dr, dg, db) * a);
651
+ }
652
+ else {
653
+ c[0] = dr * a;
654
+ c[1] = dg * a;
655
+ c[2] = db * a;
656
+ setLum(c, lum(r, g, b) * da);
657
+ }
658
+ clipColor(c, a * da);
659
+ out = [r * inv(da) + dr * inv(a) + c[0], g * inv(da) + dg * inv(a) + c[1], b * inv(da) + db * inv(a) + c[2], a + da - a * da];
660
+ }
661
+ else
662
+ out = [separable(r, dr, a, da), separable(g, dg, a, da), separable(b, db, a, da), da * inv(a) + a];
663
+ for (let k = 0; k < 4; k++)
664
+ d[i + k] = u8(clamp(0, out[k], 1) * 255 + 0.5);
665
+ }
666
+ }
667
+ function morphology(p, dilate, rx, ry) {
668
+ const { width, height, data } = p;
669
+ const xr = Math.min(Math.round(rx), width - 1), yr = Math.min(Math.round(ry), height - 1);
670
+ const tmp = new Uint8Array(data.length);
671
+ const queue = new Int32Array(Math.max(width, height));
672
+ const pass = (src, dst, start, step, n, r) => {
673
+ let head = 0, tail = 0, next = 0;
674
+ for (let i = 0; i < n; i++) {
675
+ for (; next <= Math.min(i + r, n - 1); next++) {
676
+ const v = src[start + next * step];
677
+ while (tail > head && (dilate ? src[start + queue[tail - 1] * step] <= v : src[start + queue[tail - 1] * step] >= v))
678
+ tail--;
679
+ queue[tail++] = next;
680
+ }
681
+ while (queue[head] < i - r)
682
+ head++;
683
+ dst[start + i * step] = src[start + queue[head] * step];
684
+ }
685
+ };
686
+ for (let c = 0; c < 4; c++) {
687
+ for (let y = 0; y < height; y++)
688
+ pass(data, tmp, y * width * 4 + c, 4, width, xr);
689
+ for (let x = 0; x < width; x++)
690
+ pass(tmp, data, x * 4 + c, width * 4, height, yr);
691
+ }
692
+ }
693
+ function convolve(p, m) {
694
+ const { width, height, data } = p, out = new Uint8Array(data.length);
695
+ for (let y = 0; y < height; y++)
696
+ for (let x = 0; x < width; x++) {
697
+ let r = 0, g = 0, b = 0, a = 0;
698
+ for (let oy = 0; oy < m.rows; oy++)
699
+ for (let ox = 0; ox < m.columns; ox++) {
700
+ let tx = x - m.targetX + ox, ty = y - m.targetY + oy;
701
+ if (m.edge === 'none') {
702
+ if (tx < 0 || tx >= width || ty < 0 || ty >= height)
703
+ continue;
704
+ }
705
+ else if (m.edge === 'wrap') {
706
+ tx = ((tx % width) + width) % width;
707
+ ty = ((ty % height) + height) % height;
708
+ }
709
+ else {
710
+ tx = clamp(0, tx, width - 1);
711
+ ty = clamp(0, ty, height - 1);
712
+ }
713
+ const k = m.kernel[(m.rows - oy - 1) * m.columns + m.columns - ox - 1], i = (ty * width + tx) * 4;
714
+ r += data[i] / 255 * k;
715
+ g += data[i + 1] / 255 * k;
716
+ b += data[i + 2] / 255 * k;
717
+ if (!m.preserveAlpha)
718
+ a += data[i + 3] / 255 * k;
719
+ }
720
+ const o = (y * width + x) * 4;
721
+ a = m.preserveAlpha ? data[o + 3] / 255 : a / m.divisor + m.bias;
722
+ const bounded = clamp(0, a, 1);
723
+ const calc = (v) => {
724
+ v = v / m.divisor + m.bias * a;
725
+ return u8((m.preserveAlpha ? clamp(0, v, 1) * bounded : clamp(0, v, bounded)) * 255 + 0.5);
726
+ };
727
+ out[o] = calc(r);
728
+ out[o + 1] = calc(g);
729
+ out[o + 2] = calc(b);
730
+ out[o + 3] = u8(bounded * 255 + 0.5);
731
+ }
732
+ data.set(out);
733
+ }
734
+ function turbulence(p, offsetX, offsetY, sx, sy, t) {
735
+ const M = 2147483647, A = 16807, Q = 127773, R = 2836, SIZE = 256, N = 4096;
736
+ const random = (seed) => {
737
+ const result = A * (seed % Q) - R * Math.trunc(seed / Q);
738
+ return result <= 0 ? result + M : result;
739
+ };
740
+ let seed = t.seed;
741
+ if (seed <= 0)
742
+ seed = -seed % (M - 1) + 1;
743
+ if (seed > M - 1)
744
+ seed = M - 1;
745
+ const lattice = new Int32Array(SIZE + SIZE + 2), gradient = Array.from({ length: 4 }, () => new Float64Array((SIZE + SIZE + 2) * 2));
746
+ for (let k = 0; k < 4; k++)
747
+ for (let i = 0; i < SIZE; i++) {
748
+ lattice[i] = i;
749
+ for (let j = 0; j < 2; j++) {
750
+ seed = random(seed);
751
+ gradient[k][i * 2 + j] = ((seed % (SIZE + SIZE)) - SIZE) / SIZE;
752
+ }
753
+ const g = gradient[k], len = Math.sqrt(g[i * 2] * g[i * 2] + g[i * 2 + 1] * g[i * 2 + 1]);
754
+ g[i * 2] /= len;
755
+ g[i * 2 + 1] /= len;
756
+ }
757
+ for (let i = SIZE - 1; i > 0; i--) {
758
+ const k = lattice[i];
759
+ seed = random(seed);
760
+ const j = seed % SIZE;
761
+ lattice[i] = lattice[j];
762
+ lattice[j] = k;
763
+ }
764
+ for (let i = 0; i < SIZE + 2; i++) {
765
+ lattice[SIZE + i] = lattice[i];
766
+ for (const g of gradient) {
767
+ g[(SIZE + i) * 2] = g[i * 2];
768
+ g[(SIZE + i) * 2 + 1] = g[i * 2 + 1];
769
+ }
770
+ }
771
+ const curve = (v) => v * v * (3 - 2 * v);
772
+ const lerp = (v, a, b) => a + v * (b - a);
773
+ const noise = (channel, x, y, stitch) => {
774
+ let v = x + N;
775
+ let bx0 = Math.trunc(v), bx1 = bx0 + 1;
776
+ const rx0 = v - Math.trunc(v), rx1 = rx0 - 1;
777
+ v = y + N;
778
+ let by0 = Math.trunc(v), by1 = by0 + 1;
779
+ const ry0 = v - Math.trunc(v), ry1 = ry0 - 1;
780
+ if (stitch) {
781
+ if (bx0 >= stitch[2])
782
+ bx0 -= stitch[0];
783
+ if (bx1 >= stitch[2])
784
+ bx1 -= stitch[0];
785
+ if (by0 >= stitch[3])
786
+ by0 -= stitch[1];
787
+ if (by1 >= stitch[3])
788
+ by1 -= stitch[1];
789
+ }
790
+ bx0 &= 255;
791
+ bx1 &= 255;
792
+ by0 &= 255;
793
+ by1 &= 255;
794
+ const i = lattice[bx0], j = lattice[bx1];
795
+ const b00 = lattice[i + by0], b10 = lattice[j + by0], b01 = lattice[i + by1], b11 = lattice[j + by1];
796
+ const g = gradient[channel], cx = curve(rx0), cy = curve(ry0);
797
+ const a = lerp(cx, rx0 * g[b00 * 2] + ry0 * g[b00 * 2 + 1], rx1 * g[b10 * 2] + ry0 * g[b10 * 2 + 1]);
798
+ const b = lerp(cx, rx0 * g[b01 * 2] + ry1 * g[b01 * 2 + 1], rx1 * g[b11 * 2] + ry1 * g[b11 * 2 + 1]);
799
+ return lerp(cy, a, b);
800
+ };
801
+ const sum = (channel, x, y, tileX, tileY, tileWidth, tileHeight) => {
802
+ let fx = t.fx, fy = t.fy, stitch = null;
803
+ if (t.stitch) {
804
+ if (Math.abs(fx) > 1e-6) {
805
+ const lo = Math.floor(tileWidth * fx) / tileWidth, hi = Math.ceil(tileWidth * fx) / tileWidth;
806
+ fx = fx / lo < hi / fx ? lo : hi;
807
+ }
808
+ if (Math.abs(fy) > 1e-6) {
809
+ const lo = Math.floor(tileHeight * fy) / tileHeight, hi = Math.ceil(tileHeight * fy) / tileHeight;
810
+ fy = fy / lo < hi / fy ? lo : hi;
811
+ }
812
+ const w = Math.trunc(tileWidth * fx + 0.5), h = Math.trunc(tileHeight * fy + 0.5);
813
+ stitch = [w, h, Math.trunc(tileX * fx + N + w), Math.trunc(tileY * fy + N + h)];
814
+ }
815
+ let total = 0, ratio = 1;
816
+ x *= fx;
817
+ y *= fy;
818
+ for (let o = 0; o < t.octaves; o++) {
819
+ const n = noise(channel, x, y, stitch);
820
+ total += (t.fractal ? n : Math.abs(n)) / ratio;
821
+ x *= 2;
822
+ y *= 2;
823
+ ratio *= 2;
824
+ if (stitch) {
825
+ stitch[0] *= 2;
826
+ stitch[2] = 2 * stitch[2] - N;
827
+ stitch[1] *= 2;
828
+ stitch[3] = 2 * stitch[3] - N;
829
+ }
830
+ }
831
+ return total;
832
+ };
833
+ const { width, height, data } = p;
834
+ for (let y = 0; y < height; y++)
835
+ for (let x = 0; x < width; x++) {
836
+ const tx = (x + offsetX) / sx, ty = (y + offsetY) / sy, o = (y * width + x) * 4;
837
+ for (let c = 0; c < 4; c++) {
838
+ let n = sum(c, tx, ty, x, y, width, height);
839
+ n = t.fractal ? (n * 255 + 255) / 2 : n * 255;
840
+ data[o + c] = u8(clamp(0, Math.fround(n), 255) + 0.5);
841
+ }
842
+ }
843
+ }
844
+ function transformLight(light, region, ts) {
845
+ if (light.type === 'distant')
846
+ return light;
847
+ const map = (x, y) => [ts[0] * x + ts[2] * y + ts[4] - region.x, ts[1] * x + ts[3] * y + ts[5] - region.y];
848
+ const sz = Math.sqrt(ts[0] * ts[0] + ts[3] * ts[3]) / Math.SQRT2;
849
+ const [x, y] = map(light.x, light.y);
850
+ if (light.type === 'point')
851
+ return { type: 'point', x, y, z: light.z * sz };
852
+ const [atX, atY] = map(light.atX, light.atY);
853
+ return { ...light, x, y, z: light.z * sz, atX, atY, atZ: light.atZ * sz };
854
+ }
855
+ function lighting(fe, light, src, dst) {
856
+ const { width, height } = src;
857
+ if (width < 3 || height < 3)
858
+ return;
859
+ const alpha = (x, y) => src.data[(y * width + x) * 4 + 3];
860
+ const zero = (v) => Math.abs(v) < 1e-6;
861
+ let lx = 1, ly = 1, lz = 1;
862
+ if (light.type === 'distant') {
863
+ const azimuth = light.azimuth * Math.PI / 180, elevation = light.elevation * Math.PI / 180;
864
+ lx = Math.cos(azimuth) * Math.cos(elevation);
865
+ ly = Math.sin(azimuth) * Math.cos(elevation);
866
+ lz = Math.sin(elevation);
867
+ }
868
+ let sdx = 0, sdy = 0, sdz = 0;
869
+ if (light.type === 'spot') {
870
+ sdx = light.atX - light.x;
871
+ sdy = light.atY - light.y;
872
+ sdz = light.atZ - light.z;
873
+ const len = Math.hypot(sdx, sdy, sdz);
874
+ if (!zero(len)) {
875
+ sdx /= len;
876
+ sdy /= len;
877
+ sdz /= len;
878
+ }
879
+ }
880
+ const factor = (fx, fy, nx, ny) => {
881
+ const flat = zero(nx) && zero(ny);
882
+ const x = -nx * fe.surfaceScale / 255 * fx, y = -ny * fe.surfaceScale / 255 * fy;
883
+ const length = Math.hypot(x, y, 1);
884
+ if (!fe.specular)
885
+ return fe.constant * (flat ? lz : (x * lx + y * ly + lz) / length);
886
+ const hx = lx, hy = ly, hz = lz + 1, h = Math.hypot(hx, hy, hz);
887
+ if (zero(h))
888
+ return 0;
889
+ const nh = flat ? hz / h : (x * hx + y * hy + hz) / length / h;
890
+ return fe.constant * (Math.abs(fe.exponent - 1) < 1e-6 ? nh : nh ** fe.exponent);
891
+ };
892
+ const compute = (c, f) => u8(clamp(0, c * f, 255) + 0.5);
893
+ const calc = (x, y, fx, fy, nx, ny) => {
894
+ if (light.type !== 'distant') {
895
+ const nz = alpha(x, y) / 255 * fe.surfaceScale;
896
+ lx = light.x - x;
897
+ ly = light.y - y;
898
+ lz = light.z - nz;
899
+ const len = Math.hypot(lx, ly, lz);
900
+ if (!zero(len)) {
901
+ lx /= len;
902
+ ly /= len;
903
+ lz /= len;
904
+ }
905
+ }
906
+ let color = fe.color;
907
+ if (light.type === 'spot') {
908
+ const minus = -(lx * sdx + ly * sdy + lz * sdz);
909
+ if (minus <= 0 || (light.cone !== null && minus < Math.cos(light.cone * Math.PI / 180)))
910
+ color = [0, 0, 0];
911
+ else {
912
+ const f = minus ** light.exponent;
913
+ color = color.map(c => compute(c, f));
914
+ }
915
+ }
916
+ const f = factor(fx, fy, nx, ny), o = (y * width + x) * 4;
917
+ const r = compute(color[0], f), g = compute(color[1], f), b = compute(color[2], f);
918
+ dst.data[o] = r;
919
+ dst.data[o + 1] = g;
920
+ dst.data[o + 2] = b;
921
+ dst.data[o + 3] = fe.specular ? Math.max(r, g, b) : 255;
922
+ };
923
+ const a = alpha, w = width - 1, h = height - 1;
924
+ calc(0, 0, 2 / 3, 2 / 3, -2 * a(0, 0) + 2 * a(1, 0) - a(0, 1) + a(1, 1), -2 * a(0, 0) - a(1, 0) + 2 * a(0, 1) + a(1, 1));
925
+ calc(w, 0, 2 / 3, 2 / 3, -2 * a(w - 1, 0) + 2 * a(w, 0) - a(w - 1, 1) + a(w, 1), -a(w - 1, 0) - 2 * a(w, 0) + a(w - 1, 1) + 2 * a(w, 1));
926
+ calc(0, h, 2 / 3, 2 / 3, -a(0, h - 1) + a(1, h - 1) - 2 * a(0, h) + 2 * a(1, h), -2 * a(0, h - 1) - a(1, h - 1) + 2 * a(0, h) + a(1, h));
927
+ calc(w, h, 2 / 3, 2 / 3, -a(w - 1, h - 1) + a(w, h - 1) - 2 * a(w - 1, h) + 2 * a(w, h), -a(w - 1, h - 1) - 2 * a(w, h - 1) + a(w - 1, h) + 2 * a(w, h));
928
+ for (let x = 1; x < w; x++) {
929
+ calc(x, 0, 1 / 3, 1 / 2, -2 * a(x - 1, 0) + 2 * a(x + 1, 0) - a(x - 1, 1) + a(x + 1, 1), -a(x - 1, 0) - 2 * a(x, 0) - a(x + 1, 0) + a(x - 1, 1) + 2 * a(x, 1) + a(x + 1, 1));
930
+ calc(x, h, 1 / 3, 1 / 2, -a(x - 1, h - 1) + a(x + 1, h - 1) - 2 * a(x - 1, h) + 2 * a(x + 1, h), -a(x - 1, h - 1) - 2 * a(x, h - 1) - a(x + 1, h - 1) + a(x - 1, h) + 2 * a(x, h) + a(x + 1, h));
931
+ }
932
+ for (let y = 1; y < h; y++) {
933
+ calc(0, y, 1 / 2, 1 / 3, -a(0, y - 1) + a(1, y - 1) - 2 * a(0, y) + 2 * a(1, y) - a(0, y + 1) + a(1, y + 1), -2 * a(0, y - 1) - a(1, y - 1) + 2 * a(0, y + 1) + a(1, y + 1));
934
+ calc(w, y, 1 / 2, 1 / 3, -a(w - 1, y - 1) + a(w, y - 1) - 2 * a(w - 1, y) + 2 * a(w, y) - a(w - 1, y + 1) + a(w, y + 1), -a(w - 1, y - 1) - 2 * a(w, y - 1) + a(w - 1, y + 1) + 2 * a(w, y + 1));
935
+ }
936
+ for (let y = 1; y < h; y++)
937
+ for (let x = 1; x < w; x++)
938
+ calc(x, y, 1 / 4, 1 / 4, -a(x - 1, y - 1) + a(x + 1, y - 1) - 2 * a(x - 1, y) + 2 * a(x + 1, y) - a(x - 1, y + 1) + a(x + 1, y + 1), -a(x - 1, y - 1) - 2 * a(x, y - 1) - a(x + 1, y - 1) + a(x - 1, y + 1) + 2 * a(x, y + 1) + a(x + 1, y + 1));
939
+ }