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,126 +1,126 @@
1
- import Color from './ops/color.js';
2
- import { view } from './util/mem.js';
3
- import * as ops from './ops/index.js';
4
- import * as png from '../png/src/png.js';
5
-
6
- export { Color };
7
-
8
- // todo: tree shakable context
9
- // todo: make errors more verbose
10
-
11
- export default class framebuffer {
12
- constructor(width, height, buffer) {
13
- this.width = width | 0;
14
- this.height = height | 0;
15
- this.u8 = buffer ? view(buffer) : new Uint8Array(4 * this.width * this.height);
16
- this.view = new DataView(this.u8.buffer, this.u8.byteOffset, this.u8.byteLength);
17
- this.u32 = new Uint32Array(this.u8.buffer, this.u8.byteOffset, this.u8.byteLength / 4);
18
- if (this.u8.length !== 4 * this.width * this.height) throw new RangeError('invalid capacity of buffer');
19
- }
20
-
21
- [Symbol.iterator]() { return ops.iterator.cords(this); }
22
- toString() { return `framebuffer<${this.width}x${this.height}>`; }
23
- clone() { return new this.constructor(this.width, this.height, this.u8.slice()); }
24
- toJSON() { return { width: this.width, height: this.height, buffer: Array.from(this.u8) } }
25
- get(x, y) { return this.view.getUint32(((x | 0) - 1) + ((y | 0) - 1) * this.width, false); }
26
- scale(type, factor) { return this.resize(type, factor * this.width, factor * this.height); }
27
- overlay(frame, x = 0, y = 0) { return (ops.overlay.overlay(this, frame, x | 0, y | 0), this); }
28
- replace(frame, x = 0, y = 0) { return (ops.overlay.replace(this, frame, x | 0, y | 0), this); }
29
- set(x, y, color) { this.view.setUint32(((x | 0) - 1) + ((y | 0) - 1) * this.width, color, false); }
30
- at(x, y) { const offset = 4 * (((x | 0) - 1) + ((y | 0) - 1) * this.width); return this.u8.subarray(offset, 4 + offset); }
31
- static from(framebuffer) { return new this(framebuffer.width, framebuffer.height, framebuffer.u8 || framebuffer.buffer); }
32
- static decode(format, buffer) { if (format !== 'png') throw new RangeError('invalid image format'); else return framebuffer.from(png.decode(buffer)); }
33
-
34
- encode(format, options = {}) {
35
- if (format !== 'png') throw new RangeError('invalid image format');
36
- else return png.encode(this.u8, { channels: 4, width: this.width, height: this.height, level: ({ none: 0, fast: 3, default: 6, best: 9 })[options.compression] ?? 3 });
37
- }
38
-
39
- pixels(type) {
40
- if ('rgba' === type) return ops.iterator.rgba(this);
41
- if (!type || 'int' === type) return ops.iterator.u32(this);
42
-
43
- throw new RangeError('invalid iterator type');
44
- }
45
-
46
- flip(type) {
47
- if (type === 'vertical') ops.flip.vertical(this);
48
- else if (type === 'horizontal') ops.flip.horizontal(this);
49
-
50
- else throw new RangeError('invalid flip type');
51
-
52
- return this;
53
- }
54
-
55
- crop(type, arg0, arg1, arg2, arg3) {
56
- if (type === 'circle') ops.crop.circle(arg0 || 0, this);
57
- else if (type === 'box') ops.crop.crop(arg0 | 0, arg1 | 0, arg2 | 0, arg3 | 0, this);
58
-
59
- else throw new RangeError('invalid crop type');
60
-
61
- return this;
62
- }
63
-
64
- cut(type, arg0, arg1, arg2, arg3) {
65
- if (type === 'circle') return ops.crop.circle(arg0 || 0, this.clone());
66
- else if (type === 'box') return ops.crop.cut(arg0 | 0, arg1 | 0, arg2 | 0, arg3 | 0, this);
67
-
68
- else throw new RangeError('invalid cut type');
69
- }
70
-
71
- rotate(deg, resize = true) {
72
- if (0 === (deg %= 360)) return this;
73
- else if (90 === deg) ops.rotate.rotate90(this);
74
- else if (180 === deg) ops.rotate.rotate180(this);
75
- else if (270 === deg) ops.rotate.rotate270(this);
76
-
77
- else ops.rotate.rotate(deg, this, resize);
78
-
79
- return this;
80
- }
81
-
82
- blur(type, arg0) {
83
- if (type === 'cubic') ops.blur.cubic(this);
84
- else if (type === 'box') ops.blur.box(+arg0, this);
85
- else if (type === 'gaussian') ops.blur.gaussian(+arg0, this);
86
-
87
- else throw new RangeError('invalid blur type');
88
-
89
- return this;
90
- }
91
-
92
- fill(color) {
93
- const type = typeof color;
94
- if (type === 'function') ops.fill.fn(color, this);
95
- else if (type === 'number') ops.fill.color(color, this);
96
- else if (color instanceof Color) ops.fill.color(color.valueOf(), this);
97
- else if (Array.isArray(color)) ops.fill.color(ops.color.from_rgba(...color), this);
98
-
99
- else throw new TypeError('invalid fill color');
100
-
101
- return this;
102
- }
103
-
104
- swap(old, color) {
105
- const ot = typeof old;
106
- const nt = typeof color;
107
- if (ot === nt && ot === 'number') ops.fill.swap(old, color, this);
108
- else if (old instanceof Color && color instanceof Color) ops.fill.swap(old.valueOf(), color.valueOf(), this);
109
- else if (Array.isArray(old) && Array.isArray(color)) ops.fill.swap(ops.color.from_rgba(...old), ops.color.from_rgba(...color), this);
110
-
111
- else throw new RangeError('invalid swap color');
112
-
113
- return this;
114
- }
115
-
116
- resize(type, width, height) {
117
- if (width === this.width && height === this.height) return this;
118
- else if (type === 'cubic') ops.resize.cubic(width, height, this);
119
- else if (type === 'linear') ops.resize.linear(width, height, this);
120
- else if (type === 'nearest') ops.resize.nearest(width, height, this);
121
-
122
- else throw new RangeError('invalid resize type');
123
-
124
- return this;
125
- }
1
+ import Color from './ops/color.mjs';
2
+ import { view } from './util/mem.mjs';
3
+ import * as ops from './ops/index.mjs';
4
+ import * as png from '../png/src/png.mjs';
5
+
6
+ export { Color };
7
+
8
+ // todo: tree shakable context
9
+ // todo: make errors more verbose
10
+
11
+ export default class framebuffer {
12
+ constructor(width, height, buffer) {
13
+ this.width = width | 0;
14
+ this.height = height | 0;
15
+ this.u8 = buffer ? view(buffer) : new Uint8Array(4 * this.width * this.height);
16
+ this.view = new DataView(this.u8.buffer, this.u8.byteOffset, this.u8.byteLength);
17
+ this.u32 = new Uint32Array(this.u8.buffer, this.u8.byteOffset, this.u8.byteLength / 4);
18
+ if (this.u8.length !== 4 * this.width * this.height) throw new RangeError('invalid capacity of buffer');
19
+ }
20
+
21
+ [Symbol.iterator]() { return ops.iterator.cords(this); }
22
+ toString() { return `framebuffer<${this.width}x${this.height}>`; }
23
+ get(x, y) { return this.view.getUint32((x | 0) + (y | 0) * this.width, false); }
24
+ clone() { return new this.constructor(this.width, this.height, this.u8.slice()); }
25
+ set(x, y, color) { this.view.setUint32((x | 0) + (y | 0) * this.width, color, false); }
26
+ toJSON() { return { width: this.width, height: this.height, buffer: Array.from(this.u8) } }
27
+ scale(type, factor) { return this.resize(type, factor * this.width, factor * this.height); }
28
+ overlay(frame, x = 0, y = 0) { return (ops.overlay.blend(this, frame, x | 0, y | 0), this); }
29
+ replace(frame, x = 0, y = 0) { return (ops.overlay.replace(this, frame, x | 0, y | 0), this); }
30
+ at(x, y) { const offset = 4 * ((x | 0) + (y | 0) * this.width); return this.u8.subarray(offset, 4 + offset); }
31
+ static from(framebuffer) { return new this(framebuffer.width, framebuffer.height, framebuffer.u8 || framebuffer.buffer); }
32
+ static decode(format, buffer) { if (format !== 'png') throw new RangeError('invalid image format'); else return framebuffer.from(png.decode(buffer)); }
33
+
34
+ encode(format, options = {}) {
35
+ if (format !== 'png') throw new RangeError('invalid image format');
36
+ else return png.encode(this.u8, { channels: 4, width: this.width, height: this.height, level: ({ none: 0, fast: 3, default: 6, best: 9 })[options.compression] ?? 3 });
37
+ }
38
+
39
+ pixels(type) {
40
+ if ('rgba' === type) return ops.iterator.rgba(this);
41
+ if (!type || 'int' === type) return ops.iterator.u32(this);
42
+
43
+ throw new RangeError('invalid iterator type');
44
+ }
45
+
46
+ flip(type) {
47
+ if (type === 'vertical') ops.flip.vertical(this);
48
+ else if (type === 'horizontal') ops.flip.horizontal(this);
49
+
50
+ else throw new RangeError('invalid flip type');
51
+
52
+ return this;
53
+ }
54
+
55
+ crop(type, arg0, arg1, arg2, arg3) {
56
+ if (type === 'circle') ops.crop.circle(arg0 || 0, this);
57
+ else if (type === 'box') ops.crop.crop(arg0 | 0, arg1 | 0, arg2 | 0, arg3 | 0, this);
58
+
59
+ else throw new RangeError('invalid crop type');
60
+
61
+ return this;
62
+ }
63
+
64
+ cut(type, arg0, arg1, arg2, arg3) {
65
+ if (type === 'circle') return ops.crop.circle(arg0 || 0, this.clone());
66
+ else if (type === 'box') return ops.crop.cut(arg0 | 0, arg1 | 0, arg2 | 0, arg3 | 0, this);
67
+
68
+ else throw new RangeError('invalid cut type');
69
+ }
70
+
71
+ rotate(deg, resize = true) {
72
+ if (0 === (deg %= 360)) return this;
73
+ else if (90 === deg) ops.rotate.rotate90(this);
74
+ else if (180 === deg) ops.rotate.rotate180(this);
75
+ else if (270 === deg) ops.rotate.rotate270(this);
76
+
77
+ else ops.rotate.rotate(deg, this, resize);
78
+
79
+ return this;
80
+ }
81
+
82
+ blur(type, arg0) {
83
+ if (type === 'cubic') ops.blur.cubic(this);
84
+ else if (type === 'box') ops.blur.box(+arg0, this);
85
+ else if (type === 'gaussian') ops.blur.gaussian(+arg0, this);
86
+
87
+ else throw new RangeError('invalid blur type');
88
+
89
+ return this;
90
+ }
91
+
92
+ fill(color) {
93
+ const type = typeof color;
94
+ if (type === 'function') ops.fill.fn(color, this);
95
+ else if (type === 'number') ops.fill.color(color, this);
96
+ else if (color instanceof Color) ops.fill.color(color.valueOf(), this);
97
+ else if (Array.isArray(color)) ops.fill.color(ops.color.from_rgba(...color), this);
98
+
99
+ else throw new TypeError('invalid fill color');
100
+
101
+ return this;
102
+ }
103
+
104
+ swap(old, color) {
105
+ const ot = typeof old;
106
+ const nt = typeof color;
107
+ if (ot === nt && ot === 'number') ops.fill.swap(old, color, this);
108
+ else if (old instanceof Color && color instanceof Color) ops.fill.swap(old.valueOf(), color.valueOf(), this);
109
+ else if (Array.isArray(old) && Array.isArray(color)) ops.fill.swap(ops.color.from_rgba(...old), ops.color.from_rgba(...color), this);
110
+
111
+ else throw new RangeError('invalid swap color');
112
+
113
+ return this;
114
+ }
115
+
116
+ resize(type, width, height) {
117
+ if (width === this.width && height === this.height) return this;
118
+ else if (type === 'cubic') ops.resize.cubic(width, height, this);
119
+ else if (type === 'linear') ops.resize.linear(width, height, this);
120
+ else if (type === 'nearest') ops.resize.nearest(width, height, this);
121
+
122
+ else throw new RangeError('invalid resize type');
123
+
124
+ return this;
125
+ }
126
126
  }