imagescript 1.2.9 → 1.2.15

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 (90) hide show
  1. package/ImageScript.d.ts +223 -0
  2. package/ImageScript.js +10 -46
  3. package/LICENSE +619 -0
  4. package/README.md +17 -16
  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 +17 -4
  61. package/png/src/{crc.js → crc.mjs} +0 -0
  62. package/png/src/{mem.js → mem.mjs} +0 -0
  63. package/png/src/{png.js → png.mjs} +3 -3
  64. package/png/src/{zlib.js → zlib.mjs} +0 -0
  65. package/tsconfig.json +21 -0
  66. package/v2/framebuffer.js +93 -112
  67. package/v2/framebuffer.mjs +8 -8
  68. package/v2/framebuffer.rs +1264 -0
  69. package/v2/index.js +1 -1
  70. package/v2/ops/{blur.js → blur.mjs} +0 -0
  71. package/v2/ops/{color.js → color.mjs} +0 -0
  72. package/v2/ops/{crop.js → crop.mjs} +0 -0
  73. package/v2/ops/{fill.js → fill.mjs} +0 -0
  74. package/v2/ops/{flip.js → flip.mjs} +0 -0
  75. package/v2/ops/index.mjs +9 -0
  76. package/v2/ops/{iterator.js → iterator.mjs} +6 -6
  77. package/v2/ops/overlay.mjs +63 -0
  78. package/v2/ops/{resize.js → resize.mjs} +3 -3
  79. package/v2/ops/{rotate.js → rotate.mjs} +8 -9
  80. package/v2/util/{mem.js → mem.mjs} +3 -5
  81. package/wasm/any/font.wasm +0 -0
  82. package/wasm/any/png.wasm +0 -0
  83. package/wasm/node/png.js +49 -0
  84. package/node/arm64-darwin.node +0 -0
  85. package/node/index.js +0 -7
  86. package/node/x64-darwin.node +0 -0
  87. package/v2/ops/index.js +0 -9
  88. package/v2/ops/overlay.js +0 -56
  89. package/wasm/node.js +0 -23
  90. package/wasm/web.js +0 -10
package/v2/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const mem = require('./util/mem.js');
2
2
  const png = require('../png/node.js');
3
3
  const magic = require('./codecs/magic.js');
4
- const codecs = require('../node/index.js');
4
+ const codecs = require('../codecs/node/index.js');
5
5
  const { Color, default: framebuffer } = require('./framebuffer.js');
6
6
 
7
7
  const wasm = {
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -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';
@@ -2,8 +2,8 @@ export function* cords(framebuffer) {
2
2
  const width = framebuffer.width | 0;
3
3
  const height = framebuffer.height | 0;
4
4
 
5
- for (let y = 1 | 0; y <= height; y++) {
6
- for (let x = 1 | 0; x <= width; x++) yield [x, y];
5
+ for (let y = 0 | 0; y < height; y++) {
6
+ for (let x = 0 | 0; x < width; x++) yield [x, y];
7
7
  }
8
8
  }
9
9
 
@@ -13,8 +13,8 @@ export function* rgba(framebuffer) {
13
13
  const width = framebuffer.width | 0;
14
14
  const height = framebuffer.height | 0;
15
15
 
16
- for (let y = 1 | 0; y <= height; y++) {
17
- for (let x = 1 | 0; x <= width; x++) {
16
+ for (let y = 0 | 0; y < height; y++) {
17
+ for (let x = 0 | 0; x < width; x++) {
18
18
  yield [x, y, u8.subarray(offset, offset += 4)];
19
19
  }
20
20
  }
@@ -26,8 +26,8 @@ export function* u32(framebuffer) {
26
26
  const width = framebuffer.width | 0;
27
27
  const height = framebuffer.height | 0;
28
28
 
29
- for (let y = 1 | 0; y <= height; y++) {
30
- for (let x = 1 | 0; x <= width; x++) {
29
+ for (let y = 0 | 0; y < height; y++) {
30
+ for (let x = 0 | 0; x < width; x++) {
31
31
  yield [x, y, view.getUint32(offset, false)]; offset += 4;
32
32
  }
33
33
  }
@@ -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
+ }
@@ -11,9 +11,9 @@ function clamped(x, y, width, height) {
11
11
  }
12
12
 
13
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);
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
17
 
18
18
  const t2 = t * t;
19
19
  return B + (c * t) + (b * t2) + (a * t * t2);
@@ -11,12 +11,12 @@ export function rotate90(framebuffer) {
11
11
  framebuffer.width = height;
12
12
  framebuffer.height = width;
13
13
 
14
- for (let y = 0 | 0; y < width; y++) {
14
+ for (let y = 0 | 0; y < height; y++) {
15
15
  const yoffset = y * width;
16
- const height1y = height - 1 - y;
16
+ const heighty1 = height - 1 - y;
17
17
 
18
- for (let x = 0 | 0; x < height; x++) {
19
- u32[height1y + x * width] = old[x + yoffset];
18
+ for (let x = 0 | 0; x < width; x++) {
19
+ u32[heighty1 + x * height] = old[x + yoffset];
20
20
  }
21
21
  }
22
22
  }
@@ -30,12 +30,11 @@ export function rotate270(framebuffer) {
30
30
  framebuffer.width = height;
31
31
  framebuffer.height = width;
32
32
 
33
- for (let y = 0 | 0; y < width; y++) {
34
- const yoffset = y * width | 0;
35
- const soffset = (y + width * (width - 1)) | 0;
33
+ for (let y = 0 | 0; y < height; y++) {
34
+ const yoffset = y * width;
36
35
 
37
- for (let x = 0 | 0; x < height; x++) {
38
- u32[soffset - x * width] = old[x + yoffset];
36
+ for (let x = 0 | 0; x < width; x++) {
37
+ u32[y + height * (width - 1 - x)] = old[x + yoffset];
39
38
  }
40
39
  }
41
40
  }
@@ -1,4 +1,4 @@
1
- function view(buffer, shared = false) {
1
+ export function view(buffer, shared = false) {
2
2
  if (buffer instanceof ArrayBuffer) return new Uint8Array(buffer);
3
3
  if (shared && buffer instanceof SharedArrayBuffer) return new Uint8Array(buffer);
4
4
  if (ArrayBuffer.isView(buffer)) return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
@@ -6,7 +6,7 @@ function view(buffer, shared = false) {
6
6
  throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
7
7
  }
8
8
 
9
- function from_parts(buffers, shared = false) {
9
+ export function from_parts(buffers, shared = false) {
10
10
  let length = 0;
11
11
  let offset = 0;
12
12
  buffers.forEach(buffer => length += (null == buffer.byteLength ? buffer.length : buffer.byteLength));
@@ -21,6 +21,4 @@ function from_parts(buffers, shared = false) {
21
21
  });
22
22
 
23
23
  return u8;
24
- }
25
-
26
- module.exports = { view, from_parts };
24
+ }
Binary file
Binary file
@@ -0,0 +1,49 @@
1
+ const wasm_name = 'png';
2
+ const { join } = require('path');
3
+ const { promises: { readFile } } = require('fs');
4
+ const wasm_path = process.env.IMAGESCRIPT_WASM_SIMD ? 'simd' : 'any';
5
+
6
+ let mod = null;
7
+ module.exports = {
8
+ async init() {
9
+ if (!mod) mod = new WebAssembly.Module(await readFile(join(__dirname, `../${wasm_path}/${wasm_name}.wasm`)));
10
+
11
+ return this.new();
12
+ },
13
+
14
+ new() {
15
+ let u8;
16
+
17
+ const {
18
+ wfree, walloc, decode, memory,
19
+ width: wwidth, height: wheight,
20
+ } = new WebAssembly.Instance(mod, {
21
+ env: {
22
+ emscripten_notify_memory_growth() {
23
+ u8 = new Uint8Array(memory.buffer);
24
+ },
25
+ },
26
+ }).exports;
27
+
28
+ u8 = new Uint8Array(memory.buffer);
29
+
30
+ return {
31
+ decode(buffer) {
32
+ const ptr = walloc(buffer.length);
33
+
34
+ u8.set(buffer, ptr);
35
+ const status = decode(ptr, buffer.length);
36
+
37
+ wfree(ptr);
38
+ if (0 > status) throw new Error(`png: failed to decode (${status})`);
39
+
40
+ const width = wwidth();
41
+ const height = wheight();
42
+ const framebuffer = u8.slice(status, status + 4 * width * height);
43
+
44
+ wfree(status);
45
+ return { width, height, framebuffer };
46
+ },
47
+ };
48
+ }
49
+ }
Binary file
package/node/index.js DELETED
@@ -1,7 +0,0 @@
1
- const { arch, platform } = require('os');
2
-
3
- try {
4
- module.exports = require(`./${arch()}-${platform()}.node`);
5
- } catch (err) {
6
- throw new Error('unsupported arch/platform: ' + err.message);
7
- }
Binary file
package/v2/ops/index.js DELETED
@@ -1,9 +0,0 @@
1
- export * as flip from './flip.js';
2
- export * as fill from './fill.js';
3
- export * as blur from './blur.js';
4
- export * as crop from './crop.js';
5
- export * as color from './color.js';
6
- export * as resize from './resize.js';
7
- export * as rotate from './rotate.js';
8
- export * as overlay from './overlay.js';
9
- export * as iterator from './iterator.js';
package/v2/ops/overlay.js DELETED
@@ -1,56 +0,0 @@
1
- import { blend } from './color.js';
2
-
3
- export function replace(bg, fg, x, y) {
4
- const fwidth = fg.width | 0;
5
- const bwidth = bg.width | 0;
6
- const fheight = fg.height | 0;
7
- const bheight = bg.height | 0;
8
- let mw = Math.min(bwidth, fwidth) | 0;
9
- let mh = Math.min(bheight, fheight) | 0;
10
-
11
- const b32 = bg.u32;
12
- const f32 = fg.u32;
13
-
14
- // todo: range
15
- // todo: negative speed
16
- for (let yy = y | 0; yy < mh; yy++) {
17
- const yoffset = yy * bwidth;
18
- const yyoffset = fwidth * (yy - y);
19
-
20
- for (let xx = x | 0; xx < mw; xx++) {
21
- b32[xx + yoffset] = f32[xx - x + yyoffset];
22
- }
23
- }
24
- }
25
-
26
- export function overlay(background, foreground, x, y) {
27
- x = x | 0;
28
- y = y | 0;
29
- const fwidth = foreground.width | 0;
30
- const bwidth = background.width | 0;
31
- const fheight = foreground.height | 0;
32
- const bheight = background.height | 0;
33
- const mw = Math.min(bwidth, fwidth) | 0;
34
- const mh = Math.min(bheight, fheight) | 0;
35
-
36
- const fview = foreground.view;
37
- const bview = background.view;
38
-
39
- // todo: fix range
40
- // todo: negative perf
41
- for (let yy = y | 0; yy < mh; yy++) {
42
- const yoffset = yy * bwidth;
43
- const yyoffset = fwidth * (yy - y);
44
-
45
- for (let xx = x | 0; xx < mw; xx++) {
46
- const offset = 4 * (xx + yoffset);
47
- const fg = fview.getUint32(4 * (xx - x + yyoffset), false);
48
-
49
- const bg = bview.getUint32(offset, false);
50
- if ((fg & 0xff) === 0xff) bview.setUint32(offset, fg, false);
51
- else if ((fg & 0xff) === 0x00) bview.setUint32(offset, bg, false);
52
-
53
- else bview.setUint32(offset, blend(fg, bg), false);
54
- }
55
- }
56
- }
package/wasm/node.js DELETED
@@ -1,23 +0,0 @@
1
- let mod = async () => {
2
- let t;
3
-
4
- {
5
- const simd = WebAssembly.validate(Uint8Array.of(0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,9,1,7,0,65,0,253,15,26,11));
6
- t = new WebAssembly.Module(await require('fs').promises.readFile(require('path').join(__dirname, `${simd ? 'simd' : 'unknown'}.wasm`)));
7
- }
8
-
9
- return (mod = () => t)();
10
- };
11
-
12
- module.exports = async function codecs() {
13
- const wasm = new WebAssembly.Instance(await mod(), {});
14
-
15
- return {
16
- png: null,
17
- gif: null,
18
- svg: null,
19
- webp: null,
20
- jpeg: null,
21
- tiff: null,
22
- };
23
- }
package/wasm/web.js DELETED
@@ -1,10 +0,0 @@
1
- export default async function codecs() {
2
- return {
3
- png: null,
4
- gif: null,
5
- svg: null,
6
- webp: null,
7
- jpeg: null,
8
- tiff: null,
9
- };
10
- }