From 0265d305fe332ad500361bc9fc2c02ae66602fef Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Sun, 4 Dec 2022 21:41:15 +0000 Subject: [PATCH] Ensure integral output of linear op #3468 --- docs/changelog.md | 3 +++ src/operations.cc | 4 ++-- test/unit/linear.js | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index e69d779e..c6374bec 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -18,6 +18,9 @@ Requires libvips v8.13.3 * Ignore `sequentialRead` option when calculating image statistics. [#3462](https://github.com/lovell/sharp/issues/3462) +* Ensure integral output of `linear` operation. + [#3468](https://github.com/lovell/sharp/issues/3468) + ### v0.31.2 - 4th November 2022 * Upgrade to libvips v8.13.3 for upstream bug fixes. diff --git a/src/operations.cc b/src/operations.cc index cf5af224..012712e1 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -345,9 +345,9 @@ namespace sharp { if (HasAlpha(image) && a.size() != bands && (a.size() == 1 || a.size() == bands - 1 || bands - 1 == 1)) { // Separate alpha channel VImage alpha = image[bands - 1]; - return RemoveAlpha(image).linear(a, b).bandjoin(alpha); + return RemoveAlpha(image).linear(a, b, VImage::option()->set("uchar", TRUE)).bandjoin(alpha); } else { - return image.linear(a, b); + return image.linear(a, b, VImage::option()->set("uchar", TRUE)); } } diff --git a/test/unit/linear.js b/test/unit/linear.js index 1155ac15..8b054303 100644 --- a/test/unit/linear.js +++ b/test/unit/linear.js @@ -73,6 +73,28 @@ describe('Linear adjustment', function () { }); }); + it('output is integer, not float, RGB', async () => { + const data = await sharp({ create: { width: 1, height: 1, channels: 3, background: 'red' } }) + .linear(1, 0) + .tiff({ compression: 'none' }) + .toBuffer(); + + const { channels, depth } = await sharp(data).metadata(); + assert.strictEqual(channels, 3); + assert.strictEqual(depth, 'uchar'); + }); + + it('output is integer, not float, RGBA', async () => { + const data = await sharp({ create: { width: 1, height: 1, channels: 4, background: '#ff000077' } }) + .linear(1, 0) + .tiff({ compression: 'none' }) + .toBuffer(); + + const { channels, depth } = await sharp(data).metadata(); + assert.strictEqual(channels, 4); + assert.strictEqual(depth, 'uchar'); + }); + it('Invalid linear arguments', function () { assert.throws( () => sharp().linear('foo'),