Add extractChannel operation to extract a channel from an image (#497)

This commit is contained in:
Matt Hirsch
2016-07-09 11:48:30 -04:00
committed by Lovell Fuller
parent f672f86b53
commit 83d8847f57
8 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
'use strict';
var assert = require('assert');
var sharp = require('../../index');
var fixtures = require('../fixtures');
describe('Image channel extraction', function() {
it('Red channel', function(done) {
sharp(fixtures.inputJpg)
.extractChannel('red')
.resize(320,240)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('extract-red.jpg'), data, done);
});
});
it('Green channel', function(done) {
sharp(fixtures.inputJpg)
.extractChannel('green')
.resize(320,240)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('extract-green.jpg'), data, done);
});
});
it('Blue channel', function(done) {
sharp(fixtures.inputJpg)
.extractChannel('blue')
.resize(320,240)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('extract-blue.jpg'), data, done);
});
});
it('Blue channel by number', function(done) {
sharp(fixtures.inputJpg)
.extractChannel(2)
.resize(320,240)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('extract-blue.jpg'), data, done);
});
});
it('Invalid channel number', function() {
assert.throws(function() {
sharp(fixtures.inputJpg)
.extractChannel(-1);
});
});
it('No arguments', function() {
assert.throws(function() {
sharp(fixtures.inputJpg)
.extractChannel();
});
});
});