imagescript 1.3.1 → 3.1.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 (132) hide show
  1. package/LICENSE +4 -1
  2. package/README.md +76 -26
  3. package/THIRD_PARTY_NOTICES.md +156 -0
  4. package/dist/animation.d.ts +55 -0
  5. package/dist/animation.js +167 -0
  6. package/dist/codecs/crc.d.ts +9 -0
  7. package/dist/codecs/crc.js +29 -0
  8. package/dist/codecs/exif.d.ts +11 -0
  9. package/dist/codecs/exif.js +27 -0
  10. package/dist/codecs/gif.d.ts +44 -0
  11. package/dist/codecs/gif.js +401 -0
  12. package/dist/codecs/jpeg.d.ts +35 -0
  13. package/dist/codecs/jpeg.js +980 -0
  14. package/dist/codecs/png.d.ts +97 -0
  15. package/dist/codecs/png.js +664 -0
  16. package/dist/codecs/quantize.d.ts +25 -0
  17. package/dist/codecs/quantize.js +302 -0
  18. package/dist/codecs/tga.d.ts +16 -0
  19. package/dist/codecs/tga.js +203 -0
  20. package/dist/codecs/tiff.d.ts +6 -0
  21. package/dist/codecs/tiff.js +546 -0
  22. package/dist/codecs/vp8-tables.d.ts +10 -0
  23. package/dist/codecs/vp8-tables.js +17 -0
  24. package/dist/codecs/vp8.d.ts +8 -0
  25. package/dist/codecs/vp8.js +711 -0
  26. package/dist/codecs/vp8l.d.ts +62 -0
  27. package/dist/codecs/vp8l.js +909 -0
  28. package/dist/codecs/webp.d.ts +67 -0
  29. package/dist/codecs/webp.js +280 -0
  30. package/dist/codecs/zlib.d.ts +22 -0
  31. package/dist/codecs/zlib.js +607 -0
  32. package/dist/frame.d.ts +57 -0
  33. package/dist/frame.js +65 -0
  34. package/dist/gif.d.ts +31 -0
  35. package/dist/gif.js +20 -0
  36. package/dist/image-type.d.ts +24 -0
  37. package/dist/image-type.js +64 -0
  38. package/dist/image.d.ts +553 -0
  39. package/dist/image.js +1156 -0
  40. package/dist/index.d.ts +23 -0
  41. package/dist/index.js +20 -0
  42. package/dist/package.json +1 -0
  43. package/dist/render/font.d.ts +92 -0
  44. package/dist/render/font.js +954 -0
  45. package/dist/render/hinting.d.ts +110 -0
  46. package/dist/render/hinting.js +1236 -0
  47. package/dist/render/svg.d.ts +11 -0
  48. package/dist/render/svg.js +1717 -0
  49. package/dist/render/text.d.ts +63 -0
  50. package/dist/render/text.js +342 -0
  51. package/dist/text-layout.d.ts +46 -0
  52. package/dist/text-layout.js +35 -0
  53. package/dist/util/blur.d.ts +13 -0
  54. package/dist/util/blur.js +146 -0
  55. package/dist/util/mem.d.ts +10 -0
  56. package/dist/util/mem.js +23 -0
  57. package/dist/util/resample.d.ts +12 -0
  58. package/dist/util/resample.js +99 -0
  59. package/dist/util/utf8.d.ts +5 -0
  60. package/dist/util/utf8.js +55 -0
  61. package/dist/version.d.ts +1 -0
  62. package/dist/version.js +1 -0
  63. package/licenses/Apache-2.0.txt +202 -0
  64. package/licenses/FTL.txt +169 -0
  65. package/licenses/README.ijg +260 -0
  66. package/package.json +31 -7
  67. package/ImageScript.d.ts +0 -954
  68. package/ImageScript.js +0 -1669
  69. package/bench/_.mjs +0 -166
  70. package/bench/all.mjs +0 -4
  71. package/bench/blur.mjs +0 -59
  72. package/bench/flip.mjs +0 -63
  73. package/bench/overlay.mjs +0 -35
  74. package/bench/package.json +0 -7
  75. package/bench/readme.md +0 -64
  76. package/bench/resize.mjs +0 -52
  77. package/codecs/LICENSE +0 -21
  78. package/codecs/lib.d.ts +0 -50
  79. package/codecs/lib.js +0 -6
  80. package/codecs/lib.mjs +0 -1
  81. package/codecs/node/bin/arm64-darwin.node +0 -0
  82. package/codecs/node/bin/arm64-linux.node +0 -0
  83. package/codecs/node/bin/x64-darwin.node +0 -0
  84. package/codecs/node/bin/x64-linux.node +0 -0
  85. package/codecs/node/bin/x64-win32.node +0 -0
  86. package/codecs/node/index.js +0 -3
  87. package/codecs/node/index.mjs +0 -6
  88. package/codecs/package.json +0 -14
  89. package/codecs/readme.md +0 -38
  90. package/codecs/wasm/bin/codecs.wasm +0 -0
  91. package/codecs/wasm/bin/simd.wasm +0 -0
  92. package/codecs/wasm/bundle/browser.js +0 -1
  93. package/codecs/wasm/index.js +0 -1
  94. package/codecs/wasm/index.mjs +0 -1
  95. package/png/README.md +0 -1
  96. package/png/node.js +0 -1137
  97. package/png/src/crc.mjs +0 -49
  98. package/png/src/mem.mjs +0 -24
  99. package/png/src/png.mjs +0 -254
  100. package/png/src/zlib.mjs +0 -618
  101. package/tsconfig.json +0 -21
  102. package/utils/mem.js +0 -26
  103. package/v2/codecs/magic.js +0 -25
  104. package/v2/framebuffer.d.ts +0 -67
  105. package/v2/framebuffer.js +0 -2352
  106. package/v2/framebuffer.mjs +0 -126
  107. package/v2/framebuffer.rs +0 -1264
  108. package/v2/index.d.ts +0 -84
  109. package/v2/index.js +0 -261
  110. package/v2/ops/blur.mjs +0 -303
  111. package/v2/ops/color.mjs +0 -277
  112. package/v2/ops/crop.mjs +0 -64
  113. package/v2/ops/fill.mjs +0 -33
  114. package/v2/ops/flip.mjs +0 -33
  115. package/v2/ops/index.mjs +0 -9
  116. package/v2/ops/iterator.mjs +0 -34
  117. package/v2/ops/overlay.mjs +0 -63
  118. package/v2/ops/resize.mjs +0 -166
  119. package/v2/ops/rotate.mjs +0 -117
  120. package/v2/util/mem.mjs +0 -24
  121. package/wasm/any/font.wasm +0 -0
  122. package/wasm/any/gif.wasm +0 -0
  123. package/wasm/any/jpeg.wasm +0 -0
  124. package/wasm/any/png.wasm +0 -0
  125. package/wasm/any/svg.wasm +0 -0
  126. package/wasm/any/tiff.wasm +0 -0
  127. package/wasm/node/font.js +0 -142
  128. package/wasm/node/gif.js +0 -76
  129. package/wasm/node/jpeg.js +0 -92
  130. package/wasm/node/png.js +0 -49
  131. package/wasm/node/svg.js +0 -56
  132. package/wasm/node/tiff.js +0 -47
package/v2/ops/color.mjs DELETED
@@ -1,277 +0,0 @@
1
- const short_hex_regex = /^#?([\da-f]{3,4})$/;
2
- const long_hex_regex = /^#?((?:[\da-f]{2}){3,4})$/;
3
- const rgb_regex = /^rgba?\((?<r>(?:\d*\.)?\d+)(?: +| *, *)(?<g>(?:\d*\.)?\d+)(?: +| *, *)(?<b>(?:\d*\.)?\d+)(?:(?: +| *, *)(?<a>\d+|\d*\.\d+|\d+(?:\.\d+)?%))?\)$/;
4
- const rgb_percentage_regex = /^rgba?\((?<r>(?:\d*\.)?\d+)%(?: +| *, *)(?<g>(?:\d*\.)?\d+)%(?: +| *, *)(?<b>(?:\d*\.)?\d+)%(?:(?: +| *, *)(?<a>\d+|\d*\.\d+|\d+(?:\.\d+)?%))?\)$/;
5
- const hsl_regex = /^hsla?\((?<h>(?:\d*\.)?\d+)(?<t>|deg|rad|grad|turn)(?: +| *, *)(?<s>(?:\d*\.)?\d+)%(?: +| *, *)(?<l>(?:\d*\.)?\d+)%(?:(?: +| *, *)(?<a>\d+|\d*\.\d+|\d+(?:\.\d+)?%))?\)$/;
6
-
7
- function clamp(min, max, int) {
8
- return Math.min(Math.max(Math.round(int), min), max);
9
- }
10
-
11
- export function to_rgba(int) {
12
- return [(int >> 24) & 0xff, (int >> 16) & 0xff, (int >> 8) & 0xff, int & 0xff];
13
- }
14
-
15
- export function from_rgba(r, g, b, a) {
16
- return ((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | (a & 0xff);
17
- }
18
-
19
- function parse_alpha(a) {
20
- return clamp(0, 255, ('%' === a[a.length - 1]) ? ((255 / 100) * parseFloat(a)) : (+a * 255));
21
- }
22
-
23
- function hue_from_type(h, t) {
24
- if (t === 'turn') return h / 1;
25
- if (t === 'grad') return h / 400;
26
- if (!t || t === 'deg') return h / 360;
27
- if (t === 'rad') return h / (2 * Math.PI);
28
- }
29
-
30
- function hue2rgb(p, q, t) {
31
- if (t < 0) t += 1;
32
- else if (t > 1) t -= 1;
33
- if (t < 1 / 2) return q;
34
- if (t < 1 / 6) return p + 6 * t * (q - p);
35
- if (t < 2 / 3) return p + 6 * (q - p) * (2 / 3 - t);
36
-
37
- return p;
38
- };
39
-
40
- export function blend(fg, bg) {
41
- const fa = fg & 0xff;
42
- const alpha = fa + 1 | 0;
43
- const inv_alpha = 256 - fa | 0;
44
- const r = (alpha * (fg >>> 24) + inv_alpha * (bg >>> 24)) >> 8;
45
- const b = (alpha * (fg >> 8 & 0xff) + inv_alpha * (bg >> 8 & 0xff)) >> 8;
46
- const g = (alpha * (fg >> 16 & 0xff) + inv_alpha * (bg >> 16 & 0xff)) >> 8;
47
- return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | (Math.max(fa, bg & 0xff) & 0xff));
48
- }
49
-
50
- export function parse(any) {
51
- let x = null;
52
- if (undefined !== (x = colors.get(any))) return x;
53
- if (x = long_hex_regex.exec(any)) return parseInt(`${x[1]}${8 === x[1].length ? '' : 'ff'}`, 16);
54
- if (x = hsl_regex.exec(any)) return color.hsla(hue_from_type(x[1], x[2]), x[3] / 100, x[4] / 100, x[5] ? ((1 / 255) * parse_alpha(x[5])) : 1);
55
- if (x = rgb_regex.exec(any)) return color.rgba(clamp(0, 255, +x[1]), clamp(0, 255, +x[2]), clamp(0, 255, +x[3]), x[4] ? parse_alpha(x[4]) : 255);
56
- if (x = short_hex_regex.exec(any)) return parseInt(`${x[1][0]}${x[1][0]}${x[1][1]}${x[1][1]}${x[1][2]}${x[1][2]}${3 === x[1].length ? 'ff' : `${x[1][3]}${x[1][3]}`}`, 16);
57
- if (x = rgb_percentage_regex.exec(any)) return color.rgba(clamp(0, 255, +x[1] * (255 / 100)), clamp(0, 255, +x[2] * (255 / 100)), clamp(0, 255, +x[3] * (255 / 100)), x[4] ? parse_alpha(x[4]) : 255);
58
-
59
- return null;
60
- }
61
-
62
- export default class color {
63
- constructor(any) {
64
- this.value = parse(String(any).toLowerCase());
65
- if (null === this.value) throw new Error(`invalid css color (${any})`);
66
- }
67
-
68
- static rgb(r, g, b) {
69
- return this.rgba(r, g, b, 255);
70
- }
71
-
72
- static rgba(r, g, b, a) {
73
- return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | (a & 0xff));
74
- }
75
-
76
- static hsla(h, s, l, a) {
77
- s = Math.min(1, Math.max(0, s));
78
- a = Math.min(1, Math.max(0, a));
79
- if (s === 0) return this.rgba(255, 255, 255, a * 255);
80
-
81
- h %= 1;
82
- l = Math.min(1, Math.max(0, l));
83
- const q = l < .5 ? l + s * l : l + s - l * s;
84
-
85
- const p = 2 * l - q;
86
- const g = hue2rgb(p, q, h);
87
- const r = hue2rgb(p, q, h + 1 / 3);
88
- const b = hue2rgb(p, q, h - 1 / 3);
89
- return this.rgba(r * 255, g * 355, b * 255, a * 255);
90
- }
91
-
92
- valueOf() {
93
- return this.value;
94
- }
95
-
96
- toJSON() {
97
- return this.value >>> 0;
98
- }
99
-
100
- get rgb() {
101
- return [this.value >>> 24, this.value >> 16 & 0xff, this.value >> 8 & 0xff];
102
- }
103
-
104
- get rgba() {
105
- return [this.value >>> 24, this.value >> 16 & 0xff, this.value >> 8 & 0xff, this.value & 0xff];
106
- }
107
-
108
- get name() {
109
- for (const color of colors.keys()) {
110
- if (this.value === colors.get(color)) return color;
111
- }
112
-
113
- return null;
114
- }
115
-
116
- toString(radix) {
117
- const type = String(radix).toLowerCase();
118
-
119
- const rgba = this.rgba;
120
- if (type === 'rgb' || type === 'rgba') return `rgb${type[3] ? 'a' : ''}(${rgba[0]}, ${rgba[1]}, ${rgba[2]}${type[3] ? `, ${clamp(0, 100, 100 / 255 * rgba[3])}%` : ''})`;
121
- if (type === '16' || type === 'hex') return `#${rgba[0].toString(16).padStart(2, '0')}${rgba[1].toString(16).padStart(2, '0')}${rgba[2].toString(16).padStart(2, '0')}${rgba[3] === 255 ? '' : rgba[3].toString(16).padStart(2, '0')}`;
122
-
123
- return this.value.toString();
124
- }
125
- }
126
-
127
- const colors = new Map([
128
- ['aliceblue', 0xf0f8ffff],
129
- ['antiquewhite', 0xfaebd7ff],
130
- ['aqua', 0x00ffffff],
131
- ['aquamarine', 0x7fffd4ff],
132
- ['azure', 0xf0ffffff],
133
- ['beige', 0xf5f5dcff],
134
- ['bisque', 0xffe4c4ff],
135
- ['black', 0x000000ff],
136
- ['blanchedalmond', 0xffebcdff],
137
- ['blue', 0x0000ffff],
138
- ['blueviolet', 0x8a2be2ff],
139
- ['brown', 0xa52a2aff],
140
- ['burlywood', 0xdeb887ff],
141
- ['cadetblue', 0x5f9ea0ff],
142
- ['chartreuse', 0x7fff00ff],
143
- ['chocolate', 0xd2691eff],
144
- ['coral', 0xff7f50ff],
145
- ['cornflowerblue', 0x6495edff],
146
- ['cornsilk', 0xfff8dcff],
147
- ['crimson', 0xdc143cff],
148
- ['cyan', 0x00ffffff],
149
- ['darkblue', 0x00008bff],
150
- ['darkcyan', 0x008b8bff],
151
- ['darkgoldenrod', 0xb8860bff],
152
- ['darkgray', 0xa9a9a9ff],
153
- ['darkgreen', 0x006400ff],
154
- ['darkgrey', 0xa9a9a9ff],
155
- ['darkkhaki', 0xbdb76bff],
156
- ['darkmagenta', 0x8b008bff],
157
- ['darkolivegreen', 0x556b2fff],
158
- ['darkorange', 0xff8c00ff],
159
- ['darkorchid', 0x9932ccff],
160
- ['darkred', 0x8b0000ff],
161
- ['darksalmon', 0xe9967aff],
162
- ['darkseagreen', 0x8fbc8fff],
163
- ['darkslateblue', 0x483d8bff],
164
- ['darkslategray', 0x2f4f4fff],
165
- ['darkslategrey', 0x2f4f4fff],
166
- ['darkturquoise', 0x00ced1ff],
167
- ['darkviolet', 0x9400d3ff],
168
- ['deeppink', 0xff1493ff],
169
- ['deepskyblue', 0x00bfffff],
170
- ['dimgray', 0x696969ff],
171
- ['dimgrey', 0x696969ff],
172
- ['dodgerblue', 0x1e90ffff],
173
- ['firebrick', 0xb22222ff],
174
- ['floralwhite', 0xfffaf0ff],
175
- ['forestgreen', 0x228b22ff],
176
- ['fuchsia', 0xff00ffff],
177
- ['gainsboro', 0xdcdcdcff],
178
- ['ghostwhite', 0xf8f8ffff],
179
- ['gold', 0xffd700ff],
180
- ['goldenrod', 0xdaa520ff],
181
- ['gray', 0x808080ff],
182
- ['green', 0x008000ff],
183
- ['greenyellow', 0xadff2fff],
184
- ['grey', 0x808080ff],
185
- ['honeydew', 0xf0fff0ff],
186
- ['hotpink', 0xff69b4ff],
187
- ['indianred', 0xcd5c5cff],
188
- ['indigo', 0x4b0082ff],
189
- ['ivory', 0xfffff0ff],
190
- ['khaki', 0xf0e68cff],
191
- ['lavender', 0xe6e6faff],
192
- ['lavenderblush', 0xfff0f5ff],
193
- ['lawngreen', 0x7cfc00ff],
194
- ['lemonchiffon', 0xfffacdff],
195
- ['lightblue', 0xadd8e6ff],
196
- ['lightcoral', 0xf08080ff],
197
- ['lightcyan', 0xe0ffffff],
198
- ['lightgoldenrodyellow', 0xfafad2ff],
199
- ['lightgray', 0xd3d3d3ff],
200
- ['lightgreen', 0x90ee90ff],
201
- ['lightgrey', 0xd3d3d3ff],
202
- ['lightpink', 0xffb6c1ff],
203
- ['lightsalmon', 0xffa07aff],
204
- ['lightseagreen', 0x20b2aaff],
205
- ['lightskyblue', 0x87cefaff],
206
- ['lightslategray', 0x778899ff],
207
- ['lightslategrey', 0x778899ff],
208
- ['lightsteelblue', 0xb0c4deff],
209
- ['lightyellow', 0xffffe0ff],
210
- ['lime', 0x00ff00ff],
211
- ['limegreen', 0x32cd32ff],
212
- ['linen', 0xfaf0e6ff],
213
- ['magenta', 0xff00ffff],
214
- ['maroon', 0x800000ff],
215
- ['mediumaquamarine', 0x66cdaaff],
216
- ['mediumblue', 0x0000cdff],
217
- ['mediumorchid', 0xba55d3ff],
218
- ['mediumpurple', 0x9370dbff],
219
- ['mediumseagreen', 0x3cb371ff],
220
- ['mediumslateblue', 0x7b68eeff],
221
- ['mediumspringgreen', 0x00fa9aff],
222
- ['mediumturquoise', 0x48d1ccff],
223
- ['mediumvioletred', 0xc71585ff],
224
- ['midnightblue', 0x191970ff],
225
- ['mintcream', 0xf5fffaff],
226
- ['mistyrose', 0xffe4e1ff],
227
- ['moccasin', 0xffe4b5ff],
228
- ['navajowhite', 0xffdeadff],
229
- ['navy', 0x000080ff],
230
- ['oldlace', 0xfdf5e6ff],
231
- ['olive', 0x808000ff],
232
- ['olivedrab', 0x6b8e23ff],
233
- ['orange', 0xffa500ff],
234
- ['orangered', 0xff4500ff],
235
- ['orchid', 0xda70d6ff],
236
- ['palegoldenrod', 0xeee8aaff],
237
- ['palegreen', 0x98fb98ff],
238
- ['paleturquoise', 0xafeeeeff],
239
- ['palevioletred', 0xdb7093ff],
240
- ['papayawhip', 0xffefd5ff],
241
- ['peachpuff', 0xffdab9ff],
242
- ['peru', 0xcd853fff],
243
- ['pink', 0xffc0cbff],
244
- ['plum', 0xdda0ddff],
245
- ['powderblue', 0xb0e0e6ff],
246
- ['purple', 0x800080ff],
247
- ['rebeccapurple', 0x663399ff],
248
- ['red', 0xff0000ff],
249
- ['rosybrown', 0xbc8f8fff],
250
- ['royalblue', 0x4169e1ff],
251
- ['saddlebrown', 0x8b4513ff],
252
- ['salmon', 0xfa8072ff],
253
- ['sandybrown', 0xf4a460ff],
254
- ['seagreen', 0x2e8b57ff],
255
- ['seashell', 0xfff5eeff],
256
- ['sienna', 0xa0522dff],
257
- ['silver', 0xc0c0c0ff],
258
- ['skyblue', 0x87ceebff],
259
- ['slateblue', 0x6a5acdff],
260
- ['slategray', 0x708090ff],
261
- ['slategrey', 0x708090ff],
262
- ['snow', 0xfffafaff],
263
- ['springgreen', 0x00ff7fff],
264
- ['steelblue', 0x4682b4ff],
265
- ['tan', 0xd2b48cff],
266
- ['teal', 0x008080ff],
267
- ['thistle', 0xd8bfd8ff],
268
- ['tomato', 0xff6347ff],
269
- ['transparent', 0x00000000],
270
- ['turquoise', 0x40e0d0ff],
271
- ['violet', 0xee82eeff],
272
- ['wheat', 0xf5deb3ff],
273
- ['white', 0xffffffff],
274
- ['whitesmoke', 0xf5f5f5ff],
275
- ['yellow', 0xffff00ff],
276
- ['yellowgreen', 0x9acd32ff],
277
- ]);
package/v2/ops/crop.mjs DELETED
@@ -1,64 +0,0 @@
1
- function clamp(min, int, max) {
2
- const t = int < min ? min : int; return t > max ? max : t;
3
- }
4
-
5
- export function cut(x, y, width, height, framebuffer) {
6
- width |= 0;
7
- height |= 0;
8
- const frame = new framebuffer.constructor(width, height);
9
-
10
- const n32 = frame.u32;
11
- const o32 = framebuffer.u32;
12
- const fwidth = framebuffer.width | 0;
13
-
14
- for (let yy = 0 | 0; yy < height; yy++) {
15
- const offset = x + fwidth * (y + yy);
16
- n32.set(o32.subarray(offset, width + offset), yy * width);
17
- }
18
-
19
- return frame;
20
- }
21
-
22
- export function crop(x, y, width, height, framebuffer) {
23
- width |= 0;
24
- height |= 0;
25
- const old = framebuffer.u32;
26
- const fwidth = framebuffer.width | 0;
27
- const u32 = framebuffer.u32 = new Uint32Array(width * height);
28
-
29
- for (let yy = 0; yy < height; yy++) {
30
- const offset = x + fwidth * (y + yy);
31
- u32.set(old.subarray(offset, width + offset), yy * width);
32
- }
33
-
34
- framebuffer.width = width;
35
- framebuffer.height = height;
36
- framebuffer.u8 = new Uint8Array(framebuffer.u32.buffer);
37
- framebuffer.view = new DataView(framebuffer.u32.buffer);
38
- }
39
-
40
- export function circle(feathering, framebuffer) {
41
- const u8 = framebuffer.u8;
42
- const u32 = framebuffer.u32;
43
- const width = framebuffer.width | 0;
44
- const height = framebuffer.height | 0;
45
- const rad = Math.min(width, height) / 2;
46
-
47
- const cx = width / 2;
48
- const cy = height / 2;
49
- const rad_2 = rad ** 2;
50
- const feathering_12 = feathering ** (1 / 2);
51
-
52
- for (let y = 0 | 0; y < height; y++) {
53
- const cdy = (y - cy) ** 2;
54
- const y_offset = y * width;
55
-
56
- for (let x = 0 | 0; x < width; x++) {
57
- const cd = cdy + (x - cx) ** 2;
58
- if (cd > rad_2) u32[x + y_offset] = 0;
59
- else if (feathering) u8[3 + 4 * (x + y_offset)] *= clamp(0, 1 - (cd / rad_2) * feathering_12, 1);
60
- }
61
- }
62
-
63
- return framebuffer;
64
- }
package/v2/ops/fill.mjs DELETED
@@ -1,33 +0,0 @@
1
- export function color(int, framebuffer) {
2
- framebuffer.view.setUint32(0, int);
3
- framebuffer.u32.fill(framebuffer.u32[0]);
4
- }
5
-
6
- export function fn(cb, framebuffer) {
7
- let offset = 0 | 0;
8
- const view = framebuffer.view;
9
- const width = framebuffer.width | 0;
10
- const height = framebuffer.height | 0;
11
-
12
- for (let y = 1 | 0; y <= height; y++) {
13
- for (let x = 1 | 0; x <= width; x++) {
14
- view.setUint32(offset, cb(x, y), false); offset += 4;
15
- }
16
- }
17
- }
18
-
19
- export function swap(old, int, framebuffer) {
20
- {
21
- const t = new Uint32Array(2);
22
- const v = new DataView(t.buffer);
23
- old = (v.setUint32(0, old), t[0]);
24
- int = (v.setUint32(4, int), t[1]);
25
- }
26
-
27
- const u32 = framebuffer.u32;
28
- const l = framebuffer.u32.length | 0;
29
-
30
- for (let o = 0 | 0; o < l; o++) {
31
- if (old === u32[o]) u32[o] = int;
32
- }
33
- }
package/v2/ops/flip.mjs DELETED
@@ -1,33 +0,0 @@
1
- export function horizontal(framebuffer) {
2
- let offset = 0 | 0;
3
- const u32 = framebuffer.u32;
4
- const width = framebuffer.width | 0;
5
- const height = framebuffer.height | 0;
6
-
7
- for (let y = 0 | 0; y < height; y++) {
8
- u32.subarray(offset, offset += width).reverse();
9
- }
10
- }
11
-
12
- export function vertical(framebuffer) {
13
- const u32 = framebuffer.u32;
14
- const width = framebuffer.width | 0;
15
- const oheight = framebuffer.height | 0;
16
- const height = (framebuffer.height / 2) | 0;
17
-
18
- for (let y = 0 | 0; y < height; y++) {
19
- const yo = y * width | 0;
20
- const wo1y = width * (oheight - 1 - y) | 0;
21
-
22
- for (let x = 0 | 0; x < width; x++) {
23
- const offset = x + yo;
24
- const offset2 = x + wo1y;
25
-
26
- const top = u32[offset];
27
- const bottom = u32[offset2];
28
-
29
- u32[offset2] = top;
30
- u32[offset] = bottom;
31
- }
32
- }
33
- }
package/v2/ops/index.mjs DELETED
@@ -1,9 +0,0 @@
1
- export * as flip from './flip.mjs';
2
- export * as fill from './fill.mjs';
3
- export * as blur from './blur.mjs';
4
- export * as crop from './crop.mjs';
5
- export * as color from './color.mjs';
6
- export * as resize from './resize.mjs';
7
- export * as rotate from './rotate.mjs';
8
- export * as overlay from './overlay.mjs';
9
- export * as iterator from './iterator.mjs';
@@ -1,34 +0,0 @@
1
- export function* cords(framebuffer) {
2
- const width = framebuffer.width | 0;
3
- const height = framebuffer.height | 0;
4
-
5
- for (let y = 0 | 0; y < height; y++) {
6
- for (let x = 0 | 0; x < width; x++) yield [x, y];
7
- }
8
- }
9
-
10
- export function* rgba(framebuffer) {
11
- let offset = 0 | 0;
12
- const u8 = framebuffer.u8;
13
- const width = framebuffer.width | 0;
14
- const height = framebuffer.height | 0;
15
-
16
- for (let y = 0 | 0; y < height; y++) {
17
- for (let x = 0 | 0; x < width; x++) {
18
- yield [x, y, u8.subarray(offset, offset += 4)];
19
- }
20
- }
21
- }
22
-
23
- export function* u32(framebuffer) {
24
- let offset = 0 | 0;
25
- const view = framebuffer.view;
26
- const width = framebuffer.width | 0;
27
- const height = framebuffer.height | 0;
28
-
29
- for (let y = 0 | 0; y < height; y++) {
30
- for (let x = 0 | 0; x < width; x++) {
31
- yield [x, y, view.getUint32(offset, false)]; offset += 4;
32
- }
33
- }
34
- }
@@ -1,63 +0,0 @@
1
- export function replace(bg, fg, x, y) {
2
- const b32 = bg.u32;
3
- const f32 = fg.u32;
4
- const fw = fg.width | 0;
5
- const bw = bg.width | 0;
6
- const fh = fg.height | 0;
7
- const bh = bg.height | 0;
8
- const ox = (x > 0 ? 0 : -x) | 0;
9
- const oy = (y > 0 ? 0 : -y) | 0;
10
- const top = (y > 0 ? y : 0) | 0;
11
- const left = (x > 0 ? x : 0) | 0;
12
- const width = (Math.min(bw, x + fw) - left) | 0;
13
- const height = (Math.min(bh, y + fh) - top) | 0;
14
-
15
- if (0 >= width || 0 >= height) return;
16
-
17
- for (let yy = 0 | 0; yy < height; yy++) {
18
- const yyoffset = ox + fw * (yy + oy);
19
- const yoffset = left + bw * (yy + top);
20
- b32.subarray(yoffset, width + yoffset).set(f32.subarray(yyoffset, width + yyoffset));
21
- }
22
- }
23
-
24
- export function blend(bg, fg, x, y) {
25
- const b32 = bg.u32;
26
- const f32 = fg.u32;
27
- const fw = fg.width | 0;
28
- const bw = bg.width | 0;
29
- const fh = fg.height | 0;
30
- const bh = bg.height | 0;
31
- const ox = (x > 0 ? 0 : -x) | 0;
32
- const oy = (y > 0 ? 0 : -y) | 0;
33
- const top = (y > 0 ? y : 0) | 0;
34
- const left = (x > 0 ? x : 0) | 0;
35
- const width = (Math.min(bw, x + fw) - left) | 0;
36
- const height = (Math.min(bh, y + fh) - top) | 0;
37
-
38
- if (0 >= width || 0 >= height) return;
39
-
40
- for (let yy = 0 | 0; yy < height; yy++) {
41
- const yyoffset = ox + fw * (yy + oy);
42
- const yoffset = left + bw * (yy + top);
43
-
44
- for (let xx = 0 | 0; xx < width; xx++) {
45
- const F = f32[xx + yyoffset];
46
-
47
- // todo: be?
48
- const fa = F >> 24 & 0xff;
49
- if (fa === 0x00) continue;
50
- else if (fa === 0xff) b32[xx + yoffset] = F;
51
-
52
- else {
53
- const alpha = 1 + fa;
54
- const inv_alpha = 256 - fa;
55
- const B = b32[xx + yoffset];
56
- const r = (alpha * (F & 0xff) + inv_alpha * (B & 0xff)) >> 8;
57
- const g = (alpha * ((F >> 8) & 0xff) + inv_alpha * ((B >> 8) & 0xff)) >> 8;
58
- const b = (alpha * ((F >> 16) & 0xff) + inv_alpha * ((B >> 16) & 0xff)) >> 8;
59
- b32[xx + yoffset] = (Math.max(fa, (B >> 24) & 0xff) << 24) | ((b & 0xff) << 16) | ((g & 0xff) << 8) | r;
60
- }
61
- }
62
- }
63
- }
package/v2/ops/resize.mjs DELETED
@@ -1,166 +0,0 @@
1
- function lerp(a, b, t) {
2
- return t * b + a * (1 - t);
3
- }
4
-
5
- function clamp(min, int, max) {
6
- const t = int < min ? min : int; return t > max ? max : t;
7
- }
8
-
9
- function clamped(x, y, width, height) {
10
- return 4 * (clamp(0, x, width - 1) + clamp(0, y, height - 1) * width);
11
- }
12
-
13
- function hermite(A, B, C, D, t) {
14
- const c = (C / 2) + (A / -2);
15
- const b = A + (C * 2) - (D / 2) - (B * 2.5);
16
- const a = (D / 2) + (A / -2) + (B * 1.5) - (C * 1.5);
17
-
18
- const t2 = t * t;
19
- return B + (c * t) + (b * t2) + (a * t * t2);
20
- }
21
-
22
- export function nearest(width, height, framebuffer) {
23
- width = width | 0;
24
- height = height | 0;
25
- const old = framebuffer.u32;
26
- const fwidth = framebuffer.width | 0;
27
- const fheight = framebuffer.height | 0;
28
- const u32 = framebuffer.u32 = new Uint32Array(width * height);
29
-
30
- const dw = 1 / width;
31
- const dh = 1 / height;
32
- const xw = dw * fwidth;
33
- const yw = dh * fheight;
34
- for (let y = 0 | 0; y < height; y++) {
35
- const yoffset = y * width;
36
- const yyoffset = fwidth * ((y * yw) | 0);
37
-
38
- for (let x = 0 | 0; x < width; x++) {
39
- u32[x + yoffset] = old[yyoffset + ((x * xw) | 0)];
40
- }
41
- }
42
-
43
- framebuffer.width = width;
44
- framebuffer.height = height;
45
- framebuffer.u8 = new Uint8Array(u32.buffer);
46
- framebuffer.view = new DataView(u32.buffer);
47
- }
48
-
49
- export function linear(width, height, framebuffer) {
50
- width = width | 0;
51
- height = height | 0;
52
- const old = framebuffer.u8;
53
- const old_width = framebuffer.width | 0;
54
- const old_height = framebuffer.height | 0;
55
- const u8 = new Uint8ClampedArray(4 * width * height);
56
-
57
- let offset = 0 | 0;
58
- const width1 = 1 / (width - 1);
59
- const height1 = 1 / (height - 1);
60
-
61
- for (let y = 0 | 0; y < height; y++) {
62
- const yy = old_height * (y * height1) - .5;
63
-
64
- const yyi = yy | 0;
65
- const ty = yy - yyi;
66
-
67
- for (let x = 0 | 0; x < width; x++) {
68
- const xx = old_width * (x * width1) - .5;
69
-
70
- const xxi = xx | 0;
71
- const tx = xx - xxi;
72
- const s0 = clamped(xxi, yyi, old_width, old_height);
73
- const s1 = clamped(1 + xxi, yyi, old_width, old_height);
74
- const s2 = clamped(xxi, 1 + yyi, old_width, old_height);
75
- const s3 = clamped(1 + xxi, 1 + yyi, old_width, old_height);
76
-
77
- u8[offset++] = lerp(lerp(old[s0], old[s2], tx), lerp(old[s1], old[s3], tx), ty);
78
- u8[offset++] = lerp(lerp(old[1 + s0], old[1 + s2], tx), lerp(old[1 + s1], old[1 + s3], tx), ty);
79
- u8[offset++] = lerp(lerp(old[2 + s0], old[2 + s2], tx), lerp(old[2 + s1], old[2 + s3], tx), ty);
80
- u8[offset++] = lerp(lerp(old[3 + s0], old[3 + s2], tx), lerp(old[3 + s1], old[3 + s3], tx), ty);
81
- }
82
- }
83
-
84
- framebuffer.width = width;
85
- framebuffer.height = height;
86
- framebuffer.u8 = new Uint8Array(u8.buffer);
87
- framebuffer.view = new DataView(u8.buffer);
88
- framebuffer.u32 = new Uint32Array(u8.buffer);
89
- }
90
-
91
- export function cubic(width, height, framebuffer) {
92
- width = width | 0;
93
- height = height | 0;
94
- const old = framebuffer.u8;
95
- const old_width = framebuffer.width | 0;
96
- const old_height = framebuffer.height | 0;
97
- const u8 = new Uint8ClampedArray(4 * width * height);
98
-
99
- let offset = 0 | 0;
100
- const width1 = 1 / (width - 1);
101
- const height1 = 1 / (height - 1);
102
-
103
- for (let y = 0 | 0; y < height; y++) {
104
- const yy = old_height * (y * height1) - .5;
105
-
106
- const yyi = yy | 0;
107
- const ty = yy - yyi;
108
-
109
- for (let x = 0 | 0; x < width; x++) {
110
- const xx = old_width * (x * width1) - .5;
111
-
112
- const xxi = xx | 0;
113
- const tx = xx - xxi;
114
- const s0 = clamped(xxi - 1, yyi - 1, old_width, old_height);
115
- const s1 = clamped(0 + xxi, yyi - 1, old_width, old_height);
116
- const s2 = clamped(1 + xxi, yyi - 1, old_width, old_height);
117
- const s3 = clamped(2 + xxi, yyi - 1, old_width, old_height);
118
-
119
- const s4 = clamped(xxi - 1, yyi, old_width, old_height);
120
- const s5 = clamped(0 + xxi, yyi, old_width, old_height);
121
- const s6 = clamped(1 + xxi, yyi, old_width, old_height);
122
- const s7 = clamped(2 + xxi, yyi, old_width, old_height);
123
-
124
- const s8 = clamped(xxi - 1, 1 + yyi, old_width, old_height);
125
- const s9 = clamped(0 + xxi, 1 + yyi, old_width, old_height);
126
- const s10 = clamped(1 + xxi, 1 + yyi, old_width, old_height);
127
- const s11 = clamped(2 + xxi, 1 + yyi, old_width, old_height);
128
-
129
- const s12 = clamped(xxi - 1, 2 + yyi, old_width, old_height);
130
- const s13 = clamped(0 + xxi, 2 + yyi, old_width, old_height);
131
- const s14 = clamped(1 + xxi, 2 + yyi, old_width, old_height);
132
- const s15 = clamped(2 + xxi, 2 + yyi, old_width, old_height);
133
-
134
- const c0 = hermite(old[s0], old[s1], old[s2], old[s3], tx);
135
- const c00 = hermite(old[1 + s0], old[1 + s1], old[1 + s2], old[1 + s3], tx);
136
- const c000 = hermite(old[2 + s0], old[2 + s1], old[2 + s2], old[2 + s3], tx);
137
- const c0000 = hermite(old[3 + s0], old[3 + s1], old[3 + s2], old[3 + s3], tx);
138
-
139
- const c1 = hermite(old[s4], old[s5], old[s6], old[s7], tx);
140
- const c11 = hermite(old[1 + s4], old[1 + s5], old[1 + s6], old[1 + s7], tx);
141
- const c111 = hermite(old[2 + s4], old[2 + s5], old[2 + s6], old[2 + s7], tx);
142
- const c1111 = hermite(old[3 + s4], old[3 + s5], old[3 + s6], old[3 + s7], tx);
143
-
144
- const c2 = hermite(old[s8], old[s9], old[s10], old[s11], tx);
145
- const c22 = hermite(old[1 + s8], old[1 + s9], old[1 + s10], old[1 + s11], tx);
146
- const c222 = hermite(old[2 + s8], old[2 + s9], old[2 + s10], old[2 + s11], tx);
147
- const c2222 = hermite(old[3 + s8], old[3 + s9], old[3 + s10], old[3 + s11], tx);
148
-
149
- const c3 = hermite(old[s12], old[s13], old[s14], old[s15], tx);
150
- const c33 = hermite(old[1 + s12], old[1 + s13], old[1 + s14], old[1 + s15], tx);
151
- const c333 = hermite(old[2 + s12], old[2 + s13], old[2 + s14], old[2 + s15], tx);
152
- const c3333 = hermite(old[3 + s12], old[3 + s13], old[3 + s14], old[3 + s15], tx);
153
-
154
- u8[offset++] = hermite(c0, c1, c2, c3, ty);
155
- u8[offset++] = hermite(c00, c11, c22, c33, ty);
156
- u8[offset++] = hermite(c000, c111, c222, c333, ty);
157
- u8[offset++] = hermite(c0000, c1111, c2222, c3333, ty);
158
- }
159
- }
160
-
161
- framebuffer.width = width;
162
- framebuffer.height = height;
163
- framebuffer.u8 = new Uint8Array(u8.buffer);
164
- framebuffer.view = new DataView(u8.buffer);
165
- framebuffer.u32 = new Uint32Array(u8.buffer);
166
- }