/
gif.js
203 lines (181 loc) · 6.25 KB
/
gif.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';
const fs = require('fs');
const assert = require('assert');
const sharp = require('../../');
const fixtures = require('../fixtures');
describe('GIF input', () => {
it('GIF Buffer to JPEG Buffer', () =>
sharp(fs.readFileSync(fixtures.inputGif))
.resize(8, 4)
.jpeg()
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(8, info.width);
assert.strictEqual(4, info.height);
})
);
it('2 channel GIF file to PNG Buffer', () =>
sharp(fixtures.inputGifGreyPlusAlpha)
.resize(8, 4)
.png()
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size);
assert.strictEqual('png', info.format);
assert.strictEqual(8, info.width);
assert.strictEqual(4, info.height);
assert.strictEqual(4, info.channels);
})
);
it('Animated GIF first page to PNG', () =>
sharp(fixtures.inputGifAnimated)
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size);
assert.strictEqual('gif', info.format);
assert.strictEqual(80, info.width);
assert.strictEqual(80, info.height);
assert.strictEqual(4, info.channels);
})
);
it('Animated GIF all pages to PNG "toilet roll"', () =>
sharp(fixtures.inputGifAnimated, { pages: -1 })
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size);
assert.strictEqual('gif', info.format);
assert.strictEqual(80, info.width);
assert.strictEqual(2400, info.height);
assert.strictEqual(4, info.channels);
})
);
it('GIF with reduced colours, no dither, low effort reduces file size', async () => {
const original = await sharp(fixtures.inputJpg)
.resize(120, 80)
.gif()
.toBuffer();
const reduced = await sharp(fixtures.inputJpg)
.resize(120, 80)
.gif({
colours: 128,
dither: 0,
effort: 1
})
.toBuffer();
assert.strictEqual(true, reduced.length < original.length);
});
it('valid optimise', () => {
assert.doesNotThrow(() => sharp().gif({ reoptimise: true }));
assert.doesNotThrow(() => sharp().gif({ reoptimize: true }));
});
it('invalid reoptimise throws', () => {
assert.throws(
() => sharp().gif({ reoptimise: -1 }),
/Expected boolean for gifReoptimise but received -1 of type number/
);
assert.throws(
() => sharp().gif({ reoptimize: 'fail' }),
/Expected boolean for gifReoptimise but received fail of type string/
);
});
it('invalid loop throws', () => {
assert.throws(() => {
sharp().gif({ loop: -1 });
});
assert.throws(() => {
sharp().gif({ loop: 65536 });
});
});
it('invalid delay throws', () => {
assert.throws(() => {
sharp().gif({ delay: -1 });
});
assert.throws(() => {
sharp().gif({ delay: [65536] });
});
});
it('invalid colour throws', () => {
assert.throws(() => {
sharp().gif({ colours: 1 });
});
assert.throws(() => {
sharp().gif({ colours: 'fail' });
});
});
it('invalid effort throws', () => {
assert.throws(() => {
sharp().gif({ effort: 0 });
});
assert.throws(() => {
sharp().gif({ effort: 'fail' });
});
});
it('invalid dither throws', () => {
assert.throws(() => {
sharp().gif({ dither: 1.1 });
});
assert.throws(() => {
sharp().gif({ effort: 'fail' });
});
});
it('invalid interFrameMaxError throws', () => {
assert.throws(
() => sharp().gif({ interFrameMaxError: 33 }),
/Expected number between 0.0 and 32.0 for interFrameMaxError but received 33 of type number/
);
assert.throws(
() => sharp().gif({ interFrameMaxError: 'fail' }),
/Expected number between 0.0 and 32.0 for interFrameMaxError but received fail of type string/
);
});
it('invalid interPaletteMaxError throws', () => {
assert.throws(
() => sharp().gif({ interPaletteMaxError: 257 }),
/Expected number between 0.0 and 256.0 for interPaletteMaxError but received 257 of type number/
);
assert.throws(
() => sharp().gif({ interPaletteMaxError: 'fail' }),
/Expected number between 0.0 and 256.0 for interPaletteMaxError but received fail of type string/
);
});
it('should work with streams when only animated is set', function (done) {
fs.createReadStream(fixtures.inputGifAnimated)
.pipe(sharp({ animated: true }))
.gif()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('gif', info.format);
fixtures.assertSimilar(fixtures.inputGifAnimated, data, done);
});
});
it('should work with streams when only pages is set', function (done) {
fs.createReadStream(fixtures.inputGifAnimated)
.pipe(sharp({ pages: -1 }))
.gif()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('gif', info.format);
fixtures.assertSimilar(fixtures.inputGifAnimated, data, done);
});
});
it('should optimise file size via interFrameMaxError', async () => {
const input = sharp(fixtures.inputGifAnimated, { animated: true });
const before = await input.gif({ interFrameMaxError: 0 }).toBuffer();
const after = await input.gif({ interFrameMaxError: 10 }).toBuffer();
assert.strict(before.length > after.length);
});
it('should optimise file size via interPaletteMaxError', async () => {
const input = sharp(fixtures.inputGifAnimated, { animated: true });
const before = await input.gif({ interPaletteMaxError: 0 }).toBuffer();
const after = await input.gif({ interPaletteMaxError: 100 }).toBuffer();
assert.strict(before.length > after.length);
});
});