generated from donmccurdy/typescript-library-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
56 lines (53 loc) · 1.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { NdArray } from 'ndarray';
import { getPixelsInternal } from './node-get-pixels';
import { savePixelsInternal } from './node-save-pixels';
/**
* Decodes image data to an `ndarray`.
*
* MIME type is optional when given a path or URL, and required when given a Uint8Array.
*
* Accepts `image/png` or `image/jpeg` in Node.js, and additional formats on browsers with
* the necessary support in Canvas 2D.
*
* @param data
* @param mimeType `image/jpeg`, `image/png`, etc.
* @returns
*/
async function getPixels(data: Uint8Array, mimeType: string): Promise<NdArray<Uint8Array>> {
return getPixelsInternal(data, mimeType);
}
/**
* Encodes an `ndarray` as image data in the given format.
*
* If the source `ndarray` was constructed manually with default stride, use
* `ndarray.transpose(1, 0)` to reshape it and ensure an identical result from getPixels(). For an
* ndarray created by getPixels(), this isn't necessary.
*
* Accepts `image/png` or `image/jpeg` in Node.js, and additional formats on browsers with
* the necessary support in Canvas 2D.
*
* @param pixels ndarray of shape W x H x 4.
* @param typeOrOptions object with encoding options or just the type
* @param typeOrOptions.type target format (`image/jpeg`, `image/png`, `image/webp`, etc.)
* @param typeOrOptions.quality quality as a number from 0 to 1, inclusive
* @returns
*/
async function savePixels(
pixels: NdArray<Uint8Array | Uint8ClampedArray>,
typeOrOptions: string | { type?: string; quality?: number }
): Promise<Uint8Array> {
let options;
if (typeof typeOrOptions === 'string') {
options = {
type: typeOrOptions,
quality: undefined,
};
} else {
options = {
type: typeOrOptions.type,
quality: typeOrOptions.quality,
};
}
return savePixelsInternal(pixels, options);
}
export { getPixels, savePixels };