-
Notifications
You must be signed in to change notification settings - Fork 209
/
GIFLoader.worker.js
344 lines (311 loc) · 10.3 KB
/
GIFLoader.worker.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
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
import * as GifuctJs from './gifuct-js.js';
const _createGif = async ({
url,
}) => {
for (let i = 0; i < 10; i++) {
try {
const u = url;
const res = await fetch(u, {
mode: 'cors',
});
if (res.ok) {
const ab = await res.arrayBuffer();
if (ab.byteLength > 0) {
const gif = await GifuctJs.parseGIF(ab);
const frames = await GifuctJs.decompressFrames(gif, true);
// console.log('got frames', {ab, frames});
const {width, height} = frames[0].dims;
let worldWidth = width;
let worldHeight = height;
if (worldWidth >= worldHeight) {
worldHeight /= worldWidth;
worldWidth = 1;
}
if (worldHeight >= worldWidth) {
worldWidth /= worldHeight;
worldHeight = 1;
}
/* const geometry = new THREE.PlaneBufferGeometry(worldWidth, worldHeight);
geometry.boundingBox = new THREE.Box3(
new THREE.Vector3(-worldWidth/2, -worldHeight/2, -0.1),
new THREE.Vector3(worldWidth/2, worldHeight/2, 0.1),
);
const colors = new Float32Array(geometry.attributes.position.array.length);
colors.fill(1, 0, colors.length);
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3)); */
// user canvas
const c = new OffscreenCanvas(width, height);
const ctx = c.getContext('2d');
// gif patch canvas
const tempCanvas = new OffscreenCanvas(width, height);
const tempCtx = tempCanvas.getContext('2d');
// full gif canvas
const gifCanvas = new OffscreenCanvas(width, height);
const gifCtx = gifCanvas.getContext('2d');
// let playing = false;
let bEdgeDetect = false;
let bInvert = false;
let bGrayscale = false;
let pixelPercent = 100;
let loadedFrames;
let frameIndex;
let frameImageData;
function renderGIF(frames) {
loadedFrames = frames
frameIndex = 0
c.width = frames[0].dims.width
c.height = frames[0].dims.height
gifCanvas.width = c.width
gifCanvas.height = c.height
/* if (!playing) {
playpause()
} */
}
function drawPatch(frame) {
var dims = frame.dims
if (
!frameImageData ||
dims.width != frameImageData.width ||
dims.height != frameImageData.height
) {
tempCanvas.width = dims.width
tempCanvas.height = dims.height
frameImageData = tempCtx.createImageData(dims.width, dims.height)
}
// set the patch data as an override
frameImageData.data.set(frame.patch)
// draw the patch back over the canvas
tempCtx.putImageData(frameImageData, 0, 0)
gifCtx.drawImage(tempCanvas, dims.left, dims.top)
}
var edge = function(data, output) {
var odata = output.data
var width = gif.lsd.width
var height = gif.lsd.height
var conv = [-1, -1, -1, -1, 8, -1, -1, -1, -1]
var halfside = Math.floor(3 / 2)
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var r = 0,
g = 0,
b = 0
for (var cy = 0; cy < 3; cy++) {
for (var cx = 0; cx < 3; cx++) {
var scy = y - halfside + cy
var scx = x - halfside + cx
if (scy >= 0 && scy < height && scx >= 0 && scx < width) {
var src = (scy * width + scx) * 4
var f = cy * 3 + cx
r += data[src] * conv[f]
g += data[src + 1] * conv[f]
b += data[src + 2] * conv[f]
}
}
}
var i = (y * width + x) * 4
odata[i] = r
odata[i + 1] = g
odata[i + 2] = b
odata[i + 3] = 255
}
}
return output
}
var invert = function(data) {
for (var i = 0; i < data.length; i += 4) {
data[i] = 255 - data[i] // red
data[i + 1] = 255 - data[i + 1] // green
data[i + 2] = 255 - data[i + 2] // blue
data[i + 3] = 255
}
}
var grayscale = function(data) {
for (var i = 0; i < data.length; i += 4) {
var avg = (data[i] + data[i + 1] + data[i + 2]) / 3
data[i] = avg // red
data[i + 1] = avg // green
data[i + 2] = avg // blue
data[i + 3] = 255
}
}
function manipulate() {
var imageData = gifCtx.getImageData(0, 0, gifCanvas.width, gifCanvas.height)
var other = gifCtx.createImageData(gifCanvas.width, gifCanvas.height)
if (bEdgeDetect) {
imageData = edge(imageData.data, other)
}
if (bInvert) {
invert(imageData.data)
}
if (bGrayscale) {
grayscale(imageData.data)
}
// do pixelation
var pixelsX = 5 + Math.floor((pixelPercent / 100) * (c.width - 5))
var pixelsY = (pixelsX * c.height) / c.width
if (pixelPercent != 100) {
ctx.mozImageSmoothingEnabled = false
ctx.webkitImageSmoothingEnabled = false
ctx.imageSmoothingEnabled = false
}
ctx.putImageData(imageData, 0, 0)
ctx.drawImage(c, 0, 0, c.width, c.height, 0, 0, pixelsX, pixelsY)
ctx.drawImage(c, 0, 0, pixelsX, pixelsY, 0, 0, c.width, c.height)
}
async function renderFrame() {
// get the frame
var frame = loadedFrames[frameIndex]
// var start = new Date().getTime()
if (frame.disposalType === 2) {
gifCtx.clearRect(0, 0, c.width, c.height)
}
// draw the patch
drawPatch(frame)
// perform manipulation
manipulate()
// update the frame index
frameIndex++
if (frameIndex >= loadedFrames.length) {
frameIndex = 0
}
const imageBitmap = await createImageBitmap(c);
return imageBitmap;
/* var end = new Date().getTime()
var diff = end - start
if (playing) {
// delay the next gif frame
setTimeout(function() {
requestAnimationFrame(renderFrame)
//renderFrame();
}, Math.max(0, Math.floor(frame.delay - diff)))
} */
}
async function renderFrames() {
const results = Array(loadedFrames.length);
for (let i = 0; i < loadedFrames.length; i++) {
results[i] = await renderFrame();
}
return results;
}
renderGIF(frames);
return {
renderFrame,
renderFrames,
};
} else {
throw new Error('failed to load image to to empty data: ' + u);
}
} else {
throw new Error('failed to load image: ' + res.status + ': ' + u);
}
} catch (err) {
console.warn(err.stack);
}
}
throw new Error('failed to load image: ' + res.status + ': ' + u);
};
const gifs = {};
let nextGifId = 0;
const _handleMessage = async message => {
const {method, args} = message;
switch (method) {
case 'createGif': {
// console.log('worker got create gif');
const {url} = args;
const gifId = ++nextGifId;
const gif = await _createGif({
url,
});
gifs[gifId] = gif;
return [
gifId,
[],
];
break;
}
case 'renderFrame': {
const {gifId} = args;
const gif = gifs[gifId];
if (gif) {
const frame = await gif.renderFrame();
// console.log('got frame', frame);
return [
frame,
[frame],
];
} else {
throw new Error('renderFrame called for non-existent gif: ' + gifId);
}
// console.log('worker got render frame', {gifId});
break;
}
case 'renderFrames': {
const {gifId} = args;
const gif = gifs[gifId];
if (gif) {
const frames = await gif.renderFrames();
// console.log('got frame', frame);
return [
frames,
frames,
];
} else {
throw new Error('renderFrame called for non-existent gif: ' + gifId);
}
// console.log('worker got render frame', {gifId});
break;
}
case 'destroyGif': {
const {gifId} = args;
const gif = gifs[gifId];
if (gif) {
delete gifs[gifId];
return [
null,
[],
];
} else {
throw new Error('destroyGif called for non-existent gif: ' + gifId);
}
break;
}
default: {
throw new Error('unknown method: ' + method);
break;
}
}
};
const _runAndHandleMessage = async message => {
running = true;
const {id} = message;
let error = null;
let result = null;
let transfers;
try {
const spec = await _handleMessage(message);
result = spec[0];
transfers = spec[1];
} catch (err) {
error = err;
}
self.postMessage({
id,
error,
result,
}, []);
running = false;
if (queue.length > 0) {
_runAndHandleMessage(queue.shift());
}
};
const queue = [];
let running = false;
self.addEventListener('message', async e => {
// console.log('worker got message event', e);
const message = e.data;
if (!running) {
await _runAndHandleMessage(message);
} else {
queue.push(message);
}
});