| 1 |
import * as jpeg from '@jsquash/jpeg'; |
| 2 |
import * as png from '@jsquash/png'; |
| 3 |
import optimise from '@jsquash/oxipng/optimise'; |
| 4 |
|
| 5 |
async function decode(sourceType, fileBuffer) { |
| 6 |
switch (sourceType) { |
| 7 |
//case 'avif': |
| 8 |
// return await avif.decode(fileBuffer); |
| 9 |
case 'jpeg': |
| 10 |
return await jpeg.decode(fileBuffer); |
| 11 |
case 'png': |
| 12 |
return await png.decode(fileBuffer); |
| 13 |
//case 'webp': |
| 14 |
// return await webp.decode(fileBuffer); |
| 15 |
default: |
| 16 |
throw new Error(`Unknown source type: ${sourceType}`); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
async function encode(outputType, imageData) { |
| 21 |
const options = { |
| 22 |
"jpeg_quality": 75, |
| 23 |
"jpeg_baseline": false, |
| 24 |
"jpeg_arithmetic": false, |
| 25 |
"jpeg_progressive": true, |
| 26 |
"jpeg_optimize_coding": true, |
| 27 |
"jpeg_smoothing": 0, |
| 28 |
"jpeg_color_space": 3, |
| 29 |
"jpeg_quant_table": 3, |
| 30 |
"jpeg_trellis_multipass": false, |
| 31 |
"jpeg_trellis_opt_zero": false, |
| 32 |
"jpeg_trellis_opt_table": false, |
| 33 |
"jpeg_trellis_loops": 1, |
| 34 |
"jpeg_auto_subsample": true, |
| 35 |
"jpeg_chroma_subsample": 2, |
| 36 |
"jpeg_separate_chroma_quality": false, |
| 37 |
"jpeg_chroma_quality": 75, |
| 38 |
"png_level": 2, |
| 39 |
"png_interlace": false, |
| 40 |
"auto_compress": true, |
| 41 |
"backup_original": true |
| 42 |
}; |
| 43 |
|
| 44 |
switch (outputType) { |
| 45 |
//case 'avif': |
| 46 |
// return await avif.encode(imageData); |
| 47 |
case 'jpeg': |
| 48 |
const jpegOptions = {} |
| 49 |
for (const [key, value] of Object.entries(options)) { |
| 50 |
if (key.includes('jpeg')) { |
| 51 |
const keyName = key.replace('jpeg_', '') |
| 52 |
jpegOptions[keyName] = value |
| 53 |
} |
| 54 |
} |
| 55 |
return await jpeg.encode(imageData, jpegOptions); |
| 56 |
case 'png': |
| 57 |
const pngOptions = {} |
| 58 |
for (const [key, value] of Object.entries(options)) { |
| 59 |
if (key.includes('png')) { |
| 60 |
const keyName = key.replace('png_', '') |
| 61 |
pngOptions[keyName] = value |
| 62 |
} |
| 63 |
} |
| 64 |
return await png.encode(imageData, pngOptions); |
| 65 |
//case 'webp': |
| 66 |
// return await webp.encode(imageData); |
| 67 |
default: |
| 68 |
throw new Error(`Unknown output type: ${outputType}`); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
async function convert(sourceType, outputType, fileBuffer) { |
| 73 |
const imageData = await decode(sourceType, fileBuffer); |
| 74 |
return encode(outputType, imageData); |
| 75 |
} |
| 76 |
|
| 77 |
function blobToBase64(blob) { |
| 78 |
return new Promise((resolve, _) => { |
| 79 |
const reader = new FileReader(); |
| 80 |
reader.onloadend = () => resolve(reader.result); |
| 81 |
reader.readAsDataURL(blob); |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
async function showOutput(imageBuffer, outputType) { |
| 86 |
const imageBlob = new Blob([imageBuffer], { type: `image/${outputType}` }); |
| 87 |
const base64String = await blobToBase64(imageBlob); |
| 88 |
|
| 89 |
return base64String; |
| 90 |
} |
| 91 |
|
| 92 |
self.onmessage = async function(e) { |
| 93 |
console.log('Worker: Message received from main script'); |
| 94 |
|
| 95 |
const {fileBuffer, sourceType, outputType} = e.data; |
| 96 |
|
| 97 |
let imageBuffer = await convert(sourceType, outputType, fileBuffer); |
| 98 |
let base64 = await showOutput(imageBuffer, outputType); |
| 99 |
|
| 100 |
console.log(e) |
| 101 |
|
| 102 |
postMessage(base64) |
| 103 |
|
| 104 |
} |