imagescript 1.2.9 → 1.2.16

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 (100) hide show
  1. package/ImageScript.d.ts +223 -0
  2. package/ImageScript.js +1646 -1682
  3. package/LICENSE +619 -0
  4. package/README.md +46 -45
  5. package/bench/_.mjs +166 -0
  6. package/bench/all.mjs +4 -0
  7. package/bench/blur.mjs +59 -0
  8. package/bench/flip.mjs +63 -0
  9. package/bench/overlay.mjs +35 -0
  10. package/bench/package.json +7 -0
  11. package/bench/readme.md +64 -0
  12. package/bench/resize.mjs +52 -0
  13. package/codecs/LICENSE +21 -0
  14. package/codecs/lib.d.ts +50 -0
  15. package/codecs/lib.js +6 -0
  16. package/codecs/lib.mjs +1 -0
  17. package/codecs/node/bin/arm64-darwin.node +0 -0
  18. package/{node → codecs/node/bin}/arm64-linux.node +0 -0
  19. package/codecs/node/bin/x64-darwin.node +0 -0
  20. package/{node → codecs/node/bin}/x64-linux.node +0 -0
  21. package/{node → codecs/node/bin}/x64-win32.node +0 -0
  22. package/codecs/node/index.js +3 -0
  23. package/codecs/node/index.mjs +6 -0
  24. package/codecs/package.json +14 -0
  25. package/codecs/readme.md +38 -0
  26. package/{wasm/simd.wasm → codecs/tests/gif.js} +0 -0
  27. package/{wasm/unknown.wasm → codecs/tests/jpeg.js} +0 -0
  28. package/codecs/tests/png.js +0 -0
  29. package/codecs/tests/results/gif/1024.nis.gif +0 -0
  30. package/codecs/tests/results/gif/1024.skyra.gif +0 -0
  31. package/codecs/tests/results/gif/256.nis.gif +0 -0
  32. package/codecs/tests/results/gif/256.skyra.gif +0 -0
  33. package/codecs/tests/results/jpeg/256.jpegjs.jpeg +0 -0
  34. package/codecs/tests/results/jpeg/256.nis.jpeg +0 -0
  35. package/codecs/tests/results/jpeg/256.sharp.jpeg +0 -0
  36. package/codecs/tests/results/jpeg/512.jpegjs.jpeg +0 -0
  37. package/codecs/tests/results/jpeg/512.nis.jpeg +0 -0
  38. package/codecs/tests/results/jpeg/512.sharp.jpeg +0 -0
  39. package/codecs/tests/results/jpeg/64.jpegjs.jpeg +0 -0
  40. package/codecs/tests/results/jpeg/64.nis.jpeg +0 -0
  41. package/codecs/tests/results/jpeg/64.sharp.jpeg +0 -0
  42. package/codecs/tests/results/png/256.jis.png +0 -0
  43. package/codecs/tests/results/png/256.nis.png +0 -0
  44. package/codecs/tests/results/png/256.sharp.png +0 -0
  45. package/codecs/tests/results/png/256.upng.png +0 -0
  46. package/codecs/tests/results/png/512.jis.png +0 -0
  47. package/codecs/tests/results/png/512.nis.png +0 -0
  48. package/codecs/tests/results/png/512.sharp.png +0 -0
  49. package/codecs/tests/results/png/512.upng.png +0 -0
  50. package/codecs/tests/results/png/64.jis.png +0 -0
  51. package/codecs/tests/results/png/64.nis.png +0 -0
  52. package/codecs/tests/results/png/64.sharp.png +0 -0
  53. package/codecs/tests/results/png/64.upng.png +0 -0
  54. package/codecs/tests/webp.js +0 -0
  55. package/codecs/wasm/bin/codecs.wasm +0 -0
  56. package/codecs/wasm/bin/simd.wasm +0 -0
  57. package/codecs/wasm/bundle/browser.js +1 -0
  58. package/codecs/wasm/index.js +1 -0
  59. package/codecs/wasm/index.mjs +1 -0
  60. package/package.json +41 -28
  61. package/png/node.js +1137 -1137
  62. package/png/src/{crc.js → crc.mjs} +48 -48
  63. package/png/src/{mem.js → mem.mjs} +23 -23
  64. package/png/src/{png.js → png.mjs} +254 -254
  65. package/png/src/{zlib.js → zlib.mjs} +617 -617
  66. package/tsconfig.json +21 -0
  67. package/utils/mem.js +25 -25
  68. package/v2/codecs/magic.js +24 -24
  69. package/v2/framebuffer.d.ts +66 -66
  70. package/v2/framebuffer.js +2352 -2371
  71. package/v2/framebuffer.mjs +125 -125
  72. package/v2/framebuffer.rs +1264 -0
  73. package/v2/index.d.ts +83 -83
  74. package/v2/index.js +260 -260
  75. package/v2/ops/{blur.js → blur.mjs} +302 -302
  76. package/v2/ops/{color.js → color.mjs} +276 -276
  77. package/v2/ops/{crop.js → crop.mjs} +63 -63
  78. package/v2/ops/{fill.js → fill.mjs} +32 -32
  79. package/v2/ops/{flip.js → flip.mjs} +32 -32
  80. package/v2/ops/index.mjs +9 -0
  81. package/v2/ops/{iterator.js → iterator.mjs} +33 -33
  82. package/v2/ops/overlay.mjs +63 -0
  83. package/v2/ops/{resize.js → resize.mjs} +165 -165
  84. package/v2/ops/{rotate.js → rotate.mjs} +116 -117
  85. package/v2/util/{mem.js → mem.mjs} +24 -26
  86. package/wasm/any/font.wasm +0 -0
  87. package/wasm/any/png.wasm +0 -0
  88. package/wasm/node/font.js +141 -141
  89. package/wasm/node/gif.js +76 -76
  90. package/wasm/node/jpeg.js +91 -91
  91. package/wasm/node/png.js +49 -0
  92. package/wasm/node/svg.js +55 -55
  93. package/wasm/node/tiff.js +46 -46
  94. package/node/arm64-darwin.node +0 -0
  95. package/node/index.js +0 -7
  96. package/node/x64-darwin.node +0 -0
  97. package/v2/ops/index.js +0 -9
  98. package/v2/ops/overlay.js +0 -56
  99. package/wasm/node.js +0 -23
  100. package/wasm/web.js +0 -10
@@ -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 + (2 * C) - (D / 2) - ((5 * B) / 2);
16
- const a = (D / 2) + (-A / 2) + ((3 * B) / 2) - ((3 * C) / 2);
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
  }
@@ -1,118 +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 < width; y++) {
15
- const yoffset = y * width;
16
- const height1y = height - 1 - y;
17
-
18
- for (let x = 0 | 0; x < height; x++) {
19
- u32[height1y + x * width] = 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 < width; y++) {
34
- const yoffset = y * width | 0;
35
- const soffset = (y + width * (width - 1)) | 0;
36
-
37
- for (let x = 0 | 0; x < height; x++) {
38
- u32[soffset - x * width] = old[x + yoffset];
39
- }
40
- }
41
- }
42
-
43
- // broken?
44
- export function rotate(deg, framebuffer, resize) {
45
- const rad = Math.PI * ((360 - deg) / 180);
46
-
47
- const sin = Math.sin(rad);
48
- const cos = Math.cos(rad);
49
-
50
- const width = (resize ? Math.abs(framebuffer.width * sin) + Math.abs(framebuffer.height * cos) : framebuffer.width) | 0;
51
- const height = (resize ? Math.abs(framebuffer.width * cos) + Math.abs(framebuffer.height * sin) : framebuffer.height) | 0;
52
-
53
- const same_size = width === framebuffer.width && height === framebuffer.height;
54
-
55
- const inn = same_size ? framebuffer.clone() : framebuffer;
56
- const out = { width, height, u8: same_size ? framebuffer.u8 : new Uint8Array(4 * width * height) };
57
-
58
- const out_cx = width / 2 - .5;
59
- const out_cy = height / 2 - .5;
60
- const src_cx = framebuffer.width / 2 - .5;
61
- const src_cy = framebuffer.height / 2 - .5;
62
-
63
- let h = 0;
64
- do {
65
- let w = 0;
66
- const ysin = src_cx - sin * (h - out_cy);
67
- const ycos = src_cy + cos * (h - out_cy);
68
-
69
- do {
70
- interpolate(inn, out, w, h, ysin + cos * (w - out_cx), ycos + sin * (w - out_cx));
71
- } while (w++ < width);
72
- } while (h++ < height);
73
-
74
- framebuffer.u8 = out.u8;
75
- framebuffer.width = width;
76
- framebuffer.height = height;
77
- framebuffer.view = new DataView(out.u8.buffer, out.u8.byteOffset, out.u8.byteLength);
78
- framebuffer.u32 = new Uint32Array(out.u8.buffer, out.u8.byteOffset, out.u8.byteLength / 4);
79
- }
80
-
81
- function interpolate(inn, out, x0, y0, x1, y1) {
82
- const x2 = ~~x1;
83
- const y2 = ~~y1;
84
- const xq = x1 - x2;
85
- const yq = y1 - y2;
86
- const offset = 4 * (x0 + y0 * out.width);
87
-
88
- const ref = { r: 0, g: 0, b: 0, a: 0 };
89
- pawn(x2, y2, (1 - xq) * (1 - yq), ref, inn);
90
-
91
- pawn(1 + x2, y2, xq * (1 - yq), ref, inn);
92
- pawn(x2, 1 + y2, (1 - xq) * yq, ref, inn);
93
-
94
- pawn(1 + x2, 1 + y2, xq * yq, ref, inn);
95
-
96
- out.u8[3 + offset] = ref.a;
97
- out.u8[offset] = ref.r / ref.a;
98
- out.u8[1 + offset] = ref.g / ref.a;
99
- out.u8[2 + offset] = ref.b / ref.a;
100
- }
101
-
102
- function pawn(point0, point1, weight, ref, inn) {
103
- if (
104
- point0 > 0
105
- && point1 > 0
106
- && point0 < inn.width
107
- && point1 < inn.height
108
- ) {
109
- const offset = 4 * (point0 + point1 * inn.width);
110
-
111
- const wa = weight * inn.u8[3 + offset];
112
-
113
- ref.a += wa;
114
- ref.r += wa * inn.u8[offset];
115
- ref.g += wa * inn.u8[1 + offset];
116
- ref.b += wa * inn.u8[2 + offset];
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
+ }
118
117
  }
@@ -1,26 +1,24 @@
1
- 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
- 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
- }
25
-
26
- module.exports = { view, from_parts };
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
+ }
Binary file
Binary file