forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
executable file
·81 lines (66 loc) · 2.02 KB
/
main.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
#!/usr/bin/env node
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: MIT
import * as slint from "slint-ui";
import Jimp from "jimp";
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 {
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("../ui/main.slint") as any;
const mainWindow = new demo.MainWindow();
const sourceImage = await Jimp.read("../assets/cat.jpg");
mainWindow.original_image = sourceImage.bitmap;
const filters = new Filters([
new Filter("Blur", (image) => {
return new Jimp(image).blur(4).bitmap;
}),
new Filter("Brighten", (image) => {
return new Jimp(image).brightness(0.3).bitmap;
}),
new Filter("Darken", (image) => {
return new Jimp(image).brightness(-0.3).bitmap;
}),
new Filter("Increase Contrast", (image) => {
return new Jimp(image).contrast(0.3).bitmap;
}),
new Filter("Decrease Contrast", (image) => {
return new Jimp(image).contrast(-0.3).bitmap;
}),
new Filter("Invert", (image) => {
return new Jimp(image).invert().bitmap;
}),
]);
mainWindow.filters = filters;
mainWindow.filter_image = function (index: number) {
const filterFunction = filters.at(index).applyFunction;
return filterFunction(mainWindow.original_image);
};
await mainWindow.run();