@webex/ladon-ts 5.4.1 → 5.4.3
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/CHANGELOG.md +177 -163
- package/LICENSE +5 -5
- package/README.md +34 -36
- package/build/module/index.d.ts +10 -10
- package/build/module/lib/config-builder.d.ts +179 -180
- package/build/module/lib/config-types.d.ts +101 -101
- package/build/module/lib/config.d.ts +28 -29
- package/build/module/lib/generator.d.ts +73 -74
- package/build/module/lib/pipeline.d.ts +82 -83
- package/build/module/lib/preload.d.ts +7 -8
- package/build/module/lib/renderer/blur-renderer.d.ts +17 -18
- package/build/module/lib/renderer/ladon-renderer.d.ts +5 -6
- package/build/module/lib/renderer/passthrough-renderer.d.ts +10 -11
- package/build/module/lib/renderer/replacement-renderer.d.ts +14 -15
- package/build/module/lib/renderer/webgl-renderer.d.ts +23 -24
- package/build/module/lib/renderer.d.ts +5 -5
- package/build/module/lib/shared-resource-manager.d.ts +92 -92
- package/build/module/lib/shared-resource-manager.js +25 -26
- package/build/module/lib/shared-resource-manager.js.map +1 -1
- package/build/module/lib/util.d.ts +64 -64
- package/build/module/lib/version.d.ts +1 -1
- package/build/module/lib/version.js +1 -1
- package/build/module/lib/version.js.map +1 -1
- package/build/worker/segmentation.js +253 -248
- package/build/worker/segmentation.js.map +1 -0
- package/package.json +102 -95
|
@@ -1,93 +1,93 @@
|
|
|
1
1
|
import { PreloadConfig, ResolutionConfig, WorkerConfig } from './config-types';
|
|
2
|
-
import * as ort from 'onnxruntime-web';
|
|
3
|
-
/**
|
|
4
|
-
* Shared resource manager used to preload and share resources across different pipelines.
|
|
5
|
-
* This is useful when multiple pipelines are using the same model or when the model needs
|
|
6
|
-
* to be preloaded before the pipeline is created.
|
|
7
|
-
*/
|
|
8
|
-
declare class SharedResourceManager {
|
|
9
|
-
private modelSessions;
|
|
10
|
-
private worker?;
|
|
11
|
-
private workerUrl?;
|
|
12
|
-
private resolvedUriCache;
|
|
13
|
-
/**
|
|
14
|
-
* Initialize the worker using the provided configuration.
|
|
15
|
-
*
|
|
16
|
-
* @param config - The configuration object.
|
|
17
|
-
* @returns A promise that resolves when the worker is initialized.
|
|
18
|
-
*/
|
|
19
|
-
initializeWorker(config: WorkerConfig): Promise<Worker>;
|
|
20
|
-
/**
|
|
21
|
-
* Terminate the worker.
|
|
22
|
-
*/
|
|
23
|
-
terminateWorker(): void;
|
|
24
|
-
/**
|
|
25
|
-
* Load the model using the provided configuration. This gets called by the worker when using
|
|
26
|
-
* the worker generator.
|
|
27
|
-
*
|
|
28
|
-
* @param config - The configuration object.
|
|
29
|
-
* @returns A promise that resolves when the model is preloaded.
|
|
30
|
-
*/
|
|
31
|
-
loadModel(config: PreloadConfig): Promise<ort.InferenceSession>;
|
|
32
|
-
/**
|
|
33
|
-
* Preload the worker using the provided configuration.
|
|
34
|
-
*
|
|
35
|
-
* @param config - The configuration object.
|
|
36
|
-
* @returns A promise that resolves when the worker is preloaded.
|
|
37
|
-
*/
|
|
38
|
-
preloadWorker(config: PreloadConfig): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Warmup the model session with a dummy input to avoid latency when running the model for the first time.
|
|
41
|
-
*
|
|
42
|
-
* @param {ort.InferenceSession} session The model session.
|
|
43
|
-
* @param {ResolutionConfig} inputSize The input size of the model.
|
|
44
|
-
* @param {number} inputChannels The number of input channels (e.g., RGB is 3).
|
|
45
|
-
* @param {ort.Tensor.Type} inputType The input tensor type.
|
|
46
|
-
* @returns {Promise<void>} A promise that resolves when the model is warmed up.
|
|
47
|
-
* @example
|
|
48
|
-
* await warmupModel(session, inputSize, inputChannels, inputType);
|
|
49
|
-
*/
|
|
50
|
-
warmupModel(session: ort.InferenceSession, inputSize?: ResolutionConfig, inputChannels?: number, inputType?: ort.Tensor.Type): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* Get the model session for the provided model URL.
|
|
53
|
-
*
|
|
54
|
-
* @param {string} modelUrl The model URL.
|
|
55
|
-
* @returns {ort.InferenceSession} The model session.
|
|
56
|
-
* @example
|
|
57
|
-
* const session = getModelSession(modelUrl);
|
|
58
|
-
*/
|
|
59
|
-
getModelSession(modelUrl: string): ort.InferenceSession;
|
|
60
|
-
/**
|
|
61
|
-
* Resolves the full URL for a given asset. Non-file URIs are treated as paths
|
|
62
|
-
* and resolved using the `toAbsoluteURI` method.
|
|
63
|
-
*
|
|
64
|
-
* @param config - The preload configuration.
|
|
65
|
-
* @param assetUri - The asset URI from the config.
|
|
66
|
-
* @returns The resolved full URL.
|
|
67
|
-
*/
|
|
68
|
-
/**
|
|
69
|
-
* Resolves the full URL for a given asset. Non-file URIs are treated as paths and resolved
|
|
70
|
-
* using the `toAbsoluteURI` method. If the assetUri is already resolved, or if it's found in
|
|
71
|
-
* cache, it will be returned directly. If an assetUrlResolver function is provided in the
|
|
72
|
-
* configuration, it will be used to resolve the URL if the assetUri is not fully qualified,
|
|
73
|
-
* a blob URL, or in the cache.
|
|
74
|
-
*
|
|
75
|
-
* @param config - The preload configuration.
|
|
76
|
-
* @param assetUri - The asset URI from the config.
|
|
77
|
-
* @returns The resolved full URL.
|
|
78
|
-
*/
|
|
79
|
-
resolveUri(config: PreloadConfig, assetUri?: string): Promise<string>;
|
|
80
|
-
getWorker(): Worker | undefined;
|
|
81
|
-
getWorkerUrl(): string | undefined;
|
|
82
|
-
/**
|
|
83
|
-
* Prepare the worker configuration by resolving the final URLs and paths to send to the
|
|
84
|
-
* worker. This is necessary because we can't send functions to the worker. We also need to
|
|
85
|
-
* remove the assetUrlResolver function from the configuration.
|
|
86
|
-
*
|
|
87
|
-
* @param config - The preload configuration.
|
|
88
|
-
* @returns The worker configuration.
|
|
89
|
-
*/
|
|
90
|
-
prepareWorkerConfig(config: PreloadConfig): Promise<any>;
|
|
91
|
-
}
|
|
92
|
-
export declare const sharedResourceManager: SharedResourceManager;
|
|
93
|
-
export {};
|
|
2
|
+
import * as ort from 'onnxruntime-web';
|
|
3
|
+
/**
|
|
4
|
+
* Shared resource manager used to preload and share resources across different pipelines.
|
|
5
|
+
* This is useful when multiple pipelines are using the same model or when the model needs
|
|
6
|
+
* to be preloaded before the pipeline is created.
|
|
7
|
+
*/
|
|
8
|
+
declare class SharedResourceManager {
|
|
9
|
+
private modelSessions;
|
|
10
|
+
private worker?;
|
|
11
|
+
private workerUrl?;
|
|
12
|
+
private resolvedUriCache;
|
|
13
|
+
/**
|
|
14
|
+
* Initialize the worker using the provided configuration.
|
|
15
|
+
*
|
|
16
|
+
* @param config - The configuration object.
|
|
17
|
+
* @returns A promise that resolves when the worker is initialized.
|
|
18
|
+
*/
|
|
19
|
+
initializeWorker(config: WorkerConfig): Promise<Worker>;
|
|
20
|
+
/**
|
|
21
|
+
* Terminate the worker.
|
|
22
|
+
*/
|
|
23
|
+
terminateWorker(): void;
|
|
24
|
+
/**
|
|
25
|
+
* Load the model using the provided configuration. This gets called by the worker when using
|
|
26
|
+
* the worker generator.
|
|
27
|
+
*
|
|
28
|
+
* @param config - The configuration object.
|
|
29
|
+
* @returns A promise that resolves when the model is preloaded.
|
|
30
|
+
*/
|
|
31
|
+
loadModel(config: PreloadConfig): Promise<ort.InferenceSession>;
|
|
32
|
+
/**
|
|
33
|
+
* Preload the worker using the provided configuration.
|
|
34
|
+
*
|
|
35
|
+
* @param config - The configuration object.
|
|
36
|
+
* @returns A promise that resolves when the worker is preloaded.
|
|
37
|
+
*/
|
|
38
|
+
preloadWorker(config: PreloadConfig): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Warmup the model session with a dummy input to avoid latency when running the model for the first time.
|
|
41
|
+
*
|
|
42
|
+
* @param {ort.InferenceSession} session The model session.
|
|
43
|
+
* @param {ResolutionConfig} inputSize The input size of the model.
|
|
44
|
+
* @param {number} inputChannels The number of input channels (e.g., RGB is 3).
|
|
45
|
+
* @param {ort.Tensor.Type} inputType The input tensor type.
|
|
46
|
+
* @returns {Promise<void>} A promise that resolves when the model is warmed up.
|
|
47
|
+
* @example
|
|
48
|
+
* await warmupModel(session, inputSize, inputChannels, inputType);
|
|
49
|
+
*/
|
|
50
|
+
warmupModel(session: ort.InferenceSession, inputSize?: ResolutionConfig, inputChannels?: number, inputType?: ort.Tensor.Type): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Get the model session for the provided model URL.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} modelUrl The model URL.
|
|
55
|
+
* @returns {ort.InferenceSession} The model session.
|
|
56
|
+
* @example
|
|
57
|
+
* const session = getModelSession(modelUrl);
|
|
58
|
+
*/
|
|
59
|
+
getModelSession(modelUrl: string): ort.InferenceSession;
|
|
60
|
+
/**
|
|
61
|
+
* Resolves the full URL for a given asset. Non-file URIs are treated as paths
|
|
62
|
+
* and resolved using the `toAbsoluteURI` method.
|
|
63
|
+
*
|
|
64
|
+
* @param config - The preload configuration.
|
|
65
|
+
* @param assetUri - The asset URI from the config.
|
|
66
|
+
* @returns The resolved full URL.
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* Resolves the full URL for a given asset. Non-file URIs are treated as paths and resolved
|
|
70
|
+
* using the `toAbsoluteURI` method. If the assetUri is already resolved, or if it's found in
|
|
71
|
+
* cache, it will be returned directly. If an assetUrlResolver function is provided in the
|
|
72
|
+
* configuration, it will be used to resolve the URL if the assetUri is not fully qualified,
|
|
73
|
+
* a blob URL, or in the cache.
|
|
74
|
+
*
|
|
75
|
+
* @param config - The preload configuration.
|
|
76
|
+
* @param assetUri - The asset URI from the config.
|
|
77
|
+
* @returns The resolved full URL.
|
|
78
|
+
*/
|
|
79
|
+
resolveUri(config: PreloadConfig, assetUri?: string): Promise<string>;
|
|
80
|
+
getWorker(): Worker | undefined;
|
|
81
|
+
getWorkerUrl(): string | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Prepare the worker configuration by resolving the final URLs and paths to send to the
|
|
84
|
+
* worker. This is necessary because we can't send functions to the worker. We also need to
|
|
85
|
+
* remove the assetUrlResolver function from the configuration.
|
|
86
|
+
*
|
|
87
|
+
* @param config - The preload configuration.
|
|
88
|
+
* @returns The worker configuration.
|
|
89
|
+
*/
|
|
90
|
+
prepareWorkerConfig(config: PreloadConfig): Promise<any>;
|
|
91
|
+
}
|
|
92
|
+
export declare const sharedResourceManager: SharedResourceManager;
|
|
93
|
+
export {};
|
|
@@ -14,8 +14,8 @@ class U {
|
|
|
14
14
|
async initializeWorker(e) {
|
|
15
15
|
if (typeof Worker > "u")
|
|
16
16
|
throw new Error("[ladon-ts] web workers are not supported in this environment");
|
|
17
|
-
const r = await this.resolveUri(e, e.mask.workerUri),
|
|
18
|
-
return this.workerUrl = URL.createObjectURL(new Blob([
|
|
17
|
+
const r = await this.resolveUri(e, e.mask.workerUri), s = await (await fetch(r, { cache: "force-cache" })).text();
|
|
18
|
+
return this.workerUrl = URL.createObjectURL(new Blob([s], { type: "text/javascript" })), new Worker(this.workerUrl, { type: "module" });
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* Terminate the worker.
|
|
@@ -31,28 +31,27 @@ class U {
|
|
|
31
31
|
* @returns A promise that resolves when the model is preloaded.
|
|
32
32
|
*/
|
|
33
33
|
async loadModel(e) {
|
|
34
|
-
const { mask: r, executionProviders:
|
|
34
|
+
const { mask: r, executionProviders: o } = e;
|
|
35
35
|
i.env.wasm.numThreads = 1;
|
|
36
|
-
const
|
|
37
|
-
if (
|
|
36
|
+
const s = await this.resolveUri(e, e.wasmUri);
|
|
37
|
+
if (s)
|
|
38
38
|
try {
|
|
39
|
-
i.env.wasm.wasmBinary = await h(
|
|
39
|
+
i.env.wasm.wasmBinary = await h(s + k);
|
|
40
40
|
} catch (w) {
|
|
41
|
-
console.warn("Failed to load or cache WASM file. Using WASM paths.", w), i.env.wasm.wasmPaths =
|
|
41
|
+
console.warn("Failed to load or cache WASM file. Using WASM paths.", w), i.env.wasm.wasmPaths = s;
|
|
42
42
|
}
|
|
43
43
|
const a = await this.resolveUri(e, r.modelUri);
|
|
44
|
-
let
|
|
45
|
-
if (!
|
|
46
|
-
const n = { executionProviders:
|
|
44
|
+
let t = this.modelSessions[a];
|
|
45
|
+
if (!t) {
|
|
46
|
+
const n = { executionProviders: o != null && o.length ? o : ["wasm"] };
|
|
47
47
|
try {
|
|
48
48
|
const l = await h(a);
|
|
49
|
-
|
|
49
|
+
t = await i.InferenceSession.create(l, n);
|
|
50
50
|
} catch (l) {
|
|
51
|
-
console.warn("Failed to load or cache model file. Using model URL.", l),
|
|
51
|
+
console.warn("Failed to load or cache model file. Using model URL.", l), t = await i.InferenceSession.create(a, n);
|
|
52
52
|
}
|
|
53
|
-
o = await i.InferenceSession.create(a, n);
|
|
54
53
|
}
|
|
55
|
-
return this.modelSessions[a] =
|
|
54
|
+
return this.modelSessions[a] = t, await this.warmupModel(t, r.inputSize, r.modelRank), t;
|
|
56
55
|
}
|
|
57
56
|
/**
|
|
58
57
|
* Preload the worker using the provided configuration.
|
|
@@ -75,15 +74,15 @@ class U {
|
|
|
75
74
|
* @example
|
|
76
75
|
* await warmupModel(session, inputSize, inputChannels, inputType);
|
|
77
76
|
*/
|
|
78
|
-
async warmupModel(e, r = { width: 1280, height: 720 },
|
|
79
|
-
const a = r.height * r.width,
|
|
77
|
+
async warmupModel(e, r = { width: 1280, height: 720 }, o = 3, s = "float32") {
|
|
78
|
+
const a = r.height * r.width, t = new Float32Array(o * a).fill(0), w = new i.Tensor(s, t, [
|
|
80
79
|
1,
|
|
81
|
-
|
|
80
|
+
o,
|
|
82
81
|
r.height,
|
|
83
82
|
r.width
|
|
84
83
|
]), n = { [e.inputNames[0]]: w };
|
|
85
|
-
if (
|
|
86
|
-
const l = new Float32Array(a).fill(0), d = new i.Tensor(
|
|
84
|
+
if (o === 3) {
|
|
85
|
+
const l = new Float32Array(a).fill(0), d = new i.Tensor(s, l, [
|
|
87
86
|
1,
|
|
88
87
|
1,
|
|
89
88
|
r.height,
|
|
@@ -128,10 +127,10 @@ class U {
|
|
|
128
127
|
throw new Error("Asset URI is undefined.");
|
|
129
128
|
if (this.resolvedUriCache.has(r))
|
|
130
129
|
return this.resolvedUriCache.get(r);
|
|
131
|
-
let
|
|
132
|
-
if (c(r) ?
|
|
130
|
+
let o;
|
|
131
|
+
if (c(r) ? o = r : r.match(/\.[a-z0-9]+$/i) ? typeof e.assetUrlResolver == "function" && (o = await e.assetUrlResolver(r, e)) : o = m(r, e.baseUrl), !o)
|
|
133
132
|
throw new Error(`Failed to resolve asset URI: ${r}`);
|
|
134
|
-
return this.resolvedUriCache.set(r,
|
|
133
|
+
return this.resolvedUriCache.set(r, o), o;
|
|
135
134
|
}
|
|
136
135
|
getWorker() {
|
|
137
136
|
return this.worker;
|
|
@@ -148,7 +147,7 @@ class U {
|
|
|
148
147
|
* @returns The worker configuration.
|
|
149
148
|
*/
|
|
150
149
|
async prepareWorkerConfig(e) {
|
|
151
|
-
const
|
|
150
|
+
const o = { ...{
|
|
152
151
|
...e,
|
|
153
152
|
mask: {
|
|
154
153
|
...e.mask,
|
|
@@ -157,11 +156,11 @@ class U {
|
|
|
157
156
|
},
|
|
158
157
|
wasmUri: await this.resolveUri(e, e.wasmUri)
|
|
159
158
|
}, assetUrlResolver: void 0 };
|
|
160
|
-
return JSON.parse(JSON.stringify(
|
|
159
|
+
return JSON.parse(JSON.stringify(o));
|
|
161
160
|
}
|
|
162
161
|
}
|
|
163
|
-
const
|
|
162
|
+
const p = new U();
|
|
164
163
|
export {
|
|
165
|
-
|
|
164
|
+
p as sharedResourceManager
|
|
166
165
|
};
|
|
167
166
|
//# sourceMappingURL=shared-resource-manager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-resource-manager.js","sources":["../../../src/lib/shared-resource-manager.ts"],"sourcesContent":["import * as ort from 'onnxruntime-web';\n\nimport { PreloadConfig, ResolutionConfig, WorkerConfig } from './config-types';\nimport { isResolvedUri, loadAndCacheFile, toAbsoluteURI } from './util';\n\n// There is only a single WASM file for all WebAssembly binaries now.\nconst ONNX_WASM_FILENAME = 'ort-wasm-simd-threaded.wasm';\n\n/**\n * Shared resource manager used to preload and share resources across different pipelines.\n * This is useful when multiple pipelines are using the same model or when the model needs\n * to be preloaded before the pipeline is created.\n */\nclass SharedResourceManager {\n private modelSessions: Record<string, ort.InferenceSession> = {};\n private worker?: Worker;\n private workerUrl?: string;\n\n // In-memory cache for resolved URLs\n private resolvedUriCache: Map<string, string> = new Map();\n\n /**\n * Initialize the worker using the provided configuration.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the worker is initialized.\n */\n public async initializeWorker(config: WorkerConfig): Promise<Worker> {\n if (typeof Worker === 'undefined') {\n throw new Error('[ladon-ts] web workers are not supported in this environment');\n }\n\n const scriptUri = await this.resolveUri(config, config.mask.workerUri);\n const response = await fetch(scriptUri, { cache: 'force-cache' });\n const workerSource = await response.text();\n\n this.workerUrl = URL.createObjectURL(new Blob([workerSource], { type: 'text/javascript' }));\n\n return new Worker(this.workerUrl, { type: 'module' });\n }\n\n /**\n * Terminate the worker.\n */\n public terminateWorker() {\n if (this.worker) {\n this.worker.terminate();\n this.worker = undefined;\n }\n }\n\n /**\n * Load the model using the provided configuration. This gets called by the worker when using\n * the worker generator.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the model is preloaded.\n */\n public async loadModel(config: PreloadConfig) {\n const { mask, executionProviders: providers } = config;\n\n // Lock to a single thread to avoid performance issues.\n ort.env.wasm.numThreads = 1;\n\n // Get the resolved paths for the WASM binary.\n const wasmPaths = await this.resolveUri(config, config.wasmUri);\n\n if (wasmPaths) {\n try {\n ort.env.wasm.wasmBinary = await loadAndCacheFile(wasmPaths + ONNX_WASM_FILENAME);\n } catch (error) {\n console.warn('Failed to load or cache WASM file. Using WASM paths.', error);\n ort.env.wasm.wasmPaths = wasmPaths;\n }\n }\n\n // Resolve the model URL.\n const modelUrl = await this.resolveUri(config, mask.modelUri);\n let modelSession = this.modelSessions[modelUrl];\n\n // If the model session is not already cached, create a new session and cache.\n if (!modelSession) {\n // Allows the user to specify the execution providers to use. The default is the WASM provider.\n // TODO: Add and test support for other execution providers.\n const executionProviders = providers?.length ? providers : ['wasm'];\n const sessionOptions = { executionProviders };\n\n // First try to get the model binary either from cache or by fetching and cacheing it.\n try {\n const modelBinary = await loadAndCacheFile(modelUrl);\n modelSession = await ort.InferenceSession.create(modelBinary, sessionOptions);\n } catch (error) {\n console.warn('Failed to load or cache model file. Using model URL.', error);\n modelSession = await ort.InferenceSession.create(modelUrl, sessionOptions);\n }\n modelSession = await ort.InferenceSession.create(modelUrl, sessionOptions);\n }\n\n // Cache the model session.\n this.modelSessions[modelUrl] = modelSession;\n\n // Warmup the model with a dummy input to avoid latency when running the model for the first time.\n await this.warmupModel(modelSession, mask.inputSize, mask.modelRank);\n\n return modelSession;\n }\n\n /**\n * Preload the worker using the provided configuration.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the worker is preloaded.\n */\n public async preloadWorker(config: PreloadConfig) {\n const workerConfig = await this.prepareWorkerConfig(config);\n\n this.worker = await this.initializeWorker(workerConfig);\n this.worker.postMessage({ type: 'preload', config: workerConfig });\n }\n\n /**\n * Warmup the model session with a dummy input to avoid latency when running the model for the first time.\n *\n * @param {ort.InferenceSession} session The model session.\n * @param {ResolutionConfig} inputSize The input size of the model.\n * @param {number} inputChannels The number of input channels (e.g., RGB is 3).\n * @param {ort.Tensor.Type} inputType The input tensor type.\n * @returns {Promise<void>} A promise that resolves when the model is warmed up.\n * @example\n * await warmupModel(session, inputSize, inputChannels, inputType);\n */\n public async warmupModel(\n session: ort.InferenceSession,\n inputSize: ResolutionConfig = { width: 1280, height: 720 },\n inputChannels = 3,\n inputType: ort.Tensor.Type = 'float32'\n ): Promise<void> {\n const numPixels = inputSize.height * inputSize.width;\n const dummyInputData = new Float32Array(inputChannels * numPixels).fill(0);\n const dummyInput = new ort.Tensor(inputType, dummyInputData, [\n 1,\n inputChannels,\n inputSize.height,\n inputSize.width,\n ]);\n const feed = { [session.inputNames[0]]: dummyInput };\n\n // Use an additional dummy tensor if the model expects a previous mask.\n if (inputChannels === 3) {\n const dummyMaskData = new Float32Array(numPixels).fill(0);\n const prevMaskTensor = new ort.Tensor(inputType, dummyMaskData, [\n 1,\n 1,\n inputSize.height,\n inputSize.width,\n ]);\n feed[session.inputNames[1]] = prevMaskTensor;\n }\n\n // Run the model with the dummy input to warm it up.\n await session.run(feed);\n }\n\n /**\n * Get the model session for the provided model URL.\n *\n * @param {string} modelUrl The model URL.\n * @returns {ort.InferenceSession} The model session.\n * @example\n * const session = getModelSession(modelUrl);\n */\n public getModelSession(modelUrl: string) {\n return this.modelSessions[modelUrl];\n }\n\n /**\n * Resolves the full URL for a given asset. Non-file URIs are treated as paths\n * and resolved using the `toAbsoluteURI` method.\n *\n * @param config - The preload configuration.\n * @param assetUri - The asset URI from the config.\n * @returns The resolved full URL.\n */\n /**\n * Resolves the full URL for a given asset. Non-file URIs are treated as paths and resolved\n * using the `toAbsoluteURI` method. If the assetUri is already resolved, or if it's found in\n * cache, it will be returned directly. If an assetUrlResolver function is provided in the\n * configuration, it will be used to resolve the URL if the assetUri is not fully qualified,\n * a blob URL, or in the cache.\n *\n * @param config - The preload configuration.\n * @param assetUri - The asset URI from the config.\n * @returns The resolved full URL.\n */\n public async resolveUri(config: PreloadConfig, assetUri?: string): Promise<string> {\n if (!assetUri) {\n throw new Error('Asset URI is undefined.');\n }\n\n // Check if the URL is already cached\n if (this.resolvedUriCache.has(assetUri)) {\n return this.resolvedUriCache.get(assetUri) as string;\n }\n\n let resolvedUrl: string | undefined;\n\n if (isResolvedUri(assetUri)) {\n // If the assetUri is fully qualified or a blob URL, return it directly.\n // (Assets uris are resolved before being sent to the worker.)\n resolvedUrl = assetUri;\n } else if (!assetUri.match(/\\.[a-z0-9]+$/i)) {\n // Resolve as an absolute path to a non-file endpoint (e.g., wasmPaths).\n resolvedUrl = toAbsoluteURI(assetUri, config.baseUrl);\n } else if (typeof config.assetUrlResolver === 'function') {\n // Resolve via assetUrlResolver. This delegates the resolution logic to the caller.\n resolvedUrl = await config.assetUrlResolver(assetUri, config);\n }\n\n if (!resolvedUrl) {\n throw new Error(`Failed to resolve asset URI: ${assetUri}`);\n }\n\n // Cache the resolved URL\n this.resolvedUriCache.set(assetUri, resolvedUrl);\n\n return resolvedUrl;\n }\n\n public getWorker() {\n return this.worker;\n }\n\n public getWorkerUrl() {\n return this.workerUrl;\n }\n\n /**\n * Prepare the worker configuration by resolving the final URLs and paths to send to the\n * worker. This is necessary because we can't send functions to the worker. We also need to\n * remove the assetUrlResolver function from the configuration.\n *\n * @param config - The preload configuration.\n * @returns The worker configuration.\n */\n public async prepareWorkerConfig(config: PreloadConfig) {\n // Resolve and cache the URIs before sending the config to the worker. Future calls to the\n // resolveUri method will use the cache.\n const resolvedConfig: PreloadConfig = {\n ...config,\n mask: {\n ...config.mask,\n modelUri: await this.resolveUri(config, config.mask.modelUri),\n workerUri: await this.resolveUri(config, config.mask.workerUri),\n },\n wasmUri: await this.resolveUri(config, config.wasmUri),\n };\n\n // We can't send the assetUrlResolver function to the worker so we remove it from the config.\n const workerConfig = { ...resolvedConfig, assetUrlResolver: undefined };\n\n // This is a workaround to ensure the worker configuration can be sent to the worker.\n return JSON.parse(JSON.stringify(workerConfig));\n }\n}\n\n// Global instance of the shared resource manager.\nexport const sharedResourceManager = new SharedResourceManager();\n"],"names":["ONNX_WASM_FILENAME","SharedResourceManager","config","scriptUri","workerSource","mask","providers","ort","wasmPaths","loadAndCacheFile","error","modelUrl","modelSession","sessionOptions","modelBinary","workerConfig","session","inputSize","inputChannels","inputType","numPixels","dummyInputData","dummyInput","feed","dummyMaskData","prevMaskTensor","assetUri","resolvedUrl","isResolvedUri","toAbsoluteURI","sharedResourceManager"],"mappings":";;AAMA,MAAMA,IAAqB;AAO3B,MAAMC,EAAsB;AAAA,EAA5B,cAAA;AACI,SAAQ,gBAAsD,CAAC,GAKvD,KAAA,uCAA4C,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQxD,MAAa,iBAAiBC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AClE;AAAA,MACA,SAAS,MAAM,KAAK,WAAWA,GAAQA,EAAO,OAAO;AAAA,IACzD,GAG0C,kBAAkB,OAAU;AAGtE,WAAO,KAAK,MAAM,KAAK,UAAUa,CAAY,CAAC;AAAA,EAAA;AAEtD;AAGa,MAAAe,IAAwB,IAAI7B,EAAsB;"}
|
|
1
|
+
{"version":3,"file":"shared-resource-manager.js","sources":["../../../src/lib/shared-resource-manager.ts"],"sourcesContent":["import * as ort from 'onnxruntime-web';\n\nimport { PreloadConfig, ResolutionConfig, WorkerConfig } from './config-types';\nimport { isResolvedUri, loadAndCacheFile, toAbsoluteURI } from './util';\n\n// There is only a single WASM file for all WebAssembly binaries now.\nconst ONNX_WASM_FILENAME = 'ort-wasm-simd-threaded.wasm';\n\n/**\n * Shared resource manager used to preload and share resources across different pipelines.\n * This is useful when multiple pipelines are using the same model or when the model needs\n * to be preloaded before the pipeline is created.\n */\nclass SharedResourceManager {\n private modelSessions: Record<string, ort.InferenceSession> = {};\n private worker?: Worker;\n private workerUrl?: string;\n\n // In-memory cache for resolved URLs\n private resolvedUriCache: Map<string, string> = new Map();\n\n /**\n * Initialize the worker using the provided configuration.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the worker is initialized.\n */\n public async initializeWorker(config: WorkerConfig): Promise<Worker> {\n if (typeof Worker === 'undefined') {\n throw new Error('[ladon-ts] web workers are not supported in this environment');\n }\n\n const scriptUri = await this.resolveUri(config, config.mask.workerUri);\n const response = await fetch(scriptUri, { cache: 'force-cache' });\n const workerSource = await response.text();\n\n this.workerUrl = URL.createObjectURL(new Blob([workerSource], { type: 'text/javascript' }));\n\n return new Worker(this.workerUrl, { type: 'module' });\n }\n\n /**\n * Terminate the worker.\n */\n public terminateWorker() {\n if (this.worker) {\n this.worker.terminate();\n this.worker = undefined;\n }\n }\n\n /**\n * Load the model using the provided configuration. This gets called by the worker when using\n * the worker generator.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the model is preloaded.\n */\n public async loadModel(config: PreloadConfig) {\n const { mask, executionProviders: providers } = config;\n\n // Lock to a single thread to avoid performance issues.\n ort.env.wasm.numThreads = 1;\n\n // Get the resolved paths for the WASM binary.\n const wasmPaths = await this.resolveUri(config, config.wasmUri);\n\n if (wasmPaths) {\n try {\n ort.env.wasm.wasmBinary = await loadAndCacheFile(wasmPaths + ONNX_WASM_FILENAME);\n } catch (error) {\n console.warn('Failed to load or cache WASM file. Using WASM paths.', error);\n ort.env.wasm.wasmPaths = wasmPaths;\n }\n }\n\n // Resolve the model URL.\n const modelUrl = await this.resolveUri(config, mask.modelUri);\n let modelSession = this.modelSessions[modelUrl];\n\n // If the model session is not already cached, create a new session and cache.\n if (!modelSession) {\n // Allows the user to specify the execution providers to use. The default is the WASM provider.\n // TODO: Add and test support for other execution providers.\n const executionProviders = providers?.length ? providers : ['wasm'];\n const sessionOptions = { executionProviders };\n\n // First try to get the model binary either from cache or by fetching and cacheing it.\n try {\n const modelBinary = await loadAndCacheFile(modelUrl);\n modelSession = await ort.InferenceSession.create(modelBinary, sessionOptions);\n } catch (error) {\n console.warn('Failed to load or cache model file. Using model URL.', error);\n modelSession = await ort.InferenceSession.create(modelUrl, sessionOptions);\n }\n }\n\n // Cache the model session.\n this.modelSessions[modelUrl] = modelSession;\n\n // Warmup the model with a dummy input to avoid latency when running the model for the first time.\n await this.warmupModel(modelSession, mask.inputSize, mask.modelRank);\n\n return modelSession;\n }\n\n /**\n * Preload the worker using the provided configuration.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the worker is preloaded.\n */\n public async preloadWorker(config: PreloadConfig) {\n const workerConfig = await this.prepareWorkerConfig(config);\n\n this.worker = await this.initializeWorker(workerConfig);\n this.worker.postMessage({ type: 'preload', config: workerConfig });\n }\n\n /**\n * Warmup the model session with a dummy input to avoid latency when running the model for the first time.\n *\n * @param {ort.InferenceSession} session The model session.\n * @param {ResolutionConfig} inputSize The input size of the model.\n * @param {number} inputChannels The number of input channels (e.g., RGB is 3).\n * @param {ort.Tensor.Type} inputType The input tensor type.\n * @returns {Promise<void>} A promise that resolves when the model is warmed up.\n * @example\n * await warmupModel(session, inputSize, inputChannels, inputType);\n */\n public async warmupModel(\n session: ort.InferenceSession,\n inputSize: ResolutionConfig = { width: 1280, height: 720 },\n inputChannels = 3,\n inputType: ort.Tensor.Type = 'float32'\n ): Promise<void> {\n const numPixels = inputSize.height * inputSize.width;\n const dummyInputData = new Float32Array(inputChannels * numPixels).fill(0);\n const dummyInput = new ort.Tensor(inputType, dummyInputData, [\n 1,\n inputChannels,\n inputSize.height,\n inputSize.width,\n ]);\n const feed = { [session.inputNames[0]]: dummyInput };\n\n // Use an additional dummy tensor if the model expects a previous mask.\n if (inputChannels === 3) {\n const dummyMaskData = new Float32Array(numPixels).fill(0);\n const prevMaskTensor = new ort.Tensor(inputType, dummyMaskData, [\n 1,\n 1,\n inputSize.height,\n inputSize.width,\n ]);\n feed[session.inputNames[1]] = prevMaskTensor;\n }\n\n // Run the model with the dummy input to warm it up.\n await session.run(feed);\n }\n\n /**\n * Get the model session for the provided model URL.\n *\n * @param {string} modelUrl The model URL.\n * @returns {ort.InferenceSession} The model session.\n * @example\n * const session = getModelSession(modelUrl);\n */\n public getModelSession(modelUrl: string) {\n return this.modelSessions[modelUrl];\n }\n\n /**\n * Resolves the full URL for a given asset. Non-file URIs are treated as paths\n * and resolved using the `toAbsoluteURI` method.\n *\n * @param config - The preload configuration.\n * @param assetUri - The asset URI from the config.\n * @returns The resolved full URL.\n */\n /**\n * Resolves the full URL for a given asset. Non-file URIs are treated as paths and resolved\n * using the `toAbsoluteURI` method. If the assetUri is already resolved, or if it's found in\n * cache, it will be returned directly. If an assetUrlResolver function is provided in the\n * configuration, it will be used to resolve the URL if the assetUri is not fully qualified,\n * a blob URL, or in the cache.\n *\n * @param config - The preload configuration.\n * @param assetUri - The asset URI from the config.\n * @returns The resolved full URL.\n */\n public async resolveUri(config: PreloadConfig, assetUri?: string): Promise<string> {\n if (!assetUri) {\n throw new Error('Asset URI is undefined.');\n }\n\n // Check if the URL is already cached\n if (this.resolvedUriCache.has(assetUri)) {\n return this.resolvedUriCache.get(assetUri) as string;\n }\n\n let resolvedUrl: string | undefined;\n\n if (isResolvedUri(assetUri)) {\n // If the assetUri is fully qualified or a blob URL, return it directly.\n // (Assets uris are resolved before being sent to the worker.)\n resolvedUrl = assetUri;\n } else if (!assetUri.match(/\\.[a-z0-9]+$/i)) {\n // Resolve as an absolute path to a non-file endpoint (e.g., wasmPaths).\n resolvedUrl = toAbsoluteURI(assetUri, config.baseUrl);\n } else if (typeof config.assetUrlResolver === 'function') {\n // Resolve via assetUrlResolver. This delegates the resolution logic to the caller.\n resolvedUrl = await config.assetUrlResolver(assetUri, config);\n }\n\n if (!resolvedUrl) {\n throw new Error(`Failed to resolve asset URI: ${assetUri}`);\n }\n\n // Cache the resolved URL\n this.resolvedUriCache.set(assetUri, resolvedUrl);\n\n return resolvedUrl;\n }\n\n public getWorker() {\n return this.worker;\n }\n\n public getWorkerUrl() {\n return this.workerUrl;\n }\n\n /**\n * Prepare the worker configuration by resolving the final URLs and paths to send to the\n * worker. This is necessary because we can't send functions to the worker. We also need to\n * remove the assetUrlResolver function from the configuration.\n *\n * @param config - The preload configuration.\n * @returns The worker configuration.\n */\n public async prepareWorkerConfig(config: PreloadConfig) {\n // Resolve and cache the URIs before sending the config to the worker. Future calls to the\n // resolveUri method will use the cache.\n const resolvedConfig: PreloadConfig = {\n ...config,\n mask: {\n ...config.mask,\n modelUri: await this.resolveUri(config, config.mask.modelUri),\n workerUri: await this.resolveUri(config, config.mask.workerUri),\n },\n wasmUri: await this.resolveUri(config, config.wasmUri),\n };\n\n // We can't send the assetUrlResolver function to the worker so we remove it from the config.\n const workerConfig = { ...resolvedConfig, assetUrlResolver: undefined };\n\n // This is a workaround to ensure the worker configuration can be sent to the worker.\n return JSON.parse(JSON.stringify(workerConfig));\n }\n}\n\n// Global instance of the shared resource manager.\nexport const sharedResourceManager = new SharedResourceManager();\n"],"names":["ONNX_WASM_FILENAME","SharedResourceManager","config","scriptUri","workerSource","mask","providers","ort","wasmPaths","loadAndCacheFile","error","modelUrl","modelSession","sessionOptions","modelBinary","workerConfig","session","inputSize","inputChannels","inputType","numPixels","dummyInputData","dummyInput","feed","dummyMaskData","prevMaskTensor","assetUri","resolvedUrl","isResolvedUri","toAbsoluteURI","sharedResourceManager"],"mappings":";;AAMA,MAAMA,IAAqB;AAO3B,MAAMC,EAAsB;AAAA,EAA5B,cAAA;AACI,SAAQ,gBAAsD,CAAC,GAKvD,KAAA,uCAA4C,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQxD,MAAa,iBAAiBC,GAAuC;AAC7D,QAAA,OAAO,SAAW;AACZ,YAAA,IAAI,MAAM,8DAA8D;AAGlF,UAAMC,IAAY,MAAM,KAAK,WAAWD,GAAQA,EAAO,KAAK,SAAS,GAE/DE,IAAe,OADJ,MAAM,MAAMD,GAAW,EAAE,OAAO,eAAe,GAC5B,KAAK;AAEzC,gBAAK,YAAY,IAAI,gBAAgB,IAAI,KAAK,CAACC,CAAY,GAAG,EAAE,MAAM,kBAAmB,CAAA,CAAC,GAEnF,IAAI,OAAO,KAAK,WAAW,EAAE,MAAM,UAAU;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMjD,kBAAkB;AACrB,IAAI,KAAK,WACL,KAAK,OAAO,UAAU,GACtB,KAAK,SAAS;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUJ,MAAa,UAAUF,GAAuB;AAC1C,UAAM,EAAE,MAAAG,GAAM,oBAAoBC,EAAc,IAAAJ;AAG5C,IAAAK,EAAA,IAAI,KAAK,aAAa;AAG1B,UAAMC,IAAY,MAAM,KAAK,WAAWN,GAAQA,EAAO,OAAO;AAE9D,QAAIM;AACI,UAAA;AACA,QAAAD,EAAI,IAAI,KAAK,aAAa,MAAME,EAAiBD,IAAYR,CAAkB;AAAA,eAC1EU,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AAA;AAAA,EAGT,eAAe;AAClB,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWhB,MAAa,oBAAoBzB,GAAuB;AAcpD,UAAMa,IAAe,EAAE,GAXe;AAAA,MAClC,GAAGb;AAAA,MACH,MAAM;AAAA,QACF,GAAGA,EAAO;AAAA,QACV,UAAU,MAAM,KAAK,WAAWA,GAAQA,EAAO,KAAK,QAAQ;AAAA,QAC5D,WAAW,MAAM,KAAK,WAAWA,GAAQA,EAAO,KAAK,SAAS;AAAA,MAClE;AAAA,MACA,SAAS,MAAM,KAAK,WAAWA,GAAQA,EAAO,OAAO;AAAA,IACzD,GAG0C,kBAAkB,OAAU;AAGtE,WAAO,KAAK,MAAM,KAAK,UAAUa,CAAY,CAAC;AAAA,EAAA;AAEtD;AAGa,MAAAe,IAAwB,IAAI7B,EAAsB;"}
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
export declare enum InterpolationMethod {
|
|
2
|
-
Nearest = "nearest"
|
|
3
|
-
}
|
|
4
|
-
/**
|
|
5
|
-
* Resizes an image from the source dimensions to target dimensions.
|
|
6
|
-
*
|
|
7
|
-
* @param {Uint8ClampedArray} imageData The source image data in Uint8ClampedArray format.
|
|
8
|
-
* @param {number} srcWidth The width of the source image.
|
|
9
|
-
* @param {number} srcHeight The height of the source image.
|
|
10
|
-
* @param {number} targetWidth The width of the target image.
|
|
11
|
-
* @param {number} targetHeight The height of the target image.
|
|
12
|
-
*
|
|
13
|
-
* @returns {Uint8ClampedArray} The target image data in Uint8ClampedArray format.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* const targetData = resizeImage(sourceData, 800, 600, 400, 300);
|
|
17
|
-
*
|
|
18
|
-
* @note The function uses a "nearest neighbor" algorithm for resizing,
|
|
19
|
-
* which is fast but might not provide the best quality for all images.
|
|
20
|
-
*/
|
|
21
|
-
export declare const resizeImage: (imageData: Uint8ClampedArray, srcWidth: number, srcHeight: number, targetWidth: number, targetHeight: number) => Uint8ClampedArray;
|
|
22
|
-
/**
|
|
23
|
-
* Upsamples an image using the bilinear interpolation method. The input image is expected to be in
|
|
24
|
-
* Float32Array format. The output image is in Uint8ClampedArray format. The input image is expected
|
|
25
|
-
* to be in the [0, 1] range. The output image is in the [0, 255] range.
|
|
26
|
-
*
|
|
27
|
-
* @param {Float32Array} input The input image data in Float32Array format.
|
|
28
|
-
* @param {number} srcWidth The width of the source image.
|
|
29
|
-
* @param {number} srcHeight The height of the source image.
|
|
30
|
-
* @param {number} targetWidth The width of the target image.
|
|
31
|
-
* @param {number} targetHeight The height of the target image.
|
|
32
|
-
*
|
|
33
|
-
* @returns {Uint8ClampedArray} The upsampled image data in Uint8ClampedArray format.
|
|
34
|
-
*
|
|
35
|
-
*/
|
|
36
|
-
export declare function bilinearUpsample(input: Float32Array, srcWidth: number, srcHeight: number, targetWidth: number, targetHeight: number): Uint8ClampedArray;
|
|
37
|
-
/**
|
|
38
|
-
* Upsamples an image using the Lanczos3 interpolation method. The input image is expected to be in
|
|
39
|
-
* Float32Array format. The output image is in Uint8ClampedArray format. The input image is expected
|
|
40
|
-
* to be in the [0, 1] range. The output image is in the [0, 255] range.
|
|
41
|
-
*
|
|
42
|
-
* @param {Float32Array} input The input image data in Float32Array format.
|
|
43
|
-
* @param {number} srcWidth The width of the source image.
|
|
44
|
-
* @param {number} srcHeight The height of the source image.
|
|
45
|
-
* @param {number} targetWidth The width of the target image.
|
|
46
|
-
* @param {number} targetHeight The height of the target image.
|
|
47
|
-
*
|
|
48
|
-
* @returns {Uint8ClampedArray} The upsampled image data in Uint8ClampedArray format.
|
|
49
|
-
*/
|
|
50
|
-
export declare function lanczos3Upsample(input: Float32Array, srcWidth: number, srcHeight: number, targetWidth: number, targetHeight: number): Uint8ClampedArray;
|
|
51
|
-
export declare const toAbsoluteURI: (relativeURL: string, baseURL?: string) => string;
|
|
52
|
-
export declare function compileShader(gl: WebGL2RenderingContext, shader_type: number, source: string): WebGLShader;
|
|
53
|
-
export declare function linkProgram(gl: WebGL2RenderingContext, vertex_shader: WebGLShader, fragment_shader: WebGLShader): WebGLProgram;
|
|
54
|
-
/**
|
|
55
|
-
* Determines if a URI is already resolved.
|
|
56
|
-
* A resolved URI is either:
|
|
57
|
-
* - A blob URL (starts with 'blob:')
|
|
58
|
-
* - A fully qualified URL (e.g., http:// or https://)
|
|
59
|
-
*
|
|
60
|
-
* @param uri - The URI to check.
|
|
61
|
-
* @returns Whether the URI is already resolved.
|
|
62
|
-
*/
|
|
63
|
-
export declare function isResolvedUri(uri: string): boolean;
|
|
64
|
-
export declare function loadAndCacheFile(fileUrl: string): Promise<ArrayBufferLike | Uint8Array>;
|
|
1
|
+
export declare enum InterpolationMethod {
|
|
2
|
+
Nearest = "nearest"
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Resizes an image from the source dimensions to target dimensions.
|
|
6
|
+
*
|
|
7
|
+
* @param {Uint8ClampedArray} imageData The source image data in Uint8ClampedArray format.
|
|
8
|
+
* @param {number} srcWidth The width of the source image.
|
|
9
|
+
* @param {number} srcHeight The height of the source image.
|
|
10
|
+
* @param {number} targetWidth The width of the target image.
|
|
11
|
+
* @param {number} targetHeight The height of the target image.
|
|
12
|
+
*
|
|
13
|
+
* @returns {Uint8ClampedArray} The target image data in Uint8ClampedArray format.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const targetData = resizeImage(sourceData, 800, 600, 400, 300);
|
|
17
|
+
*
|
|
18
|
+
* @note The function uses a "nearest neighbor" algorithm for resizing,
|
|
19
|
+
* which is fast but might not provide the best quality for all images.
|
|
20
|
+
*/
|
|
21
|
+
export declare const resizeImage: (imageData: Uint8ClampedArray, srcWidth: number, srcHeight: number, targetWidth: number, targetHeight: number) => Uint8ClampedArray;
|
|
22
|
+
/**
|
|
23
|
+
* Upsamples an image using the bilinear interpolation method. The input image is expected to be in
|
|
24
|
+
* Float32Array format. The output image is in Uint8ClampedArray format. The input image is expected
|
|
25
|
+
* to be in the [0, 1] range. The output image is in the [0, 255] range.
|
|
26
|
+
*
|
|
27
|
+
* @param {Float32Array} input The input image data in Float32Array format.
|
|
28
|
+
* @param {number} srcWidth The width of the source image.
|
|
29
|
+
* @param {number} srcHeight The height of the source image.
|
|
30
|
+
* @param {number} targetWidth The width of the target image.
|
|
31
|
+
* @param {number} targetHeight The height of the target image.
|
|
32
|
+
*
|
|
33
|
+
* @returns {Uint8ClampedArray} The upsampled image data in Uint8ClampedArray format.
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
export declare function bilinearUpsample(input: Float32Array, srcWidth: number, srcHeight: number, targetWidth: number, targetHeight: number): Uint8ClampedArray;
|
|
37
|
+
/**
|
|
38
|
+
* Upsamples an image using the Lanczos3 interpolation method. The input image is expected to be in
|
|
39
|
+
* Float32Array format. The output image is in Uint8ClampedArray format. The input image is expected
|
|
40
|
+
* to be in the [0, 1] range. The output image is in the [0, 255] range.
|
|
41
|
+
*
|
|
42
|
+
* @param {Float32Array} input The input image data in Float32Array format.
|
|
43
|
+
* @param {number} srcWidth The width of the source image.
|
|
44
|
+
* @param {number} srcHeight The height of the source image.
|
|
45
|
+
* @param {number} targetWidth The width of the target image.
|
|
46
|
+
* @param {number} targetHeight The height of the target image.
|
|
47
|
+
*
|
|
48
|
+
* @returns {Uint8ClampedArray} The upsampled image data in Uint8ClampedArray format.
|
|
49
|
+
*/
|
|
50
|
+
export declare function lanczos3Upsample(input: Float32Array, srcWidth: number, srcHeight: number, targetWidth: number, targetHeight: number): Uint8ClampedArray;
|
|
51
|
+
export declare const toAbsoluteURI: (relativeURL: string, baseURL?: string) => string;
|
|
52
|
+
export declare function compileShader(gl: WebGL2RenderingContext, shader_type: number, source: string): WebGLShader;
|
|
53
|
+
export declare function linkProgram(gl: WebGL2RenderingContext, vertex_shader: WebGLShader, fragment_shader: WebGLShader): WebGLProgram;
|
|
54
|
+
/**
|
|
55
|
+
* Determines if a URI is already resolved.
|
|
56
|
+
* A resolved URI is either:
|
|
57
|
+
* - A blob URL (starts with 'blob:')
|
|
58
|
+
* - A fully qualified URL (e.g., http:// or https://)
|
|
59
|
+
*
|
|
60
|
+
* @param uri - The URI to check.
|
|
61
|
+
* @returns Whether the URI is already resolved.
|
|
62
|
+
*/
|
|
63
|
+
export declare function isResolvedUri(uri: string): boolean;
|
|
64
|
+
export declare function loadAndCacheFile(fileUrl: string): Promise<ArrayBufferLike | Uint8Array>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const LIB_VERSION = "5.4.
|
|
1
|
+
export declare const LIB_VERSION = "5.4.3";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sources":["../../../src/lib/version.ts"],"sourcesContent":["export const LIB_VERSION = '5.4.
|
|
1
|
+
{"version":3,"file":"version.js","sources":["../../../src/lib/version.ts"],"sourcesContent":["export const LIB_VERSION = '5.4.3';\n"],"names":["LIB_VERSION"],"mappings":"AAAO,MAAMA,IAAc;"}
|