mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 02:30:12 +02:00
Add bounds checks on blur/sharpen parameters #108
This commit is contained in:
parent
ee513ac7a7
commit
b886db4b0d
28
index.js
28
index.js
@ -220,11 +220,11 @@ Sharp.prototype.blur = function(radius) {
|
||||
} else if (typeof radius === 'boolean') {
|
||||
// Boolean argument: apply mild blur?
|
||||
this.options.blurRadius = radius ? -1 : 0;
|
||||
} else if (typeof radius === 'number' && !Number.isNaN(radius) && (radius % 1 === 0)) {
|
||||
} else if (typeof radius === 'number' && !Number.isNaN(radius) && (radius % 1 === 0) && radius >= 1) {
|
||||
// Numeric argument: specific radius
|
||||
this.options.blurRadius = radius;
|
||||
} else {
|
||||
throw new Error('Invalid integral blur radius ' + radius);
|
||||
throw new Error('Invalid blur radius ' + radius + ' (expected integer >= 1)');
|
||||
}
|
||||
return this;
|
||||
};
|
||||
@ -244,19 +244,27 @@ Sharp.prototype.sharpen = function(radius, flat, jagged) {
|
||||
} else if (typeof radius === 'boolean') {
|
||||
// Boolean argument: apply mild sharpen?
|
||||
this.options.sharpenRadius = radius ? -1 : 0;
|
||||
} else if (typeof radius === 'number' && !Number.isNaN(radius) && (radius % 1 === 0)) {
|
||||
} else if (typeof radius === 'number' && !Number.isNaN(radius) && (radius % 1 === 0) && radius >= 1) {
|
||||
// Numeric argument: specific radius
|
||||
this.options.sharpenRadius = radius;
|
||||
if (typeof flat === 'number' && !Number.isNaN(flat)) {
|
||||
// Control over flat areas
|
||||
this.options.sharpenFlat = flat;
|
||||
// Control over flat areas
|
||||
if (typeof flat !== 'undefined' && flat !== null) {
|
||||
if (typeof flat === 'number' && !Number.isNaN(flat) && flat >= 0) {
|
||||
this.options.sharpenFlat = flat;
|
||||
} else {
|
||||
throw new Error('Invalid sharpen level for flat areas ' + flat + ' (expected >= 0)');
|
||||
}
|
||||
}
|
||||
if (typeof jagged === 'number' && !Number.isNaN(jagged)) {
|
||||
// Control over jagged areas
|
||||
this.options.sharpenJagged = jagged;
|
||||
// Control over jagged areas
|
||||
if (typeof jagged !== 'undefined' && jagged !== null) {
|
||||
if (typeof jagged === 'number' && !Number.isNaN(jagged) && jagged >= 0) {
|
||||
this.options.sharpenJagged = jagged;
|
||||
} else {
|
||||
throw new Error('Invalid sharpen level for jagged areas ' + jagged + ' (expected >= 0)');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error('Invalid integral sharpen radius ' + radius);
|
||||
throw new Error('Invalid sharpen radius ' + radius + ' (expected integer >= 1)');
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
@ -9,7 +9,20 @@ sharp.cache(0);
|
||||
|
||||
describe('Sharpen', function() {
|
||||
|
||||
it('specific radius and levels 0.5, 2.5', function(done) {
|
||||
it('specific radius 10', function(done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.sharpen(10)
|
||||
.toFile(fixtures.path('output.sharpen-10.jpg'), function(err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('specific radius 3 and levels 0.5, 2.5', function(done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.sharpen(3, 0.5, 2.5)
|
||||
@ -22,7 +35,7 @@ describe('Sharpen', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('specific radius 3 and levels 2, 4', function(done) {
|
||||
it('specific radius 5 and levels 2, 4', function(done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.sharpen(5, 2, 4)
|
||||
@ -59,6 +72,28 @@ describe('Sharpen', function() {
|
||||
done();
|
||||
});
|
||||
|
||||
it('invalid flat', function(done) {
|
||||
var isValid = true;
|
||||
try {
|
||||
sharp(fixtures.inputJpg).sharpen(1, -1);
|
||||
} catch (err) {
|
||||
isValid = false;
|
||||
}
|
||||
assert.strictEqual(false, isValid);
|
||||
done();
|
||||
});
|
||||
|
||||
it('invalid jagged', function(done) {
|
||||
var isValid = true;
|
||||
try {
|
||||
sharp(fixtures.inputJpg).sharpen(1, 1, -1);
|
||||
} catch (err) {
|
||||
isValid = false;
|
||||
}
|
||||
assert.strictEqual(false, isValid);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sharpened image is larger than non-sharpened', function(done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
|
Loading…
x
Reference in New Issue
Block a user