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,64 +1,64 @@
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;
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
64
  }
@@ -1,33 +1,33 @@
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
- }
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
33
  }
@@ -1,33 +1,33 @@
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
- }
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
33
  }
@@ -0,0 +1,9 @@
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 +1,34 @@
1
- export function* cords(framebuffer) {
2
- const width = framebuffer.width | 0;
3
- const height = framebuffer.height | 0;
4
-
5
- for (let y = 1 | 0; y <= height; y++) {
6
- for (let x = 1 | 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 = 1 | 0; y <= height; y++) {
17
- for (let x = 1 | 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 = 1 | 0; y <= height; y++) {
30
- for (let x = 1 | 0; x <= width; x++) {
31
- yield [x, y, view.getUint32(offset, false)]; offset += 4;
32
- }
33
- }
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
34
  }
@@ -0,0 +1,63 @@
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
+ }