Files
tauri/packages/api/src/image.ts
Amr Bashir 36eee37220 Restructure the repository (#10796)
* Restructure the repository

* lock file

* fmt

* fix bench

* fix cli template test

* remove accidental file

* fix mv command

* clippy

* upgrade paths-filter github action

* fix cli migration tests

* lockfile

* license headers

* clippy

* scope test-core to tauri crate

* license header

* correct --manifest-path usage

* lockfile

* fix tauri-driver on macOS [skip ci]

* build target ios

* try downgrade env_logger

* downgrade 0.1.7

* try to fix bench

* bench overflow

* revert overflow fix, fix tauri_root_path

* revert env_logger downgrade

* fmt

* raise msrv to 1.71

* fmt

* delete .cargo/config.toml [skip ci]

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
2024-08-27 18:42:30 -03:00

111 lines
3.1 KiB
TypeScript

// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { Resource, invoke } from './core'
/// Image dimensions type.
export interface ImageSize {
/// Image width.
width: number
/// Image height.
height: number
}
/** An RGBA Image in row-major order from top to bottom. */
export class Image extends Resource {
/**
* Creates an Image from a resource ID. For internal use only.
*
* @ignore
*/
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<Uint8Array> {
return invoke<number[]>('plugin:image|rgba', {
rid: this.rid
}).then((buffer) => new Uint8Array(buffer))
}
/** Returns the size of this image. */
async size(): Promise<ImageSize> {
return invoke<ImageSize>('plugin:image|size', { 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 Image
? image.rid
: image
return ret as T
}