Add joinChannel and toColourspace/toColorspace operations (#513)

This commit is contained in:
Matt Hirsch
2016-08-17 10:42:05 -04:00
committed by Lovell Fuller
parent 72354d55a8
commit 5c5d74a903
17 changed files with 310 additions and 19 deletions

View File

@@ -15,13 +15,12 @@ describe('Bandbool per-channel boolean operations', function() {
it(op + ' operation', function(done) {
sharp(fixtures.inputPngBooleanNoAlpha)
.bandbool(op)
.toColourspace('b-w')
.toBuffer(function(err, data, info) {
// should use .toColourspace('b-w') here to get 1 channel output, when it is merged
if (err) throw err;
assert.strictEqual(200, info.width);
assert.strictEqual(200, info.height);
//assert.strictEqual(1, info.channels);
assert.strictEqual(3, info.channels);
assert.strictEqual(1, info.channels);
fixtures.assertSimilar(fixtures.expected('bandbool_' + op + '_result.png'), data, done);
});
});

View File

@@ -29,6 +29,20 @@ describe('Colour space conversion', function() {
.toFile(fixtures.path('output.greyscale-not.jpg'), done);
});
it('Greyscale with single channel output', function(done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.greyscale()
.toColourspace('b-w')
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(1, info.channels);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('output.greyscale-single.jpg'), data, done);
});
});
if (sharp.format.tiff.input.file && sharp.format.webp.output.buffer) {
it('From 1-bit TIFF to sRGB WebP [slow]', function(done) {
sharp(fixtures.inputTiff)
@@ -79,4 +93,10 @@ describe('Colour space conversion', function() {
});
});
it('Invalid input', function() {
assert.throws(function() {
sharp(fixtures.inputJpg)
.toColourspace(null);
});
});
});

151
test/unit/joinChannel.js Normal file
View File

@@ -0,0 +1,151 @@
'use strict';
var assert = require('assert');
var fs = require('fs');
var sharp = require('../../index');
var fixtures = require('../fixtures');
var BluebirdPromise = require('bluebird');
describe('Image channel insertion', function() {
it('Grayscale to RGB, buffer', function(done) {
sharp(fixtures.inputPng) // gray -> red
.resize(320, 240)
.joinChannel(fixtures.inputPngTestJoinChannel) // new green channel
.joinChannel(fixtures.inputPngStripesH) // new blue channel
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
assert.strictEqual(3, info.channels);
fixtures.assertSimilar(fixtures.expected('joinChannel-rgb.jpg'), data, done);
});
});
it('Grayscale to RGB, file', function(done) {
sharp(fixtures.inputPng) // gray -> red
.resize(320, 240)
.joinChannel(fs.readFileSync(fixtures.inputPngTestJoinChannel)) // new green channel
.joinChannel(fs.readFileSync(fixtures.inputPngStripesH)) // new blue channel
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
assert.strictEqual(3, info.channels);
fixtures.assertSimilar(fixtures.expected('joinChannel-rgb.jpg'), data, done);
});
});
it('Grayscale to RGBA, buffer', function(done) {
sharp(fixtures.inputPng) // gray -> red
.resize(320, 240)
.joinChannel([fixtures.inputPngTestJoinChannel,
fixtures.inputPngStripesH,
fixtures.inputPngStripesV]) // new green + blue + alpha channel
.toColourspace(sharp.colourspace.srgb)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
assert.strictEqual(4, info.channels);
fixtures.assertSimilar(fixtures.expected('joinChannel-rgba.png'), data, done);
});
});
it('Grayscale to RGBA, file', function(done) {
sharp(fixtures.inputPng) // gray -> red
.resize(320, 240)
.joinChannel([fs.readFileSync(fixtures.inputPngTestJoinChannel), // new green channel
fs.readFileSync(fixtures.inputPngStripesH), // new blue channel
fs.readFileSync(fixtures.inputPngStripesV)]) // new alpha channel
.toColourspace('srgb')
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
assert.strictEqual(4, info.channels);
fixtures.assertSimilar(fixtures.expected('joinChannel-rgba.png'), data, done);
});
});
it('Grayscale to CMYK, buffers', function(done) {
sharp(fixtures.inputPng) // gray -> magenta
.resize(320, 240)
.joinChannel([fs.readFileSync(fixtures.inputPngTestJoinChannel), // new cyan channel
fs.readFileSync(fixtures.inputPngStripesH), // new yellow channel
fs.readFileSync(fixtures.inputPngStripesV)]) // new black channel
.toColorspace('cmyk')
.toFormat('jpeg')
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
assert.strictEqual(4, info.channels);
fixtures.assertSimilar(fixtures.expected('joinChannel-cmyk.jpg'), data, done);
});
});
it('Join raw buffers to RGB', function(done) {
BluebirdPromise.all([
sharp(fixtures.inputPngTestJoinChannel).toColourspace('b-w').raw().toBuffer(),
sharp(fixtures.inputPngStripesH).toColourspace('b-w').raw().toBuffer()
])
.then(function(buffers) {
sharp(fixtures.inputPng)
.resize(320, 240)
.joinChannel(buffers,
{ raw: {
width: 320,
height: 240,
channels: 1
}})
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
assert.strictEqual(3, info.channels);
fixtures.assertSimilar(fixtures.expected('joinChannel-rgb.jpg'), data, done);
});
})
.catch(function(err) {
throw err;
});
});
it('Grayscale to RGBA, files, two arrays', function(done) {
sharp(fixtures.inputPng) // gray -> red
.resize(320, 240)
.joinChannel([fs.readFileSync(fixtures.inputPngTestJoinChannel)]) // new green channel
.joinChannel([fs.readFileSync(fixtures.inputPngStripesH), // new blue channel
fs.readFileSync(fixtures.inputPngStripesV)]) // new alpha channel
.toColourspace('srgb')
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
assert.strictEqual(4, info.channels);
fixtures.assertSimilar(fixtures.expected('joinChannel-rgba.png'), data, done);
});
});
it('Invalid raw buffer description', function() {
assert.throws(function() {
sharp().joinChannel(fs.readFileSync(fixtures.inputPng),{raw:{}});
});
});
it('Invalid input', function() {
assert.throws(function() {
sharp(fixtures.inputJpg)
.joinChannel(1);
});
});
it('No arguments', function() {
assert.throws(function() {
sharp(fixtures.inputJpg)
.joinChannel();
});
});
});