-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavif-encoder.js
More file actions
49 lines (40 loc) · 1.13 KB
/
avif-encoder.js
File metadata and controls
49 lines (40 loc) · 1.13 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
import avifModule from './avif_enc.js';
let emscriptenModule = null;
self.onmessage = async (e) => {
const { imageData, quality, effort } = e.data;
try {
if (!emscriptenModule) {
emscriptenModule = await avifModule();
}
const { width, height, data } = imageData;
const options = {
quality: quality,
qualityAlpha: -1,
denoiseLevel: 0,
tileColsLog2: 0,
tileRowsLog2: 0,
speed: effort,
subsample: 1,
chromaDeltaQ: false,
sharpness: 0,
tune: 0,
enableSharpYUV: false,
bitDepth: 8,
lossless: false
};
const result = emscriptenModule.encode(
new Uint8Array(data.buffer),
width,
height,
options
);
if (!result || result.byteLength === 0) {
throw new Error('AVIF encoding failed');
}
const blob = new Blob([result], { type: 'image/avif' });
self.postMessage({ blob });
} catch (error) {
console.error('AVIF encoding error:', error);
self.postMessage({ error: `Encoding failed: ${error.message}` });
}
};