Add boolean feature for bitwise image operations (#501)

This commit is contained in:
Matt Hirsch
2016-07-11 04:51:43 -04:00
committed by Lovell Fuller
parent 99f960bf56
commit d17e8d3450
14 changed files with 256 additions and 22 deletions

BIN
test/fixtures/booleanTest.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@@ -95,6 +95,8 @@ module.exports = {
inputPngStripesV: getPath('stripesV.png'),
inputPngStripesH: getPath('stripesH.png'),
inputJpgBooleanTest: getPath('booleanTest.jpg'),
inputV: getPath('vfile.v'),
outputJpg: getPath('output.jpg'),

103
test/unit/boolean.js Normal file
View File

@@ -0,0 +1,103 @@
'use strict';
var fs = require('fs');
var assert = require('assert');
var fixtures = require('../fixtures');
var sharp = require('../../index');
describe('Boolean operation between two images', function() {
it('\'and\' Operation, file', function(done) {
sharp(fixtures.inputJpg) //fixtures.inputJpg
.resize(320,240)
.boolean(fixtures.inputJpgBooleanTest, 'and')
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('boolean_and_result.jpg'), data, done);
});
});
it('\'and\' Operation, buffer', function(done) {
sharp(fixtures.inputJpg) //fixtures.inputJpg
.resize(320,240)
.boolean(fs.readFileSync(fixtures.inputJpgBooleanTest), sharp.bool.and)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('boolean_and_result.jpg'), data, done);
});
});
it('\'or\' Operation, file', function(done) {
sharp(fixtures.inputJpg)
.resize(320,240)
.boolean(fixtures.inputJpgBooleanTest, sharp.bool.or)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('boolean_or_result.jpg'), data, done);
});
});
it('\'or\' Operation, buffer', function(done) {
sharp(fixtures.inputJpg)
.resize(320,240)
.boolean(fs.readFileSync(fixtures.inputJpgBooleanTest), 'or')
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('boolean_or_result.jpg'), data, done);
});
});
it('\'eor\' Operation, file', function(done) {
sharp(fixtures.inputJpg)
.resize(320,240)
.boolean(fixtures.inputJpgBooleanTest, 'eor')
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('boolean_eor_result.jpg'), data, done);
});
});
it('\'eor\' Operation, buffer', function(done) {
sharp(fixtures.inputJpg)
.resize(320,240)
.boolean(fs.readFileSync(fixtures.inputJpgBooleanTest), sharp.bool.eor)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('boolean_eor_result.jpg'), data, done);
});
});
it('Invalid operation', function() {
assert.throws(function() {
sharp(fixtures.inputJpg)
.boolean(fs.readFileSync(fixtures.inputJpgBooleanTest), 'fail');
});
});
it('Invalid operation, non-string', function() {
assert.throws(function() {
sharp(fixtures.inputJpg)
.boolean(fs.readFileSync(fixtures.inputJpgBooleanTest), null);
});
});
if('Invalid buffer input', function() {
assert.throws(function() {
sharp(fixtures.inputJpg)
.resize(320,240)
.boolean([],'eor');
});
});
});