-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
linear.js
98 lines (87 loc) · 3.18 KB
/
linear.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
'use strict';
const sharp = require('../../');
const fixtures = require('../fixtures');
const assert = require('assert');
describe('Linear adjustment', function () {
const blackPoint = 70;
const whitePoint = 203;
const a = 255 / (whitePoint - blackPoint);
const b = -blackPoint * a;
it('applies linear levels adjustment w/o alpha ch', function (done) {
sharp(fixtures.inputJpgWithLowContrast)
.linear(a, b)
.toBuffer(function (err, data, info) {
if (err) throw err;
fixtures.assertSimilar(fixtures.expected('low-contrast-linear.jpg'), data, done);
});
});
it('applies slope level adjustment w/o alpha ch', function (done) {
sharp(fixtures.inputJpgWithLowContrast)
.linear(a)
.toBuffer(function (err, data, info) {
if (err) throw err;
fixtures.assertSimilar(fixtures.expected('low-contrast-slope.jpg'), data, done);
});
});
it('applies offset level adjustment w/o alpha ch', function (done) {
sharp(fixtures.inputJpgWithLowContrast)
.linear(null, b)
.toBuffer(function (err, data, info) {
if (err) throw err;
fixtures.assertSimilar(fixtures.expected('low-contrast-offset.jpg'), data, done);
});
});
it('applies linear levels adjustment w alpha ch', function (done) {
sharp(fixtures.inputPngOverlayLayer1)
.linear(a, b)
.toBuffer(function (err, data, info) {
if (err) throw err;
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-linear.png'), data, done);
});
});
it('applies slope level adjustment w alpha ch', function (done) {
sharp(fixtures.inputPngOverlayLayer1)
.linear(a)
.toBuffer(function (err, data, info) {
if (err) throw err;
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-slope.png'), data, done);
});
});
it('applies offset level adjustment w alpha ch', function (done) {
sharp(fixtures.inputPngOverlayLayer1)
.linear(null, b)
.toBuffer(function (err, data, info) {
if (err) throw err;
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-offset.png'), data, done);
});
});
it('per channel level adjustment', function (done) {
sharp(fixtures.inputWebP)
.linear([0.25, 0.5, 0.75], [150, 100, 50]).toBuffer(function (err, data, info) {
if (err) throw err;
fixtures.assertSimilar(fixtures.expected('linear-per-channel.jpg'), data, done);
});
});
it('Invalid linear arguments', function () {
assert.throws(
() => sharp().linear('foo'),
/Expected number or array of numbers for a but received foo of type string/
);
assert.throws(
() => sharp().linear(undefined, { bar: 'baz' }),
/Expected number or array of numbers for b but received \[object Object\] of type object/
);
assert.throws(
() => sharp().linear([], [1]),
/Expected number or array of numbers for a but received {2}of type object/
);
assert.throws(
() => sharp().linear([1, 2], [1]),
/Expected a and b to be arrays of the same length/
);
assert.throws(
() => sharp().linear([1]),
/Expected a and b to be arrays of the same length/
);
});
});