imagescript 1.3.1 → 3.1.0

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 (132) hide show
  1. package/LICENSE +4 -1
  2. package/README.md +76 -26
  3. package/THIRD_PARTY_NOTICES.md +156 -0
  4. package/dist/animation.d.ts +55 -0
  5. package/dist/animation.js +167 -0
  6. package/dist/codecs/crc.d.ts +9 -0
  7. package/dist/codecs/crc.js +29 -0
  8. package/dist/codecs/exif.d.ts +11 -0
  9. package/dist/codecs/exif.js +27 -0
  10. package/dist/codecs/gif.d.ts +44 -0
  11. package/dist/codecs/gif.js +401 -0
  12. package/dist/codecs/jpeg.d.ts +35 -0
  13. package/dist/codecs/jpeg.js +980 -0
  14. package/dist/codecs/png.d.ts +97 -0
  15. package/dist/codecs/png.js +664 -0
  16. package/dist/codecs/quantize.d.ts +25 -0
  17. package/dist/codecs/quantize.js +302 -0
  18. package/dist/codecs/tga.d.ts +16 -0
  19. package/dist/codecs/tga.js +203 -0
  20. package/dist/codecs/tiff.d.ts +6 -0
  21. package/dist/codecs/tiff.js +546 -0
  22. package/dist/codecs/vp8-tables.d.ts +10 -0
  23. package/dist/codecs/vp8-tables.js +17 -0
  24. package/dist/codecs/vp8.d.ts +8 -0
  25. package/dist/codecs/vp8.js +711 -0
  26. package/dist/codecs/vp8l.d.ts +62 -0
  27. package/dist/codecs/vp8l.js +909 -0
  28. package/dist/codecs/webp.d.ts +67 -0
  29. package/dist/codecs/webp.js +280 -0
  30. package/dist/codecs/zlib.d.ts +22 -0
  31. package/dist/codecs/zlib.js +607 -0
  32. package/dist/frame.d.ts +57 -0
  33. package/dist/frame.js +65 -0
  34. package/dist/gif.d.ts +31 -0
  35. package/dist/gif.js +20 -0
  36. package/dist/image-type.d.ts +24 -0
  37. package/dist/image-type.js +64 -0
  38. package/dist/image.d.ts +553 -0
  39. package/dist/image.js +1156 -0
  40. package/dist/index.d.ts +23 -0
  41. package/dist/index.js +20 -0
  42. package/dist/package.json +1 -0
  43. package/dist/render/font.d.ts +92 -0
  44. package/dist/render/font.js +954 -0
  45. package/dist/render/hinting.d.ts +110 -0
  46. package/dist/render/hinting.js +1236 -0
  47. package/dist/render/svg.d.ts +11 -0
  48. package/dist/render/svg.js +1717 -0
  49. package/dist/render/text.d.ts +63 -0
  50. package/dist/render/text.js +342 -0
  51. package/dist/text-layout.d.ts +46 -0
  52. package/dist/text-layout.js +35 -0
  53. package/dist/util/blur.d.ts +13 -0
  54. package/dist/util/blur.js +146 -0
  55. package/dist/util/mem.d.ts +10 -0
  56. package/dist/util/mem.js +23 -0
  57. package/dist/util/resample.d.ts +12 -0
  58. package/dist/util/resample.js +99 -0
  59. package/dist/util/utf8.d.ts +5 -0
  60. package/dist/util/utf8.js +55 -0
  61. package/dist/version.d.ts +1 -0
  62. package/dist/version.js +1 -0
  63. package/licenses/Apache-2.0.txt +202 -0
  64. package/licenses/FTL.txt +169 -0
  65. package/licenses/README.ijg +260 -0
  66. package/package.json +31 -7
  67. package/ImageScript.d.ts +0 -954
  68. package/ImageScript.js +0 -1669
  69. package/bench/_.mjs +0 -166
  70. package/bench/all.mjs +0 -4
  71. package/bench/blur.mjs +0 -59
  72. package/bench/flip.mjs +0 -63
  73. package/bench/overlay.mjs +0 -35
  74. package/bench/package.json +0 -7
  75. package/bench/readme.md +0 -64
  76. package/bench/resize.mjs +0 -52
  77. package/codecs/LICENSE +0 -21
  78. package/codecs/lib.d.ts +0 -50
  79. package/codecs/lib.js +0 -6
  80. package/codecs/lib.mjs +0 -1
  81. package/codecs/node/bin/arm64-darwin.node +0 -0
  82. package/codecs/node/bin/arm64-linux.node +0 -0
  83. package/codecs/node/bin/x64-darwin.node +0 -0
  84. package/codecs/node/bin/x64-linux.node +0 -0
  85. package/codecs/node/bin/x64-win32.node +0 -0
  86. package/codecs/node/index.js +0 -3
  87. package/codecs/node/index.mjs +0 -6
  88. package/codecs/package.json +0 -14
  89. package/codecs/readme.md +0 -38
  90. package/codecs/wasm/bin/codecs.wasm +0 -0
  91. package/codecs/wasm/bin/simd.wasm +0 -0
  92. package/codecs/wasm/bundle/browser.js +0 -1
  93. package/codecs/wasm/index.js +0 -1
  94. package/codecs/wasm/index.mjs +0 -1
  95. package/png/README.md +0 -1
  96. package/png/node.js +0 -1137
  97. package/png/src/crc.mjs +0 -49
  98. package/png/src/mem.mjs +0 -24
  99. package/png/src/png.mjs +0 -254
  100. package/png/src/zlib.mjs +0 -618
  101. package/tsconfig.json +0 -21
  102. package/utils/mem.js +0 -26
  103. package/v2/codecs/magic.js +0 -25
  104. package/v2/framebuffer.d.ts +0 -67
  105. package/v2/framebuffer.js +0 -2352
  106. package/v2/framebuffer.mjs +0 -126
  107. package/v2/framebuffer.rs +0 -1264
  108. package/v2/index.d.ts +0 -84
  109. package/v2/index.js +0 -261
  110. package/v2/ops/blur.mjs +0 -303
  111. package/v2/ops/color.mjs +0 -277
  112. package/v2/ops/crop.mjs +0 -64
  113. package/v2/ops/fill.mjs +0 -33
  114. package/v2/ops/flip.mjs +0 -33
  115. package/v2/ops/index.mjs +0 -9
  116. package/v2/ops/iterator.mjs +0 -34
  117. package/v2/ops/overlay.mjs +0 -63
  118. package/v2/ops/resize.mjs +0 -166
  119. package/v2/ops/rotate.mjs +0 -117
  120. package/v2/util/mem.mjs +0 -24
  121. package/wasm/any/font.wasm +0 -0
  122. package/wasm/any/gif.wasm +0 -0
  123. package/wasm/any/jpeg.wasm +0 -0
  124. package/wasm/any/png.wasm +0 -0
  125. package/wasm/any/svg.wasm +0 -0
  126. package/wasm/any/tiff.wasm +0 -0
  127. package/wasm/node/font.js +0 -142
  128. package/wasm/node/gif.js +0 -76
  129. package/wasm/node/jpeg.js +0 -92
  130. package/wasm/node/png.js +0 -49
  131. package/wasm/node/svg.js +0 -56
  132. package/wasm/node/tiff.js +0 -47
package/v2/ops/rotate.mjs DELETED
@@ -1,117 +0,0 @@
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
- }
package/v2/util/mem.mjs DELETED
@@ -1,24 +0,0 @@
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
package/wasm/any/gif.wasm DELETED
Binary file
Binary file
package/wasm/any/png.wasm DELETED
Binary file
package/wasm/any/svg.wasm DELETED
Binary file
Binary file
package/wasm/node/font.js DELETED
@@ -1,142 +0,0 @@
1
- const wasm_name = 'font';
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 registry = null;
16
- const wasm = new WebAssembly.Instance(mod).exports;
17
-
18
- class mem {
19
- static length() { return wasm.wlen(); }
20
- static alloc(size) { return wasm.walloc(size); }
21
- static free(ptr, size) { return wasm.wfree(ptr, size); }
22
- static u8(ptr, size) { return new Uint8Array(wasm.memory.buffer, ptr, size); }
23
- static u32(ptr, size) { return new Uint32Array(wasm.memory.buffer, ptr, size); }
24
-
25
- static copy_and_free(ptr, size) {
26
- let slice = mem.u8(ptr, size).slice();
27
- return (wasm.wfree(ptr, size), slice);
28
- }
29
- }
30
- const encode_utf8 = 'Deno' in globalThis ? Deno.core.encode : (() => {
31
- const encoder = new TextEncoder();
32
- return string => encoder.encode(string);
33
- })();
34
-
35
- const decode_utf8 = 'Deno' in globalThis ? Deno.core.decode : (() => {
36
- const decoder = new TextDecoder();
37
- return buffer => decoder.decode(buffer);
38
- })();
39
-
40
- if ('FinalizationRegistry' in globalThis) {
41
- registry = new FinalizationRegistry(([t, ptr]) => {
42
- if (t === 0) wasm.font_free(ptr);
43
- if (t === 1) wasm.layout_free(ptr);
44
- });
45
- }
46
-
47
- class Font {
48
- constructor(scale, buffer) {
49
- this.scale = scale;
50
- const ptr = mem.alloc(buffer.length);
51
- mem.u8(ptr, buffer.length).set(buffer);
52
- this.ptr = wasm.font_new(ptr, buffer.length, scale);
53
-
54
- if (!this.ptr) throw new Error('invalid font');
55
- if (registry) registry.register(this, [0, this.ptr], this);
56
- }
57
-
58
- free() {
59
- this.ptr = wasm.font_free(this.ptr);
60
- if (registry) registry.unregister(this);
61
- }
62
-
63
- has(char) {
64
- return wasm.font_has(this.ptr, String.prototype.charCodeAt.call(char, 0));
65
- }
66
-
67
- metrics(char, scale = this.scale) {
68
- const ptr = wasm.font_metrics(this.ptr, String.prototype.charCodeAt.call(char, 0), scale);
69
- const metrics = JSON.parse(decode_utf8(mem.u8(wasm.font_metrics_buffer(ptr), mem.length())));
70
-
71
- return (wasm.font_metrics_free(ptr), metrics);
72
- }
73
-
74
- rasterize(char, scale = this.scale) {
75
- const ptr = wasm.font_rasterize(this.ptr, String.prototype.charCodeAt.call(char, 0), scale);
76
-
77
- const glyph = {
78
- buffer: mem.u8(wasm.font_rasterize_buffer(ptr), mem.length()).slice(),
79
- metrics: JSON.parse(decode_utf8(mem.u8(wasm.font_rasterize_metrics(ptr), mem.length()))),
80
- }
81
-
82
- return (wasm.font_rasterize_free(ptr), glyph);
83
- }
84
- }
85
-
86
- class Layout {
87
- constructor() {
88
- this.ptr = wasm.layout_new();
89
- if (registry) this.refs = [];
90
- if (registry) registry.register(this, [1, this.ptr], this);
91
- }
92
-
93
- clear() {
94
- wasm.layout_clear(this.ptr);
95
- if (registry) this.refs.length = 0;
96
- }
97
-
98
- lines() {
99
- return wasm.layout_lines(this.ptr);
100
- }
101
-
102
- free() {
103
- if (registry) this.refs.length = 0;
104
- this.ptr = wasm.layout_free(this.ptr);
105
- if (registry) registry.unregister(this);
106
- }
107
-
108
- reset(options = {}) {
109
- options = encode_utf8(JSON.stringify(options));
110
-
111
- if (registry) this.refs.length = 0;
112
- const ptr = mem.alloc(options.length);
113
- mem.u8(ptr, options.length).set(options);
114
- wasm.layout_reset(this.ptr, ptr, options.length);
115
- }
116
-
117
- append(font, text, init) {
118
- text = encode_utf8(text);
119
- const options = init || {};
120
- if (registry) this.refs.push(font);
121
- const ptr = mem.alloc(text.length);
122
- mem.u8(ptr, text.length).set(text);
123
- const has_color = ('r' in options) || ('g' in options) || ('b' in options);
124
- wasm.layout_append(this.ptr, font.ptr, ptr, text.length, options.scale == null ? font.scale : options.scale, has_color, options.r, options.g, options.b);
125
- }
126
-
127
- rasterize(r, g, b) {
128
- const ptr = wasm.layout_rasterize(this.ptr, r, g, b);
129
-
130
- const framebuffer = {
131
- width: wasm.layout_rasterize_width(ptr),
132
- height: wasm.layout_rasterize_height(ptr),
133
- buffer: mem.u8(wasm.layout_rasterize_buffer(ptr), mem.length()).slice(),
134
- }
135
-
136
- return (wasm.layout_rasterize_free(ptr), framebuffer);
137
- }
138
- }
139
-
140
- return { Font, Layout };
141
- }
142
- }
package/wasm/node/gif.js DELETED
@@ -1,76 +0,0 @@
1
- const wasm_name = 'gif';
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
- const wasm = new WebAssembly.Instance(mod, { env: { push_to_stream(id, ptr) { } } }).exports;
16
-
17
- class mem {
18
- static length() { return wasm.wlen(); }
19
- static alloc(size) { return wasm.walloc(size); }
20
- static free(ptr, size) { return wasm.wfree(ptr, size); }
21
- static u8(ptr, size) { return new Uint8Array(wasm.memory.buffer, ptr, size); }
22
- static u32(ptr, size) { return new Uint32Array(wasm.memory.buffer, ptr, size); }
23
-
24
- static copy_and_free(ptr, size) {
25
- let slice = mem.u8(ptr, size).slice();
26
- return (wasm.wfree(ptr, size), slice);
27
- }
28
- }
29
-
30
- function load(buffer) {
31
- return [...new Decoder(buffer).frames()];
32
- }
33
-
34
- class Decoder {
35
- constructor(buffer, limit = 0) {
36
- const bptr = mem.alloc(buffer.length);
37
- mem.u8(bptr, buffer.length).set(buffer);
38
- this.ptr = wasm.decoder_new(bptr, buffer.length, limit);
39
- if (0 === this.ptr) throw new Error('gif: failed to parse gif header');
40
-
41
- this.width = wasm.decoder_width(this.ptr);
42
- this.height = wasm.decoder_height(this.ptr);
43
- }
44
-
45
- free() {
46
- this.ptr = wasm.decoder_free(this.ptr);
47
- }
48
-
49
- *frames() {
50
- let frame;
51
- while (frame = this.frame()) yield frame;
52
- }
53
-
54
- frame() {
55
- const ptr = wasm.decoder_frame(this.ptr);
56
-
57
- if (1 === ptr) return null;
58
- if (0 === ptr) throw (this.free(), new Error('gif: failed to decode frame'));
59
-
60
- const framebuffer = {
61
- x: wasm.decoder_frame_x(ptr),
62
- y: wasm.decoder_frame_y(ptr),
63
- delay: wasm.decoder_frame_delay(ptr),
64
- width: wasm.decoder_frame_width(ptr),
65
- height: wasm.decoder_frame_height(ptr),
66
- dispose: wasm.decoder_frame_dispose(ptr),
67
- buffer: mem.u8(wasm.decoder_frame_buffer(ptr), mem.length()).slice(),
68
- };
69
-
70
- return (wasm.decoder_frame_free(ptr), framebuffer);
71
- }
72
- }
73
-
74
- return { load, Decoder };
75
- }
76
- }
package/wasm/node/jpeg.js DELETED
@@ -1,92 +0,0 @@
1
- const wasm_name = 'jpeg';
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
- const wasm = new WebAssembly.Instance(mod).exports;
16
-
17
- class mem {
18
- static length() { return wasm.wlen(); }
19
- static alloc(size) { return wasm.walloc(size); }
20
- static free(ptr, size) { return wasm.wfree(ptr, size); }
21
- static u8(ptr, size) { return new Uint8Array(wasm.memory.buffer, ptr, size); }
22
- static u32(ptr, size) { return new Uint32Array(wasm.memory.buffer, ptr, size); }
23
-
24
- static copy_and_free(ptr, size) {
25
- let slice = mem.u8(ptr, size).slice();
26
- return (wasm.wfree(ptr, size), slice);
27
- }
28
- }
29
-
30
- function decode(buffer, width, height) {
31
- const bptr = mem.alloc(buffer.length);
32
- mem.u8(bptr, buffer.length).set(buffer);
33
- const ptr = wasm.decode(bptr, buffer.length, width, height);
34
-
35
- if (0 === ptr) throw new Error('jpg: failed to decode');
36
- if (1 === ptr) throw new Error('jpg: failed to scale decoder');
37
-
38
- const framebuffer = {
39
- width: wasm.decode_width(ptr),
40
- height: wasm.decode_height(ptr),
41
- format: wasm.decode_format(ptr),
42
- buffer: mem.u8(wasm.decode_buffer(ptr), mem.length()).slice(),
43
- }
44
-
45
- return (wasm.decode_free(ptr), framebuffer);
46
- }
47
-
48
- function load(buffer) {
49
- const framebuffer = decode(buffer);
50
-
51
- const old = framebuffer.buffer;
52
- if (2 !== framebuffer.format) framebuffer.buffer = new Uint8Array(4 * framebuffer.width * framebuffer.height);
53
-
54
- if (0 === framebuffer.format) {
55
- let offset = 0;
56
- const view = new DataView(framebuffer.buffer.buffer);
57
- while (offset < old.length) view.setUint32(4 * offset, old[offset] << 24 | old[offset] << 16 | old[offset++] << 8 | 0xff, false);
58
- }
59
-
60
- else if (2 === framebuffer.format) {
61
- let offset = 0;
62
- while (offset < old.length) {
63
- const k = old[3 + offset];
64
- old[offset] = k * old[offset++] / 255;
65
- old[offset] = k * old[offset++] / 255;
66
- old[offset] = k * old[offset++] / 255;
67
-
68
- old[offset++] = 0xff;
69
- }
70
- }
71
-
72
- else if (1 === framebuffer.format) {
73
- let offset = 0;
74
- let foffset = 0;
75
- framebuffer.buffer.fill(0xff);
76
- const length = 3 * framebuffer.width * framebuffer.height;
77
-
78
- while (offset < length) {
79
- framebuffer.buffer[foffset++] = old[offset++];
80
- framebuffer.buffer[foffset++] = old[offset++];
81
- framebuffer.buffer[foffset++] = old[offset++];
82
-
83
- foffset++;
84
- }
85
- }
86
-
87
- return framebuffer;
88
- }
89
-
90
- return { load, decode };
91
- }
92
- }
package/wasm/node/png.js DELETED
@@ -1,49 +0,0 @@
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
- }
package/wasm/node/svg.js DELETED
@@ -1,56 +0,0 @@
1
- const wasm_name = 'svg';
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
- const wasm = new WebAssembly.Instance(mod).exports;
16
-
17
- class mem {
18
- static length() { return wasm.wlen(); }
19
- static alloc(size) { return wasm.walloc(size); }
20
- static free(ptr, size) { return wasm.wfree(ptr, size); }
21
- static u8(ptr, size) { return new Uint8Array(wasm.memory.buffer, ptr, size); }
22
- static u32(ptr, size) { return new Uint32Array(wasm.memory.buffer, ptr, size); }
23
-
24
- static copy_and_free(ptr, size) {
25
- let slice = mem.u8(ptr, size).slice();
26
- return (wasm.wfree(ptr, size), slice);
27
- }
28
- }
29
-
30
- function load(buffer, fit) {
31
- if (!fit) return rasterize(buffer, 0, 0);
32
- if (fit.zoom) return rasterize(buffer, 1, fit.zoom);
33
- if (fit.width) return rasterize(buffer, 2, fit.width);
34
- if (fit.height) return rasterize(buffer, 3, fit.height);
35
- }
36
-
37
- function rasterize(buffer, fit, scale) {
38
- const bptr = mem.alloc(buffer.length);
39
- mem.u8(bptr, buffer.length).set(buffer);
40
- const ptr = wasm.rasterize(bptr, buffer.length, fit, scale);
41
-
42
- if (0 === ptr) throw new Error('svg: failed to parse');
43
- if (1 === ptr) throw new Error('svg: failed to rasterize');
44
-
45
- const framebuffer = {
46
- width: wasm.rasterize_width(ptr),
47
- height: wasm.rasterize_height(ptr),
48
- buffer: mem.u8(wasm.rasterize_buffer(ptr), mem.length()).slice(),
49
- }
50
-
51
- return (wasm.rasterize_free(ptr), framebuffer);
52
- }
53
-
54
- return { load, rasterize };
55
- }
56
- }
package/wasm/node/tiff.js DELETED
@@ -1,47 +0,0 @@
1
- const wasm_name = 'tiff';
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
- const wasm = new WebAssembly.Instance(mod).exports;
16
-
17
- class mem {
18
- static length() { return wasm.wlen(); }
19
- static alloc(size) { return wasm.walloc(size); }
20
- static free(ptr, size) { return wasm.wfree(ptr, size); }
21
- static u8(ptr, size) { return new Uint8Array(wasm.memory.buffer, ptr, size); }
22
- static u32(ptr, size) { return new Uint32Array(wasm.memory.buffer, ptr, size); }
23
-
24
- static copy_and_free(ptr, size) {
25
- let slice = mem.u8(ptr, size).slice();
26
- return (wasm.wfree(ptr, size), slice);
27
- }
28
- }
29
-
30
- function decode(buffer) {
31
- const bptr = mem.alloc(buffer.length);
32
- mem.u8(bptr, buffer.length).set(buffer);
33
- const ptr = wasm.decode(bptr, buffer.length);
34
- if (0 === ptr) throw new Error('tiff: failed to decode');
35
-
36
- const framebuffer = {
37
- width: wasm.decode_width(ptr),
38
- height: wasm.decode_height(ptr),
39
- buffer: mem.u8(wasm.decode_buffer(ptr), mem.length()).slice(),
40
- }
41
-
42
- return (wasm.decode_free(ptr), framebuffer);
43
- }
44
-
45
- return { decode, load: decode };
46
- }
47
- }