/
extractChannel.js
113 lines (100 loc) · 3.37 KB
/
extractChannel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
const assert = require('assert');
const sharp = require('../../');
const 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('With colorspace conversion', async () => {
const [chroma] = await sharp({ create: { width: 1, height: 1, channels: 3, background: 'red' } })
.toColourspace('lch')
.extractChannel(1)
.toBuffer();
assert.strictEqual(chroma, 104);
});
it('Alpha from 16-bit PNG', function (done) {
const output = fixtures.path('output.extract-alpha-16bit.png');
sharp(fixtures.inputPngWithTransparency16bit)
.resize(16)
.extractChannel(3)
.toFile(output, function (err) {
if (err) throw err;
fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-16bit.png'));
done();
});
});
it('Alpha from 2-channel input', function (done) {
const output = fixtures.path('output.extract-alpha-2-channel.png');
sharp(fixtures.inputPngWithGreyAlpha)
.extractChannel('alpha')
.toColourspace('b-w')
.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual(1, info.channels);
fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-2-channel.png'));
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();
});
});
it('Non-existent channel', async () =>
await assert.rejects(
() => sharp({ create: { width: 1, height: 1, channels: 3, background: 'red' } })
.extractChannel(3)
.toBuffer(),
/Cannot extract channel 3 from image with channels 0-2/
)
);
});