-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
image.ts
106 lines (96 loc) · 3.18 KB
/
image.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { Resource, invoke } from './core'
/** An RGBA Image in row-major order from top to bottom. */
export class Image extends Resource {
private constructor(rid: number) {
super(rid)
}
/** Creates a new Image using RGBA data, in row-major order from top to bottom, and with specified width and height. */
static async new(
rgba: number[] | Uint8Array | ArrayBuffer,
width: number,
height: number
): Promise<Image> {
return invoke<number>('plugin:image|new', {
rgba: transformImage(rgba),
width,
height
}).then((rid) => new Image(rid))
}
/**
* Creates a new image using the provided bytes by inferring the file format.
* If the format is known, prefer [@link Image.fromPngBytes] or [@link Image.fromIcoBytes].
*
* Only `ico` and `png` are supported (based on activated feature flag).
*
* Note that you need the `image-ico` or `image-png` Cargo features to use this API.
* To enable it, change your Cargo.toml file:
* ```toml
* [dependencies]
* tauri = { version = "...", features = ["...", "image-png"] }
* ```
*/
static async fromBytes(
bytes: number[] | Uint8Array | ArrayBuffer
): Promise<Image> {
return invoke<number>('plugin:image|from_bytes', {
bytes: transformImage(bytes)
}).then((rid) => new Image(rid))
}
/**
* Creates a new image using the provided path.
*
* Only `ico` and `png` are supported (based on activated feature flag).
*
* Note that you need the `image-ico` or `image-png` Cargo features to use this API.
* To enable it, change your Cargo.toml file:
* ```toml
* [dependencies]
* tauri = { version = "...", features = ["...", "image-png"] }
* ```
*/
static async fromPath(path: string): Promise<Image> {
return invoke<number>('plugin:image|from_path', { path }).then(
(rid) => new Image(rid)
)
}
/** Returns the RGBA data for this image, in row-major order from top to bottom. */
async rgba(): Promise<ArrayBuffer | number[]> {
return invoke<ArrayBuffer | number[]>('plugin:image|rgba', {
rid: this.rid
})
}
/** Returns the width of this image. */
async width() {
return invoke<number>('plugin:image|width', { rid: this.rid })
}
/** Returns the height of this image. */
async height() {
return invoke<number>('plugin:image|height', { rid: this.rid })
}
}
/**
* Transforms image from various types into a type acceptable by Rust.
*
* See [tauri::image::JsImage](https://docs.rs/tauri/2/tauri/image/enum.JsImage.html) for more information.
* Note the API signature is not stable and might change.
*/
export function transformImage<T>(
image: string | Image | Uint8Array | ArrayBuffer | number[] | null
): T {
const ret =
image == null
? null
: typeof image === 'string'
? image
: image instanceof Uint8Array
? Array.from(image)
: image instanceof ArrayBuffer
? Array.from(new Uint8Array(image))
: image instanceof Image
? image.rid
: image
return ret as T
}