-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose PNG output options requiring libimagequant #1484
- Loading branch information
Showing
8 changed files
with
211 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
|
||
const sharp = require('../../'); | ||
const fixtures = require('../fixtures'); | ||
|
||
describe('PNG output', function () { | ||
it('compression level is valid', function () { | ||
assert.doesNotThrow(function () { | ||
sharp().png({ compressionLevel: 0 }); | ||
}); | ||
}); | ||
|
||
it('compression level is invalid', function () { | ||
assert.throws(function () { | ||
sharp().png({ compressionLevel: -1 }); | ||
}); | ||
}); | ||
|
||
it('default compressionLevel generates smaller file than compressionLevel=6', function (done) { | ||
// First generate with default compressionLevel | ||
sharp(fixtures.inputPng) | ||
.resize(320, 240) | ||
.png() | ||
.toBuffer(function (err, defaultData, defaultInfo) { | ||
if (err) throw err; | ||
assert.strictEqual(true, defaultData.length > 0); | ||
assert.strictEqual('png', defaultInfo.format); | ||
// Then generate with compressionLevel=6 | ||
sharp(fixtures.inputPng) | ||
.resize(320, 240) | ||
.png({ compressionLevel: 6 }) | ||
.toBuffer(function (err, largerData, largerInfo) { | ||
if (err) throw err; | ||
assert.strictEqual(true, largerData.length > 0); | ||
assert.strictEqual('png', largerInfo.format); | ||
assert.strictEqual(true, defaultData.length < largerData.length); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('without adaptiveFiltering generates smaller file', function (done) { | ||
// First generate with adaptive filtering | ||
sharp(fixtures.inputPng) | ||
.resize(320, 240) | ||
.png({ adaptiveFiltering: true }) | ||
.toBuffer(function (err, adaptiveData, adaptiveInfo) { | ||
if (err) throw err; | ||
assert.strictEqual(true, adaptiveData.length > 0); | ||
assert.strictEqual(adaptiveData.length, adaptiveInfo.size); | ||
assert.strictEqual('png', adaptiveInfo.format); | ||
assert.strictEqual(320, adaptiveInfo.width); | ||
assert.strictEqual(240, adaptiveInfo.height); | ||
// Then generate without | ||
sharp(fixtures.inputPng) | ||
.resize(320, 240) | ||
.png({ adaptiveFiltering: false }) | ||
.toBuffer(function (err, withoutAdaptiveData, withoutAdaptiveInfo) { | ||
if (err) throw err; | ||
assert.strictEqual(true, withoutAdaptiveData.length > 0); | ||
assert.strictEqual(withoutAdaptiveData.length, withoutAdaptiveInfo.size); | ||
assert.strictEqual('png', withoutAdaptiveInfo.format); | ||
assert.strictEqual(320, withoutAdaptiveInfo.width); | ||
assert.strictEqual(240, withoutAdaptiveInfo.height); | ||
assert.strictEqual(true, withoutAdaptiveData.length < adaptiveData.length); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('Invalid PNG adaptiveFiltering value throws error', function () { | ||
assert.throws(function () { | ||
sharp().png({ adaptiveFiltering: 1 }); | ||
}); | ||
}); | ||
|
||
it('Valid PNG libimagequant palette value does not throw error', function () { | ||
assert.doesNotThrow(function () { | ||
sharp().png({ palette: false }); | ||
}); | ||
}); | ||
|
||
it('Invalid PNG libimagequant palette value throws error', function () { | ||
assert.throws(function () { | ||
sharp().png({ palette: 'fail' }); | ||
}); | ||
}); | ||
|
||
it('Valid PNG libimagequant quality value produces image of same size or smaller', function () { | ||
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); | ||
return Promise.all([ | ||
sharp(inputPngBuffer).resize(10).png({ palette: true, quality: 80 }).toBuffer(), | ||
sharp(inputPngBuffer).resize(10).png({ palette: true, quality: 100 }).toBuffer() | ||
]).then(function (data) { | ||
assert.strictEqual(true, data[0].length <= data[1].length); | ||
}); | ||
}); | ||
|
||
it('Invalid PNG libimagequant quality value throws error', function () { | ||
assert.throws(function () { | ||
sharp().png({ palette: true, quality: 101 }); | ||
}); | ||
}); | ||
|
||
it('Valid PNG libimagequant colours value produces image of same size or smaller', function () { | ||
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); | ||
return Promise.all([ | ||
sharp(inputPngBuffer).resize(10).png({ palette: true, colours: 100 }).toBuffer(), | ||
sharp(inputPngBuffer).resize(10).png({ palette: true, colours: 200 }).toBuffer() | ||
]).then(function (data) { | ||
assert.strictEqual(true, data[0].length <= data[1].length); | ||
}); | ||
}); | ||
|
||
it('Invalid PNG libimagequant colours value throws error', function () { | ||
assert.throws(function () { | ||
sharp().png({ palette: true, colours: -1 }); | ||
}); | ||
}); | ||
|
||
it('Invalid PNG libimagequant colors value throws error', function () { | ||
assert.throws(function () { | ||
sharp().png({ palette: true, colors: 0.1 }); | ||
}); | ||
}); | ||
|
||
it('Valid PNG libimagequant dither value produces image of same size or smaller', function () { | ||
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); | ||
return Promise.all([ | ||
sharp(inputPngBuffer).resize(10).png({ palette: true, dither: 0.1 }).toBuffer(), | ||
sharp(inputPngBuffer).resize(10).png({ palette: true, dither: 0.9 }).toBuffer() | ||
]).then(function (data) { | ||
assert.strictEqual(true, data[0].length <= data[1].length); | ||
}); | ||
}); | ||
|
||
it('Invalid PNG libimagequant dither value throws error', function () { | ||
assert.throws(function () { | ||
sharp().png({ palette: true, dither: 'fail' }); | ||
}); | ||
}); | ||
}); |