smallpeg 0.13.1 → 0.14.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.
package/dist/web.d.mts ADDED
@@ -0,0 +1,98 @@
1
+ import { type Api } from "./core.mjs";
2
+ /**
3
+ * Where to load the `.wasm` from: a URL (string or `URL`), a `Request`, a
4
+ * `Response` or a promise of one (e.g. `fetch(url)`), the bytes, or a
5
+ * compiled `WebAssembly.Module`. Typed loosely so the declarations need no
6
+ * DOM types. The form wasm-bindgen documents, `{ module_or_path: ... }`,
7
+ * is accepted too.
8
+ */
9
+ export type WasmSource = string | object;
10
+ /**
11
+ * What {@link initSync} takes: the bytes or a compiled `WebAssembly.Module`
12
+ * (or wasm-bindgen's `{ module: ... }`).
13
+ */
14
+ export type WasmModuleSource = object;
15
+ /**
16
+ * Load the wasm. Call (and await) it once before the other functions;
17
+ * later calls return the same promise, and a failed load can be retried.
18
+ *
19
+ * By default the file is fetched from `web/smallpeg_wasm_bg.wasm` inside
20
+ * the package (bundlers that understand `new URL(..., import.meta.url)`
21
+ * copy it). Pass `wasm` to load it from elsewhere.
22
+ */
23
+ export declare function init(wasm?: WasmSource): Promise<void>;
24
+ /**
25
+ * Load the wasm synchronously from bytes you already have (or a compiled
26
+ * `WebAssembly.Module`). Browsers may refuse to compile a module
27
+ * synchronously on the main thread; use it in workers, or prefer
28
+ * {@link init}.
29
+ */
30
+ export declare function initSync(wasm: WasmModuleSource): void;
31
+ export default init;
32
+ export type { Bytes, Channels, EncodeOptions, Image, ImageInput, JpegInfo, OptimizeOptions, Orientation, Rect, ResizeOptions, } from "./core.mjs";
33
+ /**
34
+ * Encode pixels as a JPEG. 4 channels are RGBA: alpha is flattened onto
35
+ * white.
36
+ *
37
+ * @throws {RangeError} if the data length does not match the dimensions,
38
+ * or `quality` is not an integer from 1 to 100.
39
+ * @throws {TypeError} for arguments of the wrong kind.
40
+ * @throws {Error} if a dimension is above 65535.
41
+ */
42
+ export declare const encode: Api["encode"];
43
+ /**
44
+ * Make an existing JPEG smaller without changing a pixel. `undefined` if
45
+ * the file is not one this can re-code. An already optimized file can come
46
+ * out larger; compare lengths.
47
+ */
48
+ export declare const optimize: Api["optimize"];
49
+ /**
50
+ * Remove EXIF (including GPS), XMP, comments and other metadata without
51
+ * touching the image data. Keeps the color profile and orientation.
52
+ * `undefined` if the file cannot be parsed.
53
+ */
54
+ export declare const stripMetadata: Api["stripMetadata"];
55
+ /**
56
+ * Read a JPEG's frame header without decoding it. `undefined` if the bytes
57
+ * are not a JPEG this library understands.
58
+ */
59
+ export declare const info: Api["info"];
60
+ /**
61
+ * Decode a grayscale or color JPEG to 1-channel gray or 3-channel RGB
62
+ * pixels, as stored (see `applyOrientation`). `undefined` for CMYK files,
63
+ * codings this library does not decode, malformed input, or frames over
64
+ * 100 megapixels.
65
+ */
66
+ export declare const decode: Api["decode"];
67
+ /**
68
+ * Turn pixels upright for an EXIF orientation (`info(jpeg).orientation`).
69
+ * A number outside 1 to 8 leaves them as they are.
70
+ *
71
+ * @throws {RangeError} if the data length does not match the dimensions.
72
+ * @throws {TypeError} if `orientation` is not a number.
73
+ */
74
+ export declare const applyOrientation: Api["applyOrientation"];
75
+ /**
76
+ * Resize with a Lanczos3 filter. 4 channels are filtered independently.
77
+ *
78
+ * @throws {RangeError} if the data length does not match the dimensions,
79
+ * a new size is not an integer of at least 1, or the result would be over
80
+ * 100 megapixels.
81
+ * @throws {Error} if `linear` is set for 4 channels.
82
+ */
83
+ export declare const resize: Api["resize"];
84
+ /**
85
+ * Copy a rectangle out of an image. `undefined` if the rectangle is empty
86
+ * or not entirely inside the image.
87
+ *
88
+ * @throws {RangeError} if the data length does not match the dimensions,
89
+ * or a rectangle field is not a non-negative integer.
90
+ */
91
+ export declare const crop: Api["crop"];
92
+ /**
93
+ * The largest `[width, height]` that fits inside `maxWidth` x `maxHeight`
94
+ * with the same aspect ratio. Never enlarges.
95
+ *
96
+ * @throws {RangeError} if an argument is not an integer of at least 1.
97
+ */
98
+ export declare const fitWithin: Api["fitWithin"];
package/dist/web.mjs ADDED
@@ -0,0 +1,129 @@
1
+ // Browser (and bundler) entry point: the wasm has to be fetched and
2
+ // instantiated with `await init()` (or `initSync()`) before the first call.
3
+ import initWasm, * as glue from "../web/smallpeg_wasm.js";
4
+ import { makeApi } from "./core.mjs";
5
+ let ready;
6
+ let loaded = false;
7
+ // A plain object carrying `key` is wasm-bindgen's own argument form, which
8
+ // 0.13 documented: pass it through. Anything else is the source itself.
9
+ function isPlain(x, key) {
10
+ return (typeof x === "object" && x !== null && Object.getPrototypeOf(x) === Object.prototype && key in x);
11
+ }
12
+ /**
13
+ * Load the wasm. Call (and await) it once before the other functions;
14
+ * later calls return the same promise, and a failed load can be retried.
15
+ *
16
+ * By default the file is fetched from `web/smallpeg_wasm_bg.wasm` inside
17
+ * the package (bundlers that understand `new URL(..., import.meta.url)`
18
+ * copy it). Pass `wasm` to load it from elsewhere.
19
+ */
20
+ export function init(wasm) {
21
+ // A plain object must be wasm-bindgen's form; `{}` or initSync's
22
+ // `{ module }` here is a mistake, not something to fetch around.
23
+ if (typeof wasm === "object" &&
24
+ wasm !== null &&
25
+ Object.getPrototypeOf(wasm) === Object.prototype &&
26
+ !("module_or_path" in wasm)) {
27
+ return Promise.reject(new TypeError("smallpeg: init() takes a URL, Response, bytes or WebAssembly.Module"));
28
+ }
29
+ // The casts: WasmSource is deliberately looser than the glue's DOM types.
30
+ const arg = wasm === undefined
31
+ ? undefined
32
+ : isPlain(wasm, "module_or_path")
33
+ ? wasm
34
+ : { module_or_path: wasm };
35
+ ready ??= initWasm(arg).then(() => {
36
+ loaded = true;
37
+ }, (err) => {
38
+ ready = undefined;
39
+ throw err;
40
+ });
41
+ return ready;
42
+ }
43
+ /**
44
+ * Load the wasm synchronously from bytes you already have (or a compiled
45
+ * `WebAssembly.Module`). Browsers may refuse to compile a module
46
+ * synchronously on the main thread; use it in workers, or prefer
47
+ * {@link init}.
48
+ */
49
+ export function initSync(wasm) {
50
+ if (loaded)
51
+ return;
52
+ // The glue would instantiate a second copy under the pending load.
53
+ if (ready)
54
+ throw new Error("smallpeg: init() is still loading; await it instead");
55
+ glue.initSync(isPlain(wasm, "module") ? wasm : { module: wasm });
56
+ loaded = true;
57
+ ready = Promise.resolve();
58
+ }
59
+ export default init;
60
+ const api = makeApi(glue, () => {
61
+ if (!loaded)
62
+ throw new Error("smallpeg: call `await init()` before using it in the browser");
63
+ });
64
+ /**
65
+ * Encode pixels as a JPEG. 4 channels are RGBA: alpha is flattened onto
66
+ * white.
67
+ *
68
+ * @throws {RangeError} if the data length does not match the dimensions,
69
+ * or `quality` is not an integer from 1 to 100.
70
+ * @throws {TypeError} for arguments of the wrong kind.
71
+ * @throws {Error} if a dimension is above 65535.
72
+ */
73
+ export const encode = api.encode;
74
+ /**
75
+ * Make an existing JPEG smaller without changing a pixel. `undefined` if
76
+ * the file is not one this can re-code. An already optimized file can come
77
+ * out larger; compare lengths.
78
+ */
79
+ export const optimize = api.optimize;
80
+ /**
81
+ * Remove EXIF (including GPS), XMP, comments and other metadata without
82
+ * touching the image data. Keeps the color profile and orientation.
83
+ * `undefined` if the file cannot be parsed.
84
+ */
85
+ export const stripMetadata = api.stripMetadata;
86
+ /**
87
+ * Read a JPEG's frame header without decoding it. `undefined` if the bytes
88
+ * are not a JPEG this library understands.
89
+ */
90
+ export const info = api.info;
91
+ /**
92
+ * Decode a grayscale or color JPEG to 1-channel gray or 3-channel RGB
93
+ * pixels, as stored (see `applyOrientation`). `undefined` for CMYK files,
94
+ * codings this library does not decode, malformed input, or frames over
95
+ * 100 megapixels.
96
+ */
97
+ export const decode = api.decode;
98
+ /**
99
+ * Turn pixels upright for an EXIF orientation (`info(jpeg).orientation`).
100
+ * A number outside 1 to 8 leaves them as they are.
101
+ *
102
+ * @throws {RangeError} if the data length does not match the dimensions.
103
+ * @throws {TypeError} if `orientation` is not a number.
104
+ */
105
+ export const applyOrientation = api.applyOrientation;
106
+ /**
107
+ * Resize with a Lanczos3 filter. 4 channels are filtered independently.
108
+ *
109
+ * @throws {RangeError} if the data length does not match the dimensions,
110
+ * a new size is not an integer of at least 1, or the result would be over
111
+ * 100 megapixels.
112
+ * @throws {Error} if `linear` is set for 4 channels.
113
+ */
114
+ export const resize = api.resize;
115
+ /**
116
+ * Copy a rectangle out of an image. `undefined` if the rectangle is empty
117
+ * or not entirely inside the image.
118
+ *
119
+ * @throws {RangeError} if the data length does not match the dimensions,
120
+ * or a rectangle field is not a non-negative integer.
121
+ */
122
+ export const crop = api.crop;
123
+ /**
124
+ * The largest `[width, height]` that fits inside `maxWidth` x `maxHeight`
125
+ * with the same aspect ratio. Never enlarges.
126
+ *
127
+ * @throws {RangeError} if an argument is not an integer of at least 1.
128
+ */
129
+ export const fitWithin = api.fitWithin;
@@ -1,155 +1,67 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
+
3
4
  /**
4
- * Encode pixels as a JPEG.
5
- *
6
- * `channels` is 1 (gray), 3 (RGB) or 4 (RGBA, as from a canvas; alpha is
7
- * flattened onto white). `quality` is 1..=100 (default 80). `fullColor`
8
- * keeps chroma at full resolution (4:4:4) instead of halving it (4:2:0):
9
- * sharper colored text and lines, larger files. Both dimensions must be at
10
- * most 65535.
11
- * @param {Uint8Array} pixels
12
- * @param {number} width
13
- * @param {number} height
14
- * @param {number} channels
15
- * @param {number | undefined} [quality]
16
- * @param {boolean | undefined} [fullColor]
17
- * @returns {Uint8Array}
18
- */
19
- export function encode(pixels: Uint8Array, width: number, height: number, channels: number, quality?: number, fullColor?: boolean): Uint8Array;
20
- /**
21
- * Make an existing JPEG smaller without changing a pixel. `undefined` if
22
- * the file is not one this can re-code. The result can come out larger
23
- * for files that are already optimized; compare lengths. `baseline`
24
- * forces baseline sequential output (for targets that dislike
25
- * progressive JPEGs, such as PDF embedding).
26
- * @param {Uint8Array} jpeg
27
- * @param {boolean | undefined} [baseline]
28
- * @returns {Uint8Array | undefined}
29
- */
30
- export function optimize(jpeg: Uint8Array, baseline?: boolean): Uint8Array | undefined;
31
- /**
32
- * Remove EXIF (including GPS), XMP, comments and other metadata without
33
- * touching the image data. Keeps the color profile and the photo's
34
- * orientation. `undefined` if the file cannot be parsed.
35
- * @param {Uint8Array} jpeg
36
- * @returns {Uint8Array | undefined}
37
- */
38
- export function stripMetadata(jpeg: Uint8Array): Uint8Array | undefined;
39
- /**
40
- * Read a JPEG's frame header without decoding it. `undefined` if the
41
- * bytes are not a JPEG this library understands.
42
- * @param {Uint8Array} jpeg
43
- * @returns {JpegInfo | undefined}
44
- */
45
- export function info(jpeg: Uint8Array): JpegInfo | undefined;
46
- /**
47
- * Decode a grayscale or color JPEG to 8-bit gray or RGB pixels, as
48
- * stored (see `applyOrientation`). `undefined` for CMYK files, codings
49
- * this library does not decode, malformed input, or frames over 100
50
- * megapixels.
51
- * @param {Uint8Array} jpeg
52
- * @returns {Image | undefined}
53
- */
54
- export function decode(jpeg: Uint8Array): Image | undefined;
55
- /**
56
- * Turn decoded pixels upright for an EXIF orientation (1..=8; other
57
- * values leave them as they are). Orientations 5 to 8 swap width and
58
- * height.
59
- * @param {Uint8Array} pixels
60
- * @param {number} width
61
- * @param {number} height
62
- * @param {number} channels
63
- * @param {number} orientation
64
- * @returns {Image}
65
- */
66
- export function applyOrientation(pixels: Uint8Array, width: number, height: number, channels: number, orientation: number): Image;
67
- /**
68
- * Resize with a Lanczos3 filter. `channels` is 1 (gray), 3 (RGB) or 4
69
- * (any four-channel layout, each channel filtered independently).
70
- * `linear` filters RGB or gray in linear light, which keeps fine
71
- * light-and-dark detail from darkening when reducing; it assumes sRGB
72
- * input and is not available for 4 channels.
73
- * @param {Uint8Array} pixels
74
- * @param {number} width
75
- * @param {number} height
76
- * @param {number} channels
77
- * @param {number} newWidth
78
- * @param {number} newHeight
79
- * @param {boolean | undefined} [linear]
80
- * @returns {Uint8Array}
81
- */
82
- export function resize(pixels: Uint8Array, width: number, height: number, channels: number, newWidth: number, newHeight: number, linear?: boolean): Uint8Array;
83
- /**
84
- * Copy a rectangle out of pixels with any channel count. `undefined` if
85
- * the rectangle is empty or not entirely inside the image.
86
- * @param {Uint8Array} pixels
87
- * @param {number} width
88
- * @param {number} height
89
- * @param {number} channels
90
- * @param {number} x
91
- * @param {number} y
92
- * @param {number} cropWidth
93
- * @param {number} cropHeight
94
- * @returns {Uint8Array | undefined}
95
- */
96
- export function crop(pixels: Uint8Array, width: number, height: number, channels: number, x: number, y: number, cropWidth: number, cropHeight: number): Uint8Array | undefined;
97
- /**
98
- * The largest `[width, height]` that fits inside `maxWidth` x `maxHeight`
99
- * with the same aspect ratio. Never enlarges.
100
- * @param {number} width
101
- * @param {number} height
102
- * @param {number} maxWidth
103
- * @param {number} maxHeight
104
- * @returns {Uint32Array}
105
- */
106
- export function fitWithin(width: number, height: number, maxWidth: number, maxHeight: number): Uint32Array;
107
- /**
108
- * Pixels plus their layout, returned by `decode` and `applyOrientation`.
5
+ * Decoded or transformed pixels. The TypeScript layer reads the fields,
6
+ * moves the pixels out with `take_pixels`, and frees it.
109
7
  */
110
8
  export class Image {
111
- free(): void;
112
- /**
113
- * 1 (gray) or 3 (RGB) for decoded JPEGs; whatever was passed in for
114
- * `applyOrientation`.
115
- */
116
- readonly channels: number;
117
- /**
118
- * Height in pixels.
119
- */
120
- readonly height: number;
121
- /**
122
- * `width * height * channels` bytes, interleaved, top row first.
123
- */
124
- readonly pixels: Uint8Array;
125
- /**
126
- * Width in pixels.
127
- */
128
- readonly width: number;
9
+ private constructor();
10
+ free(): void;
11
+ [Symbol.dispose](): void;
12
+ /**
13
+ * Moves the pixels out (no copy on the Rust side); a second call
14
+ * returns an empty array.
15
+ */
16
+ take_pixels(): Uint8Array;
17
+ readonly channels: number;
18
+ readonly height: number;
19
+ readonly width: number;
129
20
  }
21
+
130
22
  /**
131
- * Frame-header facts about a JPEG, returned by `info`.
23
+ * Frame-header facts, returned by `info`.
132
24
  */
133
25
  export class JpegInfo {
134
- free(): void;
26
+ private constructor();
27
+ free(): void;
28
+ [Symbol.dispose](): void;
29
+ readonly components: number;
30
+ readonly height: number;
31
+ readonly orientation: number;
32
+ readonly progressive: boolean;
33
+ readonly width: number;
34
+ }
35
+
36
+ export function apply_orientation(pixels: Uint8Array, width: number, height: number, channels: number, orientation: number): Image;
37
+
135
38
  /**
136
- * 1 (gray), 3 (color) or 4 (CMYK / YCCK).
39
+ * `undefined` if the rectangle is empty or not entirely inside the image.
137
40
  */
138
- readonly components: number;
41
+ export function crop(pixels: Uint8Array, width: number, height: number, channels: number, x: number, y: number, crop_width: number, crop_height: number): Uint8Array | undefined;
42
+
43
+ export function decode(jpeg: Uint8Array): Image | undefined;
44
+
139
45
  /**
140
- * Height in pixels.
46
+ * Encode pixels; 4 channels are RGBA, flattened onto white.
141
47
  */
142
- readonly height: number;
48
+ export function encode(pixels: Uint8Array, width: number, height: number, channels: number, quality?: number | null, full_color?: boolean | null): Uint8Array;
49
+
143
50
  /**
144
- * EXIF orientation, 1..=8 (1 when the file has none).
51
+ * `[width, height]` that fits, keeping the aspect ratio; never enlarges.
145
52
  */
146
- readonly orientation: number;
53
+ export function fit_within(width: number, height: number, max_width: number, max_height: number): Uint32Array;
54
+
55
+ export function info(jpeg: Uint8Array): JpegInfo | undefined;
56
+
147
57
  /**
148
- * True for progressive files.
58
+ * Lossless re-coding; `baseline` forces sequential output.
149
59
  */
150
- readonly progressive: boolean;
60
+ export function optimize(jpeg: Uint8Array, baseline: boolean): Uint8Array | undefined;
61
+
151
62
  /**
152
- * Width in pixels.
63
+ * Lanczos3 resize; `linear` filters gray or RGB in linear light.
153
64
  */
154
- readonly width: number;
155
- }
65
+ export function resize(pixels: Uint8Array, width: number, height: number, channels: number, new_width: number, new_height: number, linear: boolean): Uint8Array;
66
+
67
+ export function strip_metadata(jpeg: Uint8Array): Uint8Array | undefined;