imagescript 3.2.0 → 3.3.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.
Files changed (59) hide show
  1. package/README.md +8 -3
  2. package/THIRD_PARTY_NOTICES.md +131 -0
  3. package/dist/animation.d.ts +7 -0
  4. package/dist/animation.js +22 -12
  5. package/dist/codecs/crc.js +5 -0
  6. package/dist/codecs/gif.js +30 -52
  7. package/dist/codecs/jpeg.js +9 -27
  8. package/dist/codecs/png.js +22 -10
  9. package/dist/codecs/tga.js +3 -1
  10. package/dist/codecs/tiff.js +7 -2
  11. package/dist/codecs/vp8.js +3 -1
  12. package/dist/codecs/vp8l.js +126 -58
  13. package/dist/codecs/webp.js +11 -9
  14. package/dist/codecs/zlib.js +81 -69
  15. package/dist/frame.d.ts +1 -0
  16. package/dist/frame.js +4 -2
  17. package/dist/gif.d.ts +1 -0
  18. package/dist/gif.js +2 -2
  19. package/dist/image.d.ts +42 -7
  20. package/dist/image.js +224 -113
  21. package/dist/index.js +4 -4
  22. package/dist/render/bidi.js +350 -0
  23. package/dist/render/buffer.js +362 -0
  24. package/dist/render/color.js +164 -0
  25. package/dist/render/colr.js +523 -0
  26. package/dist/render/font.js +173 -241
  27. package/dist/render/hinting.js +12 -4
  28. package/dist/render/layout.js +1553 -0
  29. package/dist/render/linebreak.js +210 -0
  30. package/dist/render/segment.js +106 -0
  31. package/dist/render/shape.js +984 -0
  32. package/dist/render/shapers/arabic.js +284 -0
  33. package/dist/render/shapers/hangul.js +142 -0
  34. package/dist/render/shapers/hebrew.js +49 -0
  35. package/dist/render/shapers/index.js +52 -0
  36. package/dist/render/shapers/indic.js +579 -0
  37. package/dist/render/shapers/khmer.js +104 -0
  38. package/dist/render/shapers/machine.js +129 -0
  39. package/dist/render/shapers/myanmar.js +121 -0
  40. package/dist/render/shapers/syllabic.js +100 -0
  41. package/dist/render/shapers/thai.js +125 -0
  42. package/dist/render/shapers/use.js +235 -0
  43. package/dist/render/svg.js +101 -22
  44. package/dist/render/text.js +373 -103
  45. package/dist/render/unicode-data.js +63 -0
  46. package/dist/render/unicode.js +58 -0
  47. package/dist/text-layout.d.ts +4 -1
  48. package/dist/text-layout.js +2 -2
  49. package/dist/util/alpha.js +43 -0
  50. package/dist/util/blur.js +80 -5
  51. package/dist/util/bytes.js +31 -0
  52. package/dist/util/limits.js +34 -0
  53. package/dist/util/options.js +7 -0
  54. package/dist/util/resample.js +359 -79
  55. package/dist/util/rotate.js +43 -0
  56. package/dist/version.js +1 -1
  57. package/licenses/HarfBuzz.txt +42 -0
  58. package/licenses/Unicode-3.0.txt +39 -0
  59. package/package.json +1 -1
@@ -1,3 +1,10 @@
1
+ /*!
2
+ * The filter coefficients and the box, bilinear and bicubic filters are translated from Pillow's
3
+ * src/libImaging/Resample.c (precompute_coeffs, box_filter, bilinear_filter, bicubic_filter). Copyright © 1997-2011 by
4
+ * Secret Labs AB, © 1995-2011 by Fredrik Lundh and contributors, © 2010 by Jeffrey 'Alex' Clark and contributors,
5
+ * MIT-CMU License (see THIRD_PARTY_NOTICES.md)
6
+ */
7
+ import { littleEndian, isOpaque, premultiply8, unpremultiply8 } from "./alpha.js";
1
8
  export const BOX = { support: 0.5, weight: x => x >= -0.5 && x < 0.5 ? 1 : 0 };
2
9
  export const TRIANGLE = { support: 1, weight: x => Math.max(0, 1 - Math.abs(x)) };
3
10
  export const CUBIC = {
@@ -12,106 +19,379 @@ export const CUBIC = {
12
19
  return 0;
13
20
  }
14
21
  };
15
- function coefficients(inSize, outSize, filter) {
22
+ const BITS = 14;
23
+ const ONE = 1 << BITS;
24
+ function coefficients(inSize, outSize, filter, bits, pad = false) {
16
25
  const scale = inSize / outSize;
17
26
  const filterScale = Math.max(1, scale);
18
27
  const support = filter.support * filterScale;
19
- const size = Math.ceil(support) * 2 + 1;
20
- const start = new Int32Array(outSize);
21
- const count = new Int32Array(outSize);
22
- const weights = new Float64Array(outSize * size);
28
+ const one = 1 << bits;
29
+ const mins = new Int32Array(outSize), maxs = new Int32Array(outSize);
30
+ let taps = 0;
23
31
  for (let i = 0; i < outSize; i++) {
24
32
  const center = (i + 0.5) * scale;
25
- const min = Math.max(0, Math.floor(center - support + 0.5));
26
- const max = Math.min(inSize, Math.floor(center + support + 0.5));
33
+ mins[i] = Math.max(0, Math.floor(center - support + 0.5));
34
+ maxs[i] = Math.min(inSize, Math.floor(center + support + 0.5));
35
+ taps = Math.max(taps, maxs[i] - mins[i]);
36
+ }
37
+ if (pad)
38
+ taps = Math.min(inSize, taps <= 2 ? 2 : taps <= 4 ? 4 : taps);
39
+ const start = new Int32Array(outSize);
40
+ const weights = new Int32Array(outSize * taps);
41
+ const w = new Float64Array(taps);
42
+ for (let i = 0; i < outSize; i++) {
43
+ const center = (i + 0.5) * scale, min = mins[i], n = maxs[i] - min;
27
44
  let total = 0;
28
- for (let x = min; x < max; x++) {
29
- const w = filter.weight((x - center + 0.5) / filterScale);
30
- weights[i * size + x - min] = w;
31
- total += w;
32
- }
33
- if (total !== 0)
34
- for (let k = 0; k < max - min; k++)
35
- weights[i * size + k] /= total;
36
- start[i] = min;
37
- count[i] = max - min;
45
+ for (let k = 0; k < n; k++)
46
+ total += w[k] = filter.weight((min + k - center + 0.5) / filterScale);
47
+ const shift = Math.max(0, min + taps - inSize), o = i * taps + shift;
48
+ start[i] = min - shift;
49
+ let sum = 0, largest = 0;
50
+ for (let k = 0; k < n; k++) {
51
+ sum += weights[o + k] = Math.round(total ? w[k] / total * one : 0);
52
+ if (Math.abs(w[k]) > Math.abs(w[largest]))
53
+ largest = k;
54
+ }
55
+ if (n)
56
+ weights[o + largest] += one - sum;
57
+ }
58
+ return { start, weights, taps };
59
+ }
60
+ const FAST_TAPS = 4;
61
+ export function resample(src, width, height, outWidth, outHeight, filter, out, fast = false) {
62
+ if (littleEndian && filter.support <= 1) {
63
+ if (fast && resampleFast(src, width, height, outWidth, outHeight, filter, out))
64
+ return;
65
+ if (resampleOpaque(src, width, height, outWidth, outHeight, filter, out))
66
+ return;
67
+ }
68
+ resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out);
69
+ }
70
+ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
71
+ const pixels = new Int32Array(src.buffer, src.byteOffset, width * height);
72
+ let all = -1;
73
+ for (let x = 0, last = (height - 1) * width; x < width; x++)
74
+ all &= pixels[x] & pixels[last + x];
75
+ if (all >>> 24 !== 255)
76
+ return false;
77
+ const lanes = new Int32Array(width * outHeight * 2);
78
+ if (outHeight === height) {
79
+ for (let i = 0, o = 0; i < pixels.length; i++, o += 2) {
80
+ const p = pixels[i];
81
+ all &= p;
82
+ lanes[o] = (p & 0xff00ff) << 8;
83
+ lanes[o + 1] = (p & 0xff00) | 0;
84
+ }
85
+ }
86
+ else {
87
+ let coefficientsV = coefficients(height, outHeight, filter, 8);
88
+ const swar = coefficientsV.taps <= 4;
89
+ if (!swar)
90
+ coefficientsV = coefficients(height, outHeight, filter, BITS);
91
+ const { start, weights, taps } = coefficientsV;
92
+ for (let y = 0, o = 0; y < outHeight; y++) {
93
+ const w = y * taps, r0 = start[y] * width;
94
+ if (swar && taps === 4) {
95
+ const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
96
+ const r1 = r0 + width, r2 = r1 + width, r3 = r2 + width;
97
+ for (let x = 0; x < width; x++, o += 2) {
98
+ const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x], p3 = pixels[r3 + x];
99
+ all &= p0 & p1 & p2 & p3;
100
+ lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3);
101
+ lanes[o + 1] = (p0 >> 8 & 255) * w0 + (p1 >> 8 & 255) * w1 + (p2 >> 8 & 255) * w2 + (p3 >> 8 & 255) * w3;
102
+ }
103
+ }
104
+ else if (swar && taps === 3) {
105
+ const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], r1 = r0 + width, r2 = r1 + width;
106
+ for (let x = 0; x < width; x++, o += 2) {
107
+ const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x];
108
+ all &= p0 & p1 & p2;
109
+ lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2);
110
+ lanes[o + 1] = (p0 >> 8 & 255) * w0 + (p1 >> 8 & 255) * w1 + (p2 >> 8 & 255) * w2;
111
+ }
112
+ }
113
+ else if (swar && taps === 2) {
114
+ const w0 = weights[w], w1 = weights[w + 1], r1 = r0 + width;
115
+ for (let x = 0; x < width; x++, o += 2) {
116
+ const p0 = pixels[r0 + x], p1 = pixels[r1 + x];
117
+ all &= p0 & p1;
118
+ lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1);
119
+ lanes[o + 1] = (p0 >> 8 & 255) * w0 + (p1 >> 8 & 255) * w1;
120
+ }
121
+ }
122
+ else if (swar) {
123
+ for (let x = 0; x < width; x++, o += 2) {
124
+ let rb = 0, g = 0;
125
+ for (let k = 0, s = r0 + x; k < taps; k++, s += width) {
126
+ const p = pixels[s], wk = weights[w + k];
127
+ all &= p;
128
+ rb += Math.imul(p & 0xff00ff, wk);
129
+ g += (p >> 8 & 255) * wk;
130
+ }
131
+ lanes[o] = rb;
132
+ lanes[o + 1] = g;
133
+ }
134
+ }
135
+ else {
136
+ for (let x = 0; x < width; x++, o += 2) {
137
+ let r = 0, g = 0, b = 0, k = 0, s = r0 + x;
138
+ for (; k + 1 < taps; k += 2, s += width * 2) {
139
+ const p = pixels[s], q = pixels[s + width], w0 = weights[w + k], w1 = weights[w + k + 1];
140
+ all &= p & q;
141
+ r += (p & 255) * w0 + (q & 255) * w1;
142
+ g += (p >> 8 & 255) * w0 + (q >> 8 & 255) * w1;
143
+ b += (p >> 16 & 255) * w0 + (q >> 16 & 255) * w1;
144
+ }
145
+ if (k < taps) {
146
+ const p = pixels[s], w0 = weights[w + k];
147
+ all &= p;
148
+ r += (p & 255) * w0;
149
+ g += (p >> 8 & 255) * w0;
150
+ b += (p >> 16 & 255) * w0;
151
+ }
152
+ lanes[o] = (r + 32 >> 6) | (b + 32 >> 6) << 16;
153
+ lanes[o + 1] = g + 32 >> 6;
154
+ }
155
+ }
156
+ }
157
+ }
158
+ if (all >>> 24 !== 255)
159
+ return false;
160
+ const result = new Int32Array(out.buffer, out.byteOffset, outWidth * outHeight);
161
+ if (outWidth === width) {
162
+ for (let i = 0, o = 0; i < result.length; i++, o += 2) {
163
+ const rb = lanes[o], g = lanes[o + 1];
164
+ result[i] = ((rb & 0xffff) + 128 >> 8) | (g + 128 >> 8) << 8 | ((rb >>> 16) + 128 >> 8) << 16 | 0xff000000;
165
+ }
166
+ return true;
167
+ }
168
+ const R = 1 << 7 + BITS, S = 8 + BITS;
169
+ const { start, weights, taps } = coefficients(width, outWidth, filter, BITS);
170
+ for (let y = 0, o = 0; y < outHeight; y++) {
171
+ const row = y * width * 2;
172
+ if (taps === 4) {
173
+ for (let x = 0; x < outWidth; x++, o++) {
174
+ const s = row + start[x] * 2, w = x * 4;
175
+ const a0 = lanes[s], a1 = lanes[s + 2], a2 = lanes[s + 4], a3 = lanes[s + 6];
176
+ const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
177
+ const r = (a0 & 0xffff) * w0 + (a1 & 0xffff) * w1 + (a2 & 0xffff) * w2 + (a3 & 0xffff) * w3;
178
+ const b = (a0 >>> 16) * w0 + (a1 >>> 16) * w1 + (a2 >>> 16) * w2 + (a3 >>> 16) * w3;
179
+ const g = lanes[s + 1] * w0 + lanes[s + 3] * w1 + lanes[s + 5] * w2 + lanes[s + 7] * w3;
180
+ result[o] = (r + R >> S) | (g + R >> S) << 8 | (b + R >> S) << 16 | 0xff000000;
181
+ }
182
+ }
183
+ else if (taps === 2) {
184
+ for (let x = 0; x < outWidth; x++, o++) {
185
+ const s = row + start[x] * 2, w = x * 2;
186
+ const a0 = lanes[s], a1 = lanes[s + 2], w0 = weights[w], w1 = weights[w + 1];
187
+ const r = (a0 & 0xffff) * w0 + (a1 & 0xffff) * w1;
188
+ const b = (a0 >>> 16) * w0 + (a1 >>> 16) * w1;
189
+ const g = lanes[s + 1] * w0 + lanes[s + 3] * w1;
190
+ result[o] = (r + R >> S) | (g + R >> S) << 8 | (b + R >> S) << 16 | 0xff000000;
191
+ }
192
+ }
193
+ else {
194
+ for (let x = 0; x < outWidth; x++, o++) {
195
+ let r = 0, g = 0, b = 0, k = 0, s = row + start[x] * 2, w = x * taps;
196
+ for (; k + 1 < taps; k += 2, s += 4) {
197
+ const a0 = lanes[s], a1 = lanes[s + 2], w0 = weights[w + k], w1 = weights[w + k + 1];
198
+ r += (a0 & 0xffff) * w0 + (a1 & 0xffff) * w1;
199
+ b += (a0 >>> 16) * w0 + (a1 >>> 16) * w1;
200
+ g += lanes[s + 1] * w0 + lanes[s + 3] * w1;
201
+ }
202
+ if (k < taps) {
203
+ const a0 = lanes[s], w0 = weights[w + k];
204
+ r += (a0 & 0xffff) * w0;
205
+ b += (a0 >>> 16) * w0;
206
+ g += lanes[s + 1] * w0;
207
+ }
208
+ result[o] = (r + R >> S) | (g + R >> S) << 8 | (b + R >> S) << 16 | 0xff000000;
209
+ }
210
+ }
38
211
  }
39
- return { start, count, weights, size };
212
+ return true;
40
213
  }
41
- export function resample(src, width, height, outWidth, outHeight, filter, out) {
42
- let plane = new Float32Array(outWidth * height * 4);
43
- if (outWidth !== width) {
44
- const { start, count, weights, size } = coefficients(width, outWidth, filter);
45
- for (let y = 0, o = 0; y < height; y++) {
46
- const row = y * width * 4;
47
- for (let x = 0; x < outWidth; x++, o += 4) {
48
- let r = 0, g = 0, b = 0, a = 0;
49
- for (let k = 0, s = row + start[x] * 4, w = x * size, n = count[x]; k < n; k++, s += 4) {
214
+ function packedSum(pixels, s, step, weights, w, taps) {
215
+ let rb = 0x800080, ga = 0x800080;
216
+ for (let k = 0; k < taps; k++, s += step) {
217
+ const p = pixels[s], wk = weights[w + k];
218
+ rb += Math.imul(p & 0xff00ff, wk);
219
+ ga += Math.imul(p >>> 8 & 0xff00ff, wk);
220
+ }
221
+ return (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
222
+ }
223
+ function verticalFast(pixels, rows, width, outHeight, { start, weights, taps }) {
224
+ let all = -1;
225
+ for (let y = 0, o = 0; y < outHeight; y++) {
226
+ const w = y * taps, r0 = start[y] * width;
227
+ if (taps === 4) {
228
+ const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
229
+ const r1 = r0 + width, r2 = r1 + width, r3 = r2 + width;
230
+ for (let x = 0; x < width; x++, o++) {
231
+ const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x], p3 = pixels[r3 + x];
232
+ all &= p0 & p1 & p2 & p3;
233
+ const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3) + 0x800080;
234
+ const ga = Math.imul(p0 >>> 8 & 0xff00ff, w0) + Math.imul(p1 >>> 8 & 0xff00ff, w1) + Math.imul(p2 >>> 8 & 0xff00ff, w2) + Math.imul(p3 >>> 8 & 0xff00ff, w3) + 0x800080;
235
+ rows[o] = (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
236
+ }
237
+ }
238
+ else if (taps === 2) {
239
+ const w0 = weights[w], w1 = weights[w + 1], r1 = r0 + width;
240
+ for (let x = 0; x < width; x++, o++) {
241
+ const p0 = pixels[r0 + x], p1 = pixels[r1 + x];
242
+ all &= p0 & p1;
243
+ const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + 0x800080;
244
+ const ga = Math.imul(p0 >>> 8 & 0xff00ff, w0) + Math.imul(p1 >>> 8 & 0xff00ff, w1) + 0x800080;
245
+ rows[o] = (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
246
+ }
247
+ }
248
+ else {
249
+ for (let x = 0; x < width; x++, o++) {
250
+ for (let k = 0, s = r0 + x; k < taps; k++, s += width)
251
+ all &= pixels[s];
252
+ rows[o] = packedSum(pixels, r0 + x, width, weights, w, taps);
253
+ }
254
+ }
255
+ }
256
+ return all;
257
+ }
258
+ function horizontalFast(rows, result, width, outWidth, outHeight, { start, weights, taps }) {
259
+ for (let y = 0, o = 0; y < outHeight; y++) {
260
+ const row = y * width;
261
+ if (taps === 4) {
262
+ for (let x = 0; x < outWidth; x++, o++) {
263
+ const s = row + start[x], w = x * 4;
264
+ const p0 = rows[s], p1 = rows[s + 1], p2 = rows[s + 2], p3 = rows[s + 3];
265
+ const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
266
+ const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3) + 0x800080;
267
+ const ga = Math.imul(p0 >>> 8 & 0xff00ff, w0) + Math.imul(p1 >>> 8 & 0xff00ff, w1) + Math.imul(p2 >>> 8 & 0xff00ff, w2) + Math.imul(p3 >>> 8 & 0xff00ff, w3) + 0x800080;
268
+ result[o] = (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
269
+ }
270
+ }
271
+ else if (taps === 2) {
272
+ for (let x = 0; x < outWidth; x++, o++) {
273
+ const s = row + start[x], w = x * 2;
274
+ const p0 = rows[s], p1 = rows[s + 1], w0 = weights[w], w1 = weights[w + 1];
275
+ const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + 0x800080;
276
+ const ga = Math.imul(p0 >>> 8 & 0xff00ff, w0) + Math.imul(p1 >>> 8 & 0xff00ff, w1) + 0x800080;
277
+ result[o] = (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
278
+ }
279
+ }
280
+ else {
281
+ for (let x = 0; x < outWidth; x++, o++)
282
+ result[o] = packedSum(rows, row + start[x], 1, weights, x * taps, taps);
283
+ }
284
+ }
285
+ }
286
+ function resampleFast(src, width, height, outWidth, outHeight, filter, out) {
287
+ const vertical = coefficients(height, outHeight, filter, 8, true), horizontal = coefficients(width, outWidth, filter, 8, true);
288
+ if (vertical.taps > FAST_TAPS || horizontal.taps > FAST_TAPS)
289
+ return false;
290
+ let pixels = new Int32Array(src.buffer, src.byteOffset, width * height), all = -1;
291
+ for (let x = 0, last = (height - 1) * width; x < width; x++)
292
+ all &= pixels[x] & pixels[last + x];
293
+ let opaque = all >>> 24 === 255 && (outHeight !== height || isOpaque(src));
294
+ const premultiplied = () => {
295
+ const bitmap = new Uint8ClampedArray(src.length);
296
+ premultiply8(src, bitmap);
297
+ return new Int32Array(bitmap.buffer);
298
+ };
299
+ if (!opaque)
300
+ pixels = premultiplied();
301
+ let rows = pixels;
302
+ if (outHeight !== height) {
303
+ rows = new Int32Array(width * outHeight);
304
+ if (verticalFast(pixels, rows, width, outHeight, vertical) >>> 24 !== 255 && opaque) {
305
+ opaque = false;
306
+ verticalFast(premultiplied(), rows, width, outHeight, vertical);
307
+ }
308
+ }
309
+ const result = new Int32Array(out.buffer, out.byteOffset, outWidth * outHeight);
310
+ if (outWidth === width)
311
+ result.set(rows);
312
+ else
313
+ horizontalFast(rows, result, width, outWidth, outHeight, horizontal);
314
+ if (!opaque)
315
+ unpremultiply8(out);
316
+ return true;
317
+ }
318
+ function resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out) {
319
+ const plane = new Int32Array(width * outHeight * 4);
320
+ if (outHeight === height) {
321
+ for (let s = 0; s < src.length; s += 4) {
322
+ const a = src[s + 3];
323
+ plane[s] = src[s] * a;
324
+ plane[s + 1] = src[s + 1] * a;
325
+ plane[s + 2] = src[s + 2] * a;
326
+ plane[s + 3] = a * 255;
327
+ }
328
+ }
329
+ else {
330
+ const { start, weights, taps } = coefficients(height, outHeight, filter, BITS);
331
+ const stride = width * 4, H = ONE >> 1;
332
+ for (let y = 0, o = 0; y < outHeight; y++) {
333
+ const w = y * taps, r0 = start[y] * stride;
334
+ for (let x = 0; x < stride; x += 4, o += 4) {
335
+ let r = 0, g = 0, b = 0, a = 0, k = 0, s = r0 + x;
336
+ for (; k + 1 < taps; k += 2, s += stride * 2) {
337
+ const t = s + stride, wa = weights[w + k] * src[s + 3], wb = weights[w + k + 1] * src[t + 3];
338
+ r += src[s] * wa + src[t] * wb;
339
+ g += src[s + 1] * wa + src[t + 1] * wb;
340
+ b += src[s + 2] * wa + src[t + 2] * wb;
341
+ a += wa + wb;
342
+ }
343
+ if (k < taps) {
50
344
  const wa = weights[w + k] * src[s + 3];
51
345
  r += src[s] * wa;
52
346
  g += src[s + 1] * wa;
53
347
  b += src[s + 2] * wa;
54
348
  a += wa;
55
349
  }
56
- plane[o] = r / 255;
57
- plane[o + 1] = g / 255;
58
- plane[o + 2] = b / 255;
59
- plane[o + 3] = a;
350
+ plane[o] = r + H >> BITS;
351
+ plane[o + 1] = g + H >> BITS;
352
+ plane[o + 2] = b + H >> BITS;
353
+ plane[o + 3] = a * 255 + H >> BITS;
60
354
  }
61
355
  }
62
356
  }
63
- else {
64
- for (let s = 0; s < plane.length; s += 4) {
65
- const a = src[s + 3];
66
- plane[s] = src[s] * a / 255;
67
- plane[s + 1] = src[s + 1] * a / 255;
68
- plane[s + 2] = src[s + 2] * a / 255;
69
- plane[s + 3] = a;
70
- }
71
- }
72
- if (outHeight !== height) {
73
- const { start, count, weights, size } = coefficients(height, outHeight, filter);
74
- const next = new Float32Array(outWidth * outHeight * 4);
75
- const stride = outWidth * 4;
76
- for (let y = 0; y < outHeight; y++) {
77
- const o = y * stride, n = count[y], w = y * size;
78
- const s0 = start[y] * stride, s1 = s0 + stride, s2 = s1 + stride, s3 = s2 + stride;
79
- const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
80
- if (n === 1)
81
- for (let x = 0; x < stride; x++)
82
- next[o + x] = 0 + plane[s0 + x] * w0;
83
- else if (n === 2)
84
- for (let x = 0; x < stride; x++)
85
- next[o + x] = Math.fround(0 + plane[s0 + x] * w0) + plane[s1 + x] * w1;
86
- else if (n === 3) {
87
- for (let x = 0; x < stride; x++)
88
- next[o + x] = Math.fround(Math.fround(0 + plane[s0 + x] * w0) + plane[s1 + x] * w1) + plane[s2 + x] * w2;
89
- }
90
- else if (n === 4) {
91
- for (let x = 0; x < stride; x++)
92
- next[o + x] = Math.fround(Math.fround(Math.fround(0 + plane[s0 + x] * w0) + plane[s1 + x] * w1) + plane[s2 + x] * w2) + plane[s3 + x] * w3;
357
+ const { start, weights, taps } = outWidth === width ? { start: null, weights: null, taps: 0 } : coefficients(width, outWidth, filter, BITS);
358
+ for (let y = 0, o = 0; y < outHeight; y++) {
359
+ const row = y * width * 4;
360
+ for (let x = 0; x < outWidth; x++, o += 4) {
361
+ let r = 0, g = 0, b = 0, a = 0;
362
+ if (!start || !weights) {
363
+ const s = row + x * 4;
364
+ r = plane[s] * ONE;
365
+ g = plane[s + 1] * ONE;
366
+ b = plane[s + 2] * ONE;
367
+ a = plane[s + 3] * ONE;
93
368
  }
94
369
  else {
95
- for (let x = 0; x < stride; x++) {
96
- let sum = 0;
97
- for (let k = 0, s = s0 + x; k < n; k++, s += stride)
98
- sum = Math.fround(sum + plane[s] * weights[w + k]);
99
- next[o + x] = sum;
370
+ let k = 0, s = row + start[x] * 4, w = x * taps;
371
+ for (; k + 1 < taps; k += 2, s += 8) {
372
+ const w0 = weights[w + k], w1 = weights[w + k + 1];
373
+ r += plane[s] * w0 + plane[s + 4] * w1;
374
+ g += plane[s + 1] * w0 + plane[s + 5] * w1;
375
+ b += plane[s + 2] * w0 + plane[s + 6] * w1;
376
+ a += plane[s + 3] * w0 + plane[s + 7] * w1;
377
+ }
378
+ if (k < taps) {
379
+ const w0 = weights[w + k];
380
+ r += plane[s] * w0;
381
+ g += plane[s + 1] * w0;
382
+ b += plane[s + 2] * w0;
383
+ a += plane[s + 3] * w0;
100
384
  }
101
385
  }
386
+ if (a < 127.5 * ONE) {
387
+ out[o] = out[o + 1] = out[o + 2] = out[o + 3] = 0;
388
+ continue;
389
+ }
390
+ const f = 255 / Math.min(a, 65025 * ONE);
391
+ out[o] = r * f;
392
+ out[o + 1] = g * f;
393
+ out[o + 2] = b * f;
394
+ out[o + 3] = a / (255 * ONE);
102
395
  }
103
- plane = next;
104
- }
105
- for (let i = 0; i < plane.length; i += 4) {
106
- const a = plane[i + 3];
107
- if (a < 0.5) {
108
- out[i] = out[i + 1] = out[i + 2] = out[i + 3] = 0;
109
- continue;
110
- }
111
- const f = 255 / Math.min(255, a);
112
- out[i] = plane[i] * f;
113
- out[i + 1] = plane[i + 1] * f;
114
- out[i + 2] = plane[i + 2] * f;
115
- out[i + 3] = a;
116
396
  }
117
397
  }
@@ -0,0 +1,43 @@
1
+ import { littleEndian, isOpaque, premultiply8, unpremultiply8 } from "./alpha.js";
2
+ function span(start, step, limit, from, to) {
3
+ if (step === 0)
4
+ return start >= 0 && start < limit ? [from, to] : [from, from];
5
+ const a = -start / step, b = (limit - start) / step;
6
+ const lo = Math.floor(Math.min(a, b)) - 1, hi = Math.ceil(Math.max(a, b)) + 1;
7
+ return [Math.max(from, lo), Math.max(Math.max(from, lo), Math.min(to, hi))];
8
+ }
9
+ export function rotateBilinear(src, width, height, out, outWidth, outHeight, sin, cos) {
10
+ if (!littleEndian)
11
+ return false;
12
+ let bitmap = src;
13
+ if (!isOpaque(src))
14
+ premultiply8(src, bitmap = new Uint8ClampedArray(src.length));
15
+ const pixels = new Int32Array(bitmap.buffer, bitmap.byteOffset, width * height);
16
+ const stride = width + 2;
17
+ const padded = new Int32Array(stride * (height + 2));
18
+ for (let y = 0; y < height; y++)
19
+ padded.set(pixels.subarray(y * width, (y + 1) * width), (y + 1) * stride + 1);
20
+ const result = new Int32Array(out.buffer, out.byteOffset, outWidth * outHeight);
21
+ const x0 = 0.5 - outWidth / 2, cx = width / 2 + 0.5, cy = height / 2 + 0.5;
22
+ for (let oy = 0; oy < outHeight; oy++) {
23
+ const dy = oy + 0.5 - outHeight / 2;
24
+ const sx0 = x0 * cos - dy * sin + cx, sy0 = x0 * sin + dy * cos + cy;
25
+ const [xFrom, xTo] = span(sx0, cos, width + 1, 0, outWidth);
26
+ const [from, to] = span(sy0, sin, height + 1, xFrom, xTo);
27
+ let o = oy * outWidth + from, sx = sx0 + from * cos, sy = sy0 + from * sin;
28
+ for (let ox = from; ox < to; ox++, o++, sx += cos, sy += sin) {
29
+ if (!(sx >= 0 && sy >= 0 && sx < width + 1 && sy < height + 1))
30
+ continue;
31
+ const ix = sx | 0, iy = sy | 0;
32
+ const fx = (sx - ix) * 256 + 0.5 | 0, fy = (sy - iy) * 256 + 0.5 | 0;
33
+ const w11 = fx * fy + 128 >> 8, w10 = fx - w11, w01 = fy - w11, w00 = 256 - fx - fy + w11;
34
+ const i = iy * stride + ix;
35
+ const p00 = padded[i], p10 = padded[i + 1], p01 = padded[i + stride], p11 = padded[i + stride + 1];
36
+ const rb = Math.imul(p00 & 0xff00ff, w00) + Math.imul(p10 & 0xff00ff, w10) + Math.imul(p01 & 0xff00ff, w01) + Math.imul(p11 & 0xff00ff, w11) + 0x800080;
37
+ const ga = Math.imul(p00 >>> 8 & 0xff00ff, w00) + Math.imul(p10 >>> 8 & 0xff00ff, w10) + Math.imul(p01 >>> 8 & 0xff00ff, w01) + Math.imul(p11 >>> 8 & 0xff00ff, w11) + 0x800080;
38
+ result[o] = (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
39
+ }
40
+ }
41
+ unpremultiply8(out);
42
+ return true;
43
+ }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.2.0';
1
+ export const VERSION = '3.3.0';
@@ -0,0 +1,42 @@
1
+ HarfBuzz is licensed under the so-called "Old MIT" license. Details follow.
2
+ For parts of HarfBuzz that are licensed under different licenses see individual
3
+ files names COPYING in subdirectories where applicable.
4
+
5
+ Copyright © 2010-2022 Google, Inc.
6
+ Copyright © 2015-2020 Ebrahim Byagowi
7
+ Copyright © 2019,2020 Facebook, Inc.
8
+ Copyright © 2012,2015 Mozilla Foundation
9
+ Copyright © 2011 Codethink Limited
10
+ Copyright © 2008,2010 Nokia Corporation and/or its subsidiary(-ies)
11
+ Copyright © 2009 Keith Stribley
12
+ Copyright © 2011 Martin Hosken and SIL International
13
+ Copyright © 2007 Chris Wilson
14
+ Copyright © 2005,2006,2020,2021,2022,2023 Behdad Esfahbod
15
+ Copyright © 2004,2007,2008,2009,2010,2013,2021,2022,2023 Red Hat, Inc.
16
+ Copyright © 1998-2005 David Turner and Werner Lemberg
17
+ Copyright © 2016 Igalia S.L.
18
+ Copyright © 2022 Matthias Clasen
19
+ Copyright © 2018,2021 Khaled Hosny
20
+ Copyright © 2018,2019,2020 Adobe, Inc
21
+ Copyright © 2013-2015 Alexei Podtelezhnikov
22
+
23
+ For full copyright notices consult the individual files in the package.
24
+
25
+
26
+ Permission is hereby granted, without written agreement and without
27
+ license or royalty fees, to use, copy, modify, and distribute this
28
+ software and its documentation for any purpose, provided that the
29
+ above copyright notice and the following two paragraphs appear in
30
+ all copies of this software.
31
+
32
+ IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
33
+ DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
34
+ ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
35
+ IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
36
+ DAMAGE.
37
+
38
+ THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
39
+ BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
40
+ FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
41
+ ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
42
+ PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
@@ -0,0 +1,39 @@
1
+ UNICODE LICENSE V3
2
+
3
+ COPYRIGHT AND PERMISSION NOTICE
4
+
5
+ Copyright © 1991-2026 Unicode, Inc.
6
+
7
+ NOTICE TO USER: Carefully read the following legal agreement. BY
8
+ DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
9
+ SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
10
+ TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
11
+ DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a
14
+ copy of data files and any associated documentation (the "Data Files") or
15
+ software and any associated documentation (the "Software") to deal in the
16
+ Data Files or Software without restriction, including without limitation
17
+ the rights to use, copy, modify, merge, publish, distribute, and/or sell
18
+ copies of the Data Files or Software, and to permit persons to whom the
19
+ Data Files or Software are furnished to do so, provided that either (a)
20
+ this copyright and permission notice appear with all copies of the Data
21
+ Files or Software, or (b) this copyright and permission notice appear in
22
+ associated Documentation.
23
+
24
+ THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
25
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
27
+ THIRD PARTY RIGHTS.
28
+
29
+ IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
30
+ BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
31
+ OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
32
+ WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
33
+ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA
34
+ FILES OR SOFTWARE.
35
+
36
+ Except as contained in this notice, the name of a copyright holder shall
37
+ not be used in advertising or otherwise to promote the sale, use or other
38
+ dealings in these Data Files or Software without prior written
39
+ authorization of the copyright holder.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "zero-dependency javascript image manipulation",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",