-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
266 lines (235 loc) · 8.81 KB
/
index.js
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
import * as placeholderJSON from "./example_algorithms.json";
const js = import("./node_modules/pconvert-rust/pconvert_rust_bundler");
const apiFunctionSelect = document.querySelector("div#api select");
const blendButton = document.querySelector("button#blend");
const benchmarkButton = document.querySelector("button#benchmark");
const canvas = document.querySelector("canvas#composition");
const inputFiles = document.querySelector("input#files");
const selectAlgorithm = document.querySelector("select#algorithm");
const selectCompression = document.querySelector("select#compression");
const selectFilter = document.querySelector("select#filter");
const metadata = document.querySelector("pre#metadata");
const textareaAlgorithms = document.querySelector("textarea#algorithms");
blendButton.addEventListener("click", () => blend());
benchmarkButton.addEventListener("click", () => benchmark());
setPConvertMetadata();
setAlgorithmsPlaceholder();
setAlgorithmSelectOptions();
setCompressionSelectOptions();
setFilterSelectOptions();
const API_FUNCTIONS = {
blendImagesData: "blendImagesData",
blendImages: "blendImages",
blendMultipleData: "blendMultipleData",
blendMultiple: "blendMultiple"
};
async function blend() {
const pconvert = await js;
const apiFunction = apiFunctionSelect.options[apiFunctionSelect.selectedIndex].value;
let composition;
switch (apiFunction) {
case API_FUNCTIONS.blendImagesData: {
const bot = getImageData(await loadImage(inputFiles.files[0]));
const top = getImageData(await loadImage(inputFiles.files[1]));
const algorithm = selectAlgorithm.value;
const compression = selectCompression.value;
const filter = selectFilter.value;
composition = pconvert.blendImagesData(
bot,
top,
algorithm === "" ? undefined : algorithm,
undefined,
{
compression: compression,
filter: filter
}
);
break;
}
case API_FUNCTIONS.blendImages: {
const bot = inputFiles.files[0];
const top = inputFiles.files[1];
const algorithm = selectAlgorithm.value;
const compression = selectCompression.value;
const filter = selectFilter.value;
const file = await pconvert.blendImages(
bot,
top,
"result",
algorithm === "" ? undefined : algorithm,
undefined,
{
compression: compression,
filter: filter
}
);
composition = getImageData(await loadImage(file));
break;
}
case API_FUNCTIONS.blendMultipleData: {
const data = [];
for (const file of inputFiles.files) {
const imageData = getImageData(await loadImage(file));
data.push(imageData);
}
const algorithms = textareaAlgorithms.value;
const compression = selectCompression.value;
const filter = selectFilter.value;
if (isJSONParsable(algorithms)) {
const algorithmsJSON = JSON.parse(algorithms).algorithms;
composition = await pconvert.blendMultipleData(
data,
undefined,
algorithmsJSON,
undefined,
{
compression: compression,
filter: filter
}
);
} else {
const algorithm = selectAlgorithm.value;
composition = await pconvert.blendMultipleData(
data,
algorithm === "" ? undefined : algorithm,
undefined,
undefined,
{
compression: compression,
filter: filter
}
);
}
break;
}
case API_FUNCTIONS.blendMultiple: {
const algorithms = textareaAlgorithms.value;
const compression = selectCompression.value;
const filter = selectFilter.value;
if (isJSONParsable(algorithms)) {
const algorithmsJSON = JSON.parse(algorithms).algorithms;
const file = await pconvert.blendMultiple(
inputFiles.files,
"result",
undefined,
algorithmsJSON,
undefined,
{
compression: compression,
filter: filter
}
);
composition = getImageData(await loadImage(file));
} else {
const algorithm = selectAlgorithm.value;
const file = await pconvert.blendMultiple(
inputFiles.files,
"result",
algorithm === "" ? undefined : algorithm,
undefined,
undefined,
{
compression: compression,
filter: filter
}
);
composition = getImageData(await loadImage(file));
}
break;
}
default:
console.log("Invalid API function");
}
drawComposition(composition);
}
async function benchmark() {
const pconvert = await js;
const apiFunction = apiFunctionSelect.options[apiFunctionSelect.selectedIndex].value;
switch (apiFunction) {
case API_FUNCTIONS.blendImagesData:
case API_FUNCTIONS.blendImages: {
const bot = inputFiles.files[0];
const top = inputFiles.files[1];
await pconvert.blendImagesBenchmarkAll(bot, top);
break;
}
case API_FUNCTIONS.blendMultipleData:
case API_FUNCTIONS.blendMultiple: {
await pconvert.blendMultipleBenchmarkAll(inputFiles.files);
break;
}
default:
console.log("Invalid API function");
}
}
async function setPConvertMetadata() {
const pconvert = await js;
const constants = pconvert.getModuleConstants();
metadata.innerHTML = JSON.stringify(constants, undefined, 2);
}
async function setAlgorithmsPlaceholder() {
const placeholder = JSON.stringify(placeholderJSON, undefined, 2);
textareaAlgorithms.setAttribute("placeholder", placeholder);
}
async function setAlgorithmSelectOptions() {
const pconvert = await js;
const options = pconvert.getModuleConstants().ALGORITHMS;
for (const option of options) {
const optionEl = document.createElement("option");
optionEl.text = option;
optionEl.value = option;
selectAlgorithm.appendChild(optionEl);
}
}
async function setCompressionSelectOptions() {
const pconvert = await js;
const options = pconvert.getModuleConstants().COMPRESSION_TYPES;
for (const option of options) {
const optionEl = document.createElement("option");
optionEl.text = option;
optionEl.value = option;
selectCompression.appendChild(optionEl);
}
}
async function setFilterSelectOptions() {
const pconvert = await js;
const options = pconvert.getModuleConstants().FILTER_TYPES;
for (const option of options) {
const optionEl = document.createElement("option");
optionEl.text = option;
optionEl.value = option;
selectFilter.appendChild(optionEl);
}
}
function loadImage(file) {
return new Promise((resolve, reject) => {
const img = new Image();
img.addEventListener("load", e => resolve(img));
img.addEventListener("error", e => {
reject(e);
});
img.src = URL.createObjectURL(file);
});
}
function getImageData(img) {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0);
return context.getImageData(0, 0, img.width, img.height);
}
function drawComposition(composition) {
canvas.width = composition.width;
canvas.height = composition.height;
const context = canvas.getContext("2d");
context.putImageData(composition, 0, 0);
}
function isJSONParsable(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}