-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Image.svelte
407 lines (374 loc) 路 9.47 KB
/
Image.svelte
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<script lang="ts">
// @ts-nocheck
import { createEventDispatcher, tick, onMount } from "svelte";
import { BlockLabel } from "@gradio/atoms";
import { Image, Sketch as SketchIcon } from "@gradio/icons";
import type { SelectData } from "@gradio/utils";
import { get_coordinates_of_clicked_image } from "./utils";
import Cropper from "./Cropper.svelte";
import Sketch from "./Sketch.svelte";
import Webcam from "./Webcam.svelte";
import ModifySketch from "./ModifySketch.svelte";
import SketchSettings from "./SketchSettings.svelte";
import { Upload, ModifyUpload } from "@gradio/upload";
export let value:
| null
| string
| { image: string | null; mask: string | null };
export let label: string | undefined = undefined;
export let show_label: boolean;
export let source: "canvas" | "webcam" | "upload" = "upload";
export let tool: "editor" | "select" | "sketch" | "color-sketch" = "editor";
export let shape: [number, number];
export let streaming: boolean = false;
export let pending: boolean = false;
export let mirror_webcam: boolean;
export let brush_radius: number;
export let brush_color = "#000000";
export let selectable: boolean = false;
let sketch: Sketch;
let cropper: Cropper;
if (
value &&
(source === "upload" || source === "webcam") &&
tool === "sketch"
) {
value = { image: value as string, mask: null };
}
function handle_upload({ detail }: CustomEvent<string>) {
if (tool === "color-sketch") {
static_image = detail;
} else {
value =
(source === "upload" || source === "webcam") && tool === "sketch"
? { image: detail, mask: null }
: detail;
}
dispatch("upload", detail);
}
function handle_clear({ detail }: CustomEvent<null>) {
value = null;
static_image = undefined;
dispatch("clear");
}
async function handle_save({ detail }: { detail: string }, initial) {
if (mode === "mask") {
if (source === "webcam" && initial) {
value = {
image: detail,
mask: null
};
} else {
value = {
image: typeof value === "string" ? value : value?.image || null,
mask: detail
};
}
} else if (
(source === "upload" || source === "webcam") &&
tool === "sketch"
) {
value = { image: detail, mask: null };
} else {
value = detail;
}
await tick();
dispatch(streaming ? "stream" : "edit");
}
const dispatch = createEventDispatcher<{
change: string | null;
stream: string | null;
edit: undefined;
clear: undefined;
drag: boolean;
upload: FileData;
select: SelectData;
}>();
let dragging = false;
$: dispatch("drag", dragging);
function handle_image_load(event: Event) {
const element = event.currentTarget as HTMLImageElement;
img_width = element.naturalWidth;
img_height = element.naturalHeight;
container_height = element.getBoundingClientRect().height;
}
async function handle_sketch_clear() {
sketch.clear();
await tick();
value = null;
static_image = undefined;
}
async function handle_mask_clear() {
sketch.clear_mask();
await tick();
}
let img_height = 0;
let img_width = 0;
let container_height = 0;
let mode;
$: {
if (source === "canvas" && tool === "sketch") {
mode = "bw-sketch";
} else if (tool === "color-sketch") {
mode = "color-sketch";
} else if (
(source === "upload" || source === "webcam") &&
tool === "sketch"
) {
mode = "mask";
} else {
mode = "editor";
}
}
let value_img;
let max_height;
let max_width;
let static_image = undefined;
$: {
if (value === null || (value.image === null && value.mask === null)) {
static_image = undefined;
}
}
$: {
if (cropper) {
if (value) {
cropper.image = value;
cropper.create();
} else {
cropper.destroy();
}
}
}
onMount(async () => {
if (tool === "color-sketch" && value && typeof value === "string") {
static_image = value;
await tick();
handle_image_load({ currentTarget: value_img });
}
});
const handle_click = (evt: MouseEvent) => {
let coordinates = get_coordinates_of_clicked_image(evt);
if (coordinates) {
dispatch("select", { index: coordinates, value: null });
}
};
</script>
<BlockLabel
{show_label}
Icon={source === "canvas" ? SketchIcon : Image}
label={label || (source === "canvas" ? "Sketch" : "Image")}
/>
<div
data-testid="image"
class="image-container"
bind:offsetHeight={max_height}
bind:offsetWidth={max_width}
>
{#if source === "upload"}
<Upload
bind:dragging
filetype="image/*"
on:load={handle_upload}
include_file_metadata={false}
disable_click={!!value}
>
{#if (value === null && !static_image) || streaming}
<slot />
{:else if tool === "select"}
<Cropper bind:this={cropper} image={value} on:crop={handle_save} />
<ModifyUpload on:clear={(e) => (handle_clear(e), (tool = "editor"))} />
{:else if tool === "editor"}
<ModifyUpload
on:edit={() => (tool = "select")}
on:clear={handle_clear}
editable
/>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<img
src={value}
alt=""
class:scale-x-[-1]={source === "webcam" && mirror_webcam}
class:selectable
on:click={handle_click}
/>
{:else if (tool === "sketch" || tool === "color-sketch") && (value !== null || static_image)}
{#key static_image}
<img
bind:this={value_img}
class="absolute-img"
src={static_image || value?.image || value}
alt=""
on:load={handle_image_load}
class:webcam={source === "webcam" && mirror_webcam}
/>
{/key}
{#if img_width > 0}
<Sketch
{value}
bind:this={sketch}
bind:brush_radius
bind:brush_color
on:change={handle_save}
{mode}
width={img_width || max_width}
height={img_height || max_height}
container_height={container_height || max_height}
{value_img}
{source}
{shape}
/>
<ModifySketch
show_eraser={value_img}
on:undo={() => sketch.undo()}
on:clear_mask={handle_mask_clear}
on:remove_image={handle_sketch_clear}
/>
{#if tool === "color-sketch" || tool === "sketch"}
<SketchSettings
bind:brush_radius
bind:brush_color
container_height={container_height || max_height}
img_width={img_width || max_width}
img_height={img_height || max_height}
{mode}
/>
{/if}
{/if}
{:else}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<img
src={value.image || value}
alt="hello"
class:webcam={source === "webcam" && mirror_webcam}
class:selectable
on:click={handle_click}
/>
{/if}
</Upload>
{:else if source === "canvas"}
<ModifySketch
on:undo={() => sketch.undo()}
on:remove_image={handle_sketch_clear}
/>
{#if tool === "color-sketch"}
<SketchSettings
bind:brush_radius
bind:brush_color
container_height={container_height || max_height}
img_width={img_width || max_width}
img_height={img_height || max_height}
/>
{/if}
<Sketch
{value}
bind:brush_radius
bind:brush_color
bind:this={sketch}
on:change={handle_save}
on:clear={handle_sketch_clear}
{mode}
width={img_width || max_width}
height={img_height || max_height}
container_height={container_height || max_height}
{shape}
/>
{:else if (value === null && !static_image) || streaming}
{#if source === "webcam" && !static_image}
<Webcam
on:capture={(e) =>
tool === "color-sketch" ? handle_upload(e) : handle_save(e, true)}
on:stream={handle_save}
on:error
{streaming}
{pending}
{mirror_webcam}
/>
{/if}
{:else if tool === "select"}
<Cropper bind:this={cropper} image={value} on:crop={handle_save} />
<ModifyUpload on:clear={(e) => (handle_clear(e), (tool = "editor"))} />
{:else if tool === "editor"}
<ModifyUpload
on:edit={() => (tool = "select")}
on:clear={handle_clear}
editable
/>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<img
src={value}
alt=""
class:selectable
class:webcam={source === "webcam" && mirror_webcam}
on:click={handle_click}
/>
{:else if (tool === "sketch" || tool === "color-sketch") && (value !== null || static_image)}
{#key static_image}
<img
bind:this={value_img}
class="absolute-img"
src={static_image || value?.image || value}
alt=""
on:load={handle_image_load}
class:webcam={source === "webcam" && mirror_webcam}
/>
{/key}
{#if img_width > 0}
<Sketch
{value}
bind:this={sketch}
bind:brush_radius
bind:brush_color
on:change={handle_save}
{mode}
width={img_width || max_width}
height={img_height || max_height}
container_height={container_height || max_height}
{value_img}
{source}
/>
<ModifySketch
on:undo={() => sketch.undo()}
on:remove_image={handle_sketch_clear}
/>
{#if tool === "color-sketch" || tool === "sketch"}
<SketchSettings
bind:brush_radius
bind:brush_color
container_height={container_height || max_height}
img_width={img_width || max_width}
img_height={img_height || max_height}
{mode}
/>
{/if}
{/if}
{:else}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<img
src={value.image || value}
alt=""
class:webcam={source === "webcam" && mirror_webcam}
class:selectable
on:click={handle_click}
/>
{/if}
</div>
<style>
.image-container,
img {
width: var(--size-full);
height: var(--size-full);
}
img {
object-fit: contain;
}
.selectable {
cursor: crosshair;
}
.absolute-img {
position: absolute;
opacity: 0;
}
.webcam {
transform: scaleX(-1);
}
</style>