deno.land / x / skia_canvas@0.5.8 / src / 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import ffi, { cstr, decodeBase64 } from "./ffi.ts";
const { sk_image_destroy, sk_image_from_encoded, sk_image_from_file, sk_image_height, sk_image_width,} = ffi;
const SK_IMAGE_FINALIZER = new FinalizationRegistry( (image: Deno.PointerValue) => { sk_image_destroy(image); },);
export type ImageSource = Uint8Array | string;
const _token = Symbol("[[token]]");const _ptr = Symbol("[[ptr]]");const _src = Symbol("[[src]]");
export class Image extends EventTarget { [_token]: { ptr: Deno.PointerValue } = { ptr: null }; [_ptr]: Deno.PointerValue = null; [_src]?: ImageSource;
get _unsafePointer(): Deno.PointerValue { return this[_ptr]; }
constructor(data?: ImageSource) { super(); this.src = data; }
get src(): ImageSource | undefined { return this[_src]; }
set src(data: ImageSource | undefined) { if (this[_ptr] !== null) { sk_image_destroy(this[_ptr]); SK_IMAGE_FINALIZER.unregister(this[_token]); this[_ptr] = null; }
if (data === undefined) { this[_src] = undefined; this[_ptr] = null; this[_token].ptr = null; return; }
if (typeof data === "string") { if (data.match(/^\s*https?:\/\//)) { fetch(data.trim()) .then((res) => res.arrayBuffer()) .then((buffer) => { this.src = new Uint8Array(buffer); }) .catch((error) => { this.dispatchEvent( new ErrorEvent("error", { error, }), ); }); return; } else if (data.match(/^\s*data:/)) { const comma = data.indexOf(","); const isBase64 = data.lastIndexOf("base64", comma) !== -1; const content = data.slice(comma + 1); const buffer = isBase64 ? decodeBase64(content) : new TextEncoder().encode(content); this.src = buffer; return; } }
this[_ptr] = data instanceof Uint8Array ? sk_image_from_encoded(data, data.byteLength) : sk_image_from_file(cstr(data));
if (this[_ptr] === null) { const error = new Error("Failed to load image"); queueMicrotask(() => { this.dispatchEvent( new ErrorEvent("error", { error, }), ); }); throw error; }
this[_token].ptr = this[_ptr]; this[_src] = data;
if (this[_ptr] !== null) { SK_IMAGE_FINALIZER.register(this, this[_ptr], this[_token]); }
queueMicrotask(() => { this.dispatchEvent(new Event("load")); }); }
#onload?: EventListenerOrEventListenerObject; #onerror?: EventListenerOrEventListenerObject;
get onload(): EventListenerOrEventListenerObject | undefined { return this.#onload; }
get onerror(): EventListenerOrEventListenerObject | undefined { return this.#onerror; }
// deno-lint-ignore adjacent-overload-signatures set onload(fn: EventListenerOrEventListenerObject | undefined) { if (this.#onload) { this.removeEventListener("load", this.#onload); } this.#onload = fn; if (fn) this.addEventListener("load", fn); }
// deno-lint-ignore adjacent-overload-signatures set onerror(fn: EventListenerOrEventListenerObject | undefined) { if (this.#onerror) { this.removeEventListener("error", this.#onerror); } this.#onerror = fn; if (fn) this.addEventListener("error", fn); }
static async load(path: string | URL): Promise<Image> { const data = path instanceof URL || path.startsWith("http") ? await fetch(path).then((e) => e.arrayBuffer()).then((e) => new Uint8Array(e) ) : await Deno.readFile(path); return new Image(data); }
/** * Load an image from a local file synchronously. */ static loadSync(path: string): Image { const data = Deno.readFileSync(path); return new Image(data); }
get width(): number { if (this._unsafePointer === null) return 0; return sk_image_width(this[_ptr]); }
get height(): number { if (this._unsafePointer === null) return 0; return sk_image_height(this[_ptr]); }
[Symbol.for("Deno.customInspect")](): string { if (this._unsafePointer === null) { return `Image { pending, src: ${Deno.inspect(this.src)} }`; } return `Image { width: ${this.width}, height: ${this.height} }`; }}
export type ColorSpace = "srgb" | "rec2020" | "display-p3";
export interface ImageDataSettings { colorSpace?: ColorSpace;}
export class ImageData { #width: number; #height: number; #data: Uint8ClampedArray; #colorSpace: ColorSpace;
constructor(width: number, height: number, settings?: ImageDataSettings); constructor( dataArray: Uint8ClampedArray | Uint8Array, width: number, height?: number, settings?: ImageDataSettings, ); constructor( width: number | Uint8ClampedArray | Uint8Array, height: number | ImageDataSettings, settings?: ImageDataSettings | number, settings2?: ImageDataSettings, ) { if (typeof width === "number") { this.#width = width; this.#height = height as number; this.#data = new Uint8ClampedArray(width * (height as number) * 4); this.#colorSpace = (settings as ImageDataSettings)?.colorSpace ?? "srgb"; } else { this.#data = new Uint8ClampedArray(width.buffer); this.#width = height as number; this.#height = settings ? settings as number : this.#data.length / 4; this.#colorSpace = settings2?.colorSpace ?? "srgb"; } }
get width(): number { return this.#width; }
get height(): number { return this.#height; }
get data(): Uint8ClampedArray { return this.#data; }
get colorSpace(): ColorSpace { return this.#colorSpace; }}
skia_canvas

Version Info

Tagged at
8 months ago