-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.js
213 lines (179 loc) · 5.9 KB
/
init.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
'use strict';
const worker = new Worker('worker.js');
const resultImg = document.getElementById('result')
const app = Elm.Main.init({
node: document.getElementById('app-container')
})
app.ports.sendImagesToJs.subscribe((files) => {
generateCollage(files).catch(console.error)
})
const benchmarkSeed = 1338;
worker.onmessage = (event) => {
if (event.data[0] == 'ready') {
if (window.location.hash.includes("blueprintLayoutTest")) {
blueprintLayoutTest()
}
if (window.location.hash.includes("randomLayoutTest")) {
randomLayoutTest()
}
if (window.location.hash.includes("benchmark")) {
prepareBenchmark().then(() => {
console.log("Benchmark prepared")
})
}
}
}
const generateCollage = async (files, seed) => {
URL.revokeObjectURL(resultImg.src);
resultImg.src = "";
const imageArrays = await Promise.all(
files.map((file) => file.arrayBuffer().then((buffer) => new Uint8Array(buffer)))
)
console.time('generate_layout');
const resultArray = await generate_layout(imageArrays, seed);
console.timeEnd('generate_layout');
resultImg.src = URL.createObjectURL(
new Blob([resultArray.buffer], {type: 'image/jpg'})
);
app.ports.imageProcessorStatus.send("done");
}
// Wrapping the message passing in a promise.
// The worker code is simple enough that we can let ourselves do that.
const generate_layout = (imageArrays, seed) => new Promise((resolve, reject) => {
worker.onmessage = (event) => { resolve(event.data) }
worker.postMessage(
['generate_layout', imageArrays, seed],
imageArrays.map((imageArray) => imageArray.buffer)
)
})
const render_specific_layout = (layoutBlueprint, imageArrays) => new Promise((resolve, reject) => {
worker.onmessage = (event) => { resolve(event.data) }
worker.postMessage(
['render_specific_layout', layoutBlueprint, imageArrays],
imageArrays.map((imageArray) => imageArray.buffer)
)
})
const orientationTest = async () => {
console.time('orientation test');
const loadFile = (n) => fetch(`vendor/orientation-test/f${n}t.jpg`)
.then((response) => response.blob())
.then((blob) => blob.arrayBuffer())
.then((buffer) => new Uint8Array(buffer));
const pairs = [[1, 2], [3, 4], [5, 6], [7, 8]];
for (const [n1, n2] of pairs) {
const result = await generate_layout([await loadFile(n1), await loadFile(n2)]);
const img = document.createElement('img');
img.onload = function() {
URL.revokeObjectURL(this.src);
}
img.src = URL.createObjectURL(
new Blob([result.buffer], {type: 'image/jpg'})
);
document.body.appendChild(img);
}
console.timeEnd('orientation test');
}
const blueprintLayoutTest = async () => {
const loadFile = (n) => fetch(`test-images/${n}`)
.then((response) => response.blob())
.then((blob) => blob.arrayBuffer())
.then((buffer) => new Uint8Array(buffer));
// digraph {
// 0 [ label = "Horizontal" ]
// 1 [ label = "Vertical" ]
// 2 [ label = "Horizontal" ]
// 3 [ label = "Horizontal" ]
// 4 [ label = "Horizontal" ]
// 5 [ label = "Vertical" ]
// 6 [ label = "Image(175, 175)" ]
// 7 [ label = "Image(200, 302)" ]
// 8 [ label = "Image(202, 192)" ]
// 9 [ label = "Image(170, 200)" ]
// 10 [ label = "Image(200, 140)" ]
// 11 [ label = "Image(306, 220)" ]
// 12 [ label = "Image(170, 170)" ]
// 0 -> 1 [ ]
// 1 -> 2 [ ]
// 1 -> 3 [ ]
// 2 -> 4 [ ]
// 0 -> 5 [ ]
// 2 -> 6 [ ]
// 3 -> 7 [ ]
// 3 -> 8 [ ]
// 4 -> 9 [ ]
// 4 -> 10 [ ]
// 5 -> 11 [ ]
// 5 -> 12 [ ]
// }
// Canvas dimensions: Dimensions { width: 370, height: 642 }
const layoutBlueprint = {
graph_representation: [
["H", [1, 5]],
["V", [2, 3]],
["H", [4]],
["H", []],
["H", []],
["V", []],
],
width: 370,
height: 642,
}
const images = await Promise.all([
'175.jpg', '170.jpg', '220.jpg', '192.jpg', '200.jpg', '140.jpg', '302.jpg'
].map((name) => loadFile(name)))
const resultArray = await render_specific_layout(layoutBlueprint, images);
resultImg.src = URL.createObjectURL(
new Blob([resultArray.buffer], {type: 'image/jpg'})
);
}
const randomLayoutTest = async () => {
const loadFile = (n) => fetch(`test-images/${n}`)
.then((response) => response.blob());
const files = await Promise.all([
'140.jpg', '175.jpg', '220.jpg', '192.jpg', '302.jpg', '200.jpg', '170.jpg'
].map((name) => loadFile(name)))
generateCollage(files).catch(console.error)
}
let benchmarkFiles = null;
const prepareBenchmark = async () => {
const scriptPromise = loadScript('vendor/lodash.min.js')
.then(() => loadScript('vendor/platform.js'))
.then(() => loadScript('vendor/benchmark.js'))
const loadFile = (n) => fetch(`test-images/${n}`)
.then((response) => response.blob());
benchmarkFiles = await Promise.all([
'140.jpg', '175.jpg', '220.jpg', '192.jpg', '302.jpg', '200.jpg', '170.jpg'
].map((name) => loadFile(name)))
await scriptPromise
}
const benchmark = async (seed = benchmarkSeed) => {
const suite = new Benchmark.Suite;
suite
.add('Generate layout', {
defer: true,
fn: (deferred) => {
generateCollage(benchmarkFiles, seed).then(() => {
deferred.resolve();
},
console.error)
}
})
.on('cycle', function(event) {
console.log(String(event.target));
console.log(event.target.stats.mean)
})
.on('complete', function() {
console.log('Benchmark finished')
})
.run();
}
const benchmarkOnce = async (seed = benchmarkSeed) => {
generateCollage(benchmarkFiles, seed).catch(console.error);
}
const loadScript = (path) => new Promise((resolve, reject) => {
let script = document.createElement('script');
script.onload = () => { resolve() };
script.onerror = () => { reject() };
script.setAttribute('src', path);
document.body.appendChild(script);
})