-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
109 lines (93 loc) · 3 KB
/
index.ts
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
import Module, { MozJPEGModule } from './mozjpeg';
import { defaultInputInfo, defaultEncodeOptions } from './options';
import { InputInfo, EncodeOptions } from './types';
import { isGrayscale, ColorSpace } from './colorspace';
let mozjpeg: MozJPEGModule | null;
const queue: Array<() => void> = [];
/**
* Initialize the mozjpeg module
*/
const initModule = (): Promise<void> => {
return new Promise((resolve) => {
// add a new job to the queue
queue.push(() => {
Module({
onRuntimeInitialized: () => {
resolve();
},
}).then((instance) => {
mozjpeg = instance;
});
});
// start it if there is no queue
if (queue.length === 1) {
queue[0]();
}
});
};
/**
* Resets the mozjpeg module to avoid some wasm related problems
* when optimizing many images after each other
*/
const resetModule = (): void => {
mozjpeg = null;
if (queue.length > 0) {
// remove finished job
queue.shift();
// trigger next job
if (queue.length > 0) {
queue[0]();
}
}
};
/**
* Encode a raw input image using MozJPEG
*
* @param {Buffer} image Raw image input buffer
* @param {InputInfo} inputInfo Information about the input image
* @param {EncodeOptions} encodeOptions Encoding options passed to MozJPEG
* @returns {Buffer} Processed image buffer
*/
const encode = async (image: Buffer, inputInfo: InputInfo, encodeOptions: EncodeOptions = {}): Promise<Buffer> => {
try {
// merge default configs
const filledInputInfo = { ...defaultInputInfo, ...inputInfo };
const filledEncodeOptions = { ...defaultEncodeOptions, ...encodeOptions };
if (filledInputInfo.width < 1 || filledInputInfo.height < 1) {
throw new Error(`Invalid input image size: ${filledInputInfo.width}x${filledInputInfo.height}`);
} else if (filledInputInfo.channels < 1 || filledInputInfo.channels > 4) {
throw new Error(`Invalid input channels: ${filledInputInfo.channels}`);
}
// wait for the wasm module to be ready
await initModule();
if (!mozjpeg) {
throw new Error('MozJPEG Module could not be initialized');
}
// check if it is a grayscale image and preserve it if so
if (
typeof encodeOptions.colorSpace === 'undefined' &&
(inputInfo.channels === 1 || isGrayscale(image, filledInputInfo.channels))
) {
filledEncodeOptions.colorSpace = ColorSpace.GRAYSCALE;
}
// encode the image and get a pointer to the result
const resultPointer = mozjpeg.encode(
image,
filledInputInfo.width,
filledInputInfo.height,
filledInputInfo.channels,
filledEncodeOptions,
);
// copy the image from wasm into the js environment
const result = mozjpeg.getImage(resultPointer);
// free used memory
mozjpeg.freeImage(resultPointer);
return Buffer.from(result);
} finally {
resetModule();
}
};
export default encode;
export { ColorSpace } from './colorspace';
export type { InputInfo, EncodeOptions } from './types';
module.exports = encode;