imagescript 1.2.15 → 1.2.17

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 (53) hide show
  1. package/ImageScript.d.ts +233 -223
  2. package/ImageScript.js +1658 -1646
  3. package/LICENSE +12 -619
  4. package/LICENSE.AGPL-3.0 +619 -0
  5. package/LICENSE.MIT +19 -0
  6. package/README.md +51 -46
  7. package/bench/_.mjs +165 -165
  8. package/bench/all.mjs +3 -3
  9. package/bench/blur.mjs +58 -58
  10. package/bench/flip.mjs +62 -62
  11. package/bench/overlay.mjs +34 -34
  12. package/bench/package.json +6 -6
  13. package/bench/readme.md +63 -63
  14. package/bench/resize.mjs +51 -51
  15. package/codecs/LICENSE +20 -20
  16. package/codecs/lib.d.ts +49 -49
  17. package/codecs/lib.js +5 -5
  18. package/codecs/node/index.js +2 -2
  19. package/codecs/node/index.mjs +5 -5
  20. package/codecs/package.json +13 -13
  21. package/codecs/readme.md +37 -37
  22. package/package.json +41 -41
  23. package/png/node.js +1137 -1137
  24. package/png/src/crc.mjs +48 -48
  25. package/png/src/mem.mjs +23 -23
  26. package/png/src/png.mjs +254 -254
  27. package/png/src/zlib.mjs +617 -617
  28. package/tsconfig.json +21 -21
  29. package/utils/mem.js +25 -25
  30. package/v2/codecs/magic.js +24 -24
  31. package/v2/framebuffer.d.ts +66 -66
  32. package/v2/framebuffer.js +2352 -2352
  33. package/v2/framebuffer.mjs +125 -125
  34. package/v2/framebuffer.rs +1263 -1263
  35. package/v2/index.d.ts +83 -83
  36. package/v2/index.js +260 -260
  37. package/v2/ops/blur.mjs +302 -302
  38. package/v2/ops/color.mjs +276 -276
  39. package/v2/ops/crop.mjs +63 -63
  40. package/v2/ops/fill.mjs +32 -32
  41. package/v2/ops/flip.mjs +32 -32
  42. package/v2/ops/index.mjs +8 -8
  43. package/v2/ops/iterator.mjs +33 -33
  44. package/v2/ops/overlay.mjs +62 -62
  45. package/v2/ops/resize.mjs +165 -165
  46. package/v2/ops/rotate.mjs +116 -116
  47. package/v2/util/mem.mjs +23 -23
  48. package/wasm/node/font.js +141 -141
  49. package/wasm/node/gif.js +76 -76
  50. package/wasm/node/jpeg.js +91 -91
  51. package/wasm/node/png.js +49 -49
  52. package/wasm/node/svg.js +55 -55
  53. package/wasm/node/tiff.js +46 -46
package/v2/ops/resize.mjs CHANGED
@@ -1,166 +1,166 @@
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);
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
166
  }
package/v2/ops/rotate.mjs CHANGED
@@ -1,117 +1,117 @@
1
- export function rotate180(framebuffer) {
2
- framebuffer.u32.reverse();
3
- }
4
-
5
- export function rotate90(framebuffer) {
6
- const u32 = framebuffer.u32;
7
- const old = framebuffer.u32.slice();
8
- const width = framebuffer.width | 0;
9
- const height = framebuffer.height | 0;
10
-
11
- framebuffer.width = height;
12
- framebuffer.height = width;
13
-
14
- for (let y = 0 | 0; y < height; y++) {
15
- const yoffset = y * width;
16
- const heighty1 = height - 1 - y;
17
-
18
- for (let x = 0 | 0; x < width; x++) {
19
- u32[heighty1 + x * height] = old[x + yoffset];
20
- }
21
- }
22
- }
23
-
24
- export function rotate270(framebuffer) {
25
- const u32 = framebuffer.u32;
26
- const old = framebuffer.u32.slice();
27
- const width = framebuffer.width | 0;
28
- const height = framebuffer.height | 0;
29
-
30
- framebuffer.width = height;
31
- framebuffer.height = width;
32
-
33
- for (let y = 0 | 0; y < height; y++) {
34
- const yoffset = y * width;
35
-
36
- for (let x = 0 | 0; x < width; x++) {
37
- u32[y + height * (width - 1 - x)] = old[x + yoffset];
38
- }
39
- }
40
- }
41
-
42
- // broken?
43
- export function rotate(deg, framebuffer, resize) {
44
- const rad = Math.PI * ((360 - deg) / 180);
45
-
46
- const sin = Math.sin(rad);
47
- const cos = Math.cos(rad);
48
-
49
- const width = (resize ? Math.abs(framebuffer.width * sin) + Math.abs(framebuffer.height * cos) : framebuffer.width) | 0;
50
- const height = (resize ? Math.abs(framebuffer.width * cos) + Math.abs(framebuffer.height * sin) : framebuffer.height) | 0;
51
-
52
- const same_size = width === framebuffer.width && height === framebuffer.height;
53
-
54
- const inn = same_size ? framebuffer.clone() : framebuffer;
55
- const out = { width, height, u8: same_size ? framebuffer.u8 : new Uint8Array(4 * width * height) };
56
-
57
- const out_cx = width / 2 - .5;
58
- const out_cy = height / 2 - .5;
59
- const src_cx = framebuffer.width / 2 - .5;
60
- const src_cy = framebuffer.height / 2 - .5;
61
-
62
- let h = 0;
63
- do {
64
- let w = 0;
65
- const ysin = src_cx - sin * (h - out_cy);
66
- const ycos = src_cy + cos * (h - out_cy);
67
-
68
- do {
69
- interpolate(inn, out, w, h, ysin + cos * (w - out_cx), ycos + sin * (w - out_cx));
70
- } while (w++ < width);
71
- } while (h++ < height);
72
-
73
- framebuffer.u8 = out.u8;
74
- framebuffer.width = width;
75
- framebuffer.height = height;
76
- framebuffer.view = new DataView(out.u8.buffer, out.u8.byteOffset, out.u8.byteLength);
77
- framebuffer.u32 = new Uint32Array(out.u8.buffer, out.u8.byteOffset, out.u8.byteLength / 4);
78
- }
79
-
80
- function interpolate(inn, out, x0, y0, x1, y1) {
81
- const x2 = ~~x1;
82
- const y2 = ~~y1;
83
- const xq = x1 - x2;
84
- const yq = y1 - y2;
85
- const offset = 4 * (x0 + y0 * out.width);
86
-
87
- const ref = { r: 0, g: 0, b: 0, a: 0 };
88
- pawn(x2, y2, (1 - xq) * (1 - yq), ref, inn);
89
-
90
- pawn(1 + x2, y2, xq * (1 - yq), ref, inn);
91
- pawn(x2, 1 + y2, (1 - xq) * yq, ref, inn);
92
-
93
- pawn(1 + x2, 1 + y2, xq * yq, ref, inn);
94
-
95
- out.u8[3 + offset] = ref.a;
96
- out.u8[offset] = ref.r / ref.a;
97
- out.u8[1 + offset] = ref.g / ref.a;
98
- out.u8[2 + offset] = ref.b / ref.a;
99
- }
100
-
101
- function pawn(point0, point1, weight, ref, inn) {
102
- if (
103
- point0 > 0
104
- && point1 > 0
105
- && point0 < inn.width
106
- && point1 < inn.height
107
- ) {
108
- const offset = 4 * (point0 + point1 * inn.width);
109
-
110
- const wa = weight * inn.u8[3 + offset];
111
-
112
- ref.a += wa;
113
- ref.r += wa * inn.u8[offset];
114
- ref.g += wa * inn.u8[1 + offset];
115
- ref.b += wa * inn.u8[2 + offset];
116
- }
1
+ export function rotate180(framebuffer) {
2
+ framebuffer.u32.reverse();
3
+ }
4
+
5
+ export function rotate90(framebuffer) {
6
+ const u32 = framebuffer.u32;
7
+ const old = framebuffer.u32.slice();
8
+ const width = framebuffer.width | 0;
9
+ const height = framebuffer.height | 0;
10
+
11
+ framebuffer.width = height;
12
+ framebuffer.height = width;
13
+
14
+ for (let y = 0 | 0; y < height; y++) {
15
+ const yoffset = y * width;
16
+ const heighty1 = height - 1 - y;
17
+
18
+ for (let x = 0 | 0; x < width; x++) {
19
+ u32[heighty1 + x * height] = old[x + yoffset];
20
+ }
21
+ }
22
+ }
23
+
24
+ export function rotate270(framebuffer) {
25
+ const u32 = framebuffer.u32;
26
+ const old = framebuffer.u32.slice();
27
+ const width = framebuffer.width | 0;
28
+ const height = framebuffer.height | 0;
29
+
30
+ framebuffer.width = height;
31
+ framebuffer.height = width;
32
+
33
+ for (let y = 0 | 0; y < height; y++) {
34
+ const yoffset = y * width;
35
+
36
+ for (let x = 0 | 0; x < width; x++) {
37
+ u32[y + height * (width - 1 - x)] = old[x + yoffset];
38
+ }
39
+ }
40
+ }
41
+
42
+ // broken?
43
+ export function rotate(deg, framebuffer, resize) {
44
+ const rad = Math.PI * ((360 - deg) / 180);
45
+
46
+ const sin = Math.sin(rad);
47
+ const cos = Math.cos(rad);
48
+
49
+ const width = (resize ? Math.abs(framebuffer.width * sin) + Math.abs(framebuffer.height * cos) : framebuffer.width) | 0;
50
+ const height = (resize ? Math.abs(framebuffer.width * cos) + Math.abs(framebuffer.height * sin) : framebuffer.height) | 0;
51
+
52
+ const same_size = width === framebuffer.width && height === framebuffer.height;
53
+
54
+ const inn = same_size ? framebuffer.clone() : framebuffer;
55
+ const out = { width, height, u8: same_size ? framebuffer.u8 : new Uint8Array(4 * width * height) };
56
+
57
+ const out_cx = width / 2 - .5;
58
+ const out_cy = height / 2 - .5;
59
+ const src_cx = framebuffer.width / 2 - .5;
60
+ const src_cy = framebuffer.height / 2 - .5;
61
+
62
+ let h = 0;
63
+ do {
64
+ let w = 0;
65
+ const ysin = src_cx - sin * (h - out_cy);
66
+ const ycos = src_cy + cos * (h - out_cy);
67
+
68
+ do {
69
+ interpolate(inn, out, w, h, ysin + cos * (w - out_cx), ycos + sin * (w - out_cx));
70
+ } while (w++ < width);
71
+ } while (h++ < height);
72
+
73
+ framebuffer.u8 = out.u8;
74
+ framebuffer.width = width;
75
+ framebuffer.height = height;
76
+ framebuffer.view = new DataView(out.u8.buffer, out.u8.byteOffset, out.u8.byteLength);
77
+ framebuffer.u32 = new Uint32Array(out.u8.buffer, out.u8.byteOffset, out.u8.byteLength / 4);
78
+ }
79
+
80
+ function interpolate(inn, out, x0, y0, x1, y1) {
81
+ const x2 = ~~x1;
82
+ const y2 = ~~y1;
83
+ const xq = x1 - x2;
84
+ const yq = y1 - y2;
85
+ const offset = 4 * (x0 + y0 * out.width);
86
+
87
+ const ref = { r: 0, g: 0, b: 0, a: 0 };
88
+ pawn(x2, y2, (1 - xq) * (1 - yq), ref, inn);
89
+
90
+ pawn(1 + x2, y2, xq * (1 - yq), ref, inn);
91
+ pawn(x2, 1 + y2, (1 - xq) * yq, ref, inn);
92
+
93
+ pawn(1 + x2, 1 + y2, xq * yq, ref, inn);
94
+
95
+ out.u8[3 + offset] = ref.a;
96
+ out.u8[offset] = ref.r / ref.a;
97
+ out.u8[1 + offset] = ref.g / ref.a;
98
+ out.u8[2 + offset] = ref.b / ref.a;
99
+ }
100
+
101
+ function pawn(point0, point1, weight, ref, inn) {
102
+ if (
103
+ point0 > 0
104
+ && point1 > 0
105
+ && point0 < inn.width
106
+ && point1 < inn.height
107
+ ) {
108
+ const offset = 4 * (point0 + point1 * inn.width);
109
+
110
+ const wa = weight * inn.u8[3 + offset];
111
+
112
+ ref.a += wa;
113
+ ref.r += wa * inn.u8[offset];
114
+ ref.g += wa * inn.u8[1 + offset];
115
+ ref.b += wa * inn.u8[2 + offset];
116
+ }
117
117
  }
package/v2/util/mem.mjs CHANGED
@@ -1,24 +1,24 @@
1
- export function view(buffer, shared = false) {
2
- if (buffer instanceof ArrayBuffer) return new Uint8Array(buffer);
3
- if (shared && buffer instanceof SharedArrayBuffer) return new Uint8Array(buffer);
4
- if (ArrayBuffer.isView(buffer)) return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
5
-
6
- throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
7
- }
8
-
9
- export function from_parts(buffers, shared = false) {
10
- let length = 0;
11
- let offset = 0;
12
- buffers.forEach(buffer => length += (null == buffer.byteLength ? buffer.length : buffer.byteLength));
13
-
14
- const u8 = new Uint8Array(shared ? new SharedArrayBuffer(length) : length);
15
-
16
- buffers.forEach(buffer => {
17
- const ref = Array.isArray(buffer) ? buffer : view(buffer, true);
18
-
19
- u8.set(ref, offset);
20
- offset += ref.length;
21
- });
22
-
23
- return u8;
1
+ export function view(buffer, shared = false) {
2
+ if (buffer instanceof ArrayBuffer) return new Uint8Array(buffer);
3
+ if (shared && buffer instanceof SharedArrayBuffer) return new Uint8Array(buffer);
4
+ if (ArrayBuffer.isView(buffer)) return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
5
+
6
+ throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
7
+ }
8
+
9
+ export function from_parts(buffers, shared = false) {
10
+ let length = 0;
11
+ let offset = 0;
12
+ buffers.forEach(buffer => length += (null == buffer.byteLength ? buffer.length : buffer.byteLength));
13
+
14
+ const u8 = new Uint8Array(shared ? new SharedArrayBuffer(length) : length);
15
+
16
+ buffers.forEach(buffer => {
17
+ const ref = Array.isArray(buffer) ? buffer : view(buffer, true);
18
+
19
+ u8.set(ref, offset);
20
+ offset += ref.length;
21
+ });
22
+
23
+ return u8;
24
24
  }