-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (45 loc) · 1.14 KB
/
index.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
const jpeg = require('jpeg-js')
const png = require('fast-png')
const bmp = require('bmp-js')
if (typeof Buffer === 'undefined') {
global.Buffer = require('buffer').Buffer
}
exports.decode = (buffer) => {
if (buffer[0] === 0xff && buffer[1] === 0xd8) {
const data = jpeg.decode(buffer)
return {
width: data.width,
height: data.height,
data: new Uint8Array(data.data),
}
}
if (buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4e && buffer[3] === 0x47) {
const data = png.decode(buffer)
return {
width: data.width,
height: data.height,
data: data.data,
}
}
if (buffer[0] === 0x42 && buffer[1] === 0x4d) {
const data = bmp.decode(buffer)
return {
width: data.width,
height: data.height,
data: new Uint8Array(data.data)
}
}
throw new Error('Unsupported image format')
}
exports.encode = (imageData, type) => {
if (type === 'jpeg') {
return jpeg.encode(imageData).data
}
if (type === 'png') {
return png.encode(imageData)
}
if (type === 'bmp') {
return bmp.encode(imageData).data
}
throw new Error('Unsupported image format')
}