diff --git a/docs/changelog.md b/docs/changelog.md index 38597e33..b27ecaec 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -16,6 +16,9 @@ Requires libvips v8.13.0 * The `flip` and `flop` operations will now occur before the `rotate` operation. +* Improve `normalise` operation with use of histogram. + [#200](https://github.com/lovell/sharp/issues/200) + * Use combined bounding box of alpha and non-alpha channels for `trim` operation. [#2166](https://github.com/lovell/sharp/issues/2166) diff --git a/src/operations.cc b/src/operations.cc index b3339d9a..89c2d6ee 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -68,10 +68,9 @@ namespace sharp { // Extract luminance VImage luminance = lab[0]; // Find luminance range - VImage stats = luminance.stats(); - double min = stats(0, 0)[0]; - double max = stats(1, 0)[0]; - if (min != max) { + int const min = luminance.percent(1); + int const max = luminance.percent(99); + if (std::abs(max - min) > 1) { // Extract chroma VImage chroma = lab.extract_band(1, VImage::option()->set("n", 2)); // Calculate multiplication factor and addition diff --git a/test/unit/normalize.js b/test/unit/normalize.js index 1bfb2862..33a25b52 100644 --- a/test/unit/normalize.js +++ b/test/unit/normalize.js @@ -13,7 +13,7 @@ const assertNormalized = function (data) { max = Math.max(max, data[i]); } assert.strictEqual(0, min); - assert.strictEqual(255, max); + assert.ok([254, 255].includes(max)); }; describe('Normalization', function () { @@ -30,7 +30,6 @@ describe('Normalization', function () { it('spreads grayscaled image values between 0 and 255', function (done) { sharp(fixtures.inputJpgWithLowContrast) - .gamma() .greyscale() .normalize(true) .raw()