Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/png/src/index.js
1126 views
1
import { PNG } from 'pngjs';
2
import { throwError, isNodePattern } from '@jimp/utils';
3
4
const MIME_TYPE = 'image/png';
5
6
// PNG filter types
7
const PNG_FILTER_AUTO = -1;
8
const PNG_FILTER_NONE = 0;
9
const PNG_FILTER_SUB = 1;
10
const PNG_FILTER_UP = 2;
11
const PNG_FILTER_AVERAGE = 3;
12
const PNG_FILTER_PATH = 4;
13
14
export default () => ({
15
mime: { [MIME_TYPE]: ['png'] },
16
17
constants: {
18
MIME_PNG: MIME_TYPE,
19
PNG_FILTER_AUTO,
20
PNG_FILTER_NONE,
21
PNG_FILTER_SUB,
22
PNG_FILTER_UP,
23
PNG_FILTER_AVERAGE,
24
PNG_FILTER_PATH
25
},
26
27
hasAlpha: { [MIME_TYPE]: true },
28
decoders: { [MIME_TYPE]: PNG.sync.read },
29
encoders: {
30
[MIME_TYPE]: data => {
31
const png = new PNG({
32
width: data.bitmap.width,
33
height: data.bitmap.height
34
});
35
36
png.data = data.bitmap.data;
37
38
return PNG.sync.write(png, {
39
width: data.bitmap.width,
40
height: data.bitmap.height,
41
deflateLevel: data._deflateLevel,
42
deflateStrategy: data._deflateStrategy,
43
filterType: data._filterType,
44
colorType:
45
typeof data._colorType === 'number'
46
? data._colorType
47
: data._rgba
48
? 6
49
: 2,
50
inputHasAlpha: data._rgba
51
});
52
}
53
},
54
55
class: {
56
_deflateLevel: 9,
57
_deflateStrategy: 3,
58
_filterType: PNG_FILTER_AUTO,
59
_colorType: null,
60
61
/**
62
* Sets the deflate level used when saving as PNG format (default is 9)
63
* @param {number} l Deflate level to use 0-9. 0 is no compression. 9 (default) is maximum compression.
64
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
65
* @returns {Jimp} this for chaining of methods
66
*/
67
deflateLevel(l, cb) {
68
if (typeof l !== 'number') {
69
return throwError.call(this, 'l must be a number', cb);
70
}
71
72
if (l < 0 || l > 9) {
73
return throwError.call(this, 'l must be a number 0 - 9', cb);
74
}
75
76
this._deflateLevel = Math.round(l);
77
78
if (isNodePattern(cb)) {
79
cb.call(this, null, this);
80
}
81
82
return this;
83
},
84
85
/**
86
* Sets the deflate strategy used when saving as PNG format (default is 3)
87
* @param {number} s Deflate strategy to use 0-3.
88
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
89
* @returns {Jimp} this for chaining of methods
90
*/
91
deflateStrategy(s, cb) {
92
if (typeof s !== 'number') {
93
return throwError.call(this, 's must be a number', cb);
94
}
95
96
if (s < 0 || s > 3) {
97
return throwError.call(this, 's must be a number 0 - 3', cb);
98
}
99
100
this._deflateStrategy = Math.round(s);
101
102
if (isNodePattern(cb)) {
103
cb.call(this, null, this);
104
}
105
106
return this;
107
},
108
109
/**
110
* Sets the filter type used when saving as PNG format (default is automatic filters)
111
* @param {number} f The quality to use -1-4.
112
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
113
* @returns {Jimp} this for chaining of methods
114
*/
115
filterType(f, cb) {
116
if (typeof f !== 'number') {
117
return throwError.call(this, 'n must be a number', cb);
118
}
119
120
if (f < -1 || f > 4) {
121
return throwError.call(
122
this,
123
'n must be -1 (auto) or a number 0 - 4',
124
cb
125
);
126
}
127
128
this._filterType = Math.round(f);
129
130
if (isNodePattern(cb)) {
131
cb.call(this, null, this);
132
}
133
134
return this;
135
},
136
/**
137
* Sets the color type used when saving as PNG format
138
* @param {number} s color type to use 0, 2, 4, 6.
139
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
140
* @returns {Jimp} this for chaining of methods
141
*/ colorType(s, cb) {
142
if (typeof s !== 'number') {
143
return throwError.call(this, 's must be a number', cb);
144
}
145
146
if (s !== 0 && s !== 2 && s !== 4 && s !== 6) {
147
return throwError.call(this, 's must be a number 0, 2, 4, 6.', cb);
148
}
149
150
this._colorType = Math.round(s);
151
152
if (isNodePattern(cb)) {
153
cb.call(this, null, this);
154
}
155
156
return this;
157
}
158
}
159
});
160
161