Implements greyscale thresholding

This commit is contained in:
David Carley
2015-11-17 12:15:34 -06:00
parent 5dfeaa9fd1
commit 3af62446fc
14 changed files with 189 additions and 4 deletions

BIN
test/fixtures/expected/threshold-1.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 882 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
test/fixtures/expected/threshold-128.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
test/fixtures/expected/threshold-40.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

105
test/unit/threshold.js Normal file
View File

@@ -0,0 +1,105 @@
'use strict';
var assert = require('assert');
var sharp = require('../../index');
var fixtures = require('../fixtures');
sharp.cache(0);
describe('Threshold', function() {
it('threshold 1 jpeg', function(done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.threshold(1)
.toBuffer(function(err, data, info) {
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('threshold-1.jpg'), data, done);
});
});
it('threshold 40 jpeg', function(done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.threshold(40)
.toBuffer(function(err, data, info) {
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('threshold-40.jpg'), data, done);
});
});
it('threshold 128', function(done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.threshold(128)
.toBuffer(function(err, data, info) {
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('threshold-128.jpg'), data, done);
});
});
it('threshold default jpeg', function(done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.threshold()
.toBuffer(function(err, data, info) {
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('threshold-128.jpg'), data, done);
});
});
it('threshold default png transparency', function(done) {
sharp(fixtures.inputPngWithTransparency)
.resize(320, 240)
.threshold()
.toBuffer(function(err, data, info) {
assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('threshold-128-transparency.png'), data, done);
});
});
it('threshold default png alpha', function(done) {
sharp(fixtures.inputPngWithGreyAlpha)
.resize(320, 240)
.threshold()
.toBuffer(function(err, data, info) {
assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('threshold-128-alpha.png'), data, done);
});
});
if (sharp.format.webp.output.file) {
it('threshold default webp transparency', function(done) {
sharp(fixtures.inputWebPWithTransparency)
.threshold()
.toBuffer(function(err, data, info) {
assert.strictEqual('webp', info.format);
fixtures.assertSimilar(fixtures.expected('threshold-128-transparency.webp'), data, done);
});
});
}
it('invalid threshold -1', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).threshold(-1);
});
});
it('invalid threshold 256', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).threshold(256);
});
});
});