Ensure stats can be calculated for 1x1 input #2372

This commit is contained in:
Lovell Fuller 2020-09-17 11:08:52 +01:00
parent c3898487c4
commit b369c4bb8a
3 changed files with 24 additions and 6 deletions

View File

@ -18,6 +18,9 @@ Requires libvips v8.10.0
[#2369](https://github.com/lovell/sharp/pull/2369) [#2369](https://github.com/lovell/sharp/pull/2369)
[@AcrylicShrimp](https://github.com/AcrylicShrimp) [@AcrylicShrimp](https://github.com/AcrylicShrimp)
* Ensure `stats` can be calculated for 1x1 input.
[#2372](https://github.com/lovell/sharp/issues/2372)
### v0.26.0 - 25th August 2020 ### v0.26.0 - 25th August 2020
* Prebuilt libvips binaries are now statically-linked and Brotli-compressed, requiring Node.js 10.16.0+. * Prebuilt libvips binaries are now statically-linked and Brotli-compressed, requiring Node.js 10.16.0+.

View File

@ -80,12 +80,14 @@ class StatsWorker : public Napi::AsyncWorker {
// Estimate entropy via histogram of greyscale value frequency // Estimate entropy via histogram of greyscale value frequency
baton->entropy = std::abs(greyscale.hist_find().hist_entropy()); baton->entropy = std::abs(greyscale.hist_find().hist_entropy());
// Estimate sharpness via standard deviation of greyscale laplacian // Estimate sharpness via standard deviation of greyscale laplacian
if (image.width() > 1 || image.height() > 1) {
VImage laplacian = VImage::new_matrixv(3, 3, VImage laplacian = VImage::new_matrixv(3, 3,
0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
1.0, -4.0, 1.0, 1.0, -4.0, 1.0,
0.0, 1.0, 0.0); 0.0, 1.0, 0.0);
laplacian.set("scale", 9.0); laplacian.set("scale", 9.0);
baton->sharpness = greyscale.conv(laplacian).deviate(); baton->sharpness = greyscale.conv(laplacian).deviate();
}
// Most dominant sRGB colour via 4096-bin 3D histogram // Most dominant sRGB colour via 4096-bin 3D histogram
vips::VImage hist = sharp::RemoveAlpha(image) vips::VImage hist = sharp::RemoveAlpha(image)
.colourspace(VIPS_INTERPRETATION_sRGB) .colourspace(VIPS_INTERPRETATION_sRGB)

View File

@ -462,6 +462,19 @@ describe('Image Stats', function () {
}) })
); );
it('Entropy and sharpness of 1x1 input are zero', async () => {
const { entropy, sharpness } = await sharp({
create: {
width: 1,
height: 1,
channels: 3,
background: 'red'
}
}).stats();
assert.strictEqual(entropy, 0);
assert.strictEqual(sharpness, 0);
});
it('Stream in, Callback out', function (done) { it('Stream in, Callback out', function (done) {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const pipeline = sharp().stats(function (err, stats) { const pipeline = sharp().stats(function (err, stats) {