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