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/dist/frame.js ADDED
@@ -0,0 +1,65 @@
1
+ import { Image } from "./image.js";
2
+ const DISPOSAL_MODES = ['any', 'keep', 'previous', 'background'];
3
+ export class Frame extends Image {
4
+ duration;
5
+ xOffset;
6
+ yOffset;
7
+ __disposalMode__ = 1;
8
+ static get DISPOSAL_ANY() {
9
+ return 'any';
10
+ }
11
+ static get DISPOSAL_KEEP() {
12
+ return 'keep';
13
+ }
14
+ static get DISPOSAL_PREVIOUS() {
15
+ return 'previous';
16
+ }
17
+ static get DISPOSAL_BACKGROUND() {
18
+ return 'background';
19
+ }
20
+ static __convert_disposal_mode__(mode) {
21
+ const id = typeof mode === 'string' ? DISPOSAL_MODES.indexOf(mode) : mode;
22
+ if (!(id >= 0 && id <= 3))
23
+ throw new RangeError('Invalid disposal mode');
24
+ return id;
25
+ }
26
+ constructor(width, height, duration = 100, xOffset = 0, yOffset = 0, disposalMode = Frame.DISPOSAL_KEEP) {
27
+ if (isNaN(duration) || duration < 0)
28
+ throw new RangeError('Invalid frame duration');
29
+ super(width, height);
30
+ this.duration = duration;
31
+ this.xOffset = xOffset;
32
+ this.yOffset = yOffset;
33
+ this.disposalMode = disposalMode;
34
+ }
35
+ get disposalMode() {
36
+ return DISPOSAL_MODES[this.__disposalMode__];
37
+ }
38
+ get __disposal_id__() {
39
+ return this.__disposalMode__;
40
+ }
41
+ set disposalMode(disposalMode) {
42
+ this.__disposalMode__ = Frame.__convert_disposal_mode__(disposalMode);
43
+ }
44
+ toString() {
45
+ return `Frame<${this.width}x${this.height}x${this.duration}ms>`;
46
+ }
47
+ static from(image, duration, xOffset, yOffset, disposalMode = Frame.DISPOSAL_KEEP) {
48
+ if (!(image instanceof Image))
49
+ throw new TypeError('Invalid image passed');
50
+ const frame = new Frame(image.width, image.height, duration, xOffset, yOffset, disposalMode);
51
+ frame.bitmap.set(image.bitmap);
52
+ return frame;
53
+ }
54
+ clone() {
55
+ return Frame.from(this, this.duration, this.xOffset, this.yOffset, this.disposalMode);
56
+ }
57
+ resize(width, height, mode = Image.RESIZE_BILINEAR) {
58
+ const originalWidth = this.width;
59
+ const originalHeight = this.height;
60
+ super.resize(width, height, mode);
61
+ this.xOffset = Math.round(this.xOffset * this.width / originalWidth);
62
+ this.yOffset = Math.round(this.yOffset * this.height / originalHeight);
63
+ return this;
64
+ }
65
+ }
package/dist/gif.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { BinaryData } from './util/mem.js';
2
+ import { Frame } from './frame.js';
3
+ import { Animation } from './animation.js';
4
+ /**
5
+ * Represents a GIF image as an array of frames, an {@link Animation} whose `encode()` and `decode()` work with GIFs.
6
+ * Array methods like map and filter return plain arrays.
7
+ * @deprecated Use {@link Animation}, which works the same and also handles APNG: `new Animation(frames, loopCount)`,
8
+ * {@link Animation.decode} and {@link Animation.encodeGIF}. `GIF` will be removed in 4.0
9
+ */
10
+ export declare class GIF extends Animation {
11
+ /**
12
+ * Creates a new GIF image
13
+ * @param frames The frames to create the GIF from
14
+ * @param loopCount How many times to repeat the GIF (-1 = forever, 0 = play once)
15
+ */
16
+ constructor(frames: Frame[], loopCount?: number);
17
+ toString(): string;
18
+ /**
19
+ * Encodes the GIF
20
+ * @param quality 1-100, scales the palette size from 8 colors (1) to 256 colors (90 and above)
21
+ * @deprecated Use {@link Animation.encodeGIF}
22
+ */
23
+ encode(quality?: number): Promise<Uint8Array>;
24
+ /**
25
+ * Decodes a GIF into fully composited frames
26
+ * @param input The binary data to decode
27
+ * @param onlyExtractFirstFrame Whether to end GIF decoding after the first frame
28
+ * @deprecated Use {@link Animation.decode}, which decodes APNGs too
29
+ */
30
+ static decode(input: BinaryData, onlyExtractFirstFrame?: boolean): Promise<GIF>;
31
+ }
package/dist/gif.js ADDED
@@ -0,0 +1,20 @@
1
+ import { view } from "./util/mem.js";
2
+ import { ImageType } from "./image-type.js";
3
+ import { Animation } from "./animation.js";
4
+ export class GIF extends Animation {
5
+ constructor(frames, loopCount = -1) {
6
+ super(frames, loopCount);
7
+ }
8
+ toString() {
9
+ return `GIF<${this.width}x${this.height}x${this.duration}ms>`;
10
+ }
11
+ async encode(quality = 95) {
12
+ return this.encodeGIF(quality);
13
+ }
14
+ static async decode(input, onlyExtractFirstFrame = false) {
15
+ const data = view(input);
16
+ if (!ImageType.isGIF(new DataView(data.buffer, data.byteOffset, data.byteLength)))
17
+ throw new Error('Unsupported image type');
18
+ return Animation.__fromGIF__(data, onlyExtractFirstFrame, GIF);
19
+ }
20
+ }
@@ -0,0 +1,24 @@
1
+ import { BinaryData } from './util/mem.js';
2
+ /** Image formats recognized by {@link ImageType.getType} */
3
+ export type ImageTypeName = 'png' | 'jpeg' | 'tiff' | 'gif' | 'webp' | 'tga';
4
+ /** Detects image formats by their magic numbers */
5
+ export declare class ImageType {
6
+ /**
7
+ * Gets an image's type from its magic number
8
+ * @param data The image binary to get the type of
9
+ * @returns The image type, or null if it isn't supported
10
+ */
11
+ static getType(data: BinaryData): ImageTypeName | null;
12
+ /** Whether the data starts like a PNG */
13
+ static isPNG(data: BinaryData): boolean;
14
+ /** Whether the data starts like a JPEG */
15
+ static isJPEG(data: BinaryData): boolean;
16
+ /** Whether the data starts like a TIFF (or BigTIFF) */
17
+ static isTIFF(data: BinaryData): boolean;
18
+ /** Whether the data starts like a GIF */
19
+ static isGIF(data: BinaryData): boolean;
20
+ /** Whether the data starts like a WebP */
21
+ static isWebP(data: BinaryData): boolean;
22
+ /** Whether the data looks like a TGA (which has no magic number: its header has to be consistent) */
23
+ static isTGA(data: BinaryData): boolean;
24
+ }
@@ -0,0 +1,64 @@
1
+ import { view as bytes } from "./util/mem.js";
2
+ import { isTGA } from "./codecs/tga.js";
3
+ const MAGIC_NUMBERS = {
4
+ PNG: 0x89504e47,
5
+ JPEG: 0xffd8ff,
6
+ TIFF_LE: 0x49492a00,
7
+ TIFF_BE: 0x4d4d002a,
8
+ BIGTIFF_LE: 0x49492b00,
9
+ BIGTIFF_BE: 0x4d4d002b,
10
+ GIF: 0x474946,
11
+ RIFF: 0x52494646,
12
+ WEBP: 0x57454250
13
+ };
14
+ function dataView(data) {
15
+ if (data instanceof DataView)
16
+ return data;
17
+ const u8 = bytes(data);
18
+ return new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
19
+ }
20
+ export class ImageType {
21
+ static getType(data) {
22
+ const view = dataView(data);
23
+ if (this.isPNG(view))
24
+ return 'png';
25
+ if (this.isJPEG(view))
26
+ return 'jpeg';
27
+ if (this.isTIFF(view))
28
+ return 'tiff';
29
+ if (this.isGIF(view))
30
+ return 'gif';
31
+ if (this.isWebP(view))
32
+ return 'webp';
33
+ if (this.isTGA(view))
34
+ return 'tga';
35
+ return null;
36
+ }
37
+ static isPNG(data) {
38
+ const view = dataView(data);
39
+ return view.byteLength >= 4 && view.getUint32(0, false) === MAGIC_NUMBERS.PNG;
40
+ }
41
+ static isJPEG(data) {
42
+ const view = dataView(data);
43
+ return view.byteLength >= 4 && (view.getUint32(0, false) >>> 8) === MAGIC_NUMBERS.JPEG;
44
+ }
45
+ static isTIFF(data) {
46
+ const view = dataView(data);
47
+ if (view.byteLength < 4)
48
+ return false;
49
+ const magic = view.getUint32(0, false);
50
+ return magic === MAGIC_NUMBERS.TIFF_LE || magic === MAGIC_NUMBERS.TIFF_BE || magic === MAGIC_NUMBERS.BIGTIFF_LE || magic === MAGIC_NUMBERS.BIGTIFF_BE;
51
+ }
52
+ static isGIF(data) {
53
+ const view = dataView(data);
54
+ return view.byteLength >= 4 && (view.getUint32(0, false) >>> 8) === MAGIC_NUMBERS.GIF;
55
+ }
56
+ static isWebP(data) {
57
+ const view = dataView(data);
58
+ return view.byteLength >= 12 && view.getUint32(0, false) === MAGIC_NUMBERS.RIFF && view.getUint32(8, false) === MAGIC_NUMBERS.WEBP;
59
+ }
60
+ static isTGA(data) {
61
+ const view = dataView(data);
62
+ return isTGA(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
63
+ }
64
+ }