forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
executable file
·113 lines (95 loc) · 3.16 KB
/
Copy pathmain.ts
File metadata and controls
executable file
·113 lines (95 loc) · 3.16 KB
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
#!/usr/bin/env node
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT
import * as slint from "slint-ui";
import { read, Image, ImageColorModel } from "image-js";
function fromImageData(bitmap: slint.ImageData): Image {
return new Image(bitmap.width, bitmap.height, {
data: bitmap.data,
colorModel: ImageColorModel.RGBA,
});
}
function toImageData(image: Image): slint.ImageData {
const raw = image.getRawImage();
return {
width: raw.width,
height: raw.height,
data: raw.data as Uint8Array,
};
}
/** Constant (r,g,b,0) image for add/subtract so only RGB change (alpha unchanged, like Rust brighten). */
function constantRgb(w: number, h: number, r: number, g: number, b: number): Image {
return new Image(w, h, { colorModel: ImageColorModel.RGBA }).fill([r, g, b, 0]);
}
class Filter {
name: string;
applyFunction: (image: slint.ImageData) => slint.ImageData;
constructor(
name: string,
applyFunction: (image: slint.ImageData) => slint.ImageData,
) {
this.name = name;
this.applyFunction = applyFunction;
}
}
class Filters extends slint.Model<string> {
#filters: Filter[];
constructor(filters: Filter[]) {
super();
this.#filters = filters;
}
at(index: number): Filter {
return this.#filters[index];
}
rowCount(): number {
return this.#filters.length;
}
rowData(row: number): string | undefined {
return this.#filters[row]?.name;
}
setRowData(row: number, data: string): void {
// not needed for this example
throw new Error("Method not implemented.");
}
}
const demo = slint.loadFile(
new URL("../ui/main.slint", import.meta.url),
) as any;
const mainWindow = new demo.MainWindow();
const imagePath = new URL("../assets/cat.jpg", import.meta.url).pathname;
const image = await read(imagePath);
mainWindow.original_image = toImageData(image.convertColor(ImageColorModel.RGBA));
const filters = new Filters([
new Filter("Blur", (bitmap) => {
return toImageData(
fromImageData(bitmap).gaussianBlur({ sigma: 4 }),
);
}),
new Filter("Brighten", (bitmap) => {
const img = fromImageData(bitmap);
return toImageData(img.add(constantRgb(img.width, img.height, 30, 30, 30)));
}),
new Filter("Darken", (bitmap) => {
const img = fromImageData(bitmap);
return toImageData(img.subtract(constantRgb(img.width, img.height, 30, 30, 30)));
}),
new Filter("Increase Contrast", (bitmap) => {
return toImageData(fromImageData(bitmap).increaseContrast());
}),
new Filter("Decrease Contrast", (bitmap) => {
return toImageData(
fromImageData(bitmap).level({ outputMin: 32, outputMax: 224 }),
);
}),
new Filter("Invert", (bitmap) => {
return toImageData(
fromImageData(bitmap).invert(),
);
}),
]);
mainWindow.filters = filters;
mainWindow.filter_image = function (index: number) {
const filterFunction = filters.at(index).applyFunction;
return filterFunction(mainWindow.original_image);
};
await mainWindow.run();