This repository was archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (90 loc) · 2.08 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
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
"use strict";
const upng = require("upng-js"),
ImageFrame = require("./ImageFrame"),
mime = "image/png",
animatedMime = "image/apng";
function decode(data) {
const d = upng.decode(data.buffer);
return {
data: Buffer.from(d.data.buffer),
width: d.width,
height: d.height,
frames: d.frames.length ? d.frames.slice(1).map(f => new ImageFrame(f)) : [],
colorDepth: d.depth,
colorType: d.ctype,
delay: d.frames[0] && d.frames[0].delay,
dispose: d.frames[0] && d.frames[0].dispose,
blend: d.frames[0] && d.frames[0].blend
};
}
function encode(img) {
return Buffer.from(upng.encode([img.bitmap.data.buffer, ...img.bitmap.frames.map(f => f.data.buffer)],
img.bitmap.width,
img.bitmap.height,
0,
[img.bitmap.delay, ...img.bitmap.frames.map(f => f.delay)]));
}
const jimpUpng = () => ({
mime: {
[mime]: "png",
[animatedMime]: ["apng", "png"]
},
constants: {
MIME_PNG: mime,
MIME_APNG: animatedMime
},
hasAlpha: {
[mime]: true,
[animatedMime]: true
},
decoders: {
[mime]() {
return decode.apply(this, arguments);
},
[animatedMime]() {
return decode.apply(this, arguments);
}
},
encoders: {
[mime]() {
return encode.apply(this, arguments);
},
[animatedMime]() {
return encode.apply(this, arguments);
}
}
});
jimpUpng.ImageFrame = ImageFrame;
/**
* The methods of frame disposal
* @enum {number}
*/
jimpUpng.APNGFrameDisposal = {
/**
* Don't do anything
*/
NONE: 0,
/**
* Restore the frame to the background
*/
BACKGROUND: 1,
/**
* Restore the frame to the previous frame
*/
PREVIOUS: 2
};
/**
* The methods of frame blending
* @enum {number}
*/
jimpUpng.APNGFrameBlend = {
/**
* Overwrite the old source
*/
SOURCE: 0,
/**
* Composite the image over the old source
*/
OVER: 1
};
module.exports = jimpUpng;